1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin freebsd linux netbsd openbsd
7 // Read system port mappings from /etc/services
13 var services map[string]map[string]int
14 var servicesError error
15 var onceReadServices sync.Once
18 services = make(map[string]map[string]int)
20 if file, servicesError = open("/etc/services"); servicesError != nil {
23 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
24 // "http 80/tcp www www-http # World Wide Web HTTP"
25 if i := byteIndex(line, '#'); i >= 0 {
32 portnet := f[1] // "tcp/80"
33 port, j, ok := dtoi(portnet, 0)
34 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
37 netw := portnet[j+1:] // "tcp"
38 m, ok1 := services[netw]
40 m = make(map[string]int)
43 for i := 0; i < len(f); i++ {
44 if i != 1 { // f[1] was port/net
52 // goLookupPort is the native Go implementation of LookupPort.
53 func goLookupPort(network, service string) (port int, err error) {
54 onceReadServices.Do(readServices)
63 if m, ok := services[network]; ok {
64 if port, ok = m[service]; ok {
68 return 0, &AddrError{"unknown port", network + "/" + service}