1
0
mirror of https://github.com/golang/go synced 2024-11-25 19:07:57 -07:00

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
This commit is contained in:
Mikio Hara 2013-08-22 10:33:37 +09:00
parent 3b961bf88b
commit fd58320f32
3 changed files with 25 additions and 7 deletions

View File

@ -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

View File

@ -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
}
}
}
}

View File

@ -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
}