diff --git a/NAMESPACE b/NAMESPACE index 20419f7..e4680a0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,3 +5,4 @@ export(whatismyip) export(hasIPformat) export(getIPaddress) export(freegeoip) +export(isPublicIP) diff --git a/R/net.tools.R b/R/net.tools.R index bd4cfc2..3199b9f 100644 --- a/R/net.tools.R +++ b/R/net.tools.R @@ -127,4 +127,26 @@ freegeoip <- function(ip, format = ifelse(length(ip) == 1,'list','dataframe')) { } return(ret) } -} +} + +#' isPublicIP +#' Returns TRUE for Public IP and returns FALSE for Private IP +#' +#' @param ip +#' +#' @return +#' Boolean +#' @export +#' +#' @examples +#' ip <- "0.0.0.0" (Public IP) returns TRUE +#' ip <- "10.0.0.0" (Private IP) returns FALSE +isPublicIP <- function(ip) { + + longIP <- ip2long(ip) + return(ifelse(longIP < 167772160 || + (longIP > 184549375 && longIP < 2851995648) || + (longIP > 2852061183 && longIP < 2886729728) || + (longIP > 2887778303 && longIP < 3232235520) || + (longIP > 3232301055), "TRUE", "FALSE")) +} diff --git a/tests/testIsPublicIP.R b/tests/testIsPublicIP.R new file mode 100644 index 0000000..a487578 --- /dev/null +++ b/tests/testIsPublicIP.R @@ -0,0 +1,36 @@ +#Testing isPublicIP + +#Primer rango de IPs publicas +test1 <- "0.0.0.0" +test2 <- "9.255.255.255" + +#Primer rango de IPs privadas +test3 <- "10.0.0.0" +test4 <- "10.255.255.255" + +#Segundo rango de IPs publicas +test5 <- "11.0.0.0" +test6 <- "169.253.255.255" + +#Segundo rango de IPs privadas +test7 <- "169.254.0.0" +test8 <- "169.254.255.255" + +#Tercer rango de IPs publicas +test5 <- "169.255.0.0" +test6 <- "172.15.255.255" + +#Tercer rango de IPs privadas +test7 <- "172.16.0.0" +test8 <- "172.31.255.255" + +#Cuarto rango de IPs publicas +test9 <- "172.32.0.0" +test10 <- "192.167.255.255" + +#Cuarto rango de IPs privadas +test11 <- "192.168.0.0" +test12 <- "192.168.255.255" + +#Quinto rango de IPs publicas +test13 <- "192.169.0.0" #En adelante