Browse Source

iptools2

master
boB Rudis 4 years ago
parent
commit
1fc2d512ec
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 1
      .Rbuildignore
  2. 21
      DESCRIPTION
  3. 2
      LICENSE
  4. 21
      LICENSE.md
  5. 5
      NAMESPACE
  6. 7
      R/RcppExports.R
  7. 21
      R/iptools2-package.R
  8. 35
      README.Rmd
  9. 76
      README.md
  10. 10
      man/iptools2.Rd
  11. 3
      src/.gitignore
  12. 3
      src/Makevars
  13. 3
      src/Makevars.win
  14. 28
      src/RcppExports.cpp
  15. 254
      src/iptools2-main.cpp

1
.Rbuildignore

@ -13,3 +13,4 @@
^tmp$
^notes$
^\.gitlab-ci\.yml$
^LICENSE\.md$

21
DESCRIPTION

@ -1,6 +1,6 @@
Package: iptools2
Type: Package
Title: iptools2 title goes here otherwise CRAN checks fail
Title: A WIP Re-imagining of {iptools}
Version: 0.1.0
Date: 2019-04-29
Authors@R: c(
@ -8,18 +8,23 @@ Authors@R: c(
comment = c(ORCID = "0000-0001-5670-2640"))
)
Maintainer: Bob Rudis <bob@rud.is>
Description: A good description goes here otherwise CRAN checks fail.
URL: https://gitlab.com/hrbrmstr/iptools2
BugReports: https://gitlab.com/hrbrmstr/iptools2/issues
Description: A WIP Re-imagining of {iptools}
URL: https://git.rud.is/hrbrmstr/iptools2
BugReports: https://git.rud.is/hrbrmstr/iptools2/issues
Encoding: UTF-8
License: AGPL
SystemRequirements: C++11
License: MIT + file LICENSE
Suggests:
testthat,
covr
Depends:
R (>= 3.2.0)
Imports:
httr,
jsonlite
Rcpp,
AsioHeaders
Roxygen: list(markdown = TRUE)
RoxygenNote: 6.1.1
RoxygenNote: 7.0.2
LinkingTo:
Rcpp,
AsioHeaders,
BH

2
LICENSE

@ -0,0 +1,2 @@
YEAR: 2019
COPYRIGHT HOLDER: Bob Rudis

21
LICENSE.md

@ -0,0 +1,21 @@
# MIT License
Copyright (c) 2019 Bob Rudis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

5
NAMESPACE

@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand
import(httr)
importFrom(jsonlite,fromJSON)
import(AsioHeaders)
importFrom(Rcpp,sourceCpp)
useDynLib(iptools2, .registration = TRUE)

7
R/RcppExports.R

@ -0,0 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
range_boundaries <- function(cidr) {
.Call(`_iptools2_range_boundaries`, cidr)
}

21
R/iptools2-package.R

@ -1,12 +1,17 @@
#' ...
#'
#' - URL: <https://gitlab.com/hrbrmstr/iptools2>
#' - BugReports: <https://gitlab.com/hrbrmstr/iptools2/issues>
#'
#' A WIP Re-imagining of {iptools}
#'
#' A WIP Re-imagining of {iptools}
#'
#' - URL: <https://git.rud.is/hrbrmstr/iptools2>
#' - BugReports: <https://git.rud.is/hrbrmstr/iptools2/issues>
#'
#' @md
#' @name iptools2
#' @docType package
#' @author Bob Rudis (bob@@rud.is)
#' @import httr
#' @importFrom jsonlite fromJSON
NULL
#' @import AsioHeaders
## usethis namespace: start
#' @importFrom Rcpp sourceCpp
#' @useDynLib iptools2, .registration = TRUE
## usethis namespace: end
NULL

35
README.Rmd

