In this function we state that if the protocol is nil the first service matching the port number is returned. It seems like that's not the case (or I'm misunderstanding).
// GetServByPort returns the Servent for a given port number and
// protocol. If the protocol is nil, the first service matching the
// port number is returned.
func GetServByPort(port int, protocol *Protoent) *Servent {
for _, servent := range Services {
if servent.Port == port && servent.Protocol.Equal(protocol) {
return servent
}
}
return nil
}
A use like the following:
servent := GetServByPort(port, nil)
Always returns nil for me. I believe this is due to how the equality between protocols is handled. Which returns true if the servent protocol is nil AND the protocol parameter is nil. We can see this here:
// Equal checks if two Protoents are the same, which is the case if
// their protocol numbers are identical or when both Protoents are
// nil.
func (this *Protoent) Equal(other *Protoent) bool {
if this == nil && other == nil {
return true
}
if this == nil || other == nil {
return false
}
return this.Number == other.Number
}
While I believe this is intentional, the documentation for GetServByPort seems inaccurate given its implementation. Something along the lines of:
// GetServByPort returns the Servent for a given port number and
// protocol. If the protocol is nil, the first service matching the
// port number is returned.
func GetServByPort(port int, protocol *Protoent) *Servent {
for _, servent := range Services {
if protocol == nil && servent.Port == port {
return servent
}
if servent.Port == port && servent.Protocol.Equal(protocol) {
return servent
}
}
return nil
}
might be more accurate.
I wanted to double check if this was correct before submitting a PR for this. Thanks for this library, it's been quite useful!
In this function we state that if the protocol is
nilthe first service matching the port number is returned. It seems like that's not the case (or I'm misunderstanding).A use like the following:
Always returns
nilfor me. I believe this is due to how the equality between protocols is handled. Which returnstrueif the servent protocol isnilAND the protocol parameter isnil. We can see this here:While I believe this is intentional, the documentation for
GetServByPortseems inaccurate given its implementation. Something along the lines of:might be more accurate.
I wanted to double check if this was correct before submitting a PR for this. Thanks for this library, it's been quite useful!