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 (
export var (
DNS_InternalError = os.NewError("internal dns error");
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_BadReply = os.NewError("malformed dns reply");
DNS_ServerFailure = os.NewError("dns server failure");
......@@ -40,7 +40,7 @@ export var (
// Send a request on the connection and hope for a reply.
// 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 {
return nil, DNS_NameTooLong
}
......@@ -77,14 +77,14 @@ func Exchange(cfg *DNS_Config, c Conn, name string) (m *DNS_Msg, err *os.Error)
}
return in, nil
}
return nil, DNS_NoAnswer
return nil, DNS_No_Answer
}
// Find answer for name in dns message.
// On return, if err == nil, addrs != nil.
// 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));
if dns.rcode == DNS_RcodeNameError && dns.authoritative {
......@@ -134,8 +134,8 @@ Cname:
}
// Do a lookup for a single name, which must be rooted
// (otherwise Answer will not find the answers).
func TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) {
// (otherwise _Answer will not find the answers).
func _TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) {
err = DNS_NoServers;
for i := 0; i < len(cfg.servers); i++ {
// 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) {
err = cerr;
continue;
}
msg, merr := Exchange(cfg, c, name);
msg, merr := _Exchange(cfg, c, name);
c.Close();
if merr != nil {
err = merr;
continue;
}
addrs, aerr := Answer(name, msg);
addrs, aerr := _Answer(name, msg);
if aerr != nil && aerr != DNS_NameNotFound {
err = aerr;
continue;
......@@ -167,7 +167,7 @@ func TryOneName(cfg *DNS_Config, name string) (addrs []string, err *os.Error) {
var cfg *DNS_Config
func LoadConfig() {
func _LoadConfig() {
cfg = DNS_ReadConfig();
}
......@@ -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
// sending stupid requests to the server?
once.Do(&LoadConfig);
once.Do(&_LoadConfig);
if cfg == nil {
err = DNS_MissingConfig;
return;
......@@ -190,7 +190,7 @@ export func LookupHost(name string) (name1 string, addrs []string, err *os.Error
rname += ".";
}
// Can try as ordinary name.
addrs, aerr := TryOneName(cfg, rname);
addrs, aerr := _TryOneName(cfg, rname);
if aerr == nil {
return rname, addrs, nil;
}
......@@ -206,7 +206,7 @@ export func LookupHost(name string) (name1 string, addrs []string, err *os.Error
if newname[len(newname)-1] != '.' {
newname += "."
}
addrs, aerr := TryOneName(cfg, newname);
addrs, aerr := _TryOneName(cfg, newname);
if aerr == nil {
return newname, addrs, nil;
}
......
......@@ -27,7 +27,7 @@ export type DNS_Config struct {
// of the host name to get the default search domain.
// We assume it's in resolv.conf anyway.
export func DNS_ReadConfig() *DNS_Config {
file := Open("/etc/resolv.conf");
file := _Open("/etc/resolv.conf");
if file == nil {
return nil
}
......@@ -40,7 +40,7 @@ export func DNS_ReadConfig() *DNS_Config {
conf.rotate = false;
var err *os.Error;
for line, ok := file.ReadLine(); ok; line, ok = file.ReadLine() {
f := GetFields(line);
f := _GetFields(line);
if len(f) < 1 {
continue;
}
......@@ -79,19 +79,19 @@ export func DNS_ReadConfig() *DNS_Config {
s := f[i];
switch {
case len(s) >= 6 && s[0:6] == "ndots:":
n, i, ok := Dtoi(s, 6);
n, i, ok := _Dtoi(s, 6);
if n < 1 {
n = 1
}
conf.ndots = n;
case len(s) >= 8 && s[0:8] == "timeout:":
n, i, ok := Dtoi(s, 8);
n, i, ok := _Dtoi(s, 8);
if n < 1 {
n = 1
}
conf.timeout = n;
case len(s) >= 8 && s[0:9] == "attempts:":
n, i, ok := Dtoi(s, 9);
n, i, ok := _Dtoi(s, 9);
if n < 1 {
n = 1
}
......
This diff is collapsed.
......@@ -27,7 +27,7 @@ export type FD struct {
}
// 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);
if e != 0 {
return os.ErrnoToError(e)
......@@ -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,
// 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.
// 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.
// This protocol is implemented as s.WaitRead() and s.WaitWrite().
//
......@@ -54,8 +54,8 @@ func SetNonblock(fd int64) *os.Error {
// 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
// buffered channel s.cr/s.cw, WaitRead/WaitWrite writes a
// byte to the pipe, causing the PollServer's poll system call to
// return. In response to the pipe being readable, the PollServer
// byte to the pipe, causing the _PollServer's poll system call to
// return. In response to the pipe being readable, the _PollServer
// re-polls its request channels.
//
// Note that the ordering is "send request" and then "wake up server".
......@@ -65,32 +65,32 @@ func SetNonblock(fd int64) *os.Error {
// 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
// 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
// might help batch requests.
type PollServer struct {
type _PollServer struct {
cr, cw chan *FD; // buffered >= 1
pr, pw *os.FD;
pending map[int64] *FD;
poll *Pollster; // low-level OS hooks
}
func (s *PollServer) Run();
func (s *_PollServer) Run();
func NewPollServer() (s *PollServer, err *os.Error) {
s = new(PollServer);
func _NewPollServer() (s *_PollServer, err *os.Error) {
s = new(_PollServer);
s.cr = make(chan *FD, 1);
s.cw = make(chan *FD, 1);
if s.pr, s.pw, err = os.Pipe(); err != nil {
return nil, err
}
if err = SetNonblock(s.pr.fd); err != nil {
if err = _SetNonblock(s.pr.fd); err != nil {
Error:
s.pr.Close();
s.pw.Close();
return nil, err
}
if err = SetNonblock(s.pw.fd); err != nil {
if err = _SetNonblock(s.pw.fd); err != nil {
goto Error
}
if s.poll, err = NewPollster(); err != nil {
......@@ -105,9 +105,9 @@ func NewPollServer() (s *PollServer, err *os.Error) {
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 {
print("PollServer AddFD: ", err.String(), "\n");
print("_PollServer AddFD: ", err.String(), "\n");
return
}
......@@ -121,7 +121,7 @@ func (s *PollServer) AddFD(fd *FD, mode int) {
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;
if mode == 'w' {
key++;
......@@ -134,12 +134,12 @@ func (s *PollServer) LookupFD(fd int64, mode int) *FD {
return netfd
}
func (s *PollServer) Run() {
func (s *_PollServer) Run() {
var scratch [100]byte;
for {
fd, mode, err := s.poll.WaitFD();
if err != nil {
print("PollServer WaitFD: ", err.String(), "\n");
print("_PollServer WaitFD: ", err.String(), "\n");
return
}
if fd == s.pr.fd {
......@@ -158,7 +158,7 @@ func (s *PollServer) Run() {
} else {
netfd := s.LookupFD(fd, mode);
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
}
if mode == 'r' {
......@@ -176,18 +176,18 @@ func (s *PollServer) Run() {
}
}
func (s *PollServer) Wakeup() {
func (s *_PollServer) Wakeup() {
var b [1]byte;
s.pw.Write(b)
}
func (s *PollServer) WaitRead(fd *FD) {
func (s *_PollServer) WaitRead(fd *FD) {
s.cr <- fd;
s.Wakeup();
<-fd.cr
}
func (s *PollServer) WaitWrite(fd *FD) {
func (s *_PollServer) WaitWrite(fd *FD) {
s.cr <- fd;
s.Wakeup();
<-fd.cr
......@@ -195,23 +195,23 @@ func (s *PollServer) WaitWrite(fd *FD) {
// 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() {
p, err := NewPollServer();
func _StartServer() {
p, err := _NewPollServer();
if err != nil {
print("Start PollServer: ", err.String(), "\n")
print("Start _PollServer: ", err.String(), "\n")
}
pollserver = p
}
export func NewFD(fd int64) (f *FD, err *os.Error) {
if pollserver == nil {
once.Do(&StartServer);
once.Do(&_StartServer);
}
if err = SetNonblock(fd); err != nil {
if err = _SetNonblock(fd); err != nil {
return nil, err
}
f = new(FD);
......
......@@ -22,7 +22,7 @@ export const (
)
// 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);
for i := 0; i < 10; i++ {
p[i] = 0
......@@ -40,10 +40,10 @@ func MakeIPv4(a, b, c, d byte) []byte {
export var IPv4bcast, IPv4allsys, IPv4allrouter, IPv4prefix, IPallbits, IPnoaddr []byte
func init() {
IPv4bcast = MakeIPv4(0xff, 0xff, 0xff, 0xff);
IPv4allsys = MakeIPv4(0xe0, 0x00, 0x00, 0x01);
IPv4allrouter = MakeIPv4(0xe0, 0x00, 0x00, 0x02);
IPv4prefix = MakeIPv4(0, 0, 0, 0);
IPv4bcast = _MakeIPv4(0xff, 0xff, 0xff, 0xff);
IPv4allsys = _MakeIPv4(0xe0, 0x00, 0x00, 0x01);
IPv4allrouter = _MakeIPv4(0xe0, 0x00, 0x00, 0x02);
IPv4prefix = _MakeIPv4(0, 0, 0, 0);
IPallbits = make([]byte, IPv6len);
for i := 0; i < IPv6len; i++ {
IPallbits[i] = 0xff
......@@ -52,7 +52,7 @@ func init() {
}
// Is p all zeros?
func IsZeros(p []byte) bool {
func _IsZeros(p []byte) bool {
for i := 0; i < len(p); i++ {
if p[i] != 0 {
return false
......@@ -68,7 +68,7 @@ export func ToIPv4(p []byte) []byte {
return p
}
if len(p) == IPv6len
&& IsZeros(p[0:10])
&& _IsZeros(p[0:10])
&& p[10] == 0xff
&& p[11] == 0xff {
return p[12:16]
......@@ -79,7 +79,7 @@ export func ToIPv4(p []byte) []byte {
// Convert p to IPv6 form.
export func ToIPv6(p []byte) []byte {
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 {
return p
......@@ -89,9 +89,9 @@ export func ToIPv6(p []byte) []byte {
// Default route masks for IPv4.
export var (
ClassAMask = MakeIPv4(0xff, 0, 0, 0);
ClassBMask = MakeIPv4(0xff, 0xff, 0, 0);
ClassCMask = MakeIPv4(0xff, 0xff, 0xff, 0);
ClassAMask = _MakeIPv4(0xff, 0, 0, 0);
ClassBMask = _MakeIPv4(0xff, 0xff, 0, 0);
ClassCMask = _MakeIPv4(0xff, 0xff, 0xff, 0);
)
export func DefaultMask(p []byte) []byte {
......@@ -204,7 +204,7 @@ export func IPToString(p []byte) string {
// If mask is a sequence of 1 bits followed by 0 bits,
// return the number of 1 bits.
func SimpleMaskLength(mask []byte) int {
func _SimpleMaskLength(mask []byte) int {
var i int;
for i = 0; i < len(mask); i++ {
if mask[i] != 0xFF {
......@@ -231,12 +231,12 @@ func SimpleMaskLength(mask []byte) int {
export func MaskToString(mask []byte) string {
switch len(mask) {
case 4:
n := SimpleMaskLength(mask);
n := _SimpleMaskLength(mask);
if n >= 0 {
return itod(uint(n+(IPv6len-IPv4len)*8))
}
case 16:
n := SimpleMaskLength(mask);
n := _SimpleMaskLength(mask);
if n >= 0 {
return itod(uint(n))
}
......@@ -245,7 +245,7 @@ export func MaskToString(mask []byte) string {
}
// Parse IPv4 address (d.d.d.d).
func ParseIPv4(s string) []byte {
func _ParseIPv4(s string) []byte {
var p [IPv4len]byte;
i := 0;
for j := 0; j < IPv4len; j++ {
......@@ -259,7 +259,7 @@ func ParseIPv4(s string) []byte {
n int;
ok bool
)
n, i, ok = Dtoi(s, i);
n, i, ok = _Dtoi(s, i);
if !ok || n > 0xFF {
return nil
}
......@@ -268,7 +268,7 @@ func ParseIPv4(s string) []byte {
if i != len(s) {
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.
......@@ -279,7 +279,7 @@ func ParseIPv4(s string) []byte {
// * A run of zeros can be replaced with "::".
// * The last 32 bits can be in IPv4 form.
// 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);
ellipsis := -1; // position of ellipsis in p
i := 0; // index in string s
......@@ -298,7 +298,7 @@ func ParseIPv6(s string) []byte {
j := 0;
L: for j < IPv6len {
// Hex number.
n, i1, ok := Xtoi(s, i);
n, i1, ok := _Xtoi(s, i);
if !ok || n > 0xFFFF {
return nil
}
......@@ -313,7 +313,7 @@ L: for j < IPv6len {
// Not enough room.
return nil
}
p4 := ParseIPv4(s[i:len(s)]);
p4 := _ParseIPv4(s[i:len(s)]);
if p4 == nil {
return nil
}
......@@ -378,10 +378,10 @@ L: for j < IPv6len {
}
export func ParseIP(s string) []byte {
p := ParseIPv4(s);
p := _ParseIPv4(s);
if p != nil {
return p
}
return ParseIPv6(s)
return _ParseIPv6(s)
}
This diff is collapsed.
......@@ -12,16 +12,16 @@ import (
"os";
)
package type File struct {
type _File struct {
fd *os.FD;
data []byte;
}
func (f *File) Close() {
func (f *_File) Close() {
f.fd.Close()
}
func (f *File) GetLineFromData() (s string, ok bool) {
func (f *_File) GetLineFromData() (s string, ok bool) {
data := f.data;
for i := 0; i < len(data); i++ {
if data[i] == '\n' {
......@@ -40,7 +40,7 @@ func (f *File) GetLineFromData() (s string, ok bool) {
return
}
func (f *File) ReadLine() (s string, ok bool) {
func (f *_File) ReadLine() (s string, ok bool) {
if s, ok = f.GetLineFromData(); ok {
return
}
......@@ -55,15 +55,15 @@ func (f *File) ReadLine() (s string, ok bool) {
return
}
package func Open(name string) *File {
func _Open(name string) *_File {
fd, err := os.Open(name, os.O_RDONLY, 0);
if err != 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++ {
if s[i] == c {
return i
......@@ -73,10 +73,10 @@ package func ByteIndex(s string, c byte) int {
}
// 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;
for i := 0; i < len(s); i++ {
if ByteIndex(t, s[i]) >= 0 {
if _ByteIndex(t, s[i]) >= 0 {
n++;
}
}
......@@ -84,12 +84,12 @@ package func CountAnyByte(s string, t string) int {
}
// Split s at any bytes in t.
package func SplitAtBytes(s string, t string) []string {
a := make([]string, 1+CountAnyByte(s, t));
func _SplitAtBytes(s string, t string) []string {
a := make([]string, 1+_CountAnyByte(s, t));
n := 0;
last := 0;
for i := 0; i < len(s); i++ {
if ByteIndex(t, s[i]) >= 0 {
if _ByteIndex(t, s[i]) >= 0 {
if last < i {
a[n] = string(s[last:i]);
n++;
......@@ -104,20 +104,20 @@ package func SplitAtBytes(s string, t string) []string {
return a[0:n];
}
package func GetFields(s string) []string {
return SplitAtBytes(s, " \r\t\n");
func _GetFields(s string) []string {
return _SplitAtBytes(s, " \r\t\n");
}
// Bigger than we need, not too big to worry about overflow
const Big = 0xFFFFFF
const _Big = 0xFFFFFF
// Decimal to integer starting at &s[i0].
// 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;
for i = i0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
n = n*10 + int(s[i] - '0');
if n >= Big {
if n >= _Big {
return 0, i, false
}
}
......@@ -129,7 +129,7 @@ package func Dtoi(s string, i0 int) (n int, i int, ok bool) {
// Hexadecimal to integer starting at &s[i0].
// 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;
for i = i0; i < len(s); i++ {
if '0' <= s[i] && s[i] <= '9' {
......@@ -144,7 +144,7 @@ package func Xtoi(s string, i0 int) (n int, i int, ok bool) {
} else {
break
}
if n >= Big {
if n >= _Big {
return 0, i, false
}
}
......
......@@ -14,7 +14,7 @@ import (
export func TestReadLine(t *testing.T) {
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 {
t.Fatalf("open %s: %v", filename, err);
}
......@@ -23,9 +23,9 @@ export func TestReadLine(t *testing.T) {
t.Fatalf("bufio.NewBufRead: %v", err1);
}
file := Open(filename);
file := _Open(filename);
if file == nil {
t.Fatalf("net.Open(%s) = nil", filename);
t.Fatalf("net._Open(%s) = nil", filename);
}
lineno := 1;
......
......@@ -16,20 +16,20 @@ import (
var services map[string] map[string] int
func ReadServices() {
func _ReadServices() {
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() {
// "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];
}
f := GetFields(line);
f := _GetFields(line);
if len(f) < 2 {
continue;
}
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] != '/' {
continue
}
......@@ -49,7 +49,7 @@ func ReadServices() {
}
export func LookupPort(netw, name string) (port int, ok bool) {
once.Do(&ReadServices);
once.Do(&_ReadServices);
switch netw {
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