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