casify DNS

R=r
DELTA=221  (0 added, 0 deleted, 221 changed)
OCL=22946
CL=22948
This commit is contained in:
Russ Cox 2009-01-16 11:04:44 -08:00
parent dec12d3654
commit c840657fe1
9 changed files with 211 additions and 211 deletions

View file

@ -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 (
// 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)
} }
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:
} }
// 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) {
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) {
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
// 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
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
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;
} }

View file

@ -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 {
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 {
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
} }

View file

@ -29,7 +29,7 @@ import (
"reflect"; "reflect";
) )
// Packet formats // _Packet formats
// Wire constants. // Wire constants.
export const ( export const (
@ -74,19 +74,19 @@ export const (
) )
// The wire format for the DNS packet header. // The wire format for the DNS packet header.
type DNS_Header struct { type _DNS_Header struct {
id uint16; id uint16;
bits uint16; bits uint16;
qdcount, ancount, nscount, arcount uint16; qdcount, ancount, nscount, arcount uint16;
} }
const ( const (
// DNS_Header.bits // _DNS_Header.bits
QR = 1<<15; // query/response (response=1) _QR = 1<<15; // query/response (response=1)
AA = 1<<10; // authoritative _AA = 1<<10; // authoritative
TC = 1<<9; // truncated _TC = 1<<9; // truncated
RD = 1<<8; // recursion desired _RD = 1<<8; // recursion desired
RA = 1<<7; // recursion available _RA = 1<<7; // recursion available
) )
// DNS queries. // DNS queries.
@ -188,7 +188,7 @@ export type DNS_RR_A struct {
} }
// Packing and unpacking. // _Packing and unpacking.
// //
// All the packers and unpackers take a (msg []byte, off int) // All the packers and unpackers take a (msg []byte, off int)
// and return (off1 int, ok bool). If they return ok==false, they // and return (off1 int, ok bool). If they return ok==false, they
@ -212,10 +212,10 @@ var rr_mk = map[int]*()DNS_RR {
DNS_TypeA: func() DNS_RR { return new(DNS_RR_A) }, DNS_TypeA: func() DNS_RR { return new(DNS_RR_A) },
} }
// Pack a domain name s into msg[off:]. // _Pack a domain name s into msg[off:].
// Domain names are a sequence of counted strings // Domain names are a sequence of counted strings
// split at the dots. They end with a zero-length string. // split at the dots. They end with a zero-length string.
func PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) { func _PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
// Add trailing dot to canonicalize name. // Add trailing dot to canonicalize name.
if n := len(s); n == 0 || s[n-1] != '.' { if n := len(s); n == 0 || s[n-1] != '.' {
s += "."; s += ".";
@ -251,7 +251,7 @@ func PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
return off, true return off, true
} }
// Unpack a domain name. // _Unpack a domain name.
// In addition to the simple sequences of counted strings above, // In addition to the simple sequences of counted strings above,
// domain names are allowed to refer to strings elsewhere in the // domain names are allowed to refer to strings elsewhere in the
// packet, to avoid repeating common suffixes when returning // packet, to avoid repeating common suffixes when returning
@ -264,7 +264,7 @@ func PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
// which is where the next record will start. // which is where the next record will start.
// In theory, the pointers are only allowed to jump backward. // In theory, the pointers are only allowed to jump backward.
// We let them jump anywhere and stop jumping after a while. // We let them jump anywhere and stop jumping after a while.
func UnpackDomainName(msg []byte, off int) (s string, off1 int, ok bool) { func _UnpackDomainName(msg []byte, off int) (s string, off1 int, ok bool) {
s = ""; s = "";
ptr := 0; // number of pointers followed ptr := 0; // number of pointers followed
Loop: Loop:
@ -315,9 +315,9 @@ Loop:
return s, off1, true return s, off1, true
} }
// Pack a reflect.StructValue into msg. Struct members can only be uint16, uint32, string, // _Pack a reflect.StructValue into msg. Struct members can only be uint16, uint32, string,
// and other (often anonymous) structs. // and other (often anonymous) structs.
func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) { func _PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) {
for i := 0; i < val.Len(); i++ { for i := 0; i < val.Len(); i++ {
fld := val.Field(i); fld := val.Field(i);
name, typ, tag, xxx := val.Type().(reflect.StructType).Field(i); name, typ, tag, xxx := val.Type().(reflect.StructType).Field(i);
@ -326,7 +326,7 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type()); fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type());
return len(msg), false; return len(msg), false;
case reflect.StructKind: case reflect.StructKind:
off, ok = PackStructValue(fld.(reflect.StructValue), msg, off); off, ok = _PackStructValue(fld.(reflect.StructValue), msg, off);
case reflect.Uint16Kind: case reflect.Uint16Kind:
i := fld.(reflect.Uint16Value).Get(); i := fld.(reflect.Uint16Value).Get();
if off+2 > len(msg) { if off+2 > len(msg) {
@ -354,7 +354,7 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok
fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag); fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag);
return len(msg), false; return len(msg), false;
case "domain-name": case "domain-name":
off, ok = PackDomainName(s, msg, off); off, ok = _PackDomainName(s, msg, off);
if !ok { if !ok {
return len(msg), false return len(msg), false
} }
@ -375,15 +375,15 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok
return off, true return off, true
} }
func PackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { func _PackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue); val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue);
off, ok = PackStructValue(val, msg, off); off, ok = _PackStructValue(val, msg, off);
return off, ok return off, ok
} }
// Unpack a reflect.StructValue from msg. // _Unpack a reflect.StructValue from msg.
// Same restrictions as PackStructValue. // Same restrictions as _PackStructValue.
func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) { func _UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) {
for i := 0; i < val.Len(); i++ { for i := 0; i < val.Len(); i++ {
name, typ, tag, xxx := val.Type().(reflect.StructType).Field(i); name, typ, tag, xxx := val.Type().(reflect.StructType).Field(i);
fld := val.Field(i); fld := val.Field(i);
@ -392,7 +392,7 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int,
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type()); fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type());
return len(msg), false; return len(msg), false;
case reflect.StructKind: case reflect.StructKind:
off, ok = UnpackStructValue(fld.(reflect.StructValue), msg, off); off, ok = _UnpackStructValue(fld.(reflect.StructValue), msg, off);
case reflect.Uint16Kind: case reflect.Uint16Kind:
if off+2 > len(msg) { if off+2 > len(msg) {
return len(msg), false return len(msg), false
@ -414,7 +414,7 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int,
fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag); fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag);
return len(msg), false; return len(msg), false;
case "domain-name": case "domain-name":
s, off, ok = UnpackDomainName(msg, off); s, off, ok = _UnpackDomainName(msg, off);
if !ok { if !ok {
return len(msg), false return len(msg), false
} }
@ -437,9 +437,9 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int,
return off, true return off, true
} }
func UnpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { func _UnpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue); val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue);
off, ok = UnpackStructValue(val, msg, off); off, ok = _UnpackStructValue(val, msg, off);
return off, ok return off, ok
} }
@ -447,7 +447,7 @@ func UnpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
// Doesn't care about the string tag "domain-name", // Doesn't care about the string tag "domain-name",
// but does look for an "ipv4" tag on uint32 variables, // but does look for an "ipv4" tag on uint32 variables,
// printing them as IP addresses. // printing them as IP addresses.
func PrintStructValue(val reflect.StructValue) string { func _PrintStructValue(val reflect.StructValue) string {
s := "{"; s := "{";
for i := 0; i < val.Len(); i++ { for i := 0; i < val.Len(); i++ {
if i > 0 { if i > 0 {
@ -461,7 +461,7 @@ func PrintStructValue(val reflect.StructValue) string {
kind := fld.Kind(); kind := fld.Kind();
switch { switch {
case kind == reflect.StructKind: case kind == reflect.StructKind:
s += PrintStructValue(fld.(reflect.StructValue)); s += _PrintStructValue(fld.(reflect.StructValue));
case kind == reflect.Uint32Kind && tag == "ipv4": case kind == reflect.Uint32Kind && tag == "ipv4":
i := fld.(reflect.Uint32Value).Get(); i := fld.(reflect.Uint32Value).Get();
s += fmt.Sprintf("%d.%d.%d.%d", (i>>24)&0xFF, (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF); s += fmt.Sprintf("%d.%d.%d.%d", (i>>24)&0xFF, (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF);
@ -473,37 +473,37 @@ func PrintStructValue(val reflect.StructValue) string {
return s; return s;
} }
func PrintStruct(any interface{}) string { func _PrintStruct(any interface{}) string {
val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue); val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue);
s := PrintStructValue(val); s := _PrintStructValue(val);
return s return s
} }
// Resource record packer. // Resource record packer.
func PackRR(rr DNS_RR, msg []byte, off int) (off2 int, ok bool) { func _PackRR(rr DNS_RR, msg []byte, off int) (off2 int, ok bool) {
var off1 int; var off1 int;
// pack twice, once to find end of header // pack twice, once to find end of header
// and again to find end of packet. // and again to find end of packet.
// a bit inefficient but this doesn't need to be fast. // a bit inefficient but this doesn't need to be fast.
// off1 is end of header // off1 is end of header
// off2 is end of rr // off2 is end of rr
off1, ok = PackStruct(rr.Header(), msg, off); off1, ok = _PackStruct(rr.Header(), msg, off);
off2, ok = PackStruct(rr, msg, off); off2, ok = _PackStruct(rr, msg, off);
if !ok { if !ok {
return len(msg), false return len(msg), false
} }
// pack a third time; redo header with correct data length // pack a third time; redo header with correct data length
rr.Header().rdlength = uint16(off2 - off1); rr.Header().rdlength = uint16(off2 - off1);
PackStruct(rr.Header(), msg, off); _PackStruct(rr.Header(), msg, off);
return off2, true return off2, true
} }
// Resource record unpacker. // Resource record unpacker.
func UnpackRR(msg []byte, off int) (rr DNS_RR, off1 int, ok bool) { func _UnpackRR(msg []byte, off int) (rr DNS_RR, off1 int, ok bool) {
// unpack just the header, to find the rr type and length // unpack just the header, to find the rr type and length
var h DNS_RR_Header; var h DNS_RR_Header;
off0 := off; off0 := off;
if off, ok = UnpackStruct(&h, msg, off); !ok { if off, ok = _UnpackStruct(&h, msg, off); !ok {
return nil, len(msg), false return nil, len(msg), false
} }
end := off+int(h.rdlength); end := off+int(h.rdlength);
@ -515,7 +515,7 @@ func UnpackRR(msg []byte, off int) (rr DNS_RR, off1 int, ok bool) {
return &h, end, true return &h, end, true
} }
rr = mk(); rr = mk();
off, ok = UnpackStruct(rr, msg, off0); off, ok = _UnpackStruct(rr, msg, off0);
if off != end { if off != end {
return &h, end, true return &h, end, true
} }
@ -526,7 +526,7 @@ func UnpackRR(msg []byte, off int) (rr DNS_RR, off1 int, ok bool) {
// A manually-unpacked version of (id, bits). // A manually-unpacked version of (id, bits).
// This is in its own struct for easy printing. // This is in its own struct for easy printing.
type DNS_Msg_Top struct { type _DNS_Msg_Top struct {
id uint16; id uint16;
response bool; response bool;
opcode int; opcode int;
@ -538,7 +538,7 @@ type DNS_Msg_Top struct {
} }
export type DNS_Msg struct { export type DNS_Msg struct {
DNS_Msg_Top; _DNS_Msg_Top;
question []DNS_Question; question []DNS_Question;
answer []DNS_RR; answer []DNS_RR;
ns []DNS_RR; ns []DNS_RR;
@ -547,25 +547,25 @@ export type DNS_Msg struct {
func (dns *DNS_Msg) Pack() (msg []byte, ok bool) { func (dns *DNS_Msg) Pack() (msg []byte, ok bool) {
var dh DNS_Header; var dh _DNS_Header;
// Convert convenient DNS_Msg into wire-like DNS_Header. // Convert convenient DNS_Msg into wire-like _DNS_Header.
dh.id = dns.id; dh.id = dns.id;
dh.bits = uint16(dns.opcode)<<11 | uint16(dns.rcode); dh.bits = uint16(dns.opcode)<<11 | uint16(dns.rcode);
if dns.recursion_available { if dns.recursion_available {
dh.bits |= RA; dh.bits |= _RA;
} }
if dns.recursion_desired { if dns.recursion_desired {
dh.bits |= RD; dh.bits |= _RD;
} }
if dns.truncated { if dns.truncated {
dh.bits |= TC; dh.bits |= _TC;
} }
if dns.authoritative { if dns.authoritative {
dh.bits |= AA; dh.bits |= _AA;
} }
if dns.response { if dns.response {
dh.bits |= QR; dh.bits |= _QR;
} }
// Prepare variable sized arrays. // Prepare variable sized arrays.
@ -584,20 +584,20 @@ func (dns *DNS_Msg) Pack() (msg []byte, ok bool) {
// big enough to hurt the allocator. // big enough to hurt the allocator.
msg = make([]byte, 2000); msg = make([]byte, 2000);
// Pack it in: header and then the pieces. // _Pack it in: header and then the pieces.
off := 0; off := 0;
off, ok = PackStruct(&dh, msg, off); off, ok = _PackStruct(&dh, msg, off);
for i := 0; i < len(question); i++ { for i := 0; i < len(question); i++ {
off, ok = PackStruct(&question[i], msg, off); off, ok = _PackStruct(&question[i], msg, off);
} }
for i := 0; i < len(answer); i++ { for i := 0; i < len(answer); i++ {
off, ok = PackStruct(answer[i], msg, off); off, ok = _PackStruct(answer[i], msg, off);
} }
for i := 0; i < len(ns); i++ { for i := 0; i < len(ns); i++ {
off, ok = PackStruct(ns[i], msg, off); off, ok = _PackStruct(ns[i], msg, off);
} }
for i := 0; i < len(extra); i++ { for i := 0; i < len(extra); i++ {
off, ok = PackStruct(extra[i], msg, off); off, ok = _PackStruct(extra[i], msg, off);
} }
if !ok { if !ok {
return nil, false return nil, false
@ -607,19 +607,19 @@ func (dns *DNS_Msg) Pack() (msg []byte, ok bool) {
func (dns *DNS_Msg) Unpack(msg []byte) bool { func (dns *DNS_Msg) Unpack(msg []byte) bool {
// Header. // Header.
var dh DNS_Header; var dh _DNS_Header;
off := 0; off := 0;
var ok bool; var ok bool;
if off, ok = UnpackStruct(&dh, msg, off); !ok { if off, ok = _UnpackStruct(&dh, msg, off); !ok {
return false return false
} }
dns.id = dh.id; dns.id = dh.id;
dns.response = (dh.bits & QR) != 0; dns.response = (dh.bits & _QR) != 0;
dns.opcode = int(dh.bits >> 11) & 0xF; dns.opcode = int(dh.bits >> 11) & 0xF;
dns.authoritative = (dh.bits & AA) != 0; dns.authoritative = (dh.bits & _AA) != 0;
dns.truncated = (dh.bits & TC) != 0; dns.truncated = (dh.bits & _TC) != 0;
dns.recursion_desired = (dh.bits & RD) != 0; dns.recursion_desired = (dh.bits & _RD) != 0;
dns.recursion_available = (dh.bits & RA) != 0; dns.recursion_available = (dh.bits & _RA) != 0;
dns.rcode = int(dh.bits & 0xF); dns.rcode = int(dh.bits & 0xF);
// Arrays. // Arrays.
@ -629,16 +629,16 @@ func (dns *DNS_Msg) Unpack(msg []byte) bool {
dns.extra = make([]DNS_RR, dh.arcount); dns.extra = make([]DNS_RR, dh.arcount);
for i := 0; i < len(dns.question); i++ { for i := 0; i < len(dns.question); i++ {
off, ok = UnpackStruct(&dns.question[i], msg, off); off, ok = _UnpackStruct(&dns.question[i], msg, off);
} }
for i := 0; i < len(dns.answer); i++ { for i := 0; i < len(dns.answer); i++ {
dns.answer[i], off, ok = UnpackRR(msg, off); dns.answer[i], off, ok = _UnpackRR(msg, off);
} }
for i := 0; i < len(dns.ns); i++ { for i := 0; i < len(dns.ns); i++ {
dns.ns[i], off, ok = UnpackRR(msg, off); dns.ns[i], off, ok = _UnpackRR(msg, off);
} }
for i := 0; i < len(dns.extra); i++ { for i := 0; i < len(dns.extra); i++ {
dns.extra[i], off, ok = UnpackRR(msg, off); dns.extra[i], off, ok = _UnpackRR(msg, off);
} }
if !ok { if !ok {
return false return false
@ -650,29 +650,29 @@ func (dns *DNS_Msg) Unpack(msg []byte) bool {
} }
func (dns *DNS_Msg) String() string { func (dns *DNS_Msg) String() string {
s := "DNS: "+PrintStruct(&dns.DNS_Msg_Top)+"\n"; s := "DNS: "+_PrintStruct(&dns._DNS_Msg_Top)+"\n";
if len(dns.question) > 0 { if len(dns.question) > 0 {
s += "-- Questions\n"; s += "-- Questions\n";
for i := 0; i < len(dns.question); i++ { for i := 0; i < len(dns.question); i++ {
s += PrintStruct(&dns.question[i])+"\n"; s += _PrintStruct(&dns.question[i])+"\n";
} }
} }
if len(dns.answer) > 0 { if len(dns.answer) > 0 {
s += "-- Answers\n"; s += "-- Answers\n";
for i := 0; i < len(dns.answer); i++ { for i := 0; i < len(dns.answer); i++ {
s += PrintStruct(dns.answer[i])+"\n"; s += _PrintStruct(dns.answer[i])+"\n";
} }
} }
if len(dns.ns) > 0 { if len(dns.ns) > 0 {
s += "-- Name servers\n"; s += "-- Name servers\n";
for i := 0; i < len(dns.ns); i++ { for i := 0; i < len(dns.ns); i++ {
s += PrintStruct(dns.ns[i])+"\n"; s += _PrintStruct(dns.ns[i])+"\n";
} }
} }
if len(dns.extra) > 0 { if len(dns.extra) > 0 {
s += "-- Extra\n"; s += "-- Extra\n";
for i := 0; i < len(dns.extra); i++ { for i := 0; i < len(dns.extra); i++ {
s += PrintStruct(dns.extra[i])+"\n"; s += _PrintStruct(dns.extra[i])+"\n";
} }
} }
return s; return s;

View file

@ -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 {
} }
// 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 {
// 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 {
// 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) {
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) {
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 {
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() {
} 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() {
} }
} }
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) {
// 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);

View file

@ -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 {
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() {
} }
// 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 {
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 {
// 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 {
// 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 {
// 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 {
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 {
} }
// 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 {
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 {
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 {
// * 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 {
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 {
// 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 {
} }
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)
} }

View file

@ -18,14 +18,14 @@ export var (
UnknownHost = os.NewError("unknown host"); UnknownHost = os.NewError("unknown host");
DNS_Error = os.NewError("dns error looking up host"); DNS_Error = os.NewError("dns error looking up host");
UnknownPort = os.NewError("unknown port"); UnknownPort = os.NewError("unknown port");
UnknownSocketFamily = os.NewError("unknown socket family"); Unknown_SocketFamily = os.NewError("unknown socket family");
) )
export func LookupHost(name string) (name1 string, addrs []string, err *os.Error) export func LookupHost(name string) (name1 string, addrs []string, err *os.Error)
// Split "host:port" into "host" and "port". // Split "host:port" into "host" and "port".
// Host cannot contain colons unless it is bracketed. // Host cannot contain colons unless it is bracketed.
func SplitHostPort(hostport string) (host, port string, err *os.Error) { func _SplitHostPort(hostport string) (host, port string, err *os.Error) {
// The port starts after the last colon. // The port starts after the last colon.
var i int; var i int;
for i = len(hostport)-1; i >= 0; i-- { for i = len(hostport)-1; i >= 0; i-- {
@ -45,7 +45,7 @@ func SplitHostPort(hostport string) (host, port string, err *os.Error) {
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 {
return "", "", BadAddress return "", "", BadAddress
} }
} }
@ -54,9 +54,9 @@ func SplitHostPort(hostport string) (host, port string, err *os.Error) {
// Join "host" and "port" into "host:port". // Join "host" and "port" into "host:port".
// If host contains colons, will join into "[host]:port". // If host contains colons, will join into "[host]:port".
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
@ -65,9 +65,9 @@ func JoinHostPort(host, port string) string {
// Convert "host:port" into IP address and port. // Convert "host:port" into IP address and port.
// For now, host and port must be numeric literals. // For now, host and port must be numeric literals.
// Eventually, we'll have name resolution. // Eventually, we'll have name resolution.
func HostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Error) { func _HostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Error) {
var host, port string; var host, port string;
host, port, err = SplitHostPort(hostport); host, port, err = _SplitHostPort(hostport);
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -101,7 +101,7 @@ func HostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Err
} }
} }
p, i, ok := Dtoi(port, 0); p, i, ok := _Dtoi(port, 0);
if !ok || i != len(port) { if !ok || i != len(port) {
p, ok = LookupPort(net, port); p, ok = LookupPort(net, port);
if !ok { if !ok {
@ -116,7 +116,7 @@ func HostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Err
} }
// Convert socket address into "host:port". // Convert socket address into "host:port".
func SockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error) { func _SockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error) {
switch sa.family { switch sa.family {
case syscall.AF_INET, syscall.AF_INET6: case syscall.AF_INET, syscall.AF_INET6:
addr, port, e := SockaddrToIP(sa); addr, port, e := SockaddrToIP(sa);
@ -124,9 +124,9 @@ func SockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error) {
return "", e return "", e
} }
host := IPToString(addr); host := IPToString(addr);
return JoinHostPort(host, strconv.Itoa(port)), nil; return _JoinHostPort(host, strconv.Itoa(port)), nil;
default: default:
return "", UnknownSocketFamily return "", Unknown_SocketFamily
} }
return "", nil // not reached return "", nil // not reached
} }
@ -139,8 +139,8 @@ func boolint(b bool) int {
return 0 return 0
} }
// Generic Socket creation. // Generic _Socket creation.
func Socket(f, p, t int64, la, ra *syscall.Sockaddr) (fd *FD, err *os.Error) { func _Socket(f, p, t int64, la, ra *syscall.Sockaddr) (fd *FD, err *os.Error) {
s, e := syscall.socket(f, p, t); s, e := syscall.socket(f, p, t);
if e != 0 { if e != 0 {
return nil, os.ErrnoToError(e) return nil, os.ErrnoToError(e)
@ -177,29 +177,29 @@ func Socket(f, p, t int64, la, ra *syscall.Sockaddr) (fd *FD, err *os.Error) {
// Generic implementation of Conn interface; not exported. // Generic implementation of Conn interface; not exported.
type ConnBase struct { type _ConnBase struct {
fd *FD; fd *FD;
raddr string; raddr string;
} }
func (c *ConnBase) FD() int64 { func (c *_ConnBase) FD() int64 {
if c == nil || c.fd == nil { if c == nil || c.fd == nil {
return -1 return -1
} }
return c.fd.fd return c.fd.fd
} }
func (c *ConnBase) Read(b []byte) (n int, err *os.Error) { func (c *_ConnBase) Read(b []byte) (n int, err *os.Error) {
n, err = c.fd.Read(b); n, err = c.fd.Read(b);
return n, err return n, err
} }
func (c *ConnBase) Write(b []byte) (n int, err *os.Error) { func (c *_ConnBase) Write(b []byte) (n int, err *os.Error) {
n, err = c.fd.Write(b); n, err = c.fd.Write(b);
return n, err return n, err
} }
func (c *ConnBase) ReadFrom(b []byte) (n int, raddr string, err *os.Error) { func (c *_ConnBase) ReadFrom(b []byte) (n int, raddr string, err *os.Error) {
if c == nil { if c == nil {
return -1, "", os.EINVAL return -1, "", os.EINVAL
} }
@ -207,7 +207,7 @@ func (c *ConnBase) ReadFrom(b []byte) (n int, raddr string, err *os.Error) {
return n, c.raddr, err return n, c.raddr, err
} }
func (c *ConnBase) WriteTo(raddr string, b []byte) (n int, err *os.Error) { func (c *_ConnBase) WriteTo(raddr string, b []byte) (n int, err *os.Error) {
if c == nil { if c == nil {
return -1, os.EINVAL return -1, os.EINVAL
} }
@ -218,7 +218,7 @@ func (c *ConnBase) WriteTo(raddr string, b []byte) (n int, err *os.Error) {
return n, err return n, err
} }
func (c *ConnBase) Close() *os.Error { func (c *_ConnBase) Close() *os.Error {
if c == nil { if c == nil {
return os.EINVAL return os.EINVAL
} }
@ -234,47 +234,47 @@ func setsockopt_tv(fd, level, opt int64, nsec int64) *os.Error {
return os.ErrnoToError(syscall.setsockopt_tv(fd, level, opt, nsec)); return os.ErrnoToError(syscall.setsockopt_tv(fd, level, opt, nsec));
} }
func (c *ConnBase) SetReadBuffer(bytes int) *os.Error { func (c *_ConnBase) SetReadBuffer(bytes int) *os.Error {
return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes); return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes);
} }
func (c *ConnBase) SetWriteBuffer(bytes int) *os.Error { func (c *_ConnBase) SetWriteBuffer(bytes int) *os.Error {
return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes); return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes);
} }
func (c *ConnBase) SetReadTimeout(nsec int64) *os.Error { func (c *_ConnBase) SetReadTimeout(nsec int64) *os.Error {
return setsockopt_tv(c.FD(), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, nsec); return setsockopt_tv(c.FD(), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, nsec);
} }
func (c *ConnBase) SetWriteTimeout(nsec int64) *os.Error { func (c *_ConnBase) SetWriteTimeout(nsec int64) *os.Error {
return setsockopt_tv(c.FD(), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, nsec); return setsockopt_tv(c.FD(), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, nsec);
} }
func (c *ConnBase) SetTimeout(nsec int64) *os.Error { func (c *_ConnBase) SetTimeout(nsec int64) *os.Error {
if e := c.SetReadTimeout(nsec); e != nil { if e := c.SetReadTimeout(nsec); e != nil {
return e return e
} }
return c.SetWriteTimeout(nsec) return c.SetWriteTimeout(nsec)
} }
func (c *ConnBase) SetReuseAddr(reuse bool) *os.Error { func (c *_ConnBase) SetReuseAddr(reuse bool) *os.Error {
return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, boolint(reuse)); return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, boolint(reuse));
} }
func (c *ConnBase) BindToDevice(dev string) *os.Error { func (c *_ConnBase) BindToDevice(dev string) *os.Error {
// TODO: call setsockopt with null-terminated string pointer // TODO: call setsockopt with null-terminated string pointer
return os.EINVAL return os.EINVAL
} }
func (c *ConnBase) SetDontRoute(dontroute bool) *os.Error { func (c *_ConnBase) SetDontRoute(dontroute bool) *os.Error {
return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_DONTROUTE, boolint(dontroute)); return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_DONTROUTE, boolint(dontroute));
} }
func (c *ConnBase) SetKeepAlive(keepalive bool) *os.Error { func (c *_ConnBase) SetKeepAlive(keepalive bool) *os.Error {
return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive)); return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive));
} }
func (c *ConnBase) SetLinger(sec int) *os.Error { func (c *_ConnBase) SetLinger(sec int) *os.Error {
e := syscall.setsockopt_linger(c.FD(), syscall.SOL_SOCKET, syscall.SO_LINGER, sec); e := syscall.setsockopt_linger(c.FD(), syscall.SOL_SOCKET, syscall.SO_LINGER, sec);
return os.ErrnoToError(e); return os.ErrnoToError(e);
} }
@ -287,23 +287,23 @@ func (c *ConnBase) SetLinger(sec int) *os.Error {
// understands IPv6, it's okay to pass IPv4 addresses to the IPv6 // understands IPv6, it's okay to pass IPv4 addresses to the IPv6
// interface. That simplifies our code and is most general. // interface. That simplifies our code and is most general.
// If we need to build on a system without IPv6 support, setting // If we need to build on a system without IPv6 support, setting
// PreferIPv4 here should fall back to the IPv4 socket interface when possible. // _PreferIPv4 here should fall back to the IPv4 socket interface when possible.
const PreferIPv4 = false const _PreferIPv4 = false
func InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD, err *os.Error) { func _InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD, err *os.Error) {
// Parse addresses (unless they are empty). // Parse addresses (unless they are empty).
var lip, rip []byte; var lip, rip []byte;
var lport, rport int; var lport, rport int;
var lerr, rerr *os.Error; var lerr, rerr *os.Error;
if laddr != "" { if laddr != "" {
lip, lport, lerr = HostPortToIP(net, laddr, mode); lip, lport, lerr = _HostPortToIP(net, laddr, mode);
if lerr != nil { if lerr != nil {
return nil, lerr return nil, lerr
} }
} }
if raddr != "" { if raddr != "" {
rip, rport, rerr = HostPortToIP(net, raddr, mode); rip, rport, rerr = _HostPortToIP(net, raddr, mode);
if rerr != nil { if rerr != nil {
return nil, rerr return nil, rerr
} }
@ -320,7 +320,7 @@ func InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD,
default: default:
// Otherwise, guess. // Otherwise, guess.
// If the addresses are IPv4 and we prefer IPv4, use 4; else 6. // If the addresses are IPv4 and we prefer IPv4, use 4; else 6.
if PreferIPv4 && ToIPv4(lip) != nil && ToIPv4(rip) != nil { if _PreferIPv4 && ToIPv4(lip) != nil && ToIPv4(rip) != nil {
vers = 4 vers = 4
} else { } else {
vers = 6 vers = 6
@ -351,7 +351,7 @@ func InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD,
} }
} }
fd, err = Socket(family, proto, 0, la, ra); fd, err = _Socket(family, proto, 0, la, ra);
return fd, err return fd, err
} }
@ -359,7 +359,7 @@ func InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD,
// TCP connections. // TCP connections.
export type ConnTCP struct { export type ConnTCP struct {
ConnBase _ConnBase
} }
func (c *ConnTCP) SetNoDelay(nodelay bool) *os.Error { func (c *ConnTCP) SetNoDelay(nodelay bool) *os.Error {
@ -369,7 +369,7 @@ func (c *ConnTCP) SetNoDelay(nodelay bool) *os.Error {
return setsockopt_int(c.FD(), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(nodelay)) return setsockopt_int(c.FD(), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(nodelay))
} }
func NewConnTCP(fd *FD, raddr string) *ConnTCP { func _NewConnTCP(fd *FD, raddr string) *ConnTCP {
c := new(ConnTCP); c := new(ConnTCP);
c.fd = fd; c.fd = fd;
c.raddr = raddr; c.raddr = raddr;
@ -381,11 +381,11 @@ export func DialTCP(net, laddr, raddr string) (c *ConnTCP, err *os.Error) {
if raddr == "" { if raddr == "" {
return nil, MissingAddress return nil, MissingAddress
} }
fd, e := InternetSocket(net, laddr, raddr, syscall.SOCK_STREAM, "dial"); fd, e := _InternetSocket(net, laddr, raddr, syscall.SOCK_STREAM, "dial");
if e != nil { if e != nil {
return nil, e return nil, e
} }
return NewConnTCP(fd, raddr), nil return _NewConnTCP(fd, raddr), nil
} }
@ -394,10 +394,10 @@ export func DialTCP(net, laddr, raddr string) (c *ConnTCP, err *os.Error) {
// TODO(rsc): UDP headers mode // TODO(rsc): UDP headers mode
export type ConnUDP struct { export type ConnUDP struct {
ConnBase _ConnBase
} }
func NewConnUDP(fd *FD, raddr string) *ConnUDP { func _NewConnUDP(fd *FD, raddr string) *ConnUDP {
c := new(ConnUDP); c := new(ConnUDP);
c.fd = fd; c.fd = fd;
c.raddr = raddr; c.raddr = raddr;
@ -408,11 +408,11 @@ export func DialUDP(net, laddr, raddr string) (c *ConnUDP, err *os.Error) {
if raddr == "" { if raddr == "" {
return nil, MissingAddress return nil, MissingAddress
} }
fd, e := InternetSocket(net, laddr, raddr, syscall.SOCK_DGRAM, "dial"); fd, e := _InternetSocket(net, laddr, raddr, syscall.SOCK_DGRAM, "dial");
if e != nil { if e != nil {
return nil, e return nil, e
} }
return NewConnUDP(fd, raddr), nil return _NewConnUDP(fd, raddr), nil
} }
@ -488,7 +488,7 @@ export type ListenerTCP struct {
} }
export func ListenTCP(net, laddr string) (l *ListenerTCP, err *os.Error) { export func ListenTCP(net, laddr string) (l *ListenerTCP, err *os.Error) {
fd, e := InternetSocket(net, laddr, "", syscall.SOCK_STREAM, "listen"); fd, e := _InternetSocket(net, laddr, "", syscall.SOCK_STREAM, "listen");
if e != nil { if e != nil {
return nil, e return nil, e
} }
@ -511,12 +511,12 @@ func (l *ListenerTCP) AcceptTCP() (c *ConnTCP, raddr string, err *os.Error) {
if e != nil { if e != nil {
return nil, "", e return nil, "", e
} }
raddr, err = SockaddrToHostPort(&sa); raddr, err = _SockaddrToHostPort(&sa);
if err != nil { if err != nil {
fd.Close(); fd.Close();
return nil, "", err return nil, "", err
} }
return NewConnTCP(fd, raddr), raddr, nil return _NewConnTCP(fd, raddr), raddr, nil
} }
func (l *ListenerTCP) Accept() (c Conn, raddr string, err *os.Error) { func (l *ListenerTCP) Accept() (c Conn, raddr string, err *os.Error) {

View file

@ -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) {
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) {
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 {
} }
// 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 {
} }
// 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 {
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) {
// 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) {
} else { } else {
break break
} }
if n >= Big { if n >= _Big {
return 0, i, false return 0, i, false
} }
} }

View file

@ -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) {
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;

View file

@ -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() {
} }
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":