-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
Why IP returned by net.ParseIP length is 16 but the length of net.ParseNet.IP is 4? #41214
Comments
@chartol can you please update your issue with a piece of sample code that demonstrates the issue. Thank you. |
Possible duplicate of #40606 |
// listIPsInRange will list all the ips in range, the range will be the whole network
// when begin and end is empty.
func listIPsInRange(cidr string, begin string, end string) (ips []string) {
// convert string to IPNet struct
_, ipv4Net, _ := net.ParseCIDR(cidr)
mask := binary.BigEndian.Uint32(ipv4Net.Mask)
var start uint32
var finish uint32
if begin != "" && end != "" {
beginIP := net.ParseIP(begin)
beginB := beginIP[12:] // But the IP returned by ParseIP is 16 bytes, I have to split it.
endIP := net.ParseIP(end)
endB := endIP[12:] // But the IP returned by ParseIP is 16 bytes, I have to split it.
start = binary.BigEndian.Uint32(beginB)
finish = binary.BigEndian.Uint32(endB)
} else {
start = binary.BigEndian.Uint32(ipv4Net.IP) // The IPNet.IP can transfer easily like this.
finish = (start & mask) | (mask ^ 0xffffffff)
}
// loop through addresses as uint32
for i := start; i <= finish; i++ {
// convert back to net.IP
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, i)
fmt.Println(ip)
ips = append(ips, ip.String())
}
if begin != "" && end != "" {
return ips
}
// Ignore network address and broadcast address
return ips[1 : len(ips)-1]
} |
What are the values of |
@davecheney I want get a ip list between begin IP and end IP of a CIDR. |
@davecheney I want a ip list between begin IP and end IP of a CIDR.
output:
issue #40606 is exactly same as mine. |
What I mean is that IPNet.IP is different from net.IP, one is 16 bytes which contains IP and mask, the other is 4 bytes which contains IP only. I think the net library should have a description at least. Or make me so confused. |
Here is a smaller reproduction package main
|
$ cat test.go
package main
import (
"fmt"
"net"
)
func main() {
cidr := "192.168.9.20/24"
_, netIP, _ := net.ParseCIDR(cidr)
begin := "192.168.9.20"
beginIP := net.ParseIP(begin)
beginB := beginIP[12:]
fmt.Println(len(beginIP), len(beginB), len(netIP.IP))
}
$ go run test.go
16 4 4 So why the length of netIP.IP returned by net.ParseCIDR is 4 but 16? |
The only answer I have is, this is just how it works. If you want to assert that the address is an ipv4 address is to call the I don't think this is a bug, just an limitation of the standard library. This is likely a duplicate of #18804 |
OK, fine, issue closed. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
The length of IP returned by net.ParseIP should equal to net.ParseCIDR's IP, or will make me confused
What did you see instead?
The text was updated successfully, but these errors were encountered: