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 // Read system port mappings from /etc/services
14 var services map[string]map[string]int
15 var servicesError os.Error
16 var onceReadServices sync.Once
19 services = make(map[string]map[string]int)
21 file, servicesError = open("/etc/services")
22 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
23 // "http 80/tcp www www-http # World Wide Web HTTP"
24 if i := byteIndex(line, '#'); i >= 0 {
31 portnet := f[1] // "tcp/80"
32 port, j, ok := dtoi(portnet, 0)
33 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
36 netw := portnet[j+1:] // "tcp"
37 m, ok1 := services[netw]
39 m = make(map[string]int)
42 for i := 0; i < len(f); i++ {
43 if i != 1 { // f[1] was port/net
51 // LookupPort looks up the port for the given network and service.
52 func LookupPort(network, service string) (port int, err os.Error) {
53 onceReadServices.Do(readServices)
62 if m, ok := services[network]; ok {
63 if port, ok = m[service]; ok {
67 return 0, &AddrError{"unknown port", network + "/" + service}