thediveo / netdb

Protocol, service, and ethertype netdb data for pure Go.
Apache License 2.0
2 stars 1 forks source link

Feature Request: Add support for /etc/ethertypes #2

Closed sapslaj closed 7 months ago

sapslaj commented 7 months ago

I'm working on replacing an ad-hoc implementation of protocol and service lookups in one of my projects, a NetFlow collection and metadata enrichment agent with this library. It was pretty simple to slot this library in place for protocols and services, but my project also supports adding some metadata about the Ethernet protocol type of the flow. Adding support for /etc/ethertypes to this project would be really helpful and I'd be able to get rid of the hard-coded mappings.

Some ideas for the API:

func main() {
    ipv4 := netdb.EtherTypeByNumber(0x0800)
    fmt.Println(ipv4.Name)
    // Output: IPv4
    fmt.Println(ipv4.Number)
    // Output: 2048
    fmt.Println(ipv4.Aliases)
    // Output: [ip ipv4]
    fmt.Println(ipv4.Comment)
    // Output: Internet IP (IPv4)
}
func main() {
    dot1q := netdb.EtherTypeByName("dot1q")
    fmt.Println(dot1q.Name)
    // Output: 802_1q
    fmt.Println(dot1q.Number)
    // Output: 33024
    fmt.Println(dot1q.Aliases)
    // Output: [8021q 1q 802.1q dot1q]
    fmt.Println(dot1q.Comment)
    // Output: 802.1Q Virtual LAN tagged frame
}

Not totally sure if the comment parsing is really necessary, but it does contain some useful info sometimes.

thediveo commented 7 months ago

Sounds good, even if Debian's /etc/ethertypes is a little bit thin. API looks good to me, it follows and should follow the pattern this module uses for protocols and services. I've checked with the upstream Debian repo to make sure it contains an ethertypes to auto-generate the builtin data from.

sapslaj commented 7 months ago

Interestingly Debian's /etc/ethertypes is more comprehensive than what ships with Arch Linux, at least based on what my machine has. Either way, it's pretty easy for someone to add in more entries to the index if needed so it's probably fine.

~I got a WIP branch that implements this that I can share in just a bit.~

WIP branch here: https://github.com/sapslaj/netdb/tree/ethertypes-support

I'll make a proper PR once I get through some TODOs

thediveo commented 7 months ago

Ah, a PR sounds really great!

(I'm a little bit disappointed that Debian doesn't include the Profinet Ethertypes 😀)

sapslaj commented 7 months ago

Oh man does Profinet have its own EtherType? I've only ever dealt with EtherNet/IP and IIRC it uses regular TCP and UDP over IP (it's been a few years though so could be wrong...)

thediveo commented 7 months ago

released as v1.1.0; thank you very much!