Commit 5d377054 authored by Robert Griesemer's avatar Robert Griesemer

gofmt-ify net

R=rsc
http://go/go-review/1017045
parent 7df45566
...@@ -26,7 +26,7 @@ func fetchGoogle(t *testing.T, fd Conn, network, addr string) { ...@@ -26,7 +26,7 @@ func fetchGoogle(t *testing.T, fd Conn, network, addr string) {
if n < 1000 { if n < 1000 {
t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err); t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err);
return return;
} }
} }
...@@ -34,13 +34,13 @@ func doDial(t *testing.T, network, addr string) { ...@@ -34,13 +34,13 @@ func doDial(t *testing.T, network, addr string) {
fd, err := Dial(network, "", addr); fd, err := Dial(network, "", addr);
if err != nil { if err != nil {
t.Errorf("Dial(%q, %q, %q) = _, %v", network, "", addr, err); t.Errorf("Dial(%q, %q, %q) = _, %v", network, "", addr, err);
return return;
} }
fetchGoogle(t, fd, network, addr); fetchGoogle(t, fd, network, addr);
fd.Close() fd.Close();
} }
var googleaddrs = []string { var googleaddrs = []string{
"74.125.19.99:80", "74.125.19.99:80",
"www.google.com:80", "www.google.com:80",
"74.125.19.99:http", "74.125.19.99:http",
...@@ -51,19 +51,20 @@ var googleaddrs = []string { ...@@ -51,19 +51,20 @@ var googleaddrs = []string {
"[0:0:0:0:0000:ffff:74.125.19.99]:80", "[0:0:0:0:0000:ffff:74.125.19.99]:80",
"[0:0:0:0:000000:ffff:74.125.19.99]:80", "[0:0:0:0:000000:ffff:74.125.19.99]:80",
"[0:0:0:0:0:ffff::74.125.19.99]:80", "[0:0:0:0:0:ffff::74.125.19.99]:80",
"[2001:4860:0:2001::68]:80" // ipv6.google.com; removed if ipv6 flag not set "[2001:4860:0:2001::68]:80" // ipv6.google.com; removed if ipv6 flag not set
,
} }
func TestDialGoogle(t *testing.T) { func TestDialGoogle(t *testing.T) {
// If no ipv6 tunnel, don't try the last address. // If no ipv6 tunnel, don't try the last address.
if !*ipv6 { if !*ipv6 {
googleaddrs[len(googleaddrs)-1] = "" googleaddrs[len(googleaddrs)-1] = "";
} }
for i := 0; i < len(googleaddrs); i++ { for i := 0; i < len(googleaddrs); i++ {
addr := googleaddrs[i]; addr := googleaddrs[i];
if addr == "" { if addr == "" {
continue continue;
} }
t.Logf("-- %s --", addr); t.Logf("-- %s --", addr);
doDial(t, "tcp", addr); doDial(t, "tcp", addr);
......
...@@ -21,9 +21,9 @@ import ( ...@@ -21,9 +21,9 @@ import (
// DNSError represents a DNS lookup error. // DNSError represents a DNS lookup error.
type DNSError struct { type DNSError struct {
Error string; // description of the error Error string; // description of the error
Name string; // name looked for Name string; // name looked for
Server string; // server used Server string; // server used
} }
func (e *DNSError) String() string { func (e *DNSError) String() string {
...@@ -41,30 +41,30 @@ const noSuchHost = "no such host" ...@@ -41,30 +41,30 @@ const noSuchHost = "no such host"
// Up to cfg.attempts attempts. // Up to cfg.attempts attempts.
func _Exchange(cfg *_DNS_Config, c Conn, name string) (m *_DNS_Msg, err os.Error) { func _Exchange(cfg *_DNS_Config, c Conn, name string) (m *_DNS_Msg, err os.Error) {
if len(name) >= 256 { if len(name) >= 256 {
return nil, &DNSError{"name too long", name, ""} return nil, &DNSError{"name too long", name, ""};
} }
out := new(_DNS_Msg); out := new(_DNS_Msg);
out.id = 0x1234; out.id = 0x1234;
out.question = []_DNS_Question{ out.question = []_DNS_Question{
_DNS_Question{ name, _DNS_TypeA, _DNS_ClassINET } _DNS_Question{name, _DNS_TypeA, _DNS_ClassINET},
}; };
out.recursion_desired = true; out.recursion_desired = true;
msg, ok := out.Pack(); msg, ok := out.Pack();
if !ok { if !ok {
return nil, &DNSError{"internal error - cannot pack message", name, ""} return nil, &DNSError{"internal error - cannot pack message", name, ""};
} }
for attempt := 0; attempt < cfg.attempts; attempt++ { for attempt := 0; attempt < cfg.attempts; attempt++ {
n, err := c.Write(msg); n, err := c.Write(msg);
if err != nil { if err != nil {
return nil, err return nil, err;
} }
c.SetReadTimeout(1e9); // nanoseconds c.SetReadTimeout(1e9); // nanoseconds
buf := make([]byte, 2000); // More than enough. buf := make([]byte, 2000); // More than enough.
n, err = c.Read(buf); n, err = c.Read(buf);
if isEAGAIN(err) { if isEAGAIN(err) {
err = nil; err = nil;
continue; continue;
} }
...@@ -74,15 +74,15 @@ func _Exchange(cfg *_DNS_Config, c Conn, name string) (m *_DNS_Msg, err os.Error ...@@ -74,15 +74,15 @@ func _Exchange(cfg *_DNS_Config, c Conn, name string) (m *_DNS_Msg, err os.Error
buf = buf[0:n]; buf = buf[0:n];
in := new(_DNS_Msg); in := new(_DNS_Msg);
if !in.Unpack(buf) || in.id != out.id { if !in.Unpack(buf) || in.id != out.id {
continue continue;
} }
return in, nil return in, nil;
} }
var server string; var server string;
if a := c.RemoteAddr(); a != nil { if a := c.RemoteAddr(); a != nil {
server = a.String(); server = a.String();
} }
return nil, &DNSError{"no answer from server", name, server} return nil, &DNSError{"no answer from server", name, server};
} }
...@@ -92,14 +92,14 @@ func answer(name, server string, dns *_DNS_Msg) (addrs []string, err *DNSError) ...@@ -92,14 +92,14 @@ func answer(name, server string, dns *_DNS_Msg) (addrs []string, err *DNSError)
addrs = make([]string, 0, len(dns.answer)); addrs = make([]string, 0, len(dns.answer));
if dns.rcode == _DNS_RcodeNameError && dns.recursion_available { if dns.rcode == _DNS_RcodeNameError && dns.recursion_available {
return nil, &DNSError{noSuchHost, name, ""} return nil, &DNSError{noSuchHost, name, ""};
} }
if dns.rcode != _DNS_RcodeSuccess { if dns.rcode != _DNS_RcodeSuccess {
// None of the error codes make sense // None of the error codes make sense
// for the query we sent. If we didn't get // for the query we sent. If we didn't get
// a name error and we didn't get success, // a name error and we didn't get success,
// the server is behaving incorrectly. // the server is behaving incorrectly.
return nil, &DNSError{"server misbehaving", name, server} return nil, &DNSError{"server misbehaving", name, server};
} }
// Look for the name. // Look for the name.
...@@ -118,29 +118,29 @@ Cname: ...@@ -118,29 +118,29 @@ Cname:
case _DNS_TypeA: case _DNS_TypeA:
n := len(addrs); n := len(addrs);
a := rr.(*_DNS_RR_A).A; a := rr.(*_DNS_RR_A).A;
addrs = addrs[0:n+1]; addrs = addrs[0 : n+1];
addrs[n] = IPv4(byte(a>>24), byte(a>>16), byte(a>>8), byte(a)).String(); addrs[n] = IPv4(byte(a>>24), byte(a>>16), byte(a>>8), byte(a)).String();
case _DNS_TypeCNAME: case _DNS_TypeCNAME:
// redirect to cname // redirect to cname
name = rr.(*_DNS_RR_CNAME).Cname; name = rr.(*_DNS_RR_CNAME).Cname;
continue Cname continue Cname;
} }
} }
} }
if len(addrs) == 0 { if len(addrs) == 0 {
return nil, &DNSError{noSuchHost, name, server} return nil, &DNSError{noSuchHost, name, server};
} }
return addrs, nil return addrs, nil;
} }
return nil, &DNSError{"too many redirects", name, server} return nil, &DNSError{"too many redirects", name, server};
} }
// Do a lookup for a single name, which must be rooted // Do a lookup for a single name, which must be rooted
// (otherwise answer will not find the answers). // (otherwise answer will not find the answers).
func tryOneName(cfg *_DNS_Config, name string) (addrs []string, err os.Error) { func tryOneName(cfg *_DNS_Config, name string) (addrs []string, err os.Error) {
if len(cfg.servers) == 0 { if len(cfg.servers) == 0 {
return nil, &DNSError{"no DNS servers", name, ""} return nil, &DNSError{"no DNS servers", name, ""};
} }
for i := 0; i < len(cfg.servers); i++ { for i := 0; i < len(cfg.servers); i++ {
// Calling Dial here is scary -- we have to be sure // Calling Dial here is scary -- we have to be sure
...@@ -255,14 +255,14 @@ func LookupHost(name string) (cname string, addrs []string, err os.Error) { ...@@ -255,14 +255,14 @@ func LookupHost(name string) (cname string, addrs []string, err os.Error) {
} }
} }
if rooted { if rooted {
return return;
} }
// Otherwise, try suffixes. // Otherwise, try suffixes.
for i := 0; i < len(cfg.search); i++ { for i := 0; i < len(cfg.search); i++ {
rname := name+"."+cfg.search[i]; rname := name + "." + cfg.search[i];
if rname[len(rname)-1] != '.' { if rname[len(rname)-1] != '.' {
rname += "." rname += ".";
} }
addrs, err = tryOneName(cfg, rname); addrs, err = tryOneName(cfg, rname);
if err == nil { if err == nil {
...@@ -270,5 +270,5 @@ func LookupHost(name string) (cname string, addrs []string, err os.Error) { ...@@ -270,5 +270,5 @@ func LookupHost(name string) (cname string, addrs []string, err os.Error) {
return; return;
} }
} }
return return;
} }
...@@ -9,15 +9,15 @@ package net ...@@ -9,15 +9,15 @@ package net
import "os" import "os"
type _DNS_Config struct { type _DNS_Config struct {
servers []string; // servers to use servers []string; // servers to use
search []string; // suffixes to append to local name search []string; // suffixes to append to local name
ndots int; // number of dots in name to trigger absolute lookup ndots int; // number of dots in name to trigger absolute lookup
timeout int; // seconds before giving up on packet timeout int; // seconds before giving up on packet
attempts int; // lost packets before giving up on server attempts int; // lost packets before giving up on server
rotate bool; // round robin among servers rotate bool; // round robin among servers
} }
var _DNS_configError os.Error; var _DNS_configError os.Error
// See resolv.conf(5) on a Linux machine. // See resolv.conf(5) on a Linux machine.
// TODO(rsc): Supposed to call uname() and chop the beginning // TODO(rsc): Supposed to call uname() and chop the beginning
...@@ -26,10 +26,10 @@ var _DNS_configError os.Error; ...@@ -26,10 +26,10 @@ var _DNS_configError os.Error;
func _DNS_ReadConfig() (*_DNS_Config, os.Error) { func _DNS_ReadConfig() (*_DNS_Config, os.Error) {
file, err := open("/etc/resolv.conf"); file, err := open("/etc/resolv.conf");
if err != nil { if err != nil {
return nil, err return nil, err;
} }
conf := new(_DNS_Config); conf := new(_DNS_Config);
conf.servers = make([]string, 3)[0:0]; // small, but the standard limit conf.servers = make([]string, 3)[0:0]; // small, but the standard limit
conf.search = make([]string, 0); conf.search = make([]string, 0);
conf.ndots = 1; conf.ndots = 1;
conf.timeout = 1; conf.timeout = 1;
...@@ -50,7 +50,7 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) { ...@@ -50,7 +50,7 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) {
// to look it up. // to look it up.
name := f[1]; name := f[1];
if len(ParseIP(name)) != 0 { if len(ParseIP(name)) != 0 {
a = a[0:n+1]; a = a[0 : n+1];
a[n] = name; a[n] = name;
conf.servers = a; conf.servers = a;
} }
...@@ -61,11 +61,11 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) { ...@@ -61,11 +61,11 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) {
conf.search = make([]string, 1); conf.search = make([]string, 1);
conf.search[0] = f[1]; conf.search[0] = f[1];
} else { } else {
conf.search = make([]string, 0) conf.search = make([]string, 0);
} }
case "search": // set search path to given servers case "search": // set search path to given servers
conf.search = make([]string, len(f) - 1); conf.search = make([]string, len(f)-1);
for i := 0; i < len(conf.search); i++ { for i := 0; i < len(conf.search); i++ {
conf.search[i] = f[i+1]; conf.search[i] = f[i+1];
} }
...@@ -77,19 +77,19 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) { ...@@ -77,19 +77,19 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) {
case len(s) >= 6 && s[0:6] == "ndots:": case len(s) >= 6 && s[0:6] == "ndots:":
n, _, _ := dtoi(s, 6); n, _, _ := dtoi(s, 6);
if n < 1 { if n < 1 {
n = 1 n = 1;
} }
conf.ndots = n; conf.ndots = n;
case len(s) >= 8 && s[0:8] == "timeout:": case len(s) >= 8 && s[0:8] == "timeout:":
n, _, _ := dtoi(s, 8); n, _, _ := dtoi(s, 8);
if n < 1 { if n < 1 {
n = 1 n = 1;
} }
conf.timeout = n; conf.timeout = n;
case len(s) >= 8 && s[0:9] == "attempts:": case len(s) >= 8 && s[0:9] == "attempts:":
n, _, _ := dtoi(s, 9); n, _, _ := dtoi(s, 9);
if n < 1 { if n < 1 {
n = 1 n = 1;
} }
conf.attempts = n; conf.attempts = n;
case s == "rotate": case s == "rotate":
...@@ -100,6 +100,5 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) { ...@@ -100,6 +100,5 @@ func _DNS_ReadConfig() (*_DNS_Config, os.Error) {
} }
file.close(); file.close();
return conf, nil return conf, nil;
} }
This diff is collapsed.
...@@ -16,26 +16,26 @@ import ( ...@@ -16,26 +16,26 @@ import (
// Network file descriptor. // Network file descriptor.
type netFD struct { type netFD struct {
// immutable until Close // immutable until Close
fd int; fd int;
family int; family int;
proto int; proto int;
file *os.File; file *os.File;
cr chan *netFD; cr chan *netFD;
cw chan *netFD; cw chan *netFD;
net string; net string;
laddr Addr; laddr Addr;
raddr Addr; raddr Addr;
// owned by client // owned by client
rdeadline_delta int64; rdeadline_delta int64;
rdeadline int64; rdeadline int64;
rio sync.Mutex; rio sync.Mutex;
wdeadline_delta int64; wdeadline_delta int64;
wdeadline int64; wdeadline int64;
wio sync.Mutex; wio sync.Mutex;
// owned by fd wait server // owned by fd wait server
ncr, ncw int; ncr, ncw int;
} }
// A pollServer helps FDs determine when to retry a non-blocking // A pollServer helps FDs determine when to retry a non-blocking
...@@ -68,11 +68,11 @@ type netFD struct { ...@@ -68,11 +68,11 @@ type netFD struct {
// might help batch requests. // might help batch requests.
type pollServer struct { type pollServer struct {
cr, cw chan *netFD; // buffered >= 1 cr, cw chan *netFD; // buffered >= 1
pr, pw *os.File; pr, pw *os.File;
pending map[int] *netFD; pending map[int]*netFD;
poll *pollster; // low-level OS hooks poll *pollster; // low-level OS hooks
deadline int64; // next deadline (nsec since 1970) deadline int64; // next deadline (nsec since 1970)
} }
func newPollServer() (s *pollServer, err os.Error) { func newPollServer() (s *pollServer, err os.Error) {
...@@ -80,7 +80,7 @@ func newPollServer() (s *pollServer, err os.Error) { ...@@ -80,7 +80,7 @@ func newPollServer() (s *pollServer, err os.Error) {
s.cr = make(chan *netFD, 1); s.cr = make(chan *netFD, 1);
s.cw = make(chan *netFD, 1); s.cw = make(chan *netFD, 1);
if s.pr, s.pw, err = os.Pipe(); err != nil { if s.pr, s.pw, err = os.Pipe(); err != nil {
return nil, err return nil, err;
} }
var e int; var e int;
if e = syscall.SetNonblock(s.pr.Fd(), true); e != 0 { if e = syscall.SetNonblock(s.pr.Fd(), true); e != 0 {
...@@ -99,11 +99,11 @@ func newPollServer() (s *pollServer, err os.Error) { ...@@ -99,11 +99,11 @@ func newPollServer() (s *pollServer, err os.Error) {
} }
if err = s.poll.AddFD(s.pr.Fd(), 'r', true); err != nil { if err = s.poll.AddFD(s.pr.Fd(), 'r', true); err != nil {
s.poll.Close(); s.poll.Close();
goto Error goto Error;
} }
s.pending = make(map[int] *netFD); s.pending = make(map[int]*netFD);
go s.Run(); go s.Run();
return s, nil return s, nil;
} }
func (s *pollServer) AddFD(fd *netFD, mode int) { func (s *pollServer) AddFD(fd *netFD, mode int) {
...@@ -122,19 +122,19 @@ func (s *pollServer) AddFD(fd *netFD, mode int) { ...@@ -122,19 +122,19 @@ func (s *pollServer) AddFD(fd *netFD, mode int) {
if intfd < 0 { if intfd < 0 {
// fd closed underfoot // fd closed underfoot
if mode == 'r' { if mode == 'r' {
fd.cr <- fd fd.cr <- fd;
} else { } else {
fd.cw <- fd fd.cw <- fd;
} }
return return;
} }
if err := s.poll.AddFD(intfd, mode, false); err != nil { if err := s.poll.AddFD(intfd, mode, false); err != nil {
panicln("pollServer AddFD ", intfd, ": ", err.String(), "\n"); panicln("pollServer AddFD ", intfd, ": ", err.String(), "\n");
return return;
} }
var t int64; var t int64;
key := intfd << 1; key := intfd<<1;
if mode == 'r' { if mode == 'r' {
fd.ncr++; fd.ncr++;
t = fd.rdeadline; t = fd.rdeadline;
...@@ -150,28 +150,28 @@ func (s *pollServer) AddFD(fd *netFD, mode int) { ...@@ -150,28 +150,28 @@ func (s *pollServer) AddFD(fd *netFD, mode int) {
} }
func (s *pollServer) LookupFD(fd int, mode int) *netFD { func (s *pollServer) LookupFD(fd int, mode int) *netFD {
key := fd << 1; key := fd<<1;
if mode == 'w' { if mode == 'w' {
key++; key++;
} }
netfd, ok := s.pending[key]; netfd, ok := s.pending[key];
if !ok { if !ok {
return nil return nil;
} }
s.pending[key] = nil, false; s.pending[key] = nil, false;
return netfd return netfd;
} }
func (s *pollServer) WakeFD(fd *netFD, mode int) { func (s *pollServer) WakeFD(fd *netFD, mode int) {
if mode == 'r' { if mode == 'r' {
for fd.ncr > 0 { for fd.ncr > 0 {
fd.ncr--; fd.ncr--;
fd.cr <- fd fd.cr <- fd;
} }
} else { } else {
for fd.ncw > 0 { for fd.ncw > 0 {
fd.ncw--; fd.ncw--;
fd.cw <- fd fd.cw <- fd;
} }
} }
} }
...@@ -181,7 +181,7 @@ func (s *pollServer) Now() int64 { ...@@ -181,7 +181,7 @@ func (s *pollServer) Now() int64 {
if err != nil { if err != nil {
panic("net: os.Time: ", err.String()); panic("net: os.Time: ", err.String());
} }
nsec += sec * 1e9; nsec += sec*1e9;
return nsec; return nsec;
} }
...@@ -237,7 +237,7 @@ func (s *pollServer) Run() { ...@@ -237,7 +237,7 @@ func (s *pollServer) Run() {
fd, mode, err := s.poll.WaitFD(t); fd, mode, err := s.poll.WaitFD(t);
if err != nil { if err != nil {
print("pollServer WaitFD: ", err.String(), "\n"); print("pollServer WaitFD: ", err.String(), "\n");
return return;
} }
if fd < 0 { if fd < 0 {
// Timeout happened. // Timeout happened.
...@@ -247,42 +247,43 @@ func (s *pollServer) Run() { ...@@ -247,42 +247,43 @@ func (s *pollServer) Run() {
if fd == s.pr.Fd() { if fd == s.pr.Fd() {
// Drain our wakeup pipe. // Drain our wakeup pipe.
for nn, _ := s.pr.Read(&scratch); nn > 0; { for nn, _ := s.pr.Read(&scratch); nn > 0; {
nn, _ = s.pr.Read(&scratch) nn, _ = s.pr.Read(&scratch);
} }
// Read from channels // Read from channels
for fd, ok := <-s.cr; ok; fd, ok = <-s.cr { for fd, ok := <-s.cr; ok; fd, ok = <-s.cr {
s.AddFD(fd, 'r') s.AddFD(fd, 'r');
} }
for fd, ok := <-s.cw; ok; fd, ok = <-s.cw { for fd, ok := <-s.cw; ok; fd, ok = <-s.cw {
s.AddFD(fd, 'w') s.AddFD(fd, 'w');
} }
} else { } else {
netfd := s.LookupFD(fd, mode); netfd := s.LookupFD(fd, mode);
if netfd == nil { if netfd == nil {
print("pollServer: unexpected wakeup for fd=", netfd, " mode=", string(mode), "\n"); print("pollServer: unexpected wakeup for fd=", netfd, " mode=", string(mode), "\n");
continue continue;
} }
s.WakeFD(netfd, mode); s.WakeFD(netfd, mode);
} }
} }
} }
var wakeupbuf [1]byte; var wakeupbuf [1]byte
func (s *pollServer) Wakeup() { func (s *pollServer) Wakeup() {
s.pw.Write(&wakeupbuf) s.pw.Write(&wakeupbuf);
} }
func (s *pollServer) WaitRead(fd *netFD) { func (s *pollServer) WaitRead(fd *netFD) {
s.cr <- fd; s.cr <- fd;
s.Wakeup(); s.Wakeup();
<-fd.cr <-fd.cr;
} }
func (s *pollServer) WaitWrite(fd *netFD) { func (s *pollServer) WaitWrite(fd *netFD) {
s.cw <- fd; s.cw <- fd;
s.Wakeup(); s.Wakeup();
<-fd.cw <-fd.cw;
} }
...@@ -294,9 +295,9 @@ var pollserver *pollServer ...@@ -294,9 +295,9 @@ var pollserver *pollServer
func startServer() { func startServer() {
p, err := newPollServer(); p, err := newPollServer();
if err != nil { if err != nil {
print("Start pollServer: ", err.String(), "\n") print("Start pollServer: ", err.String(), "\n");
} }
pollserver = p pollserver = p;
} }
func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err os.Error) { func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err os.Error) {
...@@ -319,10 +320,10 @@ func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err ...@@ -319,10 +320,10 @@ func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err
if raddr != nil { if raddr != nil {
rs = raddr.String(); rs = raddr.String();
} }
f.file = os.NewFile(fd, net + ":" + ls + "->" + rs); f.file = os.NewFile(fd, net+":"+ls+"->"+rs);
f.cr = make(chan *netFD, 1); f.cr = make(chan *netFD, 1);
f.cw = make(chan *netFD, 1); f.cw = make(chan *netFD, 1);
return f, nil return f, nil;
} }
func isEAGAIN(e os.Error) bool { func isEAGAIN(e os.Error) bool {
...@@ -334,7 +335,7 @@ func isEAGAIN(e os.Error) bool { ...@@ -334,7 +335,7 @@ func isEAGAIN(e os.Error) bool {
func (fd *netFD) Close() os.Error { func (fd *netFD) Close() os.Error {
if fd == nil || fd.file == nil { if fd == nil || fd.file == nil {
return os.EINVAL return os.EINVAL;
} }
// In case the user has set linger, // In case the user has set linger,
...@@ -348,12 +349,12 @@ func (fd *netFD) Close() os.Error { ...@@ -348,12 +349,12 @@ func (fd *netFD) Close() os.Error {
e := fd.file.Close(); e := fd.file.Close();
fd.file = nil; fd.file = nil;
fd.fd = -1; fd.fd = -1;
return e return e;
} }
func (fd *netFD) Read(p []byte) (n int, err os.Error) { func (fd *netFD) Read(p []byte) (n int, err os.Error) {
if fd == nil || fd.file == nil { if fd == nil || fd.file == nil {
return 0, os.EINVAL return 0, os.EINVAL;
} }
fd.rio.Lock(); fd.rio.Lock();
defer fd.rio.Unlock(); defer fd.rio.Unlock();
...@@ -375,7 +376,7 @@ func (fd *netFD) Read(p []byte) (n int, err os.Error) { ...@@ -375,7 +376,7 @@ func (fd *netFD) Read(p []byte) (n int, err os.Error) {
func (fd *netFD) Write(p []byte) (n int, err os.Error) { func (fd *netFD) Write(p []byte) (n int, err os.Error) {
if fd == nil || fd.file == nil { if fd == nil || fd.file == nil {
return 0, os.EINVAL return 0, os.EINVAL;
} }
fd.wio.Lock(); fd.wio.Lock();
defer fd.wio.Unlock(); defer fd.wio.Unlock();
...@@ -389,7 +390,7 @@ func (fd *netFD) Write(p []byte) (n int, err os.Error) { ...@@ -389,7 +390,7 @@ func (fd *netFD) Write(p []byte) (n int, err os.Error) {
for nn < len(p) { for nn < len(p) {
n, err = fd.file.Write(p[nn:len(p)]); n, err = fd.file.Write(p[nn:len(p)]);
if n > 0 { if n > 0 {
nn += n nn += n;
} }
if nn == len(p) { if nn == len(p) {
break; break;
...@@ -402,12 +403,12 @@ func (fd *netFD) Write(p []byte) (n int, err os.Error) { ...@@ -402,12 +403,12 @@ func (fd *netFD) Write(p []byte) (n int, err os.Error) {
break; break;
} }
} }
return nn, err return nn, err;
} }
func (fd *netFD) accept(toAddr func(syscall.Sockaddr)Addr) (nfd *netFD, err os.Error) { func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err os.Error) {
if fd == nil || fd.file == nil { if fd == nil || fd.file == nil {
return nil, os.EINVAL return nil, os.EINVAL;
} }
// See ../syscall/exec.go for description of ForkLock. // See ../syscall/exec.go for description of ForkLock.
...@@ -427,14 +428,14 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr)Addr) (nfd *netFD, err os.E ...@@ -427,14 +428,14 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr)Addr) (nfd *netFD, err os.E
} }
if e != 0 { if e != 0 {
syscall.ForkLock.RUnlock(); syscall.ForkLock.RUnlock();
return nil, &OpError{"accept", fd.net, fd.laddr, os.Errno(e)} return nil, &OpError{"accept", fd.net, fd.laddr, os.Errno(e)};
} }
syscall.CloseOnExec(s); syscall.CloseOnExec(s);
syscall.ForkLock.RUnlock(); syscall.ForkLock.RUnlock();
if nfd, err = newFD(s, fd.family, fd.proto, fd.net, fd.laddr, toAddr(sa)); err != nil { if nfd, err = newFD(s, fd.family, fd.proto, fd.net, fd.laddr, toAddr(sa)); err != nil {
syscall.Close(s); syscall.Close(s);
return nil, err return nil, err;
} }
return nfd, nil return nfd, nil;
} }
...@@ -12,27 +12,27 @@ import ( ...@@ -12,27 +12,27 @@ import (
) )
type pollster struct { type pollster struct {
kq int; kq int;
eventbuf [10]syscall.Kevent_t; eventbuf [10]syscall.Kevent_t;
events []syscall.Kevent_t; events []syscall.Kevent_t;
} }
func newpollster() (p *pollster, err os.Error) { func newpollster() (p *pollster, err os.Error) {
p = new(pollster); p = new(pollster);
var e int; var e int;
if p.kq, e = syscall.Kqueue(); e != 0 { if p.kq, e = syscall.Kqueue(); e != 0 {
return nil, os.NewSyscallError("kqueue", e) return nil, os.NewSyscallError("kqueue", e);
} }
p.events = p.eventbuf[0:0]; p.events = p.eventbuf[0:0];
return p, nil return p, nil;
} }
func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error { func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
var kmode int; var kmode int;
if mode == 'r' { if mode == 'r' {
kmode = syscall.EVFILT_READ kmode = syscall.EVFILT_READ;
} else { } else {
kmode = syscall.EVFILT_WRITE kmode = syscall.EVFILT_WRITE;
} }
var events [1]syscall.Kevent_t; var events [1]syscall.Kevent_t;
ev := &events[0]; ev := &events[0];
...@@ -42,7 +42,7 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error { ...@@ -42,7 +42,7 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
// EV_ONESHOT - delete the event the first time it triggers // EV_ONESHOT - delete the event the first time it triggers
flags := syscall.EV_ADD | syscall.EV_RECEIPT; flags := syscall.EV_ADD | syscall.EV_RECEIPT;
if !repeat { if !repeat {
flags |= syscall.EV_ONESHOT flags |= syscall.EV_ONESHOT;
} }
syscall.SetKevent(ev, fd, kmode, flags); syscall.SetKevent(ev, fd, kmode, flags);
...@@ -54,17 +54,17 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error { ...@@ -54,17 +54,17 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
return os.ErrorString("kqueue phase error"); return os.ErrorString("kqueue phase error");
} }
if ev.Data != 0 { if ev.Data != 0 {
return os.Errno(int(ev.Data)) return os.Errno(int(ev.Data));
} }
return nil return nil;
} }
func (p *pollster) DelFD(fd int, mode int) { func (p *pollster) DelFD(fd int, mode int) {
var kmode int; var kmode int;
if mode == 'r' { if mode == 'r' {
kmode = syscall.EVFILT_READ kmode = syscall.EVFILT_READ;
} else { } else {
kmode = syscall.EVFILT_WRITE kmode = syscall.EVFILT_WRITE;
} }
var events [1]syscall.Kevent_t; var events [1]syscall.Kevent_t;
ev := &events[0]; ev := &events[0];
...@@ -87,26 +87,26 @@ func (p *pollster) WaitFD(nsec int64) (fd int, mode int, err os.Error) { ...@@ -87,26 +87,26 @@ func (p *pollster) WaitFD(nsec int64) (fd int, mode int, err os.Error) {
nn, e := syscall.Kevent(p.kq, nil, &p.eventbuf, t); nn, e := syscall.Kevent(p.kq, nil, &p.eventbuf, t);
if e != 0 { if e != 0 {
if e == syscall.EINTR { if e == syscall.EINTR {
continue continue;
} }
return -1, 0, os.NewSyscallError("kevent", e) return -1, 0, os.NewSyscallError("kevent", e);
} }
if nn == 0 { if nn == 0 {
return -1, 0, nil; return -1, 0, nil;
} }
p.events = p.eventbuf[0:nn] p.events = p.eventbuf[0:nn];
} }
ev := &p.events[0]; ev := &p.events[0];
p.events = p.events[1:len(p.events)]; p.events = p.events[1:len(p.events)];
fd = int(ev.Ident); fd = int(ev.Ident);
if ev.Filter == syscall.EVFILT_READ { if ev.Filter == syscall.EVFILT_READ {
mode = 'r' mode = 'r';
} else { } else {
mode = 'w' mode = 'w';
} }
return fd, mode, nil return fd, mode, nil;
} }
func (p *pollster) Close() os.Error { func (p *pollster) Close() os.Error {
return os.NewSyscallError("close", syscall.Close(p.kq)) return os.NewSyscallError("close", syscall.Close(p.kq));
} }
...@@ -12,15 +12,15 @@ import ( ...@@ -12,15 +12,15 @@ import (
) )
const ( const (
readFlags = syscall.EPOLLIN | syscall.EPOLLRDHUP; readFlags = syscall.EPOLLIN | syscall.EPOLLRDHUP;
writeFlags = syscall.EPOLLOUT writeFlags = syscall.EPOLLOUT;
) )
type pollster struct { type pollster struct {
epfd int; epfd int;
// Events we're already waiting for // Events we're already waiting for
events map[int] uint32; events map[int]uint32;
} }
func newpollster() (p *pollster, err os.Error) { func newpollster() (p *pollster, err os.Error) {
...@@ -31,10 +31,10 @@ func newpollster() (p *pollster, err os.Error) { ...@@ -31,10 +31,10 @@ func newpollster() (p *pollster, err os.Error) {
// about the number of FDs we will care about. // about the number of FDs we will care about.
// We don't know. // We don't know.
if p.epfd, e = syscall.EpollCreate(16); e != 0 { if p.epfd, e = syscall.EpollCreate(16); e != 0 {
return nil, os.NewSyscallError("epoll_create", e) return nil, os.NewSyscallError("epoll_create", e);
} }
p.events = make(map[int] uint32); p.events = make(map[int]uint32);
return p, nil return p, nil;
} }
func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error { func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
...@@ -58,10 +58,10 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error { ...@@ -58,10 +58,10 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
op = syscall.EPOLL_CTL_ADD; op = syscall.EPOLL_CTL_ADD;
} }
if e := syscall.EpollCtl(p.epfd, op, fd, &ev); e != 0 { if e := syscall.EpollCtl(p.epfd, op, fd, &ev); e != 0 {
return os.NewSyscallError("epoll_ctl", e) return os.NewSyscallError("epoll_ctl", e);
} }
p.events[fd] = ev.Events; p.events[fd] = ev.Events;
return nil return nil;
} }
func (p *pollster) StopWaiting(fd int, bits uint) { func (p *pollster) StopWaiting(fd int, bits uint) {
...@@ -111,7 +111,7 @@ func (p *pollster) WaitFD(nsec int64) (fd int, mode int, err os.Error) { ...@@ -111,7 +111,7 @@ func (p *pollster) WaitFD(nsec int64) (fd int, mode int, err os.Error) {
ev := &evarray[0]; ev := &evarray[0];
var msec int = -1; var msec int = -1;
if nsec > 0 { if nsec > 0 {
msec = int((nsec + 1e6 - 1)/1e6); msec = int((nsec+1e6-1)/1e6);
} }
n, e := syscall.EpollWait(p.epfd, &evarray, msec); n, e := syscall.EpollWait(p.epfd, &evarray, msec);
for e == syscall.EAGAIN || e == syscall.EINTR { for e == syscall.EAGAIN || e == syscall.EINTR {
......
...@@ -9,11 +9,10 @@ import ( ...@@ -9,11 +9,10 @@ import (
"syscall"; "syscall";
) )
type pollster struct { type pollster struct{}
}
func newpollster() (p *pollster, err os.Error) { func newpollster() (p *pollster, err os.Error) {
return nil, os.NewSyscallError("networking", syscall.ENACL) return nil, os.NewSyscallError("networking", syscall.ENACL);
} }
func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error { func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
......
...@@ -14,8 +14,8 @@ package net ...@@ -14,8 +14,8 @@ package net
// IP address lengths (bytes). // IP address lengths (bytes).
const ( const (
IPv4len = 4; IPv4len = 4;
IPv6len = 16 IPv6len = 16;
) )
// An IP is a single IP address, an array of bytes. // An IP is a single IP address, an array of bytes.
...@@ -30,17 +30,17 @@ const ( ...@@ -30,17 +30,17 @@ const (
// is a semantic property of the address, not just the // is a semantic property of the address, not just the
// length of the byte array: a 16-byte array can still // length of the byte array: a 16-byte array can still
// be an IPv4 address. // be an IPv4 address.
type IP []byte; type IP []byte
// An IP mask is an IP address. // An IP mask is an IP address.
type IPMask []byte; type IPMask []byte
// IPv4 returns the IP address (in 16-byte form) of the // IPv4 returns the IP address (in 16-byte form) of the
// IPv4 address a.b.c.d. // IPv4 address a.b.c.d.
func IPv4(a, b, c, d byte) IP { func IPv4(a, b, c, d byte) IP {
p := make(IP, IPv6len); p := make(IP, IPv6len);
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
p[i] = 0 p[i] = 0;
} }
p[10] = 0xff; p[10] = 0xff;
p[11] = 0xff; p[11] = 0xff;
...@@ -48,15 +48,15 @@ func IPv4(a, b, c, d byte) IP { ...@@ -48,15 +48,15 @@ func IPv4(a, b, c, d byte) IP {
p[13] = b; p[13] = b;
p[14] = c; p[14] = c;
p[15] = d; p[15] = d;
return p return p;
} }
// Well-known IPv4 addresses // Well-known IPv4 addresses
var ( var (
IPv4bcast = IPv4(255, 255, 255, 255); // broadcast IPv4bcast = IPv4(255, 255, 255, 255); // broadcast
IPv4allsys = IPv4(224, 0, 0, 1); // all systems IPv4allsys = IPv4(224, 0, 0, 1); // all systems
IPv4allrouter = IPv4(224, 0, 0, 2); // all routers IPv4allrouter = IPv4(224, 0, 0, 2); // all routers
IPv4zero = IPv4(0, 0, 0, 0); // all zeros IPv4zero = IPv4(0, 0, 0, 0); // all zeros
) )
// Well-known IPv6 addresses // Well-known IPv6 addresses
...@@ -68,52 +68,52 @@ var ( ...@@ -68,52 +68,52 @@ var (
func isZeros(p IP) bool { func isZeros(p IP) bool {
for i := 0; i < len(p); i++ { for i := 0; i < len(p); i++ {
if p[i] != 0 { if p[i] != 0 {
return false return false;
} }
} }
return true return true;
} }
// To4 converts the IPv4 address ip to a 4-byte representation. // To4 converts the IPv4 address ip to a 4-byte representation.
// If ip is not an IPv4 address, To4 returns nil. // If ip is not an IPv4 address, To4 returns nil.
func (ip IP) To4() IP { func (ip IP) To4() IP {
if len(ip) == IPv4len { if len(ip) == IPv4len {
return ip return ip;
} }
if len(ip) == IPv6len if len(ip) == IPv6len &&
&& isZeros(ip[0:10]) isZeros(ip[0:10]) &&
&& ip[10] == 0xff ip[10] == 0xff &&
&& ip[11] == 0xff { ip[11] == 0xff {
return ip[12:16] return ip[12:16];
} }
return nil return nil;
} }
// To16 converts the IP address ip to a 16-byte representation. // To16 converts the IP address ip to a 16-byte representation.
// If ip is not an IP address (it is the wrong length), To16 returns nil. // If ip is not an IP address (it is the wrong length), To16 returns nil.
func (ip IP) To16() IP { func (ip IP) To16() IP {
if len(ip) == IPv4len { if len(ip) == IPv4len {
return IPv4(ip[0], ip[1], ip[2], ip[3]) return IPv4(ip[0], ip[1], ip[2], ip[3]);
} }
if len(ip) == IPv6len { if len(ip) == IPv6len {
return ip return ip;
} }
return nil return nil;
} }
// Default route masks for IPv4. // Default route masks for IPv4.
var ( var (
classAMask = IPMask(IPv4(0xff, 0, 0, 0)); classAMask = IPMask(IPv4(0xff, 0, 0, 0));
classBMask = IPMask(IPv4(0xff, 0xff, 0, 0)); classBMask = IPMask(IPv4(0xff, 0xff, 0, 0));
classCMask = IPMask(IPv4(0xff, 0xff, 0xff, 0)); classCMask = IPMask(IPv4(0xff, 0xff, 0xff, 0));
) )
// DefaultMask returns the default IP mask for the IP address ip. // DefaultMask returns the default IP mask for the IP address ip.
// Only IPv4 addresses have default masks; DefaultMask returns // Only IPv4 addresses have default masks; DefaultMask returns
// nil if ip is not a valid IPv4 address. // nil if ip is not a valid IPv4 address.
func (ip IP) DefaultMask() IPMask { func (ip IP) DefaultMask() IPMask {
if ip = ip.To4(); ip == nil { if ip = ip.To4(); ip == nil {
return nil return nil;
} }
switch true { switch true {
case ip[0] < 0x80: case ip[0] < 0x80:
...@@ -130,19 +130,19 @@ func (ip IP) DefaultMask() IPMask { ...@@ -130,19 +130,19 @@ func (ip IP) DefaultMask() IPMask {
func (ip IP) Mask(mask IPMask) IP { func (ip IP) Mask(mask IPMask) IP {
n := len(ip); n := len(ip);
if n != len(mask) { if n != len(mask) {
return nil return nil;
} }
out := make(IP, n); out := make(IP, n);
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
out[i] = ip[i] & mask[i]; out[i] = ip[i]&mask[i];
} }
return out return out;
} }
// Convert i to decimal string. // Convert i to decimal string.
func itod(i uint) string { func itod(i uint) string {
if i == 0 { if i == 0 {
return "0" return "0";
} }
// Assemble decimal in reverse order. // Assemble decimal in reverse order.
...@@ -150,16 +150,16 @@ func itod(i uint) string { ...@@ -150,16 +150,16 @@ func itod(i uint) string {
bp := len(b); bp := len(b);
for ; i > 0; i /= 10 { for ; i > 0; i /= 10 {
bp--; bp--;
b[bp] = byte(i%10) + '0' b[bp] = byte(i%10)+'0';
} }
return string(b[bp:len(b)]) return string(b[bp:len(b)]);
} }
// Convert i to hexadecimal string. // Convert i to hexadecimal string.
func itox(i uint) string { func itox(i uint) string {
if i == 0 { if i == 0 {
return "0" return "0";
} }
// Assemble hexadecimal in reverse order. // Assemble hexadecimal in reverse order.
...@@ -167,10 +167,10 @@ func itox(i uint) string { ...@@ -167,10 +167,10 @@ func itox(i uint) string {
bp := len(b); bp := len(b);
for ; i > 0; i /= 16 { for ; i > 0; i /= 16 {
bp--; bp--;
b[bp] = "0123456789abcdef"[byte(i%16)] b[bp] = "0123456789abcdef"[byte(i%16)];
} }
return string(b[bp:len(b)]) return string(b[bp:len(b)]);
} }
// String returns the string form of the IP address ip. // String returns the string form of the IP address ip.
...@@ -186,26 +186,26 @@ func (ip IP) String() string { ...@@ -186,26 +186,26 @@ func (ip IP) String() string {
// If IPv4, use dotted notation. // If IPv4, use dotted notation.
if p4 := p.To4(); len(p4) == 4 { if p4 := p.To4(); len(p4) == 4 {
return itod(uint(p4[0]))+"." return itod(uint(p4[0])) + "." +
+itod(uint(p4[1]))+"." itod(uint(p4[1])) + "." +
+itod(uint(p4[2]))+"." itod(uint(p4[2])) + "." +
+itod(uint(p4[3])) itod(uint(p4[3]));
} }
if len(p) != IPv6len { if len(p) != IPv6len {
return "?" return "?";
} }
// Find longest run of zeros. // Find longest run of zeros.
e0 := -1; e0 := -1;
e1 := -1; e1 := -1;
for i := 0; i < 16; i+=2 { for i := 0; i < 16; i += 2 {
j := i; j := i;
for j < 16 && p[j] == 0 && p[j+1] == 0 { for j < 16 && p[j] == 0 && p[j+1] == 0 {
j += 2 j += 2;
} }
if j > i && j - i > e1 - e0 { if j > i && j-i > e1-e0 {
e0 = i; e0 = i;
e1 = j e1 = j;
} }
} }
...@@ -216,14 +216,14 @@ func (ip IP) String() string { ...@@ -216,14 +216,14 @@ func (ip IP) String() string {
s += "::"; s += "::";
i = e1; i = e1;
if i >= 16 { if i >= 16 {
break break;
} }
} else if i > 0 { } else if i > 0 {
s += ":" s += ":";
} }
s += itox((uint(p[i])<<8) | uint(p[i+1])) s += itox((uint(p[i])<<8)|uint(p[i+1]));
} }
return s return s;
} }
// If mask is a sequence of 1 bits followed by 0 bits, // If mask is a sequence of 1 bits followed by 0 bits,
...@@ -232,24 +232,24 @@ func simpleMaskLength(mask IPMask) int { ...@@ -232,24 +232,24 @@ func simpleMaskLength(mask IPMask) int {
var i int; var i int;
for i = 0; i < len(mask); i++ { for i = 0; i < len(mask); i++ {
if mask[i] != 0xFF { if mask[i] != 0xFF {
break break;
} }
} }
n := 8*i; n := 8*i;
v := mask[i]; v := mask[i];
for v & 0x80 != 0 { for v&0x80 != 0 {
n++; n++;
v <<= 1 v <<= 1;
} }
if v != 0 { if v != 0 {
return -1 return -1;
} }
for i++; i < len(mask); i++ { for i++; i < len(mask); i++ {
if mask[i] != 0 { if mask[i] != 0 {
return -1 return -1;
} }
} }
return n return n;
} }
// String returns the string representation of mask. // String returns the string representation of mask.
...@@ -262,12 +262,12 @@ func (mask IPMask) String() string { ...@@ -262,12 +262,12 @@ func (mask IPMask) String() string {
case 4: case 4:
n := simpleMaskLength(mask); n := simpleMaskLength(mask);
if n >= 0 { if n >= 0 {
return itod(uint(n+(IPv6len-IPv4len)*8)) return itod(uint(n + (IPv6len-IPv4len)*8));
} }
case 16: case 16:
n := simpleMaskLength(mask); n := simpleMaskLength(mask);
if n >= 0 { if n >= 0 {
return itod(uint(n)) return itod(uint(n));
} }
} }
return IP(mask).String(); return IP(mask).String();
...@@ -280,24 +280,24 @@ func parseIPv4(s string) IP { ...@@ -280,24 +280,24 @@ func parseIPv4(s string) IP {
for j := 0; j < IPv4len; j++ { for j := 0; j < IPv4len; j++ {
if j > 0 { if j > 0 {
if s[i] != '.' { if s[i] != '.' {
return nil return nil;
} }
i++; i++;
} }
var ( var (
n int; n int;
ok bool ok bool;
) )
n, i, ok = dtoi(s, i); n, i, ok = dtoi(s, i);
if !ok || n > 0xFF { if !ok || n > 0xFF {
return nil return nil;
} }
p[j] = byte(n) p[j] = byte(n);
} }
if i != len(s) { if i != len(s) {
return nil return nil;
} }
return IPv4(p[0], p[1], p[2], p[3]) return IPv4(p[0], p[1], p[2], p[3]);
} }
// Parse IPv6 address. Many forms. // Parse IPv6 address. Many forms.
...@@ -311,7 +311,7 @@ func parseIPv4(s string) IP { ...@@ -311,7 +311,7 @@ func parseIPv4(s string) IP {
func parseIPv6(s string) IP { func parseIPv6(s string) IP {
p := make(IP, 16); p := make(IP, 16);
ellipsis := -1; // position of ellipsis in p ellipsis := -1; // position of ellipsis in p
i := 0; // index in string s i := 0; // index in string s
// Might have leading ellipsis // Might have leading ellipsis
if len(s) >= 2 && s[0] == ':' && s[1] == ':' { if len(s) >= 2 && s[0] == ':' && s[1] == ':' {
...@@ -319,7 +319,7 @@ func parseIPv6(s string) IP { ...@@ -319,7 +319,7 @@ func parseIPv6(s string) IP {
i = 2; i = 2;
// Might be only ellipsis // Might be only ellipsis
if i == len(s) { if i == len(s) {
return p return p;
} }
} }
...@@ -329,22 +329,22 @@ L: for j < IPv6len { ...@@ -329,22 +329,22 @@ L: for j < IPv6len {
// Hex number. // Hex number.
n, i1, ok := xtoi(s, i); n, i1, ok := xtoi(s, i);
if !ok || n > 0xFFFF { if !ok || n > 0xFFFF {
return nil return nil;
} }
// If followed by dot, might be in trailing IPv4. // If followed by dot, might be in trailing IPv4.
if i1 < len(s) && s[i1] == '.' { if i1 < len(s) && s[i1] == '.' {
if ellipsis < 0 && j != IPv6len - IPv4len { if ellipsis < 0 && j != IPv6len-IPv4len {
// Not the right place. // Not the right place.
return nil return nil;
} }
if j+IPv4len > IPv6len { if j+IPv4len > IPv6len {
// Not enough room. // Not enough room.
return nil return nil;
} }
p4 := parseIPv4(s[i:len(s)]); p4 := parseIPv4(s[i:len(s)]);
if p4 == nil { if p4 == nil {
return nil return nil;
} }
p[j] = p4[12]; p[j] = p4[12];
p[j+1] = p4[13]; p[j+1] = p4[13];
...@@ -352,7 +352,7 @@ L: for j < IPv6len { ...@@ -352,7 +352,7 @@ L: for j < IPv6len {
p[j+3] = p4[15]; p[j+3] = p4[15];
i = len(s); i = len(s);
j += 4; j += 4;
break break;
} }
// Save this 16-bit chunk. // Save this 16-bit chunk.
...@@ -363,46 +363,46 @@ L: for j < IPv6len { ...@@ -363,46 +363,46 @@ L: for j < IPv6len {
// Stop at end of string. // Stop at end of string.
i = i1; i = i1;
if i == len(s) { if i == len(s) {
break break;
} }
// Otherwise must be followed by colon and more. // Otherwise must be followed by colon and more.
if s[i] != ':' && i+1 == len(s) { if s[i] != ':' && i+1 == len(s) {
return nil return nil;
} }
i++; i++;
// Look for ellipsis. // Look for ellipsis.
if s[i] == ':' { if s[i] == ':' {
if ellipsis >= 0 { // already have one if ellipsis >= 0 { // already have one
return nil return nil;
} }
ellipsis = j; ellipsis = j;
if i++; i == len(s) { // can be at end if i++; i == len(s) { // can be at end
break break;
} }
} }
} }
// Must have used entire string. // Must have used entire string.
if i != len(s) { if i != len(s) {
return nil return nil;
} }
// If didn't parse enough, expand ellipsis. // If didn't parse enough, expand ellipsis.
if j < IPv6len { if j < IPv6len {
if ellipsis < 0 { if ellipsis < 0 {
return nil return nil;
} }
n := IPv6len - j; n := IPv6len-j;
for k := j-1; k >= ellipsis; k-- { for k := j-1; k >= ellipsis; k-- {
p[k+n] = p[k] p[k+n] = p[k];
} }
for k := ellipsis+n-1; k>=ellipsis; k-- { for k := ellipsis+n-1; k >= ellipsis; k-- {
p[k] = 0 p[k] = 0;
} }
} }
return p return p;
} }
// ParseIP parses s as an IP address, returning the result. // ParseIP parses s as an IP address, returning the result.
...@@ -413,8 +413,7 @@ L: for j < IPv6len { ...@@ -413,8 +413,7 @@ L: for j < IPv6len {
func ParseIP(s string) IP { func ParseIP(s string) IP {
p := parseIPv4(s); p := parseIPv4(s);
if p != nil { if p != nil {
return p return p;
} }
return parseIPv6(s) return parseIPv6(s);
} }
...@@ -5,28 +5,29 @@ ...@@ -5,28 +5,29 @@
package net package net
import ( import (
"testing" "testing";
) )
func isEqual(a, b IP) bool { func isEqual(a, b IP) bool {
if a == nil && b == nil { if a == nil && b == nil {
return true return true;
} }
if a == nil || b == nil || len(a) != len(b) { if a == nil || b == nil || len(a) != len(b) {
return false return false;
} }
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
if a[i] != b[i] { if a[i] != b[i] {
return false return false;
} }
} }
return true return true;
} }
type parseIPTest struct { type parseIPTest struct {
in string; in string;
out IP; out IP;
} }
var parseiptests = []parseIPTest{ var parseiptests = []parseIPTest{
parseIPTest{"127.0.1.2", IPv4(127, 0, 1, 2)}, parseIPTest{"127.0.1.2", IPv4(127, 0, 1, 2)},
parseIPTest{"127.0.0.1", IPv4(127, 0, 0, 1)}, parseIPTest{"127.0.0.1", IPv4(127, 0, 0, 1)},
...@@ -34,8 +35,10 @@ var parseiptests = []parseIPTest{ ...@@ -34,8 +35,10 @@ var parseiptests = []parseIPTest{
parseIPTest{"abc", nil}, parseIPTest{"abc", nil},
parseIPTest{"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)}, parseIPTest{"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)},
parseIPTest{"2001:4860:0:2001::68", parseIPTest{"2001:4860:0:2001::68",
IP{0x20,0x01, 0x48,0x60, 0,0, 0x20,0x01, IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01,
0,0, 0,0, 0,0, 0x00,0x68}}, 0, 0, 0, 0, 0, 0, 0x00, 0x68,
},
},
parseIPTest{"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)}, parseIPTest{"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)},
} }
......
...@@ -20,9 +20,9 @@ import ( ...@@ -20,9 +20,9 @@ import (
func kernelSupportsIPv6() bool { func kernelSupportsIPv6() bool {
fd, e := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP); fd, e := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP);
if fd >= 0 { if fd >= 0 {
syscall.Close(fd) syscall.Close(fd);
} }
return e == 0 return e == 0;
} }
var preferIPv4 = !kernelSupportsIPv6() var preferIPv4 = !kernelSupportsIPv6()
...@@ -31,7 +31,7 @@ var preferIPv4 = !kernelSupportsIPv6() ...@@ -31,7 +31,7 @@ var preferIPv4 = !kernelSupportsIPv6()
// /proc/sys/net/core/somaxconn, // /proc/sys/net/core/somaxconn,
// to take advantage of kernels that have raised the limit. // to take advantage of kernels that have raised the limit.
func listenBacklog() int { func listenBacklog() int {
return syscall.SOMAXCONN return syscall.SOMAXCONN;
} }
// Internet sockets (TCP, UDP) // Internet sockets (TCP, UDP)
...@@ -50,7 +50,7 @@ func internetSocket(net string, laddr, raddr sockaddr, proto int, mode string, t ...@@ -50,7 +50,7 @@ func internetSocket(net string, laddr, raddr sockaddr, proto int, mode string, t
family := syscall.AF_INET6; family := syscall.AF_INET6;
switch net[len(net)-1] { switch net[len(net)-1] {
case '4': case '4':
family = syscall.AF_INET family = syscall.AF_INET;
case '6': case '6':
// nothing to do // nothing to do
default: default:
...@@ -114,7 +114,7 @@ func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) { ...@@ -114,7 +114,7 @@ func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) {
ip = IPv4zero; ip = IPv4zero;
} }
if ip = ip.To4(); ip == nil { if ip = ip.To4(); ip == nil {
return nil, os.EINVAL return nil, os.EINVAL;
} }
s := new(syscall.SockaddrInet4); s := new(syscall.SockaddrInet4);
for i := 0; i < IPv4len; i++ { for i := 0; i < IPv4len; i++ {
...@@ -133,7 +133,7 @@ func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) { ...@@ -133,7 +133,7 @@ func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) {
ip = IPzero; ip = IPzero;
} }
if ip = ip.To16(); ip == nil { if ip = ip.To16(); ip == nil {
return nil, os.EINVAL return nil, os.EINVAL;
} }
s := new(syscall.SockaddrInet6); s := new(syscall.SockaddrInet6);
for i := 0; i < IPv6len; i++ { for i := 0; i < IPv6len; i++ {
...@@ -155,11 +155,11 @@ func splitHostPort(hostport string) (host, port string, err os.Error) { ...@@ -155,11 +155,11 @@ func splitHostPort(hostport string) (host, port string, err os.Error) {
return; return;
} }
host, port = hostport[0:i], hostport[i+1:len(hostport)]; host, port = hostport[0:i], hostport[i+1 : len(hostport)];
// Can put brackets around host ... // Can put brackets around host ...
if len(host) > 0 && host[0] == '[' && host[len(host)-1] == ']' { if len(host) > 0 && host[0] == '[' && host[len(host)-1] == ']' {
host = host[1:len(host)-1] host = host[1 : len(host)-1];
} else { } else {
// ... but if there are no brackets, no colons. // ... but if there are no brackets, no colons.
if byteIndex(host, ':') >= 0 { if byteIndex(host, ':') >= 0 {
...@@ -175,9 +175,9 @@ func splitHostPort(hostport string) (host, port string, err os.Error) { ...@@ -175,9 +175,9 @@ func splitHostPort(hostport string) (host, port string, err os.Error) {
func joinHostPort(host, port string) string { func joinHostPort(host, port string) string {
// If host has colons, have to bracket it. // If host has colons, have to bracket it.
if byteIndex(host, ':') >= 0 { if byteIndex(host, ':') >= 0 {
return "[" + host + "]:" + port return "["+host+"]:"+port;
} }
return host + ":" + port return host+":"+port;
} }
// Convert "host:port" into IP address and port. // Convert "host:port" into IP address and port.
...@@ -224,4 +224,3 @@ func hostPortToIP(net, hostport string) (ip IP, iport int, err os.Error) { ...@@ -224,4 +224,3 @@ func hostPortToIP(net, hostport string) (ip IP, iport int, err os.Error) {
Error: Error:
return nil, 0, err; return nil, 0, err;
} }
...@@ -13,7 +13,7 @@ import "os" ...@@ -13,7 +13,7 @@ import "os"
// Addr represents a network end point address. // Addr represents a network end point address.
type Addr interface { type Addr interface {
Network() string; // name of the network Network() string; // name of the network
String() string; // string form of address String() string; // string form of address
} }
// Conn is a generic stream-oriented network connection. // Conn is a generic stream-oriented network connection.
...@@ -233,10 +233,10 @@ func ListenPacket(net, laddr string) (c PacketConn, err os.Error) { ...@@ -233,10 +233,10 @@ func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
var errMissingAddress = os.ErrorString("missing address") var errMissingAddress = os.ErrorString("missing address")
type OpError struct { type OpError struct {
Op string; Op string;
Net string; Net string;
Addr Addr; Addr Addr;
Error os.Error; Error os.Error;
} }
func (e *OpError) String() string { func (e *OpError) String() string {
...@@ -252,8 +252,8 @@ func (e *OpError) String() string { ...@@ -252,8 +252,8 @@ func (e *OpError) String() string {
} }
type AddrError struct { type AddrError struct {
Error string; Error string;
Addr string; Addr string;
} }
func (e *AddrError) String() string { func (e *AddrError) String() string {
...@@ -265,7 +265,7 @@ func (e *AddrError) String() string { ...@@ -265,7 +265,7 @@ func (e *AddrError) String() string {
} }
type UnknownNetworkError string type UnknownNetworkError string
func (e UnknownNetworkError) String() string { func (e UnknownNetworkError) String() string {
return "unknown network " + string(e); return "unknown network " + string(e);
} }
...@@ -10,13 +10,13 @@ import ( ...@@ -10,13 +10,13 @@ import (
) )
type DialErrorTest struct { type DialErrorTest struct {
Net string; Net string;
Laddr string; Laddr string;
Raddr string; Raddr string;
Pattern string; Pattern string;
} }
var dialErrorTests = []DialErrorTest { var dialErrorTests = []DialErrorTest{
DialErrorTest{ DialErrorTest{
"datakit", "", "mh/astro/r70", "datakit", "", "mh/astro/r70",
"dial datakit mh/astro/r70: unknown network datakit", "dial datakit mh/astro/r70: unknown network datakit",
......
...@@ -13,12 +13,12 @@ import ( ...@@ -13,12 +13,12 @@ import (
) )
type file struct { type file struct {
file *os.File; file *os.File;
data []byte; data []byte;
} }
func (f *file) close() { func (f *file) close() {
f.file.Close() f.file.Close();
} }
func (f *file) getLineFromData() (s string, ok bool) { func (f *file) getLineFromData() (s string, ok bool) {
...@@ -29,30 +29,30 @@ func (f *file) getLineFromData() (s string, ok bool) { ...@@ -29,30 +29,30 @@ func (f *file) getLineFromData() (s string, ok bool) {
ok = true; ok = true;
// move data // move data
i++; i++;
n := len(data) - i; n := len(data)-i;
for j := 0; j < n; j++ { for j := 0; j < n; j++ {
data[j] = data[i+j]; data[j] = data[i+j];
} }
f.data = data[0:n]; f.data = data[0:n];
return return;
} }
} }
return return;
} }
func (f *file) readLine() (s string, ok bool) { func (f *file) readLine() (s string, ok bool) {
if s, ok = f.getLineFromData(); ok { if s, ok = f.getLineFromData(); ok {
return return;
} }
if len(f.data) < cap(f.data) { if len(f.data) < cap(f.data) {
ln := len(f.data); ln := len(f.data);
n, _ := io.ReadFull(f.file, f.data[ln:cap(f.data)]); n, _ := io.ReadFull(f.file, f.data[ln:cap(f.data)]);
if n >= 0 { if n >= 0 {
f.data = f.data[0:ln+n]; f.data = f.data[0 : ln+n];
} }
} }
s, ok = f.getLineFromData(); s, ok = f.getLineFromData();
return return;
} }
func open(name string) (*file, os.Error) { func open(name string) (*file, os.Error) {
...@@ -66,10 +66,10 @@ func open(name string) (*file, os.Error) { ...@@ -66,10 +66,10 @@ func open(name string) (*file, os.Error) {
func byteIndex(s string, c byte) int { func byteIndex(s string, c byte) int {
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if s[i] == c { if s[i] == c {
return i return i;
} }
} }
return -1 return -1;
} }
// Count occurrences in s of any bytes in t. // Count occurrences in s of any bytes in t.
...@@ -80,12 +80,12 @@ func countAnyByte(s string, t string) int { ...@@ -80,12 +80,12 @@ func countAnyByte(s string, t string) int {
n++; n++;
} }
} }
return n return n;
} }
// Split s at any bytes in t. // Split s at any bytes in t.
func splitAtBytes(s string, t string) []string { func splitAtBytes(s string, t string) []string {
a := make([]string, 1+countAnyByte(s, t)); a := make([]string, 1 + countAnyByte(s, t));
n := 0; n := 0;
last := 0; last := 0;
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
...@@ -116,15 +116,15 @@ const big = 0xFFFFFF ...@@ -116,15 +116,15 @@ const big = 0xFFFFFF
func dtoi(s string, i0 int) (n int, i int, ok bool) { func dtoi(s string, i0 int) (n int, i int, ok bool) {
n = 0; n = 0;
for i = i0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ { for i = i0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
n = n*10 + int(s[i] - '0'); n = n*10 + int(s[i]-'0');
if n >= big { if n >= big {
return 0, i, false return 0, i, false;
} }
} }
if i == i0 { if i == i0 {
return 0, i, false return 0, i, false;
} }
return n, i, true return n, i, true;
} }
// Hexadecimal to integer starting at &s[i0]. // Hexadecimal to integer starting at &s[i0].
...@@ -134,24 +134,24 @@ func xtoi(s string, i0 int) (n int, i int, ok bool) { ...@@ -134,24 +134,24 @@ func xtoi(s string, i0 int) (n int, i int, ok bool) {
for i = i0; i < len(s); i++ { for i = i0; i < len(s); i++ {
if '0' <= s[i] && s[i] <= '9' { if '0' <= s[i] && s[i] <= '9' {
n *= 16; n *= 16;
n += int(s[i] - '0') n += int(s[i]-'0');
} else if 'a' <= s[i] && s[i] <= 'f' { } else if 'a' <= s[i] && s[i] <= 'f' {
n *= 16; n *= 16;
n += int(s[i] - 'a') + 10 n += int(s[i]-'a')+10;
} else if 'A' <= s[i] && s[i] <= 'F' { } else if 'A' <= s[i] && s[i] <= 'F' {
n *= 16; n *= 16;
n += int(s[i] -'A') + 10 n += int(s[i]-'A')+10;
} else { } else {
break break;
} }
if n >= big { if n >= big {
return 0, i, false return 0, i, false;
} }
} }
if i == i0 { if i == i0 {
return 0, i, false return 0, i, false;
} }
return n, i, true return n, i, true;
} }
// Integer to decimal. // Integer to decimal.
......
...@@ -30,7 +30,7 @@ func TestReadLine(t *testing.T) { ...@@ -30,7 +30,7 @@ func TestReadLine(t *testing.T) {
for { for {
bline, berr := br.ReadString('\n'); bline, berr := br.ReadString('\n');
if n := len(bline); n > 0 { if n := len(bline); n > 0 {
bline = bline[0:n-1]; bline = bline[0 : n-1];
} }
line, ok := file.readLine(); line, ok := file.readLine();
if (berr != nil) != !ok || bline != line { if (berr != nil) != !ok || bline != line {
...@@ -38,9 +38,9 @@ func TestReadLine(t *testing.T) { ...@@ -38,9 +38,9 @@ func TestReadLine(t *testing.T) {
filename, lineno, byteno, bline, berr, line, ok); filename, lineno, byteno, bline, berr, line, ok);
} }
if !ok { if !ok {
break break;
} }
lineno++; lineno++;
byteno += len(line) + 1; byteno += len(line)+1;
} }
} }
...@@ -11,11 +11,11 @@ import ( ...@@ -11,11 +11,11 @@ import (
"os"; "os";
) )
var services map[string] map[string] int var services map[string]map[string]int
var servicesError os.Error var servicesError os.Error
func readServices() { func readServices() {
services = make(map[string] map[string] int); services = make(map[string]map[string]int);
var file *file; var file *file;
file, servicesError = open("/etc/services"); file, servicesError = open("/etc/services");
for line, ok := file.readLine(); ok; line, ok = file.readLine() { for line, ok := file.readLine(); ok; line, ok = file.readLine() {
...@@ -30,12 +30,12 @@ func readServices() { ...@@ -30,12 +30,12 @@ func readServices() {
portnet := f[1]; // "tcp/80" portnet := f[1]; // "tcp/80"
port, j, ok := dtoi(portnet, 0); port, j, ok := dtoi(portnet, 0);
if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' { if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
continue continue;
} }
netw := portnet[j+1:len(portnet)]; // "tcp" netw := portnet[j+1 : len(portnet)]; // "tcp"
m, ok1 := services[netw]; m, ok1 := services[netw];
if !ok1 { if !ok1 {
m = make(map[string] int); m = make(map[string]int);
services[netw] = m; services[netw] = m;
} }
for i := 0; i < len(f); i++ { for i := 0; i < len(f); i++ {
...@@ -63,5 +63,5 @@ func LookupPort(network, service string) (port int, err os.Error) { ...@@ -63,5 +63,5 @@ func LookupPort(network, service string) (port int, err os.Error) {
return; return;
} }
} }
return 0, &AddrError{"unknown port", network + "/" + service}; return 0, &AddrError{"unknown port", network+"/"+service};
} }
...@@ -9,42 +9,42 @@ import ( ...@@ -9,42 +9,42 @@ import (
) )
type portTest struct { type portTest struct {
netw string; netw string;
name string; name string;
port int; port int;
ok bool; ok bool;
} }
var porttests = []portTest { var porttests = []portTest{
portTest{ "tcp", "echo", 7, true }, portTest{"tcp", "echo", 7, true},
portTest{ "tcp", "discard", 9, true }, portTest{"tcp", "discard", 9, true},
portTest{ "tcp", "systat", 11, true }, portTest{"tcp", "systat", 11, true},
portTest{ "tcp", "daytime", 13, true }, portTest{"tcp", "daytime", 13, true},
portTest{ "tcp", "chargen", 19, true }, portTest{"tcp", "chargen", 19, true},
portTest{ "tcp", "ftp-data", 20, true }, portTest{"tcp", "ftp-data", 20, true},
portTest{ "tcp", "ftp", 21, true }, portTest{"tcp", "ftp", 21, true},
portTest{ "tcp", "ssh", 22, true }, portTest{"tcp", "ssh", 22, true},
portTest{ "tcp", "telnet", 23, true }, portTest{"tcp", "telnet", 23, true},
portTest{ "tcp", "smtp", 25, true }, portTest{"tcp", "smtp", 25, true},
portTest{ "tcp", "time", 37, true }, portTest{"tcp", "time", 37, true},
portTest{ "tcp", "domain", 53, true }, portTest{"tcp", "domain", 53, true},
portTest{ "tcp", "gopher", 70, true }, portTest{"tcp", "gopher", 70, true},
portTest{ "tcp", "finger", 79, true }, portTest{"tcp", "finger", 79, true},
portTest{ "tcp", "http", 80, true }, portTest{"tcp", "http", 80, true},
portTest{ "udp", "echo", 7, true }, portTest{"udp", "echo", 7, true},
portTest{ "udp", "tacacs", 49, true }, portTest{"udp", "tacacs", 49, true},
portTest{ "udp", "tftp", 69, true }, portTest{"udp", "tftp", 69, true},
portTest{ "udp", "bootpc", 68, true }, portTest{"udp", "bootpc", 68, true},
portTest{ "udp", "bootps", 67, true }, portTest{"udp", "bootps", 67, true},
portTest{ "udp", "domain", 53, true }, portTest{"udp", "domain", 53, true},
portTest{ "udp", "ntp", 123, true }, portTest{"udp", "ntp", 123, true},
portTest{ "udp", "snmp", 161, true }, portTest{"udp", "snmp", 161, true},
portTest{ "udp", "syslog", 514, true }, portTest{"udp", "syslog", 514, true},
portTest{ "udp", "nfs", 2049, true }, portTest{"udp", "nfs", 2049, true},
portTest{ "--badnet--", "zzz", 0, false }, portTest{"--badnet--", "zzz", 0, false},
portTest{ "tcp", "--badport--", 0, false }, portTest{"tcp", "--badport--", 0, false},
} }
func TestLookupPort(t *testing.T) { func TestLookupPort(t *testing.T) {
......
...@@ -20,9 +20,9 @@ func runEcho(fd io.ReadWriter, done chan<- int) { ...@@ -20,9 +20,9 @@ func runEcho(fd io.ReadWriter, done chan<- int) {
if err != nil || n == 0 { if err != nil || n == 0 {
break; break;
} }
fd.Write(buf[0:n]) fd.Write(buf[0:n]);
} }
done <- 1 done <- 1;
} }
func runServe(t *testing.T, network, addr string, listening chan<- string, done chan<- int) { func runServe(t *testing.T, network, addr string, listening chan<- string, done chan<- int) {
...@@ -42,13 +42,13 @@ func runServe(t *testing.T, network, addr string, listening chan<- string, done ...@@ -42,13 +42,13 @@ func runServe(t *testing.T, network, addr string, listening chan<- string, done
<-echodone; // make sure Echo stops <-echodone; // make sure Echo stops
l.Close(); l.Close();
} }
done <- 1 done <- 1;
} }
func connect(t *testing.T, network, addr string) { func connect(t *testing.T, network, addr string) {
var laddr string; var laddr string;
if network == "unixgram" { if network == "unixgram" {
laddr = addr + ".local"; laddr = addr+".local";
} }
fd, err := Dial(network, laddr, addr); fd, err := Dial(network, laddr, addr);
if err != nil { if err != nil {
...@@ -80,14 +80,14 @@ func doTest(t *testing.T, network, listenaddr, dialaddr string) { ...@@ -80,14 +80,14 @@ func doTest(t *testing.T, network, listenaddr, dialaddr string) {
go runServe(t, network, listenaddr, listening, done); go runServe(t, network, listenaddr, listening, done);
addr := <-listening; // wait for server to start addr := <-listening; // wait for server to start
if network == "tcp" { if network == "tcp" {
dialaddr += addr[strings.LastIndex(addr, ":"):len(addr)]; dialaddr += addr[strings.LastIndex(addr, ":") : len(addr)];
} }
connect(t, network, dialaddr); connect(t, network, dialaddr);
<-done; // make sure server stopped <-done; // make sure server stopped
} }
func TestTCPServer(t *testing.T) { func TestTCPServer(t *testing.T) {
doTest(t, "tcp", "0.0.0.0", "127.0.0.1"); doTest(t, "tcp", "0.0.0.0", "127.0.0.1");
doTest(t, "tcp", "[::]", "[::ffff:127.0.0.1]"); doTest(t, "tcp", "[::]", "[::ffff:127.0.0.1]");
doTest(t, "tcp", "[::]", "127.0.0.1"); doTest(t, "tcp", "[::]", "127.0.0.1");
doTest(t, "tcp", "", "127.0.0.1"); doTest(t, "tcp", "", "127.0.0.1");
...@@ -141,7 +141,7 @@ func doTestPacket(t *testing.T, network, listenaddr, dialaddr string) { ...@@ -141,7 +141,7 @@ func doTestPacket(t *testing.T, network, listenaddr, dialaddr string) {
go runPacket(t, network, listenaddr, listening, done); go runPacket(t, network, listenaddr, listening, done);
addr := <-listening; // wait for server to start addr := <-listening; // wait for server to start
if network == "udp" { if network == "udp" {
dialaddr += addr[strings.LastIndex(addr, ":"):len(addr)]; dialaddr += addr[strings.LastIndex(addr, ":") : len(addr)];
} }
connect(t, network, dialaddr); connect(t, network, dialaddr);
<-done; // tell server to stop <-done; // tell server to stop
...@@ -149,7 +149,7 @@ func doTestPacket(t *testing.T, network, listenaddr, dialaddr string) { ...@@ -149,7 +149,7 @@ func doTestPacket(t *testing.T, network, listenaddr, dialaddr string) {
} }
func TestUDPServer(t *testing.T) { func TestUDPServer(t *testing.T) {
doTestPacket(t, "udp", "0.0.0.0", "127.0.0.1"); doTestPacket(t, "udp", "0.0.0.0", "127.0.0.1");
doTestPacket(t, "udp", "[::]", "[::ffff:127.0.0.1]"); doTestPacket(t, "udp", "[::]", "[::ffff:127.0.0.1]");
doTestPacket(t, "udp", "[::]", "127.0.0.1"); doTestPacket(t, "udp", "[::]", "127.0.0.1");
doTestPacket(t, "udp", "", "127.0.0.1"); doTestPacket(t, "udp", "", "127.0.0.1");
......
...@@ -15,9 +15,9 @@ import ( ...@@ -15,9 +15,9 @@ import (
// Boolean to int. // Boolean to int.
func boolint(b bool) int { func boolint(b bool) int {
if b { if b {
return 1 return 1;
} }
return 0 return 0;
} }
// Generic socket creation. // Generic socket creation.
...@@ -27,7 +27,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal ...@@ -27,7 +27,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal
s, e := syscall.Socket(f, p, t); s, e := syscall.Socket(f, p, t);
if e != 0 { if e != 0 {
syscall.ForkLock.RUnlock(); syscall.ForkLock.RUnlock();
return nil, os.Errno(e) return nil, os.Errno(e);
} }
syscall.CloseOnExec(s); syscall.CloseOnExec(s);
syscall.ForkLock.RUnlock(); syscall.ForkLock.RUnlock();
...@@ -39,7 +39,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal ...@@ -39,7 +39,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal
e = syscall.Bind(s, la); e = syscall.Bind(s, la);
if e != 0 { if e != 0 {
syscall.Close(s); syscall.Close(s);
return nil, os.Errno(e) return nil, os.Errno(e);
} }
} }
...@@ -47,7 +47,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal ...@@ -47,7 +47,7 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal
e = syscall.Connect(s, ra); e = syscall.Connect(s, ra);
if e != 0 { if e != 0 {
syscall.Close(s); syscall.Close(s);
return nil, os.Errno(e) return nil, os.Errno(e);
} }
} }
...@@ -59,10 +59,10 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal ...@@ -59,10 +59,10 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal
fd, err = newFD(s, f, p, net, laddr, raddr); fd, err = newFD(s, f, p, net, laddr, raddr);
if err != nil { if err != nil {
syscall.Close(s); syscall.Close(s);
return nil, err return nil, err;
} }
return fd, nil return fd, nil;
} }
func setsockoptInt(fd, level, opt int, value int) os.Error { func setsockoptInt(fd, level, opt int, value int) os.Error {
...@@ -94,9 +94,9 @@ func setWriteTimeout(fd *netFD, nsec int64) os.Error { ...@@ -94,9 +94,9 @@ func setWriteTimeout(fd *netFD, nsec int64) os.Error {
func setTimeout(fd *netFD, nsec int64) os.Error { func setTimeout(fd *netFD, nsec int64) os.Error {
if e := setReadTimeout(fd, nsec); e != nil { if e := setReadTimeout(fd, nsec); e != nil {
return e return e;
} }
return setWriteTimeout(fd, nsec) return setWriteTimeout(fd, nsec);
} }
func setReuseAddr(fd *netFD, reuse bool) os.Error { func setReuseAddr(fd *netFD, reuse bool) os.Error {
...@@ -105,7 +105,7 @@ func setReuseAddr(fd *netFD, reuse bool) os.Error { ...@@ -105,7 +105,7 @@ func setReuseAddr(fd *netFD, reuse bool) os.Error {
func bindToDevice(fd *netFD, dev string) os.Error { func bindToDevice(fd *netFD, dev string) os.Error {
// TODO(rsc): call setsockopt with null-terminated string pointer // TODO(rsc): call setsockopt with null-terminated string pointer
return os.EINVAL return os.EINVAL;
} }
func setDontRoute(fd *netFD, dontroute bool) os.Error { func setDontRoute(fd *netFD, dontroute bool) os.Error {
...@@ -132,8 +132,9 @@ func setLinger(fd *netFD, sec int) os.Error { ...@@ -132,8 +132,9 @@ func setLinger(fd *netFD, sec int) os.Error {
type UnknownSocketError struct { type UnknownSocketError struct {
sa syscall.Sockaddr; sa syscall.Sockaddr;
} }
func (e *UnknownSocketError) String() string { func (e *UnknownSocketError) String() string {
return "unknown socket address type " + reflect.Typeof(e.sa).String() return "unknown socket address type " + reflect.Typeof(e.sa).String();
} }
func sockaddrToString(sa syscall.Sockaddr) (name string, err os.Error) { func sockaddrToString(sa syscall.Sockaddr) (name string, err os.Error) {
...@@ -148,4 +149,3 @@ func sockaddrToString(sa syscall.Sockaddr) (name string, err os.Error) { ...@@ -148,4 +149,3 @@ func sockaddrToString(sa syscall.Sockaddr) (name string, err os.Error) {
return "", &UnknownSocketError{sa}; return "", &UnknownSocketError{sa};
} }
...@@ -23,8 +23,8 @@ func sockaddrToTCP(sa syscall.Sockaddr) Addr { ...@@ -23,8 +23,8 @@ func sockaddrToTCP(sa syscall.Sockaddr) Addr {
// TCPAddr represents the address of a TCP end point. // TCPAddr represents the address of a TCP end point.
type TCPAddr struct { type TCPAddr struct {
IP IP; IP IP;
Port int; Port int;
} }
// Network returns the address's network name, "tcp". // Network returns the address's network name, "tcp".
...@@ -78,11 +78,10 @@ type TCPConn struct { ...@@ -78,11 +78,10 @@ type TCPConn struct {
func newTCPConn(fd *netFD) *TCPConn { func newTCPConn(fd *netFD) *TCPConn {
c := &TCPConn{fd}; c := &TCPConn{fd};
setsockoptInt(fd.fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1); setsockoptInt(fd.fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1);
return c return c;
} }
func (c *TCPConn) ok() bool { func (c *TCPConn) ok() bool {
if c == nil || c.fd == nil { panic() }
return c != nil && c.fd != nil; return c != nil && c.fd != nil;
} }
...@@ -94,7 +93,7 @@ if c == nil || c.fd == nil { panic() } ...@@ -94,7 +93,7 @@ if c == nil || c.fd == nil { panic() }
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *TCPConn) Read(b []byte) (n int, err os.Error) { func (c *TCPConn) Read(b []byte) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
return c.fd.Read(b); return c.fd.Read(b);
} }
...@@ -105,7 +104,7 @@ func (c *TCPConn) Read(b []byte) (n int, err os.Error) { ...@@ -105,7 +104,7 @@ func (c *TCPConn) Read(b []byte) (n int, err os.Error) {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *TCPConn) Write(b []byte) (n int, err os.Error) { func (c *TCPConn) Write(b []byte) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
return c.fd.Write(b); return c.fd.Write(b);
} }
...@@ -113,7 +112,7 @@ func (c *TCPConn) Write(b []byte) (n int, err os.Error) { ...@@ -113,7 +112,7 @@ func (c *TCPConn) Write(b []byte) (n int, err os.Error) {
// Close closes the TCP connection. // Close closes the TCP connection.
func (c *TCPConn) Close() os.Error { func (c *TCPConn) Close() os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
err := c.fd.Close(); err := c.fd.Close();
c.fd = nil; c.fd = nil;
...@@ -140,7 +139,7 @@ func (c *TCPConn) RemoteAddr() Addr { ...@@ -140,7 +139,7 @@ func (c *TCPConn) RemoteAddr() Addr {
// with the connection. // with the connection.
func (c *TCPConn) SetTimeout(nsec int64) os.Error { func (c *TCPConn) SetTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setTimeout(c.fd, nsec); return setTimeout(c.fd, nsec);
} }
...@@ -150,7 +149,7 @@ func (c *TCPConn) SetTimeout(nsec int64) os.Error { ...@@ -150,7 +149,7 @@ func (c *TCPConn) SetTimeout(nsec int64) os.Error {
// Setting nsec == 0 (the default) disables the deadline. // Setting nsec == 0 (the default) disables the deadline.
func (c *TCPConn) SetReadTimeout(nsec int64) os.Error { func (c *TCPConn) SetReadTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setReadTimeout(c.fd, nsec); return setReadTimeout(c.fd, nsec);
} }
...@@ -162,7 +161,7 @@ func (c *TCPConn) SetReadTimeout(nsec int64) os.Error { ...@@ -162,7 +161,7 @@ func (c *TCPConn) SetReadTimeout(nsec int64) os.Error {
// some of the data was successfully written. // some of the data was successfully written.
func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error { func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setWriteTimeout(c.fd, nsec); return setWriteTimeout(c.fd, nsec);
} }
...@@ -171,7 +170,7 @@ func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error { ...@@ -171,7 +170,7 @@ func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error {
// receive buffer associated with the connection. // receive buffer associated with the connection.
func (c *TCPConn) SetReadBuffer(bytes int) os.Error { func (c *TCPConn) SetReadBuffer(bytes int) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setReadBuffer(c.fd, bytes); return setReadBuffer(c.fd, bytes);
} }
...@@ -180,7 +179,7 @@ func (c *TCPConn) SetReadBuffer(bytes int) os.Error { ...@@ -180,7 +179,7 @@ func (c *TCPConn) SetReadBuffer(bytes int) os.Error {
// transmit buffer associated with the connection. // transmit buffer associated with the connection.
func (c *TCPConn) SetWriteBuffer(bytes int) os.Error { func (c *TCPConn) SetWriteBuffer(bytes int) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setWriteBuffer(c.fd, bytes); return setWriteBuffer(c.fd, bytes);
} }
...@@ -198,7 +197,7 @@ func (c *TCPConn) SetWriteBuffer(bytes int) os.Error { ...@@ -198,7 +197,7 @@ func (c *TCPConn) SetWriteBuffer(bytes int) os.Error {
// data to be sent and acknowledged. // data to be sent and acknowledged.
func (c *TCPConn) SetLinger(sec int) os.Error { func (c *TCPConn) SetLinger(sec int) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setLinger(c.fd, sec); return setLinger(c.fd, sec);
} }
...@@ -207,7 +206,7 @@ func (c *TCPConn) SetLinger(sec int) os.Error { ...@@ -207,7 +206,7 @@ func (c *TCPConn) SetLinger(sec int) os.Error {
// keepalive messages on the connection. // keepalive messages on the connection.
func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error { func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setKeepAlive(c.fd, keepalive); return setKeepAlive(c.fd, keepalive);
} }
...@@ -216,13 +215,13 @@ func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error { ...@@ -216,13 +215,13 @@ func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error {
// and returns a TCPConn structure. // and returns a TCPConn structure.
func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) { func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) {
if raddr == nil { if raddr == nil {
return nil, &OpError{"dial", "tcp", nil, errMissingAddress} return nil, &OpError{"dial", "tcp", nil, errMissingAddress};
} }
fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, "dial", sockaddrToTCP); fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, "dial", sockaddrToTCP);
if e != nil { if e != nil {
return nil, e return nil, e;
} }
return newTCPConn(fd), nil return newTCPConn(fd), nil;
} }
// TCPListener is a TCP network listener. // TCPListener is a TCP network listener.
...@@ -239,7 +238,7 @@ type TCPListener struct { ...@@ -239,7 +238,7 @@ type TCPListener struct {
func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) { func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) {
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, "listen", sockaddrToTCP); fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, "listen", sockaddrToTCP);
if err != nil { if err != nil {
return nil, err return nil, err;
} }
errno := syscall.Listen(fd.fd, listenBacklog()); errno := syscall.Listen(fd.fd, listenBacklog());
if errno != 0 { if errno != 0 {
...@@ -248,20 +247,20 @@ func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) { ...@@ -248,20 +247,20 @@ func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) {
} }
l = new(TCPListener); l = new(TCPListener);
l.fd = fd; l.fd = fd;
return l, nil return l, nil;
} }
// AcceptTCP accepts the next incoming call and returns the new connection // AcceptTCP accepts the next incoming call and returns the new connection
// and the remote address. // and the remote address.
func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) { func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) {
if l == nil || l.fd == nil || l.fd.fd < 0 { if l == nil || l.fd == nil || l.fd.fd < 0 {
return nil, os.EINVAL return nil, os.EINVAL;
} }
fd, err := l.fd.accept(sockaddrToTCP); fd, err := l.fd.accept(sockaddrToTCP);
if err != nil { if err != nil {
return nil, err return nil, err;
} }
return newTCPConn(fd), nil return newTCPConn(fd), nil;
} }
// Accept implements the Accept method in the Listener interface; // Accept implements the Accept method in the Listener interface;
...@@ -269,7 +268,7 @@ func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) { ...@@ -269,7 +268,7 @@ func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) {
func (l *TCPListener) Accept() (c Conn, err os.Error) { func (l *TCPListener) Accept() (c Conn, err os.Error) {
c1, err := l.AcceptTCP(); c1, err := l.AcceptTCP();
if err != nil { if err != nil {
return nil, err return nil, err;
} }
return c1, nil; return c1, nil;
} }
...@@ -278,9 +277,9 @@ func (l *TCPListener) Accept() (c Conn, err os.Error) { ...@@ -278,9 +277,9 @@ func (l *TCPListener) Accept() (c Conn, err os.Error) {
// Already Accepted connections are not closed. // Already Accepted connections are not closed.
func (l *TCPListener) Close() os.Error { func (l *TCPListener) Close() os.Error {
if l == nil || l.fd == nil { if l == nil || l.fd == nil {
return os.EINVAL return os.EINVAL;
} }
return l.fd.Close() return l.fd.Close();
} }
// Addr returns the listener's network address, a *TCPAddr. // Addr returns the listener's network address, a *TCPAddr.
......
...@@ -23,8 +23,8 @@ func testTimeout(t *testing.T, network, addr string) { ...@@ -23,8 +23,8 @@ func testTimeout(t *testing.T, network, addr string) {
if n != 0 || !isEAGAIN(err1) { if n != 0 || !isEAGAIN(err1) {
t.Errorf("fd.Read on %s %s did not return 0, EAGAIN: %v, %v", network, addr, n, err1); t.Errorf("fd.Read on %s %s did not return 0, EAGAIN: %v, %v", network, addr, n, err1);
} }
if t1 - t0 < 0.5e8 || t1 - t0 > 1.5e8 { if t1-t0 < 0.5e8 || t1-t0 > 1.5e8 {
t.Errorf("fd.Read on %s %s took %f seconds, expected 0.1", network, addr, float64(t1 - t0) / 1e9); t.Errorf("fd.Read on %s %s took %f seconds, expected 0.1", network, addr, float64(t1-t0)/1e9);
} }
} }
......
...@@ -23,8 +23,8 @@ func sockaddrToUDP(sa syscall.Sockaddr) Addr { ...@@ -23,8 +23,8 @@ func sockaddrToUDP(sa syscall.Sockaddr) Addr {
// UDPAddr represents the address of a UDP end point. // UDPAddr represents the address of a UDP end point.
type UDPAddr struct { type UDPAddr struct {
IP IP; IP IP;
Port int; Port int;
} }
// Network returns the address's network name, "udp". // Network returns the address's network name, "udp".
...@@ -94,7 +94,7 @@ func (c *UDPConn) ok() bool { ...@@ -94,7 +94,7 @@ func (c *UDPConn) ok() bool {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UDPConn) Read(b []byte) (n int, err os.Error) { func (c *UDPConn) Read(b []byte) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
return c.fd.Read(b); return c.fd.Read(b);
} }
...@@ -105,7 +105,7 @@ func (c *UDPConn) Read(b []byte) (n int, err os.Error) { ...@@ -105,7 +105,7 @@ func (c *UDPConn) Read(b []byte) (n int, err os.Error) {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UDPConn) Write(b []byte) (n int, err os.Error) { func (c *UDPConn) Write(b []byte) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
return c.fd.Write(b); return c.fd.Write(b);
} }
...@@ -113,7 +113,7 @@ func (c *UDPConn) Write(b []byte) (n int, err os.Error) { ...@@ -113,7 +113,7 @@ func (c *UDPConn) Write(b []byte) (n int, err os.Error) {
// Close closes the UDP connection. // Close closes the UDP connection.
func (c *UDPConn) Close() os.Error { func (c *UDPConn) Close() os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
err := c.fd.Close(); err := c.fd.Close();
c.fd = nil; c.fd = nil;
...@@ -140,7 +140,7 @@ func (c *UDPConn) RemoteAddr() Addr { ...@@ -140,7 +140,7 @@ func (c *UDPConn) RemoteAddr() Addr {
// with the connection. // with the connection.
func (c *UDPConn) SetTimeout(nsec int64) os.Error { func (c *UDPConn) SetTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setTimeout(c.fd, nsec); return setTimeout(c.fd, nsec);
} }
...@@ -150,7 +150,7 @@ func (c *UDPConn) SetTimeout(nsec int64) os.Error { ...@@ -150,7 +150,7 @@ func (c *UDPConn) SetTimeout(nsec int64) os.Error {
// Setting nsec == 0 (the default) disables the deadline. // Setting nsec == 0 (the default) disables the deadline.
func (c *UDPConn) SetReadTimeout(nsec int64) os.Error { func (c *UDPConn) SetReadTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setReadTimeout(c.fd, nsec); return setReadTimeout(c.fd, nsec);
} }
...@@ -162,7 +162,7 @@ func (c *UDPConn) SetReadTimeout(nsec int64) os.Error { ...@@ -162,7 +162,7 @@ func (c *UDPConn) SetReadTimeout(nsec int64) os.Error {
// some of the data was successfully written. // some of the data was successfully written.
func (c *UDPConn) SetWriteTimeout(nsec int64) os.Error { func (c *UDPConn) SetWriteTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setWriteTimeout(c.fd, nsec); return setWriteTimeout(c.fd, nsec);
} }
...@@ -171,7 +171,7 @@ func (c *UDPConn) SetWriteTimeout(nsec int64) os.Error { ...@@ -171,7 +171,7 @@ func (c *UDPConn) SetWriteTimeout(nsec int64) os.Error {
// receive buffer associated with the connection. // receive buffer associated with the connection.
func (c *UDPConn) SetReadBuffer(bytes int) os.Error { func (c *UDPConn) SetReadBuffer(bytes int) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setReadBuffer(c.fd, bytes); return setReadBuffer(c.fd, bytes);
} }
...@@ -180,7 +180,7 @@ func (c *UDPConn) SetReadBuffer(bytes int) os.Error { ...@@ -180,7 +180,7 @@ func (c *UDPConn) SetReadBuffer(bytes int) os.Error {
// transmit buffer associated with the connection. // transmit buffer associated with the connection.
func (c *UDPConn) SetWriteBuffer(bytes int) os.Error { func (c *UDPConn) SetWriteBuffer(bytes int) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setWriteBuffer(c.fd, bytes); return setWriteBuffer(c.fd, bytes);
} }
...@@ -195,7 +195,7 @@ func (c *UDPConn) SetWriteBuffer(bytes int) os.Error { ...@@ -195,7 +195,7 @@ func (c *UDPConn) SetWriteBuffer(bytes int) os.Error {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error) { func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, nil, os.EINVAL return 0, nil, os.EINVAL;
} }
n, sa, errno := syscall.Recvfrom(c.fd.fd, b, 0); n, sa, errno := syscall.Recvfrom(c.fd.fd, b, 0);
if errno != 0 { if errno != 0 {
...@@ -218,7 +218,7 @@ func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error) { ...@@ -218,7 +218,7 @@ func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error) {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) { func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, nil, os.EINVAL return 0, nil, os.EINVAL;
} }
n, uaddr, err := c.ReadFromUDP(b); n, uaddr, err := c.ReadFromUDP(b);
return n, uaddr.toAddr(), err; return n, uaddr.toAddr(), err;
...@@ -231,7 +231,7 @@ func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) { ...@@ -231,7 +231,7 @@ func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) {
// On packet-oriented connections such as UDP, write timeouts are rare. // On packet-oriented connections such as UDP, write timeouts are rare.
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error) { func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
sa, err := addr.sockaddr(c.fd.family); sa, err := addr.sockaddr(c.fd.family);
if err != nil { if err != nil {
...@@ -250,7 +250,7 @@ func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error) { ...@@ -250,7 +250,7 @@ func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error) {
// On packet-oriented connections such as UDP, write timeouts are rare. // On packet-oriented connections such as UDP, write timeouts are rare.
func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) { func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
a, ok := addr.(*UDPAddr); a, ok := addr.(*UDPAddr);
if !ok { if !ok {
...@@ -266,16 +266,16 @@ func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err os.Error) { ...@@ -266,16 +266,16 @@ func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err os.Error) {
switch net { switch net {
case "udp", "udp4", "udp6": case "udp", "udp4", "udp6":
default: default:
return nil, UnknownNetworkError(net) return nil, UnknownNetworkError(net);
} }
if raddr == nil { if raddr == nil {
return nil, &OpError{"dial", "udp", nil, errMissingAddress} return nil, &OpError{"dial", "udp", nil, errMissingAddress};
} }
fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_DGRAM, "dial", sockaddrToUDP); fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_DGRAM, "dial", sockaddrToUDP);
if e != nil { if e != nil {
return nil, e return nil, e;
} }
return newUDPConn(fd), nil return newUDPConn(fd), nil;
} }
// ListenUDP listens for incoming UDP packets addressed to the // ListenUDP listens for incoming UDP packets addressed to the
...@@ -286,14 +286,14 @@ func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err os.Error) { ...@@ -286,14 +286,14 @@ func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err os.Error) {
switch net { switch net {
case "udp", "udp4", "udp6": case "udp", "udp4", "udp6":
default: default:
return nil, UnknownNetworkError(net) return nil, UnknownNetworkError(net);
} }
if laddr == nil { if laddr == nil {
return nil, &OpError{"listen", "udp", nil, errMissingAddress} return nil, &OpError{"listen", "udp", nil, errMissingAddress};
} }
fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, "dial", sockaddrToUDP); fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, "dial", sockaddrToUDP);
if e != nil { if e != nil {
return nil, e return nil, e;
} }
return newUDPConn(fd), nil return newUDPConn(fd), nil;
} }
...@@ -34,16 +34,16 @@ func unixSocket(net string, laddr, raddr *UnixAddr, mode string) (fd *netFD, err ...@@ -34,16 +34,16 @@ func unixSocket(net string, laddr, raddr *UnixAddr, mode string) (fd *netFD, err
if raddr != nil { if raddr != nil {
ra = &syscall.SockaddrUnix{Name: raddr.Name}; ra = &syscall.SockaddrUnix{Name: raddr.Name};
} else if proto != syscall.SOCK_DGRAM || laddr == nil { } else if proto != syscall.SOCK_DGRAM || laddr == nil {
return nil, &OpError{mode, net, nil, errMissingAddress} return nil, &OpError{mode, net, nil, errMissingAddress};
} }
case "listen": case "listen":
if laddr == nil { if laddr == nil {
return nil, &OpError{mode, net, nil, errMissingAddress} return nil, &OpError{mode, net, nil, errMissingAddress};
} }
la = &syscall.SockaddrUnix{Name: laddr.Name}; la = &syscall.SockaddrUnix{Name: laddr.Name};
if raddr != nil { if raddr != nil {
return nil, &OpError{mode, net, raddr, &AddrError{"unexpected remote address", raddr.String()}} return nil, &OpError{mode, net, raddr, &AddrError{"unexpected remote address", raddr.String()}};
} }
} }
...@@ -67,8 +67,8 @@ Error: ...@@ -67,8 +67,8 @@ Error:
// UnixAddr represents the address of a Unix domain socket end point. // UnixAddr represents the address of a Unix domain socket end point.
type UnixAddr struct { type UnixAddr struct {
Name string; Name string;
Datagram bool; Datagram bool;
} }
func sockaddrToUnix(sa syscall.Sockaddr) Addr { func sockaddrToUnix(sa syscall.Sockaddr) Addr {
...@@ -95,7 +95,7 @@ func (a *UnixAddr) Network() string { ...@@ -95,7 +95,7 @@ func (a *UnixAddr) Network() string {
func (a *UnixAddr) String() string { func (a *UnixAddr) String() string {
if a == nil { if a == nil {
return "<nil>" return "<nil>";
} }
return a.Name; return a.Name;
} }
...@@ -128,7 +128,7 @@ type UnixConn struct { ...@@ -128,7 +128,7 @@ type UnixConn struct {
} }
func newUnixConn(fd *netFD) *UnixConn { func newUnixConn(fd *netFD) *UnixConn {
return &UnixConn{fd} return &UnixConn{fd};
} }
func (c *UnixConn) ok() bool { func (c *UnixConn) ok() bool {
...@@ -143,7 +143,7 @@ func (c *UnixConn) ok() bool { ...@@ -143,7 +143,7 @@ func (c *UnixConn) ok() bool {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UnixConn) Read(b []byte) (n int, err os.Error) { func (c *UnixConn) Read(b []byte) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
return c.fd.Read(b); return c.fd.Read(b);
} }
...@@ -154,7 +154,7 @@ func (c *UnixConn) Read(b []byte) (n int, err os.Error) { ...@@ -154,7 +154,7 @@ func (c *UnixConn) Read(b []byte) (n int, err os.Error) {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UnixConn) Write(b []byte) (n int, err os.Error) { func (c *UnixConn) Write(b []byte) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
return c.fd.Write(b); return c.fd.Write(b);
} }
...@@ -162,7 +162,7 @@ func (c *UnixConn) Write(b []byte) (n int, err os.Error) { ...@@ -162,7 +162,7 @@ func (c *UnixConn) Write(b []byte) (n int, err os.Error) {
// Close closes the Unix domain connection. // Close closes the Unix domain connection.
func (c *UnixConn) Close() os.Error { func (c *UnixConn) Close() os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
err := c.fd.Close(); err := c.fd.Close();
c.fd = nil; c.fd = nil;
...@@ -192,7 +192,7 @@ func (c *UnixConn) RemoteAddr() Addr { ...@@ -192,7 +192,7 @@ func (c *UnixConn) RemoteAddr() Addr {
// with the connection. // with the connection.
func (c *UnixConn) SetTimeout(nsec int64) os.Error { func (c *UnixConn) SetTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setTimeout(c.fd, nsec); return setTimeout(c.fd, nsec);
} }
...@@ -202,7 +202,7 @@ func (c *UnixConn) SetTimeout(nsec int64) os.Error { ...@@ -202,7 +202,7 @@ func (c *UnixConn) SetTimeout(nsec int64) os.Error {
// Setting nsec == 0 (the default) disables the deadline. // Setting nsec == 0 (the default) disables the deadline.
func (c *UnixConn) SetReadTimeout(nsec int64) os.Error { func (c *UnixConn) SetReadTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setReadTimeout(c.fd, nsec); return setReadTimeout(c.fd, nsec);
} }
...@@ -214,7 +214,7 @@ func (c *UnixConn) SetReadTimeout(nsec int64) os.Error { ...@@ -214,7 +214,7 @@ func (c *UnixConn) SetReadTimeout(nsec int64) os.Error {
// some of the data was successfully written. // some of the data was successfully written.
func (c *UnixConn) SetWriteTimeout(nsec int64) os.Error { func (c *UnixConn) SetWriteTimeout(nsec int64) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setWriteTimeout(c.fd, nsec); return setWriteTimeout(c.fd, nsec);
} }
...@@ -223,7 +223,7 @@ func (c *UnixConn) SetWriteTimeout(nsec int64) os.Error { ...@@ -223,7 +223,7 @@ func (c *UnixConn) SetWriteTimeout(nsec int64) os.Error {
// receive buffer associated with the connection. // receive buffer associated with the connection.
func (c *UnixConn) SetReadBuffer(bytes int) os.Error { func (c *UnixConn) SetReadBuffer(bytes int) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setReadBuffer(c.fd, bytes); return setReadBuffer(c.fd, bytes);
} }
...@@ -232,7 +232,7 @@ func (c *UnixConn) SetReadBuffer(bytes int) os.Error { ...@@ -232,7 +232,7 @@ func (c *UnixConn) SetReadBuffer(bytes int) os.Error {
// transmit buffer associated with the connection. // transmit buffer associated with the connection.
func (c *UnixConn) SetWriteBuffer(bytes int) os.Error { func (c *UnixConn) SetWriteBuffer(bytes int) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL;
} }
return setWriteBuffer(c.fd, bytes); return setWriteBuffer(c.fd, bytes);
} }
...@@ -245,7 +245,7 @@ func (c *UnixConn) SetWriteBuffer(bytes int) os.Error { ...@@ -245,7 +245,7 @@ func (c *UnixConn) SetWriteBuffer(bytes int) os.Error {
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err os.Error) { func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, nil, os.EINVAL return 0, nil, os.EINVAL;
} }
n, sa, errno := syscall.Recvfrom(c.fd.fd, b, 0); n, sa, errno := syscall.Recvfrom(c.fd.fd, b, 0);
if errno != 0 { if errno != 0 {
...@@ -266,7 +266,7 @@ func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err os.Error) ...@@ -266,7 +266,7 @@ func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err os.Error)
// after a fixed time limit; see SetTimeout and SetReadTimeout. // after a fixed time limit; see SetTimeout and SetReadTimeout.
func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) { func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, nil, os.EINVAL return 0, nil, os.EINVAL;
} }
n, uaddr, err := c.ReadFromUnix(b); n, uaddr, err := c.ReadFromUnix(b);
return n, uaddr.toAddr(), err; return n, uaddr.toAddr(), err;
...@@ -279,7 +279,7 @@ func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) { ...@@ -279,7 +279,7 @@ func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) {
// On packet-oriented connections such as UDP, write timeouts are rare. // On packet-oriented connections such as UDP, write timeouts are rare.
func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err os.Error) { func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
if addr.Datagram != (c.fd.proto == syscall.SOCK_DGRAM) { if addr.Datagram != (c.fd.proto == syscall.SOCK_DGRAM) {
return 0, os.EAFNOSUPPORT; return 0, os.EAFNOSUPPORT;
...@@ -298,7 +298,7 @@ func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err os.Error) { ...@@ -298,7 +298,7 @@ func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err os.Error) {
// On packet-oriented connections such as UDP, write timeouts are rare. // On packet-oriented connections such as UDP, write timeouts are rare.
func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) { func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) {
if !c.ok() { if !c.ok() {
return 0, os.EINVAL return 0, os.EINVAL;
} }
a, ok := addr.(*UnixAddr); a, ok := addr.(*UnixAddr);
if !ok { if !ok {
...@@ -313,7 +313,7 @@ func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) { ...@@ -313,7 +313,7 @@ func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) {
func DialUnix(net string, laddr, raddr *UnixAddr) (c *UnixConn, err os.Error) { func DialUnix(net string, laddr, raddr *UnixAddr) (c *UnixConn, err os.Error) {
fd, e := unixSocket(net, laddr, raddr, "dial"); fd, e := unixSocket(net, laddr, raddr, "dial");
if e != nil { if e != nil {
return nil, e return nil, e;
} }
return newUnixConn(fd), nil; return newUnixConn(fd), nil;
} }
...@@ -322,8 +322,8 @@ func DialUnix(net string, laddr, raddr *UnixAddr) (c *UnixConn, err os.Error) { ...@@ -322,8 +322,8 @@ func DialUnix(net string, laddr, raddr *UnixAddr) (c *UnixConn, err os.Error) {
// Clients should typically use variables of type Listener // Clients should typically use variables of type Listener
// instead of assuming Unix domain sockets. // instead of assuming Unix domain sockets.
type UnixListener struct { type UnixListener struct {
fd *netFD; fd *netFD;
path string; path string;
} }
// ListenUnix announces on the Unix domain socket laddr and returns a Unix listener. // ListenUnix announces on the Unix domain socket laddr and returns a Unix listener.
...@@ -342,7 +342,7 @@ func ListenUnix(net string, laddr *UnixAddr) (l *UnixListener, err os.Error) { ...@@ -342,7 +342,7 @@ func ListenUnix(net string, laddr *UnixAddr) (l *UnixListener, err os.Error) {
} }
return nil, e; return nil, e;
} }
e1 := syscall.Listen(fd.fd, 8); // listenBacklog()); e1 := syscall.Listen(fd.fd, 8); // listenBacklog());
if e1 != 0 { if e1 != 0 {
syscall.Close(fd.fd); syscall.Close(fd.fd);
return nil, &OpError{"listen", "unix", laddr, os.Errno(e1)}; return nil, &OpError{"listen", "unix", laddr, os.Errno(e1)};
...@@ -354,14 +354,14 @@ func ListenUnix(net string, laddr *UnixAddr) (l *UnixListener, err os.Error) { ...@@ -354,14 +354,14 @@ func ListenUnix(net string, laddr *UnixAddr) (l *UnixListener, err os.Error) {
// and the remote address. // and the remote address.
func (l *UnixListener) AcceptUnix() (c *UnixConn, err os.Error) { func (l *UnixListener) AcceptUnix() (c *UnixConn, err os.Error) {
if l == nil || l.fd == nil || l.fd.fd < 0 { if l == nil || l.fd == nil || l.fd.fd < 0 {
return nil, os.EINVAL return nil, os.EINVAL;
} }
fd, e := l.fd.accept(sockaddrToUnix); fd, e := l.fd.accept(sockaddrToUnix);
if e != nil { if e != nil {
return nil, e return nil, e;
} }
c = newUnixConn(fd); c = newUnixConn(fd);
return c, nil return c, nil;
} }
// Accept implements the Accept method in the Listener interface; // Accept implements the Accept method in the Listener interface;
...@@ -378,7 +378,7 @@ func (l *UnixListener) Accept() (c Conn, err os.Error) { ...@@ -378,7 +378,7 @@ func (l *UnixListener) Accept() (c Conn, err os.Error) {
// Already accepted connections are not closed. // Already accepted connections are not closed.
func (l *UnixListener) Close() os.Error { func (l *UnixListener) Close() os.Error {
if l == nil || l.fd == nil { if l == nil || l.fd == nil {
return os.EINVAL return os.EINVAL;
} }
// The operating system doesn't clean up // The operating system doesn't clean up
...@@ -412,14 +412,14 @@ func ListenUnixgram(net string, laddr *UnixAddr) (c *UDPConn, err os.Error) { ...@@ -412,14 +412,14 @@ func ListenUnixgram(net string, laddr *UnixAddr) (c *UDPConn, err os.Error) {
switch net { switch net {
case "unixgram": case "unixgram":
default: default:
return nil, UnknownNetworkError(net) return nil, UnknownNetworkError(net);
} }
if laddr == nil { if laddr == nil {
return nil, &OpError{"listen", "unixgram", nil, errMissingAddress} return nil, &OpError{"listen", "unixgram", nil, errMissingAddress};
} }
fd, e := unixSocket(net, laddr, nil, "listen"); fd, e := unixSocket(net, laddr, nil, "listen");
if e != nil { if e != nil {
return nil, e return nil, e;
} }
return newUDPConn(fd), nil return newUDPConn(fd), nil;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment