Created
July 12, 2013 17:45
-
-
Save r4um/5986319 to your computer and use it in GitHub Desktop.
Check ipv4 address is rfc1918
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net" | |
"fmt" | |
) | |
func IsPrivateIPv4(s string) bool { | |
// RFC 1918 CIDRS | |
var rfc1918_cidrs []string = []string{"10.0.0.0/8","172.16.0.0/12","192.168.0.0/16"} | |
var ip net.IP = net.ParseIP(s) | |
if ip == nil { | |
return false | |
} | |
for _, cidr := range rfc1918_cidrs { | |
_, net, _ := net.ParseCIDR(cidr) | |
if net.Contains(ip) { | |
return true | |
} | |
} | |
return false | |
} | |
func main(){ | |
var addrs []string = []string{"10.1.1.1", "192.168.10.2", "20.202.1.1"} | |
for _, addr := range addrs { | |
fmt.Println("IsPrivateIPv4", addr, IsPrivateIPv4(addr)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment