| LEFT | RIGHT |
| (no file at all) |
| 1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris | 5 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris |
| 6 | 6 |
| 7 // Read system port mappings from /etc/services | 7 // Read system port mappings from /etc/services |
| 8 | 8 |
| 9 package net | 9 package net |
| 10 | 10 |
| 11 import "sync" | 11 import "sync" |
| 12 | 12 |
| 13 // services contains minimal mappings between services names and port | 13 // services contains minimal mappings between services names and port |
| 14 // numbers for platforms that don't have a complete list of port numbers | 14 // numbers for platforms that don't have a complete list of port numbers |
| 15 // (some Solaris distros). | 15 // (some Solaris distros). |
| 16 var services = map[string]map[string]int{ | 16 var services = map[string]map[string]int{ |
| 17 "tcp": {"http": 80}, | 17 "tcp": {"http": 80}, |
| 18 } | 18 } |
| 19 var servicesError error | 19 var servicesError error |
| 20 var onceReadServices sync.Once | 20 var onceReadServices sync.Once |
| 21 | 21 |
| 22 // ServicesPath points to the file with port/service mapping. |
| 23 var ServicesPath = "/etc/services" |
| 24 |
| 22 func readServices() { | 25 func readServices() { |
| 23 var file *file | 26 var file *file |
| 24 » if file, servicesError = open("/etc/services"); servicesError != nil { | 27 » if file, servicesError = open(ServicesPath); servicesError != nil { |
| 25 return | 28 return |
| 26 } | 29 } |
| 27 for line, ok := file.readLine(); ok; line, ok = file.readLine() { | 30 for line, ok := file.readLine(); ok; line, ok = file.readLine() { |
| 28 // "http 80/tcp www www-http # World Wide Web HTTP" | 31 // "http 80/tcp www www-http # World Wide Web HTTP" |
| 29 if i := byteIndex(line, '#'); i >= 0 { | 32 if i := byteIndex(line, '#'); i >= 0 { |
| 30 line = line[0:i] | 33 line = line[0:i] |
| 31 } | 34 } |
| 32 f := getFields(line) | 35 f := getFields(line) |
| 33 if len(f) < 2 { | 36 if len(f) < 2 { |
| 34 continue | 37 continue |
| (...skipping 29 matching lines...) Loading... |
| 64 network = "udp" | 67 network = "udp" |
| 65 } | 68 } |
| 66 | 69 |
| 67 if m, ok := services[network]; ok { | 70 if m, ok := services[network]; ok { |
| 68 if port, ok = m[service]; ok { | 71 if port, ok = m[service]; ok { |
| 69 return | 72 return |
| 70 } | 73 } |
| 71 } | 74 } |
| 72 return 0, &AddrError{"unknown port", network + "/" + service} | 75 return 0, &AddrError{"unknown port", network + "/" + service} |
| 73 } | 76 } |
| LEFT | RIGHT |