-
-
Save azilber/c79e891157549757b58ebe1e3ec57c43 to your computer and use it in GitHub Desktop.
Go: Convert CIDR to IP
This file contains 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
// Convert CIDR to IPv4 range | |
func CIDRRangeToIPv4Range(cidr string) (ipStart string, ipEnd string, err error) { | |
var ip uint32 // ip address | |
var ipS uint32 // Start IP address range | |
var ipE uint32 // End IP address range | |
cidrParts := strings.Split(cidr, "/") | |
ip = iPv4ToUint32(cidrParts[0]) | |
bits, _ := strconv.ParseUint(cidrParts[1], 10, 32) | |
if ipS == 0 || ipS > ip { | |
ipS = ip | |
} | |
ip = ip | (0xFFFFFFFF >> bits) | |
if ipE < ip { | |
ipE = ip | |
} | |
ipStart = uInt32ToIPv4(ipS) | |
ipEnd = uInt32ToIPv4(ipE) | |
return ipStart, ipEnd, err | |
} | |
//Convert IPv4 to uint32 | |
func iPv4ToUint32(iPv4 string ) uint32 { | |
ipOctets := [4]uint64{} | |
for i, v := range strings.SplitN(iPv4,".", 4) { | |
ipOctets[i], _ = strconv.ParseUint(v, 10, 32) | |
} | |
result := (ipOctets[0] << 24) | (ipOctets[1] << 16) | (ipOctets[2] << 8) | ipOctets[3] | |
return uint32(result) | |
} | |
//Convert uint32 to IP | |
func uInt32ToIPv4(iPuInt32 uint32) (iP string) { | |
iP = fmt.Sprintf ("%d.%d.%d.%d", | |
iPuInt32 >> 24, | |
(iPuInt32 & 0x00FFFFFF)>> 16, | |
(iPuInt32 & 0x0000FFFF) >> 8, | |
iPuInt32 & 0x000000FF) | |
return iP | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment