...

Source file src/net/port_unix.go

Documentation: net

		 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  //go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris
		 6  // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
		 7  
		 8  // Read system port mappings from /etc/services
		 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  		// "http 80/tcp www www-http # World Wide Web HTTP"
		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] // "80/tcp"
		36  		port, j, ok := dtoi(portnet)
		37  		if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
		38  			continue
		39  		}
		40  		netw := portnet[j+1:] // "tcp"
		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 { // f[1] was port/net
		48  				m[f[i]] = port
		49  			}
		50  		}
		51  	}
		52  }
		53  
		54  // goLookupPort is the native Go implementation of LookupPort.
		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