Source file
src/net/port_unix.go
1
2
3
4
5
6
7
8
9 package net
10
11 import (
12 "internal/bytealg"
13 "sync"
14 )
15
16 var onceReadServices sync.Once
17
18 func readServices() {
19 file, err := open("/etc/services")
20 if err != nil {
21 return
22 }
23 defer file.close()
24
25 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
26
27 if i := bytealg.IndexByteString(line, '#'); i >= 0 {
28 line = line[:i]
29 }
30 f := getFields(line)
31 if len(f) < 2 {
32 continue
33 }
34 portnet := f[1]
35 port, j, ok := dtoi(portnet)
36 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
37 continue
38 }
39 netw := portnet[j+1:]
40 m, ok1 := services[netw]
41 if !ok1 {
42 m = make(map[string]int)
43 services[netw] = m
44 }
45 for i := 0; i < len(f); i++ {
46 if i != 1 {
47 m[f[i]] = port
48 }
49 }
50 }
51 }
52
53
54 func goLookupPort(network, service string) (port int, err error) {
55 onceReadServices.Do(readServices)
56 return lookupPortMap(network, service)
57 }
58
View as plain text