Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isPublicIP first commit #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export(whatismyip)
export(hasIPformat)
export(getIPaddress)
export(freegeoip)
export(isPublicIP)
24 changes: 23 additions & 1 deletion R/net.tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
36 changes: 36 additions & 0 deletions tests/testIsPublicIP.R
Original file line number Diff line number Diff line change
@@ -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