@ -1,31 +1,32 @@
---
output: rmarkdown::github_document
editor_options:
chunk_output_type: inline
chunk_output_type: console
---
```{r pkg-knitr-opts, include=FALSE}
knitr$opts_chunk$set(collapse=TRUE, fig.retina=2, message=FALSE, warning=FALSE)
options(width=120)
hrbrpkghelpr::global_opts()
```
[![Travis-CI Build Status](https://travis-ci.org/hrbrmstr/iptools2.svg?branch=master)](https://travis-ci.org/hrbrmstr/iptools2)
[![Coverage Status](https://codecov.io/gh/hrbrmstr/iptools2/branch/master/graph/badge.svg)](https://codecov.io/gh/hrbrmstr/iptools2)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/iptools2)](https://cran.r-project.org/package=iptools2)
# iptools2
```{r badges, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::stinking_badges()
```
## Description
```{r description, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::yank_title_and_description()
```
## What's Inside The Tin
The following functions are implemented:
```{r ingredients, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::describe_ingredients()
```
## Installation
```{r install-ex, eval=FALSE}
devtools::install_git("https://gitlab.com/hrbrmstr/iptools2.git")
# or
devtools::install_github("hrbrmstr/iptools2")
```{r install-ex, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::install_block()
```
## Usage
@ -38,6 +39,12 @@ packageVersion("iptools2")
```
```{r u-1}
iptools2:::range_boundaries(
c("2001:4801::/32", "2001:0db8::/127", "10.1.10.0/24", "2001:0db8::/119", "steve")
)
```
## iptools2 Metrics
```{r cloc, echo=FALSE}
@ -46,5 +53,5 @@ cloc::cloc_pkg_md()
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).
Please note that this project is released with a Contributor Code of Conduct.
By participating in this project you agree to abide by its terms.

76
README.md

@ -1,2 +1,78 @@
[![Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Signed
by](https://img.shields.io/badge/Keybase-Verified-brightgreen.svg)](https://keybase.io/hrbrmstr)
![Signed commit
%](https://img.shields.io/badge/Signed_Commits-50%25-lightgrey.svg)
[![Linux build
Status](https://travis-ci.org/hrbrmstr/iptools2.svg?branch=master)](https://travis-ci.org/hrbrmstr/iptools2)
![Minimal R
Version](https://img.shields.io/badge/R%3E%3D-3.2.0-blue.svg)
![License](https://img.shields.io/badge/License-MIT-blue.svg)
# iptools2
A WIP Re-imagining of {iptools}
## Description
A WIP Re-imagining of {iptools}
## What’s Inside The Tin
The following functions are implemented:
## Installation
``` r
remotes::install_git("https://git.rud.is/hrbrmstr/iptools2.git")
# or
remotes::install_git("https://git.sr.ht/~hrbrmstr/iptools2")
# or
remotes::install_gitlab("hrbrmstr/iptools2")
# or
remotes::install_bitbucket("hrbrmstr/iptools2")
# or
remotes::install_github("hrbrmstr/iptools2")
```
NOTE: To use the ‘remotes’ install options you will need to have the
[{remotes} package](https://github.com/r-lib/remotes) installed.
## Usage
``` r
library(iptools2)
# current version
packageVersion("iptools2")
## [1] '0.1.0'
```
``` r
iptools2:::range_boundaries(
c("2001:4801::/32", "2001:0db8::/127", "10.1.10.0/24", "2001:0db8::/119", "steve")
)
## cidr canonical start end n_hosts
## 1 2001:4801::/32 2001:4801::/32 2001:4801:: 2001:4802:: 7.922816e+28
## 2 2001:0db8::/127 2001:db8::/127 2001:db8:: 2001:db8::2 2.000000e+00
## 3 10.1.10.0/24 10.1.10.0/24 10.1.10.1 10.1.10.255 2.540000e+02
## 4 2001:0db8::/119 2001:db8::/119 2001:db8:: 2001:db8::200 5.120000e+02
## 5 steve <NA> <NA> <NA> NA
```
## iptools2 Metrics
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: |
| C++ | 2 | 0.29 | 242 | 0.92 | 33 | 0.62 | 7 | 0.12 |
| Rmd | 1 | 0.14 | 11 | 0.04 | 16 | 0.30 | 30 | 0.54 |
| R | 4 | 0.57 | 9 | 0.03 | 4 | 0.08 | 19 | 0.34 |
## Code of Conduct
Please note that this project is released with a Contributor Code of
Conduct. By participating in this project you agree to abide by its
terms.

10
man/iptools2.Rd

@ -3,12 +3,14 @@
\docType{package}
\name{iptools2}
\alias{iptools2}
\alias{iptools2-package}
\title{...}
\title{A WIP Re-imagining of {iptools}}
\description{
A WIP Re-imagining of {iptools}
}
\details{
\itemize{
\item URL: \url{https://gitlab.com/hrbrmstr/iptools2}
\item BugReports: \url{https://gitlab.com/hrbrmstr/iptools2/issues}
\item URL: \url{https://git.rud.is/hrbrmstr/iptools2}
\item BugReports: \url{https://git.rud.is/hrbrmstr/iptools2/issues}
}
}
\author{

3
src/.gitignore

@ -0,0 +1,3 @@
*.o
*.so
*.dll

3
src/Makevars

@ -0,0 +1,3 @@
CXX_STD = CXX11
PKG_CXXFLAGS =
PKG_LIBS =

3
src/Makevars.win

@ -0,0 +1,3 @@
CXX_STD = CXX11
PKG_CXXFLAGS =
PKG_LIBS = -lwsock32 -lws2_32

28
src/RcppExports.cpp

@ -0,0 +1,28 @@
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#include <Rcpp.h>
using namespace Rcpp;
// range_boundaries
DataFrame range_boundaries(std::vector < std::string > cidr);
RcppExport SEXP _iptools2_range_boundaries(SEXP cidrSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector < std::string > >::type cidr(cidrSEXP);
rcpp_result_gen = Rcpp::wrap(range_boundaries(cidr));
return rcpp_result_gen;
END_RCPP
}
static const R_CallMethodDef CallEntries[] = {
{"_iptools2_range_boundaries", (DL_FUNC) &_iptools2_range_boundaries, 1},
{NULL, NULL, 0}
};
RcppExport void R_init_iptools2(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}

254
src/iptools2-main.cpp

@ -0,0 +1,254 @@
// [[Rcpp::depends(BH)]]
// [[Rcpp::depends(AsioHeaders)]]
#include <Rcpp.h>
#include <stdio.h>
#include <asio.hpp>
#include <asio/ip/network_v4.hpp>
#include <asio/ip/network_v6.hpp>
using namespace Rcpp;
using namespace asio;
namespace ip = asio::ip;
static double power_lookup_tbl(unsigned pow) {
static double pow2[] = {
1.0,
2.0,
4.0,
8.0,
16.0,
32.0,
64.0,
128.0,
256.0,
512.0,
1024.0,
2048.0,
4096.0,
8192.0,
16384.0,
32768.0,
65536.0,
131072.0,
262144.0,
524288.0,
1048576.0,
2097152.0,
4194304.0,
8388608.0,
16777216.0,
33554432.0,
67108864.0,
134217728.0,
268435456.0,
536870912.0,
1073741824.0,
2147483648.0,
4294967296.0,
8589934592.0,
17179869184.0,
34359738368.0,
68719476736.0,
137438953472.0,
274877906944.0,
549755813888.0,
1099511627776.0,
2199023255552.0,
4398046511104.0,
8796093022208.0,
17592186044416.0,
35184372088832.0,
70368744177664.0,
140737488355328.0,
281474976710656.0,
562949953421312.0,
1125899906842624.0,
2251799813685248.0,
4503599627370496.0,
9007199254740992.0,
18014398509481984.0,
36028797018963968.0,
72057594037927936.0,
144115188075855872.0,
288230376151711744.0,
576460752303423488.0,
1152921504606846976.0,
2305843009213693952.0,
4611686018427387904.0,
9223372036854775808.0,
18446744073709551616.0,
36893488147419103232.0,
73786976294838206464.0,
147573952589676412928.0,
295147905179352825856.0,
590295810358705651712.0,
1180591620717411303424.0,
2361183241434822606848.0,
4722366482869645213696.0,
9444732965739290427392.0,
18889465931478580854784.0,
37778931862957161709568.0,
75557863725914323419136.0,
151115727451828646838272.0,
302231454903657293676544.0,
604462909807314587353088.0,
1208925819614629174706176.0,
2417851639229258349412352.0,
4835703278458516698824704.0,
9671406556917033397649408.0,
19342813113834066795298816.0,
38685626227668133590597632.0,
77371252455336267181195264.0,
154742504910672534362390528.0,
309485009821345068724781056.0,
618970019642690137449562112.0,
1237940039285380274899124224.0,
2475880078570760549798248448.0,
4951760157141521099596496896.0,
9903520314283042199192993792.0,
19807040628566084398385987584.0,
39614081257132168796771975168.0,
79228162514264337593543950336.0,
158456325028528675187087900672.0,
316912650057057350374175801344.0,
633825300114114700748351602688.0,
1267650600228229401496703205376.0,
2535301200456458802993406410752.0,
5070602400912917605986812821504.0,
10141204801825835211973625643008.0,
20282409603651670423947251286016.0,
40564819207303340847894502572032.0,
81129638414606681695789005144064.0,
162259276829213363391578010288128.0,
324518553658426726783156020576256.0,
649037107316853453566312041152512.0,
1298074214633706907132624082305024.0,
2596148429267413814265248164610048.0,
5192296858534827628530496329220096.0,
10384593717069655257060992658440192.0,
20769187434139310514121985316880384.0,
41538374868278621028243970633760768.0,
83076749736557242056487941267521536.0,
166153499473114484112975882535043072.0,
332306998946228968225951765070086144.0,
664613997892457936451903530140172288.0,
1329227995784915872903807060280344576.0,
2658455991569831745807614120560689152.0,
5316911983139663491615228241121378304.0,
10633823966279326983230456482242756608.0,
21267647932558653966460912964485513216.0,
42535295865117307932921825928971026432.0,
85070591730234615865843651857942052864.0,
170141183460469231731687303715884105728.0,
};
if (pow <= 127)
return pow2[pow];
return -1.0;
}
struct split {
enum empties_t { empties_ok, no_empties };
};
template <typename Container>
Container& split(
Container& result,
const typename Container::value_type& s,
const typename Container::value_type& delimiters,
split::empties_t empties = split::empties_ok )
{
result.clear();
size_t current;
size_t next = -1;
do
{
if (empties == split::no_empties)
{
next = s.find_first_not_of( delimiters, next + 1 );
if (next == Container::value_type::npos) break;
next -= 1;
}
current = next + 1;
next = s.find_first_of( delimiters, current );
result.push_back( s.substr( current, next - current ) );
}
while (next != Container::value_type::npos);
return result;
}
// [[Rcpp::export]]
DataFrame range_boundaries(std::vector < std::string > cidr) {
StringVector cstart(cidr.size());
StringVector cend(cidr.size());
StringVector canon(cidr.size());
NumericVector sz(cidr.size());
for (R_xlen_t i = 0; i < cidr.size(); i++) {
if (cidr[i].find(":") != std::string::npos) {
try {
ip::network_v6 net = ip::make_network_v6(cidr[i]);
ip::address_v6_range rng = net.hosts();
sz[i] = power_lookup_tbl(128-net.prefix_length());
canon[i] = net.canonical().to_string();
cstart[i] = (rng.begin())->to_string();
cend[i] = (rng.end())->to_string();
} catch (...) {
sz[i] = NA_REAL;
cstart[i] = NA_STRING;
cend[i] = NA_STRING;
canon[i] = NA_STRING;
}
} else {
try {
ip::network_v4 net = ip::make_network_v4(cidr[i]);
ip::address_v4_range rng = net.hosts();
if (net.prefix_length() >= 31) {
sz[i] = power_lookup_tbl(32 - net.prefix_length());
} else {
sz[i] = (1 << (32 - net.prefix_length())) - 2;
};
canon[i] = net.canonical().to_string();
cstart[i] = (rng.begin())->to_string();
cend[i] = (rng.end())->to_string();
} catch(...) {
sz[i] = NA_REAL;
cstart[i] = NA_STRING;
cend[i] = NA_STRING;
canon[i] = NA_STRING;
}
}
}
return(
DataFrame::create(
_["cidr"] = cidr,
_["canonical"] = canon,
_["start"] = cstart,
_["end"] = cend,
_["n_hosts"] = sz,
_["stringsAsFactors"] = false
)
);
}
// iptools2:::range_boundaries(c("2001:4801::/32", "2001:0db8::/127", "10.1.10.0/24", "2001:0db8::/119", "steve"))
Loading…
Cancel
Save