You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 21, 2021. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 21, 2021. It is now read-only.
GetServByPort documentation does not reflect actual implementation #1
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.funcGetServByPort(portint, protocol*Protoent) *Servent {
for_, servent:=rangeServices {
ifservent.Port==port&&servent.Protocol.Equal(protocol) {
returnservent
}
}
returnnil
}
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 {
ifthis==nil&&other==nil {
returntrue
}
ifthis==nil||other==nil {
returnfalse
}
returnthis.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.funcGetServByPort(portint, protocol*Protoent) *Servent {
for_, servent:=rangeServices {
ifprotocol==nil&&servent.Port==port {
returnservent
}
ifservent.Port==port&&servent.Protocol.Equal(protocol) {
returnservent
}
}
returnnil
}
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!
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.
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.
@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.
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 😁.
Activity
dominikh commentedon Jun 24, 2020
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 ininit… 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.Gabri3l commentedon Jun 25, 2020
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.
dominikh commentedon Aug 30, 2020
@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.Virtually everything. We shouldn't
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.
thediveo commentedon Sep 16, 2021
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.
init()and does notpanic()💣 anywhere.There's more:
netdb.Serviceinstead ofServent.etc/protocolsandetc/services.I highly appreciate the original work spent on
go-netdbas well as this issue discussion: it put me on the right track to use auto-generated protocol and service descriptions. And to avoid parsing ininit😁.dominikh commentedon Sep 18, 2021
I'll see about archiving this repository and possibly linking to yours.