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 openbsd
7 // Read system port mappings from /etc/services
16 var services map[string]map[string]int
17 var servicesError os.Error
18 var onceReadServices sync.Once
21 services = make(map[string]map[string]int)
23 if file, servicesError = open("/etc/services"); servicesError != nil {
26 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
27 // "http 80/tcp www www-http # World Wide Web HTTP"
28 if i := byteIndex(line, '#'); i >= 0 {
35 portnet := f[1] // "tcp/80"
36 port, j, ok := dtoi(portnet, 0)
37 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
40 netw := portnet[j+1:] // "tcp"
41 m, ok1 := services[netw]
43 m = make(map[string]int)
46 for i := 0; i < len(f); i++ {
47 if i != 1 { // f[1] was port/net
55 // goLookupPort is the native Go implementation of LookupPort.
56 func goLookupPort(network, service string) (port int, err os.Error) {
57 onceReadServices.Do(readServices)
66 if m, ok := services[network]; ok {
67 if port, ok = m[service]; ok {
71 return 0, &AddrError{"unknown port", network + "/" + service}