Commit fd58320f authored by Mikio Hara's avatar Mikio Hara

net: add minimal internet protocol number information base

This CL adds minimal information for supporting platforms that don't
have a complete list of internet protocol numbers.

Fixes #5344.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/12898045
parent 3b961bf8
......@@ -8,6 +8,19 @@ import (
"time"
)
// protocols contains minimal mappings between internet protocol
// names and numbers for platforms that don't have a complete list of
// protocol numbers.
//
// See http://www.iana.org/assignments/protocol-numbers
var protocols = map[string]int{
"icmp": 1, "ICMP": 1,
"igmp": 2, "IGMP": 2,
"tcp": 6, "TCP": 6,
"udp": 17, "UDP": 17,
"ipv6-icmp": 58, "IPV6-ICMP": 58, "IPv6-ICMP": 58,
}
var lookupGroup singleflight
// lookupHostMerge wraps lookupHost, but makes sure that for any given
......
......@@ -11,15 +11,11 @@ import (
"sync"
)
var (
protocols map[string]int
onceReadProtocols sync.Once
)
var onceReadProtocols sync.Once
// readProtocols loads contents of /etc/protocols into protocols map
// for quick access.
func readProtocols() {
protocols = make(map[string]int)
if file, err := open("/etc/protocols"); err == nil {
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
// tcp 6 TCP # transmission control protocol
......@@ -31,9 +27,13 @@ func readProtocols() {
continue
}
if proto, _, ok := dtoi(f[1], 0); ok {
protocols[f[0]] = proto
if _, ok := protocols[f[0]]; !ok {
protocols[f[0]] = proto
}
for _, alias := range f[2:] {
protocols[alias] = proto
if _, ok := protocols[alias]; !ok {
protocols[alias] = proto
}
}
}
}
......
......@@ -42,6 +42,11 @@ func lookupProtocol(name string) (proto int, err error) {
ch <- result{proto: proto, err: err}
}()
r := <-ch
if r.err != nil {
if proto, ok := protocols[name]; ok {
return protol, nil
}
}
return r.proto, r.err
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment