2013-02-11 Sebastian Huber <sebastian.huber@embedded-brains.de>
[official-gcc.git] / libgo / go / net / port_unix.go
blob16780da1160cd81dc6065924c364ae0f5adcf589
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
9 package net
11 import "sync"
13 var services map[string]map[string]int
14 var servicesError error
15 var onceReadServices sync.Once
17 func readServices() {
18 services = make(map[string]map[string]int)
19 var file *file
20 if file, servicesError = open("/etc/services"); servicesError != nil {
21 return
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 {
26 line = line[0:i]
28 f := getFields(line)
29 if len(f) < 2 {
30 continue
32 portnet := f[1] // "tcp/80"
33 port, j, ok := dtoi(portnet, 0)
34 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
35 continue
37 netw := portnet[j+1:] // "tcp"
38 m, ok1 := services[netw]
39 if !ok1 {
40 m = make(map[string]int)
41 services[netw] = m
43 for i := 0; i < len(f); i++ {
44 if i != 1 { // f[1] was port/net
45 m[f[i]] = port
49 file.close()
52 // goLookupPort is the native Go implementation of LookupPort.
53 func goLookupPort(network, service string) (port int, err error) {
54 onceReadServices.Do(readServices)
56 switch network {
57 case "tcp4", "tcp6":
58 network = "tcp"
59 case "udp4", "udp6":
60 network = "udp"
63 if m, ok := services[network]; ok {
64 if port, ok = m[service]; ok {
65 return
68 return 0, &AddrError{"unknown port", network + "/" + service}