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 if file, servicesError = open("/etc/services"); servicesError != nil {
24 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
25 // "http 80/tcp www www-http # World Wide Web HTTP"
26 if i := byteIndex(line, '#'); i >= 0 {
33 portnet := f[1] // "tcp/80"
34 port, j, ok := dtoi(portnet, 0)
35 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
38 netw := portnet[j+1:] // "tcp"
39 m, ok1 := services[netw]
41 m = make(map[string]int)
44 for i := 0; i < len(f); i++ {
45 if i != 1 { // f[1] was port/net
53 // LookupPort looks up the port for the given network and service.
54 func LookupPort(network, service string) (port int, err os.Error) {
55 onceReadServices.Do(readServices)
64 if m, ok := services[network]; ok {
65 if port, ok = m[service]; ok {
69 return 0, &AddrError{"unknown port", network + "/" + service}