Commit c840657f authored by Russ Cox's avatar Russ Cox

casify DNS

R=r
DELTA=221  (0 added, 0 deleted, 221 changed)
OCL=22946
CL=22948
parent dec12d36
...@@ -28,7 +28,7 @@ import ( ...@@ -28,7 +28,7 @@ import (
export var ( export var (
DNS_InternalError = os.NewError("internal dns error"); DNS_InternalError = os.NewError("internal dns error");
DNS_MissingConfig = os.NewError("no dns configuration"); DNS_MissingConfig = os.NewError("no dns configuration");
DNS_NoAnswer = os.NewError("dns got no answer"); DNS_No_Answer = os.NewError("dns got no answer");
DNS_BadRequest = os.NewError("malformed dns request"); DNS_BadRequest = os.NewError("malformed dns request");
DNS_BadReply = os.NewError("malformed dns reply"); DNS_BadReply = os.NewError("malformed dns reply");
DNS_ServerFailure = os.NewError("dns server failure"); DNS_ServerFailure = os.NewError("dns server failure");
...@@ -40,7 +40,7 @@ export var ( ...@@ -40,7 +40,7 @@ export var (
// Send a request on the connection and hope for a reply. // Send a request on the connection and hope for a reply.
// 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, DNS_NameTooLong return nil, DNS_NameTooLong
} }
...@@ -77,14 +77,14 @@ func Exchange(cfg *DNS_Config, c Conn, name string) (m *DNS_Msg, err *os.Error) ...@@ -77,14 +77,14 @@ func Exchange(cfg *DNS_Config, c Conn, name string) (m *DNS_Msg, err *os.Error)
} }
return in, nil return in, nil
} }
return nil, DNS_NoAnswer return nil, DNS_No_Answer
} }
// 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
...@@ -149,13 +149,13 @@ func TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) { ...@@ -149,13 +149,13 @@ func TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) {
err = cerr; err = cerr;
continue; continue;
} }
msg, merr := Exchange(cfg, c, name); msg, merr := _Exchange(cfg, c, name);
c.Close(); c.Close();
if merr != nil { if merr != nil {
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 @@ export func LookupHost(name string) (name1 string, addrs []string, err *os.Error ...@@ -175,7 +175,7 @@ export 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 @@ export func LookupHost(name string) (name1 string, addrs []string, err *os.Error ...@@ -190,7 +190,7 @@ export 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 @@ export func LookupHost(name string) (name1 string, addrs []string, err *os.Error ...@@ -206,7 +206,7 @@ export 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,7 @@ export type DNS_Config struct { ...@@ -27,7 +27,7 @@ export 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.
export func DNS_ReadConfig() *DNS_Config { export func DNS_ReadConfig() *DNS_Config {
file := Open("/etc/resolv.conf"); file := _Open("/etc/resolv.conf");
if file == nil { if file == nil {
return nil return nil
} }
...@@ -40,7 +40,7 @@ export func DNS_ReadConfig() *DNS_Config { ...@@ -40,7 +40,7 @@ export func DNS_ReadConfig() *DNS_Config {
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 +79,19 @@ export func DNS_ReadConfig() *DNS_Config { ...@@ -79,19 +79,19 @@ export 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
} }
......
This diff is collapsed.
...@@ -27,7 +27,7 @@ export type FD struct { ...@@ -27,7 +27,7 @@ export type FD struct {
} }
// 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 +40,11 @@ func SetNonblock(fd int64) *os.Error { ...@@ -40,11 +40,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 +54,8 @@ func SetNonblock(fd int64) *os.Error { ...@@ -54,8 +54,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 +65,32 @@ func SetNonblock(fd int64) *os.Error { ...@@ -65,32 +65,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 *FD; // buffered >= 1
pr, pw *os.FD; pr, pw *os.FD;
pending map[int64] *FD; pending map[int64] *FD;
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 *FD, 1);
s.cw = make(chan *FD, 1); s.cw = make(chan *FD, 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 {
...@@ -105,9 +105,9 @@ func NewPollServer() (s *PollServer, err *os.Error) { ...@@ -105,9 +105,9 @@ func NewPollServer() (s *PollServer, err *os.Error) {
return s, nil return s, nil
} }
func (s *PollServer) AddFD(fd *FD, mode int) { func (s *_PollServer) AddFD(fd *FD, 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 +121,7 @@ func (s *PollServer) AddFD(fd *FD, mode int) { ...@@ -121,7 +121,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) *FD {
key := fd << 1; key := fd << 1;
if mode == 'w' { if mode == 'w' {
key++; key++;
...@@ -134,12 +134,12 @@ func (s *PollServer) LookupFD(fd int64, mode int) *FD { ...@@ -134,12 +134,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 +158,7 @@ func (s *PollServer) Run() { ...@@ -158,7 +158,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 +176,18 @@ func (s *PollServer) Run() { ...@@ -176,18 +176,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 *FD) {
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 *FD) {
s.cr <- fd; s.cr <- fd;
s.Wakeup(); s.Wakeup();
<-fd.cr <-fd.cr
...@@ -195,23 +195,23 @@ func (s *PollServer) WaitWrite(fd *FD) { ...@@ -195,23 +195,23 @@ 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
} }
export func NewFD(fd int64) (f *FD, err *os.Error) { export func NewFD(fd int64) (f *FD, 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(FD);
......
...@@ -22,7 +22,7 @@ export const ( ...@@ -22,7 +22,7 @@ export 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 {
export var IPv4bcast, IPv4allsys, IPv4allrouter, IPv4prefix, IPallbits, IPnoaddr []byte export 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 @@ export func ToIPv4(p []byte) []byte { ...@@ -68,7 +68,7 @@ export 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 @@ export func ToIPv4(p []byte) []byte { ...@@ -79,7 +79,7 @@ export func ToIPv4(p []byte) []byte {
// Convert p to IPv6 form. // Convert p to IPv6 form.
export func ToIPv6(p []byte) []byte { export 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 @@ export func ToIPv6(p []byte) []byte { ...@@ -89,9 +89,9 @@ export func ToIPv6(p []byte) []byte {
// Default route masks for IPv4. // Default route masks for IPv4.
export var ( export 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);
) )
export func DefaultMask(p []byte) []byte { export func DefaultMask(p []byte) []byte {
...@@ -204,7 +204,7 @@ export func IPToString(p []byte) string { ...@@ -204,7 +204,7 @@ export 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 {
export func MaskToString(mask []byte) string { export 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 @@ export func MaskToString(mask []byte) string { ...@@ -245,7 +245,7 @@ export 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 {
} }
export func ParseIP(s string) []byte { export 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)
} }
This diff is collapsed.
...@@ -12,16 +12,16 @@ import ( ...@@ -12,16 +12,16 @@ import (
"os"; "os";
) )
package 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,7 +40,7 @@ func (f *File) GetLineFromData() (s string, ok bool) { ...@@ -40,7 +40,7 @@ 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
} }
...@@ -55,15 +55,15 @@ func (f *File) ReadLine() (s string, ok bool) { ...@@ -55,15 +55,15 @@ func (f *File) ReadLine() (s string, ok bool) {
return return
} }
package 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]};
} }
package 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 @@ package func ByteIndex(s string, c byte) int { ...@@ -73,10 +73,10 @@ package 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.
package 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 @@ package func CountAnyByte(s string, t string) int { ...@@ -84,12 +84,12 @@ package func CountAnyByte(s string, t string) int {
} }
// Split s at any bytes in t. // Split s at any bytes in t.
package 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 @@ package func SplitAtBytes(s string, t string) []string { ...@@ -104,20 +104,20 @@ package func SplitAtBytes(s string, t string) []string {
return a[0:n]; return a[0:n];
} }
package 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.
package 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 @@ package func Dtoi(s string, i0 int) (n int, i int, ok bool) { ...@@ -129,7 +129,7 @@ package 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.
package 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 @@ package func Xtoi(s string, i0 int) (n int, i int, ok bool) { ...@@ -144,7 +144,7 @@ package 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
} }
} }
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
export func TestReadLine(t *testing.T) { export func TestReadLine(t *testing.T) {
filename := "/etc/services"; // a nice big file filename := "/etc/services"; // a nice big file
fd, err := os.Open(filename, os.O_RDONLY, 0); fd, err := os._Open(filename, os.O_RDONLY, 0);
if err != nil { if err != nil {
t.Fatalf("open %s: %v", filename, err); t.Fatalf("open %s: %v", filename, err);
} }
...@@ -23,9 +23,9 @@ export func TestReadLine(t *testing.T) { ...@@ -23,9 +23,9 @@ export func TestReadLine(t *testing.T) {
t.Fatalf("bufio.NewBufRead: %v", err1); t.Fatalf("bufio.NewBufRead: %v", err1);
} }
file := Open(filename); 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;
......
...@@ -16,20 +16,20 @@ import ( ...@@ -16,20 +16,20 @@ 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"); file := _Open("/etc/services");
for line, ok := file.ReadLine(); ok; line, ok = file.ReadLine() { 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
} }
...@@ -49,7 +49,7 @@ func ReadServices() { ...@@ -49,7 +49,7 @@ func ReadServices() {
} }
export func LookupPort(netw, name string) (port int, ok bool) { export 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":
......
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