Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

GetServByPort documentation does not reflect actual implementation #1

@Gabri3l

Description

@Gabri3l

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions