Remove the types float and complex.
[platform/upstream/gcc.git] / libgo / go / net / port.go
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.
4
5 // Read system port mappings from /etc/services
6
7 package net
8
9 import (
10         "os"
11         "sync"
12 )
13
14 var services map[string]map[string]int
15 var servicesError os.Error
16 var onceReadServices sync.Once
17
18 func readServices() {
19         services = make(map[string]map[string]int)
20         var file *file
21         if file, servicesError = open("/etc/services"); servicesError != nil {
22                 return
23         }
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 {
27                         line = line[0:i]
28                 }
29                 f := getFields(line)
30                 if len(f) < 2 {
31                         continue
32                 }
33                 portnet := f[1] // "tcp/80"
34                 port, j, ok := dtoi(portnet, 0)
35                 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
36                         continue
37                 }
38                 netw := portnet[j+1:] // "tcp"
39                 m, ok1 := services[netw]
40                 if !ok1 {
41                         m = make(map[string]int)
42                         services[netw] = m
43                 }
44                 for i := 0; i < len(f); i++ {
45                         if i != 1 { // f[1] was port/net
46                                 m[f[i]] = port
47                         }
48                 }
49         }
50         file.close()
51 }
52
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)
56
57         switch network {
58         case "tcp4", "tcp6":
59                 network = "tcp"
60         case "udp4", "udp6":
61                 network = "udp"
62         }
63
64         if m, ok := services[network]; ok {
65                 if port, ok = m[service]; ok {
66                         return
67                 }
68         }
69         return 0, &AddrError{"unknown port", network + "/" + service}
70 }