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

net: Addr and Interface documentation is not clear #31276

Open
AlekSi opened this issue Apr 5, 2019 · 2 comments
Open

net: Addr and Interface documentation is not clear #31276

AlekSi opened this issue Apr 5, 2019 · 2 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@AlekSi
Copy link
Contributor

AlekSi commented Apr 5, 2019

What version of Go are you using (go version)?

go version go1.12.1 darwin/amd64

I also checked older versions – they have the same problem.

What did you do?

package main

import (
    "log"
    "net"
)

func main() {
    ifaces, _ := net.Interfaces()
    for _, iface := range ifaces {
        addrs, _ := iface.Addrs()
        for _, addr := range addrs {
            log.Print(addr.String())
        }
    }
}

What did you expect to see?

Addresses without network mask.

type Addr interface {
        Network() string // name of the network (for example, "tcp", "udp")
        String() string  // string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
}

What did you see instead?

Addresses with network mask.

2019/04/05 14:12:06 127.0.0.1/8
2019/04/05 14:12:06 ::1/128
2019/04/05 14:12:06 fe80::1/64
2019/04/05 14:12:06 192.168.1.35/24
…

I think net.Addr and net.Interface.Addrs() documentation should mention that. Probably the best way is to say that it returns *IPNets.

@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 10, 2019
@mennis
Copy link

mennis commented Apr 26, 2019

Addr is more general than local network interfaces as indicated by the comment above it:

// The two methods Network and String conventionally return strings
// that can be passed as the arguments to Dial, but the exact form
// and meaning of the strings is up to the implementation.

and net.Interface.Addrs() is more general than IP .

net.Interface.Addrs() does return []Addr. In this case the type of Addr the system returns is IPNet. I would have also found this to be surprising (that I need to call ParseCIDR on addr.String() to get the IP portion); however, if a local interface has an IP address IPNet is more complete as the mask is important to making decisions about broadcasting and routing.

@zalegrala
Copy link

I find it confusing that the net.IPNet returned from a net.ParseCIDR is not the same as one returned from interface.Addrs(). The docs mention "meaning of the strings is up to the implementation", but when working with net.IPNet from a parse, the IP is the network address.

https://go.dev/play/p/jPSumAvd3RM

Is there another approach to getting a net.IPNet object that behaves like the one from interface.Addrs() without actually making the syscall?

@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants