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

Open

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!

Activity

  1. dominikh commented on Jun 24, 2020

    @dominikh
    Owner

    Congratulations for finding one of my first ever Go projects. Your prize? A poorly written package.

    Yes, GetServByPort doesn't seem to behave as documented. The same is probably true for GetServByName, too.

    I'm not sure there's much point in fixing that bug, however. The entire package is in dire need of a rewrite. Its API is bad in way too many ways, including parsing files in init, and panicking in init… I guess I'll have to put a rewrite on my to-do list. But I'd also accept a PR that fixes this specific bug.

  2. Gabri3l commented on Jun 25, 2020

    @Gabri3l
    Author

    Hi @dominikh ! I'm surprised I got a response, thank you! I noticed this was from years ago so no worries. I just stumbled upon it randomly for something I needed to do and I learned a few things thanks to your early work. If you don't feel like rewriting this and you have a mental list of things you would change feel free to share and I might just help out.

  3. dominikh commented on Aug 30, 2020

    @dominikh
    Owner

    @Gabri3l my suggestion is to take the code and run. Or don't, really. The entirety of this package would be better implemented as two maps, go generate'd from some source.

    things you would change

    Virtually everything. We shouldn't

    • parse files in init
    • panic in init
    • fail on non-unix systems
    • be parsing anything in the first place
    • have APIs that make it easy to accidentally modify global state

    All this needs to be is two maps - probably generated from the files we're currently parsing - and a trivial API on top of those maps.

    And I don't think that code has to live with me, either. It's not like this package has thousands of users that would benefit from a v2 with a new API.

  4. thediveo commented on Sep 16, 2021

    @thediveo

    In some sense, I've picked up the baton after having come across this issue a few days ago. @thediveo/netdb is written from scratch, with (hopefully most of) the suggestions here in mind.

    • doesn't parse anything in some init() and does not panic() 💣 anywhere.
    • might work on non-unix systems, because it has a built-in database -- but I didn't test 🙈.
    • does no parsing, unless explicitly told so by a programmer.

    There's more:

    • Go-ish design not following the POSIX C API to make things more Go-like, such as netdb.Service instead of Servent.
    • built-in database auto-generated from the md/netdb Debian package files for etc/protocols and etc/services.
    • allows for either builtin or /etc loaded data, as well as both, or even from other sources.
    • tests, tests, tests.

    I highly appreciate the original work spent on go-netdb as well as this issue discussion: it put me on the right track to use auto-generated protocol and service descriptions. And to avoid parsing in init 😁.

  5. dominikh commented on Sep 18, 2021

    @dominikh
    Owner

    I'll see about archiving this repository and possibly linking to yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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