ipproto

package module
v0.0.0-...-112e9b6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 9 Imported by: 0

README

ipproto

ipproto is a Go library that translates between IP protocol numbers (0–255) and their official IANA names, using an embedded copy of protocol-numbers.csv.

No configuration needed.
No external files.
Just import and use.

Installation

go get github.com/brnuts/ipproto

Basic Usage

Lookup short name (Keyword) from protocol number
name, ok := ipproto.LookupKeyword(6)
fmt.Println(name, ok)  // Output: TCP true
Lookup long protocol name from number
longName, ok := ipproto.LookupProtocolName(6)
fmt.Println(longName, ok)  // Output: Transmission Control true
Lookup protocol number from short name (Keyword)
num, ok := ipproto.LookupDecimal("UDP")
fmt.Println(num, ok)   // Output: 17 true
Lookup number from long protocol name
num, ok := ipproto.LookupDecimal("Internet Control Message")
fmt.Println(num, ok)   // Output: 1 true
Flexible name matching

Keyword and Protocol names are matched case-insensitively and with whitespace normalized.

num, ok := ipproto.LookupDecimal("transMission   control")
fmt.Println(num, ok)   // Output: 6 true

Lookup full Entry

entry, ok := ipproto.LookupByNumber(41)
if ok {
    fmt.Println("Keyword:", entry.Keyword)
    fmt.Println("Protocol:", entry.Protocol)
}

Range Handling

Some IANA entries specify ranges, such as:

148-252   Unassigned

Calling:

start, ok := ipproto.LookupDecimal("Unassigned")
fmt.Println(start)  // Output: 148

returns the start of the range.

To inspect the full range:

entry, ok := ipproto.LookupByNumber(start)
fmt.Println(entry.DecimalStart, entry.DecimalEnd)

Optional: Overriding the Embedded CSV

err := ipproto.LoadFromFile("protocol-numbers-latest.csv")

or:

ipproto.LoadFromReader(bytes.NewReader(customCSV))

File Structure

protocols.go
protocol-numbers.csv
go.mod
README.md

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadFromFile

func LoadFromFile(path string) error

LoadFromFile parses the given CSV file and overrides the embedded data. You don't need to call this for normal use.

func LoadFromReader

func LoadFromReader(r io.Reader) error

LoadFromReader parses protocol data from any io.Reader and overrides the embedded data. You don't need to call this for normal use.

func LookupDecimal

func LookupDecimal(name string) (int, bool)

LookupDecimal returns the Decimal value for a given name.

"name" can be either:

  • the Keyword (short name, e.g. "TCP")
  • the Protocol (long name, e.g. "Transmission Control")

For ranges like "148-252", this returns DecimalStart.

func LookupKeyword

func LookupKeyword(n int) (string, bool)

LookupKeyword returns the short name (Keyword) for a protocol number, e.g. 6 -> "TCP".

func LookupProtocolName

func LookupProtocolName(n int) (string, bool)

LookupProtocolName returns the long "Protocol" field for a protocol number, e.g. 6 -> "Transmission Control".

Types

type Entry

type Entry struct {
	DecimalStart int    // first number in the range
	DecimalEnd   int    // last number in the range (== DecimalStart for non-range)
	Keyword      string // "Keyword" column, short name, e.g. "TCP"
	Protocol     string // "Protocol" column, long name, e.g. "Transmission Control"
	IPv6ExtHdr   string // "IPv6 Extension Header" column (usually "Y" or empty)
	Reference    string // "Reference" column, raw text
}

Entry represents one row (or range) from the IANA protocol numbers CSV.

The "Decimal" column can be either a single number (e.g. "6") or a range (e.g. "148-252"). For ranges, DecimalStart/DecimalEnd represent the inclusive range.

func LookupByNumber

func LookupByNumber(n int) (*Entry, bool)

LookupByNumber returns the Entry for a given protocol number (0–255).

If not found, ok will be false.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL