Skip to content

Commit

Permalink
pre-flight
Browse files Browse the repository at this point in the history
  • Loading branch information
hrbrmstr committed Sep 16, 2018
1 parent 16f0a46 commit 9581431
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 83 deletions.
3 changes: 0 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ Imports:
utils,
stats,
AsioHeaders,
dplyr,
readr,
tidyr,
stringi,
triebeard
Encoding: UTF-8
Expand Down
7 changes: 1 addition & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 17 additions & 17 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

54 changes: 34 additions & 20 deletions R/cidr.r
Original file line number Diff line number Diff line change
@@ -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)

}

Expand Down Expand Up @@ -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

}

Expand All @@ -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)))

}
10 changes: 1 addition & 9 deletions R/iptools.R → R/iptools-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions man/asn_table_to_trie.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/iptools.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 19 additions & 9 deletions src/asio_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <Rcpp.h>
#include <stdio.h>

#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 <asio.hpp>
#ifdef __APPLE__
#pragma clang diagnostic pop
#endif
// #ifdef __APPLE__
// #pragma clang diagnostic pop
// #endif

#include "asio_bindings.h"

Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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");
Expand Down
15 changes: 8 additions & 7 deletions src/asio_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

#include <Rcpp.h>

#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 <asio.hpp>
#ifdef __APPLE__
#pragma clang diagnostic pop
#endif
// #ifdef __APPLE__
// #pragma clang diagnostic pop
// #endif

#include <iostream>
#include <sstream>

Expand Down
16 changes: 8 additions & 8 deletions src/iptools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <Rcpp.h>
#include <cstdint>

#ifdef __APPLE__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-local-typedef"
#endif
#include <asio.hpp>
#ifdef __APPLE__
#pragma clang diagnostic pop
#endif
// #ifdef __APPLE__
// #pragma clang diagnostic push
// #pragma clang diagnostic ignored "-Wunused-local-typedef"
// #endif
// #include <asio.hpp>
// #ifdef __APPLE__
// #pragma clang diagnostic pop
// #endif

#include <bitset>

Expand Down

0 comments on commit 9581431

Please sign in to comment.