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 aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris nacl
7 // Read system port mappings from /etc/services
13 var onceReadServices sync
.Once
16 file
, err
:= open("/etc/services")
22 for line
, ok
:= file
.readLine(); ok
; line
, ok
= file
.readLine() {
23 // "http 80/tcp www www-http # World Wide Web HTTP"
24 if i
:= byteIndex(line
, '#'); i
>= 0 {
31 portnet
:= f
[1] // "80/tcp"
32 port
, j
, ok
:= dtoi(portnet
)
33 if !ok || port
<= 0 || j
>= len(portnet
) || portnet
[j
] != '/' {
36 netw
:= portnet
[j
+1:] // "tcp"
37 m
, ok1
:= services
[netw
]
39 m
= make(map[string]int)
42 for i
:= 0; i
< len(f
); i
++ {
43 if i
!= 1 { // f[1] was port/net
50 // goLookupPort is the native Go implementation of LookupPort.
51 func goLookupPort(network
, service
string) (port
int, err error
) {
52 onceReadServices
.Do(readServices
)
53 return lookupPortMap(network
, service
)