2008-12-18 16:42:39 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
// +build darwin freebsd linux openbsd
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
// Read system port mappings from /etc/services
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"os"
|
2010-08-05 14:14:41 -06:00
|
|
|
"sync"
|
2008-12-18 16:42:39 -07:00
|
|
|
)
|
|
|
|
|
2009-11-05 00:16:46 -07:00
|
|
|
var services map[string]map[string]int
|
2009-04-17 01:08:24 -06:00
|
|
|
var servicesError os.Error
|
2010-08-05 14:14:41 -06:00
|
|
|
var onceReadServices sync.Once
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2009-02-15 15:18:39 -07:00
|
|
|
func readServices() {
|
2009-12-15 16:35:38 -07:00
|
|
|
services = make(map[string]map[string]int)
|
|
|
|
var file *file
|
2011-01-19 07:55:46 -07:00
|
|
|
if file, servicesError = open("/etc/services"); servicesError != nil {
|
|
|
|
return
|
|
|
|
}
|
2009-02-15 15:18:39 -07:00
|
|
|
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
|
2008-12-18 16:42:39 -07:00
|
|
|
// "http 80/tcp www www-http # World Wide Web HTTP"
|
2009-02-15 15:18:39 -07:00
|
|
|
if i := byteIndex(line, '#'); i >= 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
line = line[0:i]
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
f := getFields(line)
|
2008-12-18 16:42:39 -07:00
|
|
|
if len(f) < 2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
portnet := f[1] // "tcp/80"
|
|
|
|
port, j, ok := dtoi(portnet, 0)
|
2008-12-18 16:42:39 -07:00
|
|
|
if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
netw := portnet[j+1:] // "tcp"
|
|
|
|
m, ok1 := services[netw]
|
2008-12-18 16:42:39 -07:00
|
|
|
if !ok1 {
|
2009-12-15 16:35:38 -07:00
|
|
|
m = make(map[string]int)
|
|
|
|
services[netw] = m
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < len(f); i++ {
|
2009-12-15 16:35:38 -07:00
|
|
|
if i != 1 { // f[1] was port/net
|
2009-11-09 13:07:39 -07:00
|
|
|
m[f[i]] = port
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
file.close()
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2011-03-28 21:28:42 -06:00
|
|
|
// goLookupPort is the native Go implementation of LookupPort.
|
|
|
|
func goLookupPort(network, service string) (port int, err os.Error) {
|
2010-08-05 14:14:41 -06:00
|
|
|
onceReadServices.Do(readServices)
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
switch network {
|
2008-12-18 16:42:39 -07:00
|
|
|
case "tcp4", "tcp6":
|
2009-11-09 13:07:39 -07:00
|
|
|
network = "tcp"
|
2008-12-18 16:42:39 -07:00
|
|
|
case "udp4", "udp6":
|
2009-11-09 13:07:39 -07:00
|
|
|
network = "udp"
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-06-25 21:24:55 -06:00
|
|
|
if m, ok := services[network]; ok {
|
|
|
|
if port, ok = m[service]; ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return 0, &AddrError{"unknown port", network + "/" + service}
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|