Commit d8921c52 authored by Russ Cox's avatar Russ Cox

cleanups:

	get rid of _ on private names in net.
	fix os_test file name list.
	newline not needed on Errorf.

R=r
DELTA=305  (34 added, 2 deleted, 269 changed)
OCL=25047
CL=25047
parent 78a6d68c
...@@ -84,7 +84,7 @@ func _Exchange(cfg *DNS_Config, c Conn, name string) (m *DNS_Msg, err *os.Error) ...@@ -84,7 +84,7 @@ func _Exchange(cfg *DNS_Config, c Conn, name string) (m *DNS_Msg, err *os.Error)
// Find answer for name in dns message. // Find answer for name in dns message.
// On return, if err == nil, addrs != nil. // On return, if err == nil, addrs != nil.
// TODO(rsc): Maybe return [][]byte (==[]IPAddr) instead? // TODO(rsc): Maybe return [][]byte (==[]IPAddr) instead?
func _Answer(name string, dns *DNS_Msg) (addrs []string, err *os.Error) { func answer(name string, dns *DNS_Msg) (addrs []string, err *os.Error) {
addrs = make([]string, 0, len(dns.answer)); addrs = make([]string, 0, len(dns.answer));
if dns.rcode == DNS_RcodeNameError && dns.authoritative { if dns.rcode == DNS_RcodeNameError && dns.authoritative {
...@@ -134,8 +134,8 @@ Cname: ...@@ -134,8 +134,8 @@ Cname:
} }
// 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) {
err = DNS_NoServers; err = DNS_NoServers;
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
...@@ -155,7 +155,7 @@ func _TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) { ...@@ -155,7 +155,7 @@ func _TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) {
err = merr; err = merr;
continue; continue;
} }
addrs, aerr := _Answer(name, msg); addrs, aerr := answer(name, msg);
if aerr != nil && aerr != DNS_NameNotFound { if aerr != nil && aerr != DNS_NameNotFound {
err = aerr; err = aerr;
continue; continue;
...@@ -167,7 +167,7 @@ func _TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) { ...@@ -167,7 +167,7 @@ func _TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) {
var cfg *DNS_Config var cfg *DNS_Config
func _LoadConfig() { func loadConfig() {
cfg = DNS_ReadConfig(); cfg = DNS_ReadConfig();
} }
...@@ -175,7 +175,7 @@ func LookupHost(name string) (name1 string, addrs []string, err *os.Error) { ...@@ -175,7 +175,7 @@ func LookupHost(name string) (name1 string, addrs []string, err *os.Error) {
// TODO(rsc): Pick out obvious non-DNS names to avoid // TODO(rsc): Pick out obvious non-DNS names to avoid
// sending stupid requests to the server? // sending stupid requests to the server?
once.Do(_LoadConfig); once.Do(loadConfig);
if cfg == nil { if cfg == nil {
err = DNS_MissingConfig; err = DNS_MissingConfig;
return; return;
...@@ -190,7 +190,7 @@ func LookupHost(name string) (name1 string, addrs []string, err *os.Error) { ...@@ -190,7 +190,7 @@ func LookupHost(name string) (name1 string, addrs []string, err *os.Error) {
rname += "."; rname += ".";
} }
// Can try as ordinary name. // Can try as ordinary name.
addrs, aerr := _TryOneName(cfg, rname); addrs, aerr := tryOneName(cfg, rname);
if aerr == nil { if aerr == nil {
return rname, addrs, nil; return rname, addrs, nil;
} }
...@@ -206,7 +206,7 @@ func LookupHost(name string) (name1 string, addrs []string, err *os.Error) { ...@@ -206,7 +206,7 @@ func LookupHost(name string) (name1 string, addrs []string, err *os.Error) {
if newname[len(newname)-1] != '.' { if newname[len(newname)-1] != '.' {
newname += "." newname += "."
} }
addrs, aerr := _TryOneName(cfg, newname); addrs, aerr := tryOneName(cfg, newname);
if aerr == nil { if aerr == nil {
return newname, addrs, nil; return newname, addrs, nil;
} }
......
...@@ -27,7 +27,8 @@ type DNS_Config struct { ...@@ -27,7 +27,8 @@ type DNS_Config struct {
// of the host name to get the default search domain. // of the host name to get the default search domain.
// We assume it's in resolv.conf anyway. // We assume it's in resolv.conf anyway.
func DNS_ReadConfig() *DNS_Config { func DNS_ReadConfig() *DNS_Config {
file := _Open("/etc/resolv.conf"); // TODO(rsc): 6g won't let me use "file :="
var file = open("/etc/resolv.conf");
if file == nil { if file == nil {
return nil return nil
} }
...@@ -39,8 +40,8 @@ func DNS_ReadConfig() *DNS_Config { ...@@ -39,8 +40,8 @@ func DNS_ReadConfig() *DNS_Config {
conf.attempts = 1; conf.attempts = 1;
conf.rotate = false; conf.rotate = false;
var err *os.Error; var err *os.Error;
for line, ok := file.ReadLine(); ok; line, ok = file.ReadLine() { for line, ok := file.readLine(); ok; line, ok = file.readLine() {
f := _GetFields(line); f := getFields(line);
if len(f) < 1 { if len(f) < 1 {
continue; continue;
} }
...@@ -79,19 +80,19 @@ func DNS_ReadConfig() *DNS_Config { ...@@ -79,19 +80,19 @@ func DNS_ReadConfig() *DNS_Config {
s := f[i]; s := f[i];
switch { switch {
case len(s) >= 6 && s[0:6] == "ndots:": case len(s) >= 6 && s[0:6] == "ndots:":
n, i, ok := _Dtoi(s, 6); n, i, ok := 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, i, ok := _Dtoi(s, 8); n, i, ok := 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, i, ok := _Dtoi(s, 9); n, i, ok := dtoi(s, 9);
if n < 1 { if n < 1 {
n = 1 n = 1
} }
...@@ -102,7 +103,7 @@ func DNS_ReadConfig() *DNS_Config { ...@@ -102,7 +103,7 @@ func DNS_ReadConfig() *DNS_Config {
} }
} }
} }
file.Close(); file.close();
return conf return conf
} }
......
This diff is collapsed.
...@@ -13,21 +13,23 @@ import ( ...@@ -13,21 +13,23 @@ import (
"syscall"; "syscall";
) )
// Network file descriptor. Only intended to be used internally, // Network file descriptor.
// but have to export to make it available in other files implementing package net. type netFD struct {
type FD struct {
// immutable until Close // immutable until Close
fd int64; fd int64;
osfd *os.FD; osfd *os.FD;
cr chan *FD; cr chan *netFD;
cw chan *FD; cw chan *netFD;
net string;
laddr string;
raddr string;
// owned by fd wait server // owned by fd wait server
ncr, ncw int; ncr, ncw int;
} }
// Make reads and writes on fd return EAGAIN instead of blocking. // Make reads and writes on fd return EAGAIN instead of blocking.
func _SetNonblock(fd int64) *os.Error { func setNonblock(fd int64) *os.Error {
flags, e := syscall.Fcntl(fd, syscall.F_GETFL, 0); flags, e := syscall.Fcntl(fd, syscall.F_GETFL, 0);
if e != 0 { if e != 0 {
return os.ErrnoToError(e) return os.ErrnoToError(e)
...@@ -40,11 +42,11 @@ func _SetNonblock(fd int64) *os.Error { ...@@ -40,11 +42,11 @@ func _SetNonblock(fd int64) *os.Error {
} }
// A _PollServer helps FDs determine when to retry a non-blocking // A pollServer helps FDs determine when to retry a non-blocking
// read or write after they get EAGAIN. When an FD needs to wait, // read or write after they get EAGAIN. When an FD needs to wait,
// send the fd on s.cr (for a read) or s.cw (for a write) to pass the // send the fd on s.cr (for a read) or s.cw (for a write) to pass the
// request to the poll server. Then receive on fd.cr/fd.cw. // request to the poll server. Then receive on fd.cr/fd.cw.
// When the _PollServer finds that i/o on FD should be possible // When the pollServer finds that i/o on FD should be possible
// again, it will send fd on fd.cr/fd.cw to wake any waiting processes. // again, it will send fd on fd.cr/fd.cw to wake any waiting processes.
// This protocol is implemented as s.WaitRead() and s.WaitWrite(). // This protocol is implemented as s.WaitRead() and s.WaitWrite().
// //
...@@ -54,8 +56,8 @@ func _SetNonblock(fd int64) *os.Error { ...@@ -54,8 +56,8 @@ func _SetNonblock(fd int64) *os.Error {
// To resolve this, the poll server waits not just on the FDs it has // To resolve this, the poll server waits not just on the FDs it has
// been given but also its own pipe. After sending on the // been given but also its own pipe. After sending on the
// buffered channel s.cr/s.cw, WaitRead/WaitWrite writes a // buffered channel s.cr/s.cw, WaitRead/WaitWrite writes a
// byte to the pipe, causing the _PollServer's poll system call to // byte to the pipe, causing the pollServer's poll system call to
// return. In response to the pipe being readable, the _PollServer // return. In response to the pipe being readable, the pollServer
// re-polls its request channels. // re-polls its request channels.
// //
// Note that the ordering is "send request" and then "wake up server". // Note that the ordering is "send request" and then "wake up server".
...@@ -65,32 +67,32 @@ func _SetNonblock(fd int64) *os.Error { ...@@ -65,32 +67,32 @@ func _SetNonblock(fd int64) *os.Error {
// to send the request. Because the send must complete before the wakeup, // to send the request. Because the send must complete before the wakeup,
// the request channel must be buffered. A buffer of size 1 is sufficient // the request channel must be buffered. A buffer of size 1 is sufficient
// for any request load. If many processes are trying to submit requests, // for any request load. If many processes are trying to submit requests,
// one will succeed, the _PollServer will read the request, and then the // one will succeed, the pollServer will read the request, and then the
// channel will be empty for the next process's request. A larger buffer // channel will be empty for the next process's request. A larger buffer
// might help batch requests. // might help batch requests.
type _PollServer struct { type pollServer struct {
cr, cw chan *FD; // buffered >= 1 cr, cw chan *netFD; // buffered >= 1
pr, pw *os.FD; pr, pw *os.FD;
pending map[int64] *FD; pending map[int64] *netFD;
poll *Pollster; // low-level OS hooks poll *Pollster; // low-level OS hooks
} }
func (s *_PollServer) Run(); func (s *pollServer) Run();
func _NewPollServer() (s *_PollServer, err *os.Error) { func newPollServer() (s *pollServer, err *os.Error) {
s = new(_PollServer); s = new(pollServer);
s.cr = make(chan *FD, 1); s.cr = make(chan *netFD, 1);
s.cw = make(chan *FD, 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
} }
if err = _SetNonblock(s.pr.Fd()); err != nil { if err = setNonblock(s.pr.Fd()); err != nil {
Error: Error:
s.pr.Close(); s.pr.Close();
s.pw.Close(); s.pw.Close();
return nil, err return nil, err
} }
if err = _SetNonblock(s.pw.Fd()); err != nil { if err = setNonblock(s.pw.Fd()); err != nil {
goto Error goto Error
} }
if s.poll, err = NewPollster(); err != nil { if s.poll, err = NewPollster(); err != nil {
...@@ -100,14 +102,14 @@ func _NewPollServer() (s *_PollServer, err *os.Error) { ...@@ -100,14 +102,14 @@ func _NewPollServer() (s *_PollServer, err *os.Error) {
s.poll.Close(); s.poll.Close();
goto Error goto Error
} }
s.pending = make(map[int64] *FD); s.pending = make(map[int64] *netFD);
go s.Run(); go s.Run();
return s, nil return s, nil
} }
func (s *_PollServer) AddFD(fd *FD, mode int) { func (s *pollServer) AddFD(fd *netFD, mode int) {
if err := s.poll.AddFD(fd.fd, mode, false); err != nil { if err := s.poll.AddFD(fd.fd, mode, false); err != nil {
print("_PollServer AddFD: ", err.String(), "\n"); print("pollServer AddFD: ", err.String(), "\n");
return return
} }
...@@ -121,7 +123,7 @@ func (s *_PollServer) AddFD(fd *FD, mode int) { ...@@ -121,7 +123,7 @@ func (s *_PollServer) AddFD(fd *FD, mode int) {
s.pending[key] = fd s.pending[key] = fd
} }
func (s *_PollServer) LookupFD(fd int64, mode int) *FD { func (s *pollServer) LookupFD(fd int64, mode int) *netFD {
key := fd << 1; key := fd << 1;
if mode == 'w' { if mode == 'w' {
key++; key++;
...@@ -134,12 +136,12 @@ func (s *_PollServer) LookupFD(fd int64, mode int) *FD { ...@@ -134,12 +136,12 @@ func (s *_PollServer) LookupFD(fd int64, mode int) *FD {
return netfd return netfd
} }
func (s *_PollServer) Run() { func (s *pollServer) Run() {
var scratch [100]byte; var scratch [100]byte;
for { for {
fd, mode, err := s.poll.WaitFD(); fd, mode, err := s.poll.WaitFD();
if err != nil { if err != nil {
print("_PollServer WaitFD: ", err.String(), "\n"); print("pollServer WaitFD: ", err.String(), "\n");
return return
} }
if fd == s.pr.Fd() { if fd == s.pr.Fd() {
...@@ -158,7 +160,7 @@ func (s *_PollServer) Run() { ...@@ -158,7 +160,7 @@ func (s *_PollServer) Run() {
} 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
} }
if mode == 'r' { if mode == 'r' {
...@@ -176,18 +178,18 @@ func (s *_PollServer) Run() { ...@@ -176,18 +178,18 @@ func (s *_PollServer) Run() {
} }
} }
func (s *_PollServer) Wakeup() { func (s *pollServer) Wakeup() {
var b [1]byte; var b [1]byte;
s.pw.Write(b) s.pw.Write(b)
} }
func (s *_PollServer) WaitRead(fd *FD) { 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 *FD) { func (s *pollServer) WaitWrite(fd *netFD) {
s.cr <- fd; s.cr <- fd;
s.Wakeup(); s.Wakeup();
<-fd.cr <-fd.cr
...@@ -195,34 +197,37 @@ func (s *_PollServer) WaitWrite(fd *FD) { ...@@ -195,34 +197,37 @@ func (s *_PollServer) WaitWrite(fd *FD) {
// Network FD methods. // Network FD methods.
// All the network FDs use a single _PollServer. // All the network FDs use a single pollServer.
var pollserver *_PollServer 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 int64) (f *FD, err *os.Error) { func newFD(fd int64, net, laddr, raddr string) (f *netFD, err *os.Error) {
if pollserver == nil { if pollserver == nil {
once.Do(_StartServer); once.Do(_StartServer);
} }
if err = _SetNonblock(fd); err != nil { if err = setNonblock(fd); err != nil {
return nil, err return nil, err
} }
f = new(FD); f = new(netFD);
f.fd = fd; f.fd = fd;
f.osfd = os.NewFD(fd, "socket"); f.net = net;
f.cr = make(chan *FD, 1); f.laddr = laddr;
f.cw = make(chan *FD, 1); f.raddr = raddr;
f.osfd = os.NewFD(fd, "net: " + net + " " + laddr + " " + raddr);
f.cr = make(chan *netFD, 1);
f.cw = make(chan *netFD, 1);
return f, nil return f, nil
} }
func (fd *FD) Close() *os.Error { func (fd *netFD) Close() *os.Error {
if fd == nil || fd.osfd == nil { if fd == nil || fd.osfd == nil {
return os.EINVAL return os.EINVAL
} }
...@@ -232,7 +237,7 @@ func (fd *FD) Close() *os.Error { ...@@ -232,7 +237,7 @@ func (fd *FD) Close() *os.Error {
return e return e
} }
func (fd *FD) Read(p []byte) (n int, err *os.Error) { func (fd *netFD) Read(p []byte) (n int, err *os.Error) {
if fd == nil || fd.osfd == nil { if fd == nil || fd.osfd == nil {
return -1, os.EINVAL return -1, os.EINVAL
} }
...@@ -244,7 +249,7 @@ func (fd *FD) Read(p []byte) (n int, err *os.Error) { ...@@ -244,7 +249,7 @@ func (fd *FD) Read(p []byte) (n int, err *os.Error) {
return n, err return n, err
} }
func (fd *FD) Write(p []byte) (n int, err *os.Error) { func (fd *netFD) Write(p []byte) (n int, err *os.Error) {
if fd == nil || fd.osfd == nil { if fd == nil || fd.osfd == nil {
return -1, os.EINVAL return -1, os.EINVAL
} }
...@@ -268,19 +273,30 @@ func (fd *FD) Write(p []byte) (n int, err *os.Error) { ...@@ -268,19 +273,30 @@ func (fd *FD) Write(p []byte) (n int, err *os.Error) {
return nn, err return nn, err
} }
func (fd *FD) Accept(sa *syscall.Sockaddr) (nfd *FD, err *os.Error) { func sockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error)
func (fd *netFD) Accept(sa *syscall.Sockaddr) (nfd *netFD, err *os.Error) {
if fd == nil || fd.osfd == nil { if fd == nil || fd.osfd == nil {
return nil, os.EINVAL return nil, os.EINVAL
} }
s, e := syscall.Accept(fd.fd, sa);
for e == syscall.EAGAIN { var s, e int64;
for {
s, e = syscall.Accept(fd.fd, sa);
if e != syscall.EAGAIN {
break;
}
pollserver.WaitRead(fd); pollserver.WaitRead(fd);
s, e = syscall.Accept(fd.fd, sa)
} }
if e != 0 { if e != 0 {
return nil, os.ErrnoToError(e) return nil, os.ErrnoToError(e)
} }
if nfd, err = NewFD(s); err != nil {
raddr, err1 := sockaddrToHostPort(sa);
if err1 != nil {
raddr = "invalid-address";
}
if nfd, err = newFD(s, fd.net, fd.laddr, raddr); err != nil {
syscall.Close(s); syscall.Close(s);
return nil, err return nil, err
} }
......
...@@ -22,7 +22,7 @@ const ( ...@@ -22,7 +22,7 @@ const (
) )
// Make the 4 bytes into an IPv4 address (in IPv6 form) // Make the 4 bytes into an IPv4 address (in IPv6 form)
func _MakeIPv4(a, b, c, d byte) []byte { func makeIPv4(a, b, c, d byte) []byte {
p := make([]byte, IPv6len); p := make([]byte, IPv6len);
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
p[i] = 0 p[i] = 0
...@@ -40,10 +40,10 @@ func _MakeIPv4(a, b, c, d byte) []byte { ...@@ -40,10 +40,10 @@ func _MakeIPv4(a, b, c, d byte) []byte {
var IPv4bcast, IPv4allsys, IPv4allrouter, IPv4prefix, IPallbits, IPnoaddr []byte var IPv4bcast, IPv4allsys, IPv4allrouter, IPv4prefix, IPallbits, IPnoaddr []byte
func init() { func init() {
IPv4bcast = _MakeIPv4(0xff, 0xff, 0xff, 0xff); IPv4bcast = makeIPv4(0xff, 0xff, 0xff, 0xff);
IPv4allsys = _MakeIPv4(0xe0, 0x00, 0x00, 0x01); IPv4allsys = makeIPv4(0xe0, 0x00, 0x00, 0x01);
IPv4allrouter = _MakeIPv4(0xe0, 0x00, 0x00, 0x02); IPv4allrouter = makeIPv4(0xe0, 0x00, 0x00, 0x02);
IPv4prefix = _MakeIPv4(0, 0, 0, 0); IPv4prefix = makeIPv4(0, 0, 0, 0);
IPallbits = make([]byte, IPv6len); IPallbits = make([]byte, IPv6len);
for i := 0; i < IPv6len; i++ { for i := 0; i < IPv6len; i++ {
IPallbits[i] = 0xff IPallbits[i] = 0xff
...@@ -52,7 +52,7 @@ func init() { ...@@ -52,7 +52,7 @@ func init() {
} }
// Is p all zeros? // Is p all zeros?
func _IsZeros(p []byte) bool { func isZeros(p []byte) 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
...@@ -68,7 +68,7 @@ func ToIPv4(p []byte) []byte { ...@@ -68,7 +68,7 @@ func ToIPv4(p []byte) []byte {
return p return p
} }
if len(p) == IPv6len if len(p) == IPv6len
&& _IsZeros(p[0:10]) && isZeros(p[0:10])
&& p[10] == 0xff && p[10] == 0xff
&& p[11] == 0xff { && p[11] == 0xff {
return p[12:16] return p[12:16]
...@@ -79,7 +79,7 @@ func ToIPv4(p []byte) []byte { ...@@ -79,7 +79,7 @@ func ToIPv4(p []byte) []byte {
// Convert p to IPv6 form. // Convert p to IPv6 form.
func ToIPv6(p []byte) []byte { func ToIPv6(p []byte) []byte {
if len(p) == IPv4len { if len(p) == IPv4len {
return _MakeIPv4(p[0], p[1], p[2], p[3]) return makeIPv4(p[0], p[1], p[2], p[3])
} }
if len(p) == IPv6len { if len(p) == IPv6len {
return p return p
...@@ -89,9 +89,9 @@ func ToIPv6(p []byte) []byte { ...@@ -89,9 +89,9 @@ func ToIPv6(p []byte) []byte {
// Default route masks for IPv4. // Default route masks for IPv4.
var ( var (
ClassAMask = _MakeIPv4(0xff, 0, 0, 0); ClassAMask = makeIPv4(0xff, 0, 0, 0);
ClassBMask = _MakeIPv4(0xff, 0xff, 0, 0); ClassBMask = makeIPv4(0xff, 0xff, 0, 0);
ClassCMask = _MakeIPv4(0xff, 0xff, 0xff, 0); ClassCMask = makeIPv4(0xff, 0xff, 0xff, 0);
) )
func DefaultMask(p []byte) []byte { func DefaultMask(p []byte) []byte {
...@@ -204,7 +204,7 @@ func IPToString(p []byte) string { ...@@ -204,7 +204,7 @@ func IPToString(p []byte) string {
// If mask is a sequence of 1 bits followed by 0 bits, // If mask is a sequence of 1 bits followed by 0 bits,
// return the number of 1 bits. // return the number of 1 bits.
func _SimpleMaskLength(mask []byte) int { func simpleMaskLength(mask []byte) 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 {
...@@ -231,12 +231,12 @@ func _SimpleMaskLength(mask []byte) int { ...@@ -231,12 +231,12 @@ func _SimpleMaskLength(mask []byte) int {
func MaskToString(mask []byte) string { func MaskToString(mask []byte) string {
switch len(mask) { switch len(mask) {
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))
} }
...@@ -245,7 +245,7 @@ func MaskToString(mask []byte) string { ...@@ -245,7 +245,7 @@ func MaskToString(mask []byte) string {
} }
// Parse IPv4 address (d.d.d.d). // Parse IPv4 address (d.d.d.d).
func _ParseIPv4(s string) []byte { func parseIPv4(s string) []byte {
var p [IPv4len]byte; var p [IPv4len]byte;
i := 0; i := 0;
for j := 0; j < IPv4len; j++ { for j := 0; j < IPv4len; j++ {
...@@ -259,7 +259,7 @@ func _ParseIPv4(s string) []byte { ...@@ -259,7 +259,7 @@ func _ParseIPv4(s string) []byte {
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
} }
...@@ -268,7 +268,7 @@ func _ParseIPv4(s string) []byte { ...@@ -268,7 +268,7 @@ func _ParseIPv4(s string) []byte {
if i != len(s) { if i != len(s) {
return nil return nil
} }
return _MakeIPv4(p[0], p[1], p[2], p[3]) return makeIPv4(p[0], p[1], p[2], p[3])
} }
// Parse IPv6 address. Many forms. // Parse IPv6 address. Many forms.
...@@ -279,7 +279,7 @@ func _ParseIPv4(s string) []byte { ...@@ -279,7 +279,7 @@ func _ParseIPv4(s string) []byte {
// * A run of zeros can be replaced with "::". // * A run of zeros can be replaced with "::".
// * The last 32 bits can be in IPv4 form. // * The last 32 bits can be in IPv4 form.
// Thus, ::ffff:1.2.3.4 is the IPv4 address 1.2.3.4. // Thus, ::ffff:1.2.3.4 is the IPv4 address 1.2.3.4.
func _ParseIPv6(s string) []byte { func parseIPv6(s string) []byte {
p := make([]byte, 16); p := make([]byte, 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
...@@ -298,7 +298,7 @@ func _ParseIPv6(s string) []byte { ...@@ -298,7 +298,7 @@ func _ParseIPv6(s string) []byte {
j := 0; j := 0;
L: for j < IPv6len { 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
} }
...@@ -313,7 +313,7 @@ L: for j < IPv6len { ...@@ -313,7 +313,7 @@ L: for j < 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
} }
...@@ -378,10 +378,10 @@ L: for j < IPv6len { ...@@ -378,10 +378,10 @@ L: for j < IPv6len {
} }
func ParseIP(s string) []byte { func ParseIP(s string) []byte {
p := _ParseIPv4(s); p := parseIPv4(s);
if p != nil { if p != nil {
return p return p
} }
return _ParseIPv6(s) return parseIPv6(s)
} }
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"testing" "testing"
) )
func _IPv4(a, b, c, d byte) []byte { func ipv4(a, b, c, d byte) []byte {
return []byte( 0,0,0,0, 0,0,0,0, 0,0,255,255, a,b,c,d ) return []byte( 0,0,0,0, 0,0,0,0, 0,0,255,255, a,b,c,d )
} }
...@@ -33,14 +33,14 @@ type parseIPTest struct { ...@@ -33,14 +33,14 @@ type parseIPTest struct {
out []byte; out []byte;
} }
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)),
parseIPTest("127.0.0.256", nil), parseIPTest("127.0.0.256", nil),
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",
[]byte(0x20,0x01, 0x48,0x60, 0,0, 0x20,0x01, 0,0, 0,0, 0,0, 0x00,0x68)), []byte(0x20,0x01, 0x48,0x60, 0,0, 0x20,0x01, 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)),
) )
func TestParseIP(t *testing.T) { func TestParseIP(t *testing.T) {
......
This diff is collapsed.
...@@ -26,7 +26,7 @@ func IPv4ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) { ...@@ -26,7 +26,7 @@ func IPv4ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) {
return unsafe.Pointer(sa).(*syscall.Sockaddr), nil return unsafe.Pointer(sa).(*syscall.Sockaddr), nil
} }
var _IPv6zero [16]byte; var ipv6zero [16]byte;
func IPv6ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) { func IPv6ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) {
p = ToIPv6(p); p = ToIPv6(p);
...@@ -38,7 +38,7 @@ func IPv6ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) { ...@@ -38,7 +38,7 @@ func IPv6ToSockaddr(p []byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) {
// In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0", // In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
// which it refuses to do. Rewrite to the IPv6 all zeros. // which it refuses to do. Rewrite to the IPv6 all zeros.
if p4 := ToIPv4(p); p4 != nil && p4[0] == 0 && p4[1] == 0 && p4[2] == 0 && p4[3] == 0 { if p4 := ToIPv4(p); p4 != nil && p4[0] == 0 && p4[1] == 0 && p4[2] == 0 && p4[3] == 0 {
p = _IPv6zero; p = ipv6zero;
} }
sa := new(syscall.SockaddrInet6); sa := new(syscall.SockaddrInet6);
......
...@@ -12,16 +12,16 @@ import ( ...@@ -12,16 +12,16 @@ import (
"os"; "os";
) )
type _File struct { type file struct {
fd *os.FD; fd *os.FD;
data []byte; data []byte;
} }
func (f *_File) Close() { func (f *file) close() {
f.fd.Close() f.fd.Close()
} }
func (f *_File) GetLineFromData() (s string, ok bool) { func (f *file) getLineFromData() (s string, ok bool) {
data := f.data; data := f.data;
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
if data[i] == '\n' { if data[i] == '\n' {
...@@ -40,8 +40,8 @@ func (f *_File) GetLineFromData() (s string, ok bool) { ...@@ -40,8 +40,8 @@ func (f *_File) GetLineFromData() (s string, ok bool) {
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) {
...@@ -51,19 +51,19 @@ func (f *_File) ReadLine() (s string, ok bool) { ...@@ -51,19 +51,19 @@ func (f *_File) ReadLine() (s string, ok bool) {
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 { func open(name string) *file {
fd, err := os.Open(name, os.O_RDONLY, 0); fd, err := os.Open(name, os.O_RDONLY, 0);
if err != nil { if err != nil {
return nil return nil
} }
return &_File(fd, make([]byte, 1024)[0:0]); return &file(fd, make([]byte, 1024)[0:0]);
} }
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
...@@ -73,10 +73,10 @@ func _ByteIndex(s string, c byte) int { ...@@ -73,10 +73,10 @@ func _ByteIndex(s string, c byte) int {
} }
// Count occurrences in s of any bytes in t. // Count occurrences in s of any bytes in t.
func _CountAnyByte(s string, t string) int { func countAnyByte(s string, t string) int {
n := 0; n := 0;
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if _ByteIndex(t, s[i]) >= 0 { if byteIndex(t, s[i]) >= 0 {
n++; n++;
} }
} }
...@@ -84,12 +84,12 @@ func _CountAnyByte(s string, t string) int { ...@@ -84,12 +84,12 @@ func _CountAnyByte(s string, t string) int {
} }
// 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++ {
if _ByteIndex(t, s[i]) >= 0 { if byteIndex(t, s[i]) >= 0 {
if last < i { if last < i {
a[n] = string(s[last:i]); a[n] = string(s[last:i]);
n++; n++;
...@@ -104,20 +104,20 @@ func _SplitAtBytes(s string, t string) []string { ...@@ -104,20 +104,20 @@ func _SplitAtBytes(s string, t string) []string {
return a[0:n]; return a[0:n];
} }
func _GetFields(s string) []string { func getFields(s string) []string {
return _SplitAtBytes(s, " \r\t\n"); return splitAtBytes(s, " \r\t\n");
} }
// Bigger than we need, not too big to worry about overflow // Bigger than we need, not too big to worry about overflow
const _Big = 0xFFFFFF const big = 0xFFFFFF
// Decimal to integer starting at &s[i0]. // Decimal to integer starting at &s[i0].
// Returns number, new offset, success. // Returns number, new offset, success.
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
} }
} }
...@@ -129,7 +129,7 @@ func _Dtoi(s string, i0 int) (n int, i int, ok bool) { ...@@ -129,7 +129,7 @@ func _Dtoi(s string, i0 int) (n int, i int, ok bool) {
// Hexadecimal to integer starting at &s[i0]. // Hexadecimal to integer starting at &s[i0].
// Returns number, new offset, success. // Returns number, new offset, success.
func _Xtoi(s string, i0 int) (n int, i int, ok bool) { func xtoi(s string, i0 int) (n int, i int, ok bool) {
n = 0; n = 0;
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' {
...@@ -144,7 +144,7 @@ func _Xtoi(s string, i0 int) (n int, i int, ok bool) { ...@@ -144,7 +144,7 @@ func _Xtoi(s string, i0 int) (n int, i int, ok bool) {
} else { } else {
break break
} }
if n >= _Big { if n >= big {
return 0, i, false return 0, i, false
} }
} }
......
...@@ -20,16 +20,17 @@ func TestReadLine(t *testing.T) { ...@@ -20,16 +20,17 @@ func TestReadLine(t *testing.T) {
} }
br := bufio.NewBufRead(fd); br := bufio.NewBufRead(fd);
file := _Open(filename); // TODO(rsc): 6g rejects "file :="
var file = open(filename);
if file == nil { if file == nil {
t.Fatalf("net._Open(%s) = nil", filename); t.Fatalf("net.open(%s) = nil", filename);
} }
lineno := 1; lineno := 1;
byteno := 0; byteno := 0;
for { for {
bline, berr := br.ReadLineString('\n', false); bline, berr := br.ReadLineString('\n', false);
line, ok := file.ReadLine(); line, ok := file.readLine();
if (berr != nil) != !ok || bline != line { if (berr != nil) != !ok || bline != line {
t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v", t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v",
filename, lineno, byteno, bline, berr, line, ok); filename, lineno, byteno, bline, berr, line, ok);
......
...@@ -16,20 +16,21 @@ import ( ...@@ -16,20 +16,21 @@ import (
var services map[string] map[string] int var services map[string] map[string] int
func _ReadServices() { func readServices() {
services = make(map[string] map[string] int); services = make(map[string] map[string] int);
file := _Open("/etc/services"); // TODO(rsc): 6g won't let me do "file := "
for line, ok := file.ReadLine(); ok; line, ok = file.ReadLine() { var file = open("/etc/services");
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
// "http 80/tcp www www-http # World Wide Web HTTP" // "http 80/tcp www www-http # World Wide Web HTTP"
if i := _ByteIndex(line, '#'); i >= 0 { if i := byteIndex(line, '#'); i >= 0 {
line = line[0:i]; line = line[0:i];
} }
f := _GetFields(line); f := getFields(line);
if len(f) < 2 { if len(f) < 2 {
continue; continue;
} }
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
} }
...@@ -45,11 +46,11 @@ func _ReadServices() { ...@@ -45,11 +46,11 @@ func _ReadServices() {
} }
} }
} }
file.Close(); file.close();
} }
func LookupPort(netw, name string) (port int, ok bool) { func LookupPort(netw, name string) (port int, ok bool) {
once.Do(_ReadServices); once.Do(readServices);
switch netw { switch netw {
case "tcp4", "tcp6": case "tcp4", "tcp6":
......
...@@ -13,12 +13,12 @@ import ( ...@@ -13,12 +13,12 @@ import (
var dot = []string( var dot = []string(
"dir_amd64_darwin.go", "dir_amd64_darwin.go",
"dir_amd64_linux.go", "dir_amd64_linux.go",
"os_env.go", "env.go",
"os_error.go", "error.go",
"os_file.go", "file.go",
"os_test.go", "os_test.go",
"os_time.go", "time.go",
"os_types.go", "types.go",
"stat_amd64_darwin.go", "stat_amd64_darwin.go",
"stat_amd64_linux.go" "stat_amd64_linux.go"
) )
...@@ -101,7 +101,7 @@ func testReaddirnames(dir string, contents []string, t *testing.T) { ...@@ -101,7 +101,7 @@ func testReaddirnames(dir string, contents []string, t *testing.T) {
fd, err := Open(dir, O_RDONLY, 0); fd, err := Open(dir, O_RDONLY, 0);
defer fd.Close(); defer fd.Close();
if err != nil { if err != nil {
t.Fatalf("open %q failed: %v\n", dir, err); t.Fatalf("open %q failed: %v", dir, err);
} }
s, err2 := Readdirnames(fd, -1); s, err2 := Readdirnames(fd, -1);
if err2 != nil { if err2 != nil {
...@@ -192,12 +192,13 @@ func TestReaddirnamesOneAtATime(t *testing.T) { ...@@ -192,12 +192,13 @@ func TestReaddirnamesOneAtATime(t *testing.T) {
} }
fd1, err2 := Open(dir, O_RDONLY, 0); fd1, err2 := Open(dir, O_RDONLY, 0);
if err2 != nil { if err2 != nil {
t.Fatalf("open %q failed: %v\n", dir, err2); t.Fatalf("open %q failed: %v", dir, err2);
} }
small := smallReaddirnames(fd1, len(all)+100, t); // +100 in case we screw up small := smallReaddirnames(fd1, len(all)+100, t); // +100 in case we screw up
for i, n := range all { for i, n := range all {
if small[i] != n { if small[i] != n {
t.Errorf("small read %q %q mismatch: %v\n", small[i], n); t.Errorf("small read %q %q mismatch: %v", small[i], n);
} }
} }
} }
...@@ -29,6 +29,7 @@ maketest \ ...@@ -29,6 +29,7 @@ maketest \
lib/json\ lib/json\
lib/math\ lib/math\
lib/net\ lib/net\
lib/os\
lib/reflect\ lib/reflect\
lib/regexp\ lib/regexp\
lib/strconv\ lib/strconv\
......
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