github.com/prebid/prebid-server/v2@v2.18.0/util/iputil/parse.go (about) 1 package iputil 2 3 import ( 4 "net" 5 "strings" 6 ) 7 8 // IPVersion is the numerical version of the IP address spec (4 or 6). 9 type IPVersion int 10 11 // IP address versions. 12 const ( 13 IPvUnknown IPVersion = 0 14 IPv4 IPVersion = 4 15 IPv6 IPVersion = 6 16 ) 17 18 const ( 19 IPv4BitSize = 32 20 IPv6BitSize = 128 21 22 IPv4DefaultMaskingBitSize = 24 23 IPv6DefaultMaskingBitSize = 56 24 ) 25 26 // ParseIP parses v as an ip address returning the result and version, or nil and unknown if invalid. 27 func ParseIP(v string) (net.IP, IPVersion) { 28 if ip := net.ParseIP(v); ip != nil { 29 if strings.ContainsRune(v, ':') { 30 return ip, IPv6 31 } 32 return ip, IPv4 33 } 34 return nil, IPvUnknown 35 }