From 9581431fe459e3d441cefb2157b51c89b84ca4f3 Mon Sep 17 00:00:00 2001 From: hrbrmstr Date: Sun, 16 Sep 2018 16:23:42 -0400 Subject: [PATCH] pre-flight --- DESCRIPTION | 3 -- NAMESPACE | 7 +--- NEWS.md | 2 +- R/RcppExports.R | 34 +++++++++---------- R/cidr.r | 54 +++++++++++++++++++----------- R/{iptools.R => iptools-package.R} | 10 +----- man/asn_table_to_trie.Rd | 4 +-- man/iptools.Rd | 2 +- src/asio_bindings.cpp | 28 +++++++++++----- src/asio_bindings.h | 15 +++++---- src/iptools.cpp | 16 ++++----- 11 files changed, 92 insertions(+), 83 deletions(-) rename R/{iptools.R => iptools-package.R} (56%) diff --git a/DESCRIPTION b/DESCRIPTION index 95ca202..c57b905 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,9 +26,6 @@ Imports: utils, stats, AsioHeaders, - dplyr, - readr, - tidyr, stringi, triebeard Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index 12bc06e..3083666 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -39,9 +39,4 @@ import(stringi) import(triebeard) import(utils) importFrom(Rcpp,sourceCpp) -importFrom(dplyr,"%>%") -importFrom(dplyr,data_frame) -importFrom(dplyr,mutate) -importFrom(readr,read_tsv) -importFrom(tidyr,separate) -useDynLib(iptools, .registration=TRUE) +useDynLib(iptools) diff --git a/NEWS.md b/NEWS.md index 0c33eb3..7cd4896 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -iptools 0.6.0 [WIP] +iptools 0.6.0 ============= * `range_boundaries_to_cidr()` will take a start/stop numeric range and turn it into representative/encompassing CIDR blocks diff --git a/R/RcppExports.R b/R/RcppExports.R index 59dc005..bee4f0d 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -7,7 +7,7 @@ #' @param bpp Hilbert depth (max 12) #' @export hilbert_encode <- function(x, bpp = 8L) { - .Call(`_iptools_hilbert_encode`, x, bpp) + .Call('_iptools_hilbert_encode', PACKAGE = 'iptools', x, bpp) } #' @title Convert a start+end IP address range pair to representative CIDR blocks @@ -23,7 +23,7 @@ hilbert_encode <- function(x, bpp = 8L) { #' ) #' ## [1] "192.100.176.0/22" range_boundaries_to_cidr <- function(ip_start, ip_end) { - .Call(`_iptools_range_boundaries_to_cidr`, ip_start, ip_end) + .Call('_iptools_range_boundaries_to_cidr', PACKAGE = 'iptools', ip_start, ip_end) } #' @title Returns the IP addresses associated with a hostname. @@ -58,7 +58,7 @@ range_boundaries_to_cidr <- function(ip_start, ip_end) { #' } #' @export hostname_to_ip <- function(hostnames) { - .Call(`_iptools_hostname_to_ip`, hostnames) + .Call('_iptools_hostname_to_ip', PACKAGE = 'iptools', hostnames) } #' @title Return the hostname associated with particular IP addresses @@ -84,7 +84,7 @@ hostname_to_ip <- function(hostnames) { #' #' @export ip_to_hostname <- function(ip_addresses) { - .Call(`_iptools_ip_to_hostname`, ip_addresses) + .Call('_iptools_ip_to_hostname', PACKAGE = 'iptools', ip_addresses) } #' @title convert between numeric and dotted-decimal IPv4 forms. @@ -117,7 +117,7 @@ ip_to_hostname <- function(ip_addresses) { #' @rdname ip_numeric #' @export ip_to_numeric <- function(ip_addresses) { - .Call(`_iptools_ip_to_numeric`, ip_addresses) + .Call('_iptools_ip_to_numeric', PACKAGE = 'iptools', ip_addresses) } #' Return the scope of an IPv6 address (string) @@ -127,7 +127,7 @@ ip_to_numeric <- function(ip_addresses) { #' @return a numeric vector of scopes #' @export v6_scope <- function(ip_addresses) { - .Call(`_iptools_v6_scope`, ip_addresses) + .Call('_iptools_v6_scope', PACKAGE = 'iptools', ip_addresses) } #' Expand an IPv6 address from an abbreviated version @@ -137,13 +137,13 @@ v6_scope <- function(ip_addresses) { #' @return a character vector of expanded IPv6 addresses #' @export expand_ipv6 <- function(ip_addresses) { - .Call(`_iptools_expand_ipv6`, ip_addresses) + .Call('_iptools_expand_ipv6', PACKAGE = 'iptools', ip_addresses) } #' @rdname ip_numeric #' @export numeric_to_ip <- function(ip_addresses) { - .Call(`_iptools_numeric_to_ip`, ip_addresses) + .Call('_iptools_numeric_to_ip', PACKAGE = 'iptools', ip_addresses) } #'@title Identify whether an IP address is IPv4 or IPv6 @@ -176,7 +176,7 @@ numeric_to_ip <- function(ip_addresses) { #' #'@export ip_classify <- function(ip_addresses) { - .Call(`_iptools_ip_classify`, ip_addresses) + .Call('_iptools_ip_classify', PACKAGE = 'iptools', ip_addresses) } #' @title calculate the maximum and minimum IPs in an IP range @@ -205,7 +205,7 @@ ip_classify <- function(ip_addresses) { #' #' @export range_boundaries <- function(ranges) { - .Call(`_iptools_range_boundaries`, ranges) + .Call('_iptools_range_boundaries', PACKAGE = 'iptools', ranges) } #'@title check if IP addresses fall within particular IP ranges @@ -235,7 +235,7 @@ range_boundaries <- function(ranges) { #' #'@export ip_in_range <- function(ip_addresses, ranges) { - .Call(`_iptools_ip_in_range`, ip_addresses, ranges) + .Call('_iptools_ip_in_range', PACKAGE = 'iptools', ip_addresses, ranges) } #'@title check if IP address falls within any of the ranges specified @@ -271,7 +271,7 @@ ip_in_range <- function(ip_addresses, ranges) { #'} #'@export ip_in_any <- function(ip_addresses, ranges) { - .Call(`_iptools_ip_in_any`, ip_addresses, ranges) + .Call('_iptools_ip_in_any', PACKAGE = 'iptools', ip_addresses, ranges) } #'@title check whether IPv4 ranges are valid @@ -297,7 +297,7 @@ ip_in_any <- function(ip_addresses, ranges) { #' #' @export validate_range <- function(ranges) { - .Call(`_iptools_validate_range`, ranges) + .Call('_iptools_validate_range', PACKAGE = 'iptools', ranges) } #'@title Take vectors of IPs and X-Forwarded-For headers and produce single, normalised @@ -319,7 +319,7 @@ validate_range <- function(ranges) { #' #'@export xff_extract <- function(ip_addresses, x_forwarded_for) { - .Call(`_iptools_xff_extract`, ip_addresses, x_forwarded_for) + .Call('_iptools_xff_extract', PACKAGE = 'iptools', ip_addresses, x_forwarded_for) } #'@title Logical checks for IP addresses @@ -348,7 +348,7 @@ xff_extract <- function(ip_addresses, x_forwarded_for) { #'@rdname is_checks #'@export is_multicast <- function(ip_addresses) { - .Call(`_iptools_is_multicast`, ip_addresses) + .Call('_iptools_is_multicast', PACKAGE = 'iptools', ip_addresses) } #' Convert a charcter vector of IPv4 addresses to a character vector of @@ -357,7 +357,7 @@ is_multicast <- function(ip_addresses) { #' @param input numeric vector of IP addresses #' @export ip_numeric_to_binary_string <- function(input) { - .Call(`_iptools_ip_numeric_to_binary_string`, input) + .Call('_iptools_ip_numeric_to_binary_string', PACKAGE = 'iptools', input) } #' Convert a numeric vector of IPv4 addresses to a character vector of @@ -366,6 +366,6 @@ ip_numeric_to_binary_string <- function(input) { #' @param input character vector of IP addresses #' @export ip_to_binary_string <- function(input) { - .Call(`_iptools_ip_to_binary_string`, input) + .Call('_iptools_ip_to_binary_string', PACKAGE = 'iptools', input) } diff --git a/R/cidr.r b/R/cidr.r index d2b1bec..ab7700d 100644 --- a/R/cidr.r +++ b/R/cidr.r @@ -1,15 +1,23 @@ -#' Convert a \emph{pyasn} generated CIDR dat to a trie +#' Convert a \emph{pyasn} generated CIDR data file to a trie #' #' @param asn_table_file filename of dat file (can be gzip'd) #' @export asn_table_to_trie <- function(asn_table_file) { - rip <- readr::read_tsv(asn_table_file, comment=";", col_names=c("cidr", "asn")) - rip %>% - tidyr::separate(cidr, c("ip", "mask"), "/") %>% - dplyr::mutate(prefix=stringi::stri_sub(ip_to_binary_string(ip), 1, mask)) -> rip_df + read.csv( + file = asn_table_file, + sep = "\t", + comment.char = ";", + col.names = c("cidr", "asn") + ) -> rip - triebeard::trie(rip_df$prefix, rip_df$asn) + cidr_split <- stri_split_fixed(rip$cidr, "/", 2, simplify = TRUE) + + ip <- cidr_split[,1] + mask <- cidr_split[,1] + prefix = stri_sub(ip_to_binary_string(ip), 1, mask) + + triebeard::trie(prefix, rip$asn) } @@ -39,19 +47,25 @@ ip_to_asn <- function(cidr_trie, ip) { #' @export ips_in_cidrs <- function(ips, cidrs) { - cidrs[!grepl("/", cidrs)] <- sprintf("%s/32", cidrs[!grepl("/", cidrs)]) + cidrs[!stri_detect_fixed(cidrs, "/")] <- sprintf("%s/32", cidrs[!stri_detect_fixed(cidrs, "/")]) + + cidr_split <- stri_split_fixed(cidr, "/", 2, simplify = TRUE) + + ip <- cidr_split[,1] + mask <- cidr_split[,2] + prefix <- stri_sub(ip_to_binary_string(ip), 1, mask) + value <- rep(TRUE, length(prefix)) + + cidr_trie <- trie(prefix, value) - dplyr::data_frame(cidr=cidrs) %>% - tidyr::separate(cidr, c("ip", "mask"), "/") %>% - dplyr::mutate(prefix=stri_sub(ip_to_binary_string(ip), 1, mask), - value=TRUE) -> tr + data.frame( + ips = ips, + in_cidr = !is.na(longest_match(cidr_trie, ip_to_binary_string(ips))) + ) -> out - cidr_trie <- trie(tr$prefix, tr$value) + class(out) <- c("tbl_df", "tbl", "data.frame") - dplyr::data_frame(ips=ips, - in_cidr=longest_match(cidr_trie, - ip_to_binary_string(ips))) %>% - mutate(in_cidr=!is.na(in_cidr)) + out } @@ -67,9 +81,9 @@ host_count <- function(cidrs) { cidrs[!is_cidr] <- sprintf("%s/32", cidrs[!is_cidr]) - stri_split_fixed(cidrs, "/") %>% - sapply("[", 2) %>% - as.numeric() %>% - sapply(function(x) (2^(32-x))) + tmp <- stri_split_fixed(cidrs, "/") + tmp <- sapply(tmp, "[", 2) + tmp <- as.numeric(tmp) + sapply(tmp, function(x) (2^(32-x))) } \ No newline at end of file diff --git a/R/iptools.R b/R/iptools-package.R similarity index 56% rename from R/iptools.R rename to R/iptools-package.R index 0a15f86..f0ec850 100644 --- a/R/iptools.R +++ b/R/iptools-package.R @@ -7,17 +7,9 @@ #' #' @name iptools #' @docType package -#' @useDynLib iptools, .registration=TRUE +#' @useDynLib iptools #' @importFrom Rcpp sourceCpp -# @import grid -# @importFrom RColorBrewer brewer.pal -# @importFrom ggplot2 ggplot geom_point aes labs scale_color_manual element_blank -# @importFrom ggplot2 scale_x_continuous scale_y_reverse coord_equal -# @importFrom ggplot2 element_blank element_rect element_text theme theme_bw -#' @importFrom readr read_tsv #' @import stringi -#' @importFrom tidyr separate -#' @importFrom dplyr mutate data_frame %>% #' @import triebeard #' @import utils #' @import stats diff --git a/man/asn_table_to_trie.Rd b/man/asn_table_to_trie.Rd index 95f4d07..6b661e5 100644 --- a/man/asn_table_to_trie.Rd +++ b/man/asn_table_to_trie.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/cidr.r \name{asn_table_to_trie} \alias{asn_table_to_trie} -\title{Convert a \emph{pyasn} generated CIDR dat to a trie} +\title{Convert a \emph{pyasn} generated CIDR data file to a trie} \usage{ asn_table_to_trie(asn_table_file) } @@ -10,5 +10,5 @@ asn_table_to_trie(asn_table_file) \item{asn_table_file}{filename of dat file (can be gzip'd)} } \description{ -Convert a \emph{pyasn} generated CIDR dat to a trie +Convert a \emph{pyasn} generated CIDR data file to a trie } diff --git a/man/iptools.Rd b/man/iptools.Rd index 906082e..d4ad802 100644 --- a/man/iptools.Rd +++ b/man/iptools.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/iptools.R +% Please edit documentation in R/iptools-package.R \docType{package} \name{iptools} \alias{iptools} diff --git a/src/asio_bindings.cpp b/src/asio_bindings.cpp index 4001984..9f1feee 100644 --- a/src/asio_bindings.cpp +++ b/src/asio_bindings.cpp @@ -4,14 +4,14 @@ #include #include -#ifdef __APPLE__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-local-typedef" -#endif +// #ifdef __APPLE__ +// #pragma clang diagnostic push +// #pragma clang diagnostic ignored "-Wunused-local-typedef" +// #endif #include -#ifdef __APPLE__ -#pragma clang diagnostic pop -#endif +// #ifdef __APPLE__ +// #pragma clang diagnostic pop +// #endif #include "asio_bindings.h" @@ -232,7 +232,12 @@ bool asio_bindings::single_ip_in_range(std::string ip_address, std::string range char *slash_pos; bool output = false; - strncpy(range_copy, range.c_str(), 24); // safe copy + int sz = strnlen(range.c_str(), 23); + + // safe'r' copy according to CRAN & gcc-8 + memcpy(&range_copy[0], range.c_str(), sz); + range_copy[sz] = '\0'; + // strncpy(range_copy, range.c_str(), 24); // safe copy slash_pos = strchr(range_copy, '/'); // find the "/" if(slash_pos == NULL){ @@ -266,7 +271,12 @@ std::vector < std::string > asio_bindings::calculate_ip_range(std::string range) char *slash_pos; std::vector < std::string > output; - strncpy(cidr_copy, range.c_str(), 24); + int sz = strnlen(range.c_str(), 23); + + // safe'r' copy according to CRAN & gcc-8 + memcpy(&cidr_copy[0], range.c_str(), sz); + cidr_copy[sz] = '\0'; + // strncpy(cidr_copy, range.c_str(), 24); slash_pos = strchr(cidr_copy, '/'); if (slash_pos == NULL){ output.push_back("Invalid"); diff --git a/src/asio_bindings.h b/src/asio_bindings.h index bf39ed5..62456fb 100644 --- a/src/asio_bindings.h +++ b/src/asio_bindings.h @@ -3,14 +3,15 @@ #include -#ifdef __APPLE__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-local-typedef" -#endif +// #ifdef __APPLE__ +// #pragma clang diagnostic push +// #pragma clang diagnostic ignored "-Wunused-local-typedef" +// #endif #include -#ifdef __APPLE__ -#pragma clang diagnostic pop -#endif +// #ifdef __APPLE__ +// #pragma clang diagnostic pop +// #endif + #include #include diff --git a/src/iptools.cpp b/src/iptools.cpp index f9bb3c2..b925ced 100644 --- a/src/iptools.cpp +++ b/src/iptools.cpp @@ -4,14 +4,14 @@ #include #include -#ifdef __APPLE__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-local-typedef" -#endif -#include -#ifdef __APPLE__ -#pragma clang diagnostic pop -#endif +// #ifdef __APPLE__ +// #pragma clang diagnostic push +// #pragma clang diagnostic ignored "-Wunused-local-typedef" +// #endif +// #include +// #ifdef __APPLE__ +// #pragma clang diagnostic pop +// #endif #include