79d303 | 2004-04-13 | Jeff Hungerford | |
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
2253ed | 2002-12-01 | Martin Nilsson | |
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
a20af6 | 2000-09-26 | Fredrik Hübinette (Hubbe) | |
|
a580e1 | 2000-09-27 | Fredrik Hübinette (Hubbe) | | #pike __REAL_VERSION__
|
189c79 | 2000-09-11 | Leif Stensson | |
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | constant NOERROR=0;
constant FORMERR=1;
constant SERVFAIL=2;
constant NXDOMAIN=3;
constant NOTIMPL=4;
constant NXRRSET=8;
constant QUERY=0;
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
enum ResourceClass
{
C_IN=1,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
C_CS=2,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
C_CH=3,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
C_HS=4,
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
C_ANY=255,
};
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
enum EntryType
{
T_A=1,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_NS=2,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_MD=3,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_MF=4,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_CNAME=5,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_SOA=6,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_MB=7,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_MG=8,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_MR=9,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_NULL=10,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_WKS=11,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_PTR=12,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_HINFO=13,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_MINFO=14,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_MX=15,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_TXT=16,
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_AAAA=28,
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
79d303 | 2004-04-13 | Jeff Hungerford | |
T_LOC=29,
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
T_SRV=33,
T_A6=38,
};
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | class protocol
{
string mklabel(string s)
{
|
ead972 | 2003-01-20 | Martin Nilsson | | if(sizeof(s)>63)
|
484a74 | 2002-03-09 | Martin Nilsson | | error("Too long component in domain name.\n");
|
ead972 | 2003-01-20 | Martin Nilsson | | return sprintf("%c%s",sizeof(s),s);
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
8be18a | 2000-04-12 | Marcus Comstedt | | static private string mkname(string|array(string) labels, int pos,
mapping(string:int) comp)
{
if(stringp(labels))
labels = labels/"."-({""});
if(!labels || !sizeof(labels))
return "\0";
string n = labels*".";
if(comp[n])
return sprintf("%2c", comp[n]|0xc000);
else {
if(pos<0x4000)
comp[n]=pos;
string l = mklabel(labels[0]);
|
ead972 | 2003-01-20 | Martin Nilsson | | return l + mkname(labels[1..], pos+sizeof(l), comp);
|
8be18a | 2000-04-12 | Marcus Comstedt | | }
}
|
e94d1a | 2003-04-23 | Marcus Comstedt | | static string make_raw_addr6(string addr6)
{
if(!addr6) return "\0"*16;
if(has_value(addr6, "::")) {
int parts = sizeof((addr6/":")-({""}));
if(has_value(addr6, ".")) parts++;
addr6 = replace(addr6, "::", ":"+"0:"*(8-parts));
sscanf(addr6, ":%s", addr6);
}
if(has_value(addr6, "."))
return sprintf("%2c%2c%2c%2c%2c%2c%c%c%c%c",
array_sscanf(addr6, "%x:%x:%x:%x:%x:%x:%x.%x.%x.%x"));
else
return sprintf("%@2c",
array_sscanf(addr6, "%x:%x:%x:%x:%x:%x:%x:%x"));
}
|
8be18a | 2000-04-12 | Marcus Comstedt | | static private string mkrdata(mapping entry, int pos, mapping(string:int) c)
{
switch(entry->type) {
case T_CNAME:
return mkname(entry->cname, pos, c);
case T_PTR:
return mkname(entry->ptr, pos, c);
case T_NS:
return mkname(entry->ns, pos, c);
case T_MD:
return mkname(entry->md, pos, c);
case T_MF:
return mkname(entry->mf, pos, c);
case T_MB:
return mkname(entry->mb, pos, c);
case T_MG:
return mkname(entry->mg, pos, c);
case T_MR:
return mkname(entry->mr, pos, c);
case T_MX:
return sprintf("%2c", entry->preference)+mkname(entry->mx, pos+2, c);
case T_HINFO:
|
ead972 | 2003-01-20 | Martin Nilsson | | return sprintf("%1c%s%1c%s", sizeof(entry->cpu||""), entry->cpu||"",
sizeof(entry->os||""), entry->os||"");
|
8be18a | 2000-04-12 | Marcus Comstedt | | case T_MINFO:
string rmailbx = mkname(entry->rmailbx, pos, c);
|
ead972 | 2003-01-20 | Martin Nilsson | | return rmailbx + mkname(entry->emailbx, pos+sizeof(rmailbx), c);
|
b6e344 | 2002-11-26 | H. William Welliver III | | case T_SRV:
return sprintf("%2c%2c%2c", entry->priority, entry->weight, entry->port) +
mkname(entry->target||"", pos+6, c);
|
8be18a | 2000-04-12 | Marcus Comstedt | | case T_A:
return sprintf("%@1c", (array(int))((entry->a||"0.0.0.0")/".")[0..3]);
|
d00400 | 2003-04-22 | Martin Nilsson | | case T_AAAA:
|
e94d1a | 2003-04-23 | Marcus Comstedt | | return make_raw_addr6(entry->aaaa);
|
8be18a | 2000-04-12 | Marcus Comstedt | | case T_SOA:
string mname = mkname(entry->mname, pos, c);
|
ead972 | 2003-01-20 | Martin Nilsson | | return mname + mkname(entry->rname, pos+sizeof(mname), c) +
|
8be18a | 2000-04-12 | Marcus Comstedt | | sprintf("%4c%4c%4c%4c%4c", entry->serial, entry->refresh,
entry->retry, entry->expire, entry->minimum);
case T_TXT:
return Array.map(stringp(entry->txt)? ({entry->txt}):(entry->txt||({})),
lambda(string t) {
|
ead972 | 2003-01-20 | Martin Nilsson | | return sprintf("%1c%s", sizeof(t), t);
|
8be18a | 2000-04-12 | Marcus Comstedt | | })*"";
|
79d303 | 2004-04-13 | Jeff Hungerford | | case T_LOC:
|
8be18a | 2000-04-12 | Marcus Comstedt | | default:
return "";
}
}
static private string encode_entries(array(mapping) entries, int pos,
mapping(string:int) comp)
{
string res="";
foreach(entries, mapping entry) {
string e = mkname(entry->name, pos, comp)+
sprintf("%2c%2c%4c", entry->type, entry->cl, entry->ttl);
|
ead972 | 2003-01-20 | Martin Nilsson | | pos += sizeof(e)+2;
|
8be18a | 2000-04-12 | Marcus Comstedt | | string rd = entry->rdata || mkrdata(entry, pos, comp);
|
ead972 | 2003-01-20 | Martin Nilsson | | res += e + sprintf("%2c", sizeof(rd)) + rd;
pos += sizeof(rd);
|
8be18a | 2000-04-12 | Marcus Comstedt | | }
return res;
}
string low_low_mkquery(mapping q)
{
array qd = q->qd && (arrayp(q->qd)? q->qd : ({q->qd}));
array an = q->an && (arrayp(q->an)? q->an : ({q->an}));
array ns = q->ns && (arrayp(q->ns)? q->ns : ({q->ns}));
array ar = q->ar && (arrayp(q->ar)? q->ar : ({q->ar}));
string r = sprintf("%2c%c%c%2c%2c%2c%2c", q->id,
((q->rd)&1)|(((q->tc)&1)<<1)|(((q->aa)&1)<<2)|
(((q->opcode)&15)<<3)|(((q->qr)&1)<<7),
((q->rcode)&15)|(((q->cd)&1)<<4)|(((q->ad)&1)<<5)|
(((q->ra)&1)<<7),
qd && sizeof(qd), an && sizeof(an),
ns && sizeof(ns), ar && sizeof(ar));
mapping(string:int) c = ([]);
if(qd)
foreach(qd, mapping _qd)
|
ead972 | 2003-01-20 | Martin Nilsson | | r += mkname(_qd->name, sizeof(r), c) +
|
8be18a | 2000-04-12 | Marcus Comstedt | | sprintf("%2c%2c", _qd->type, _qd->cl);
if(an)
|
ead972 | 2003-01-20 | Martin Nilsson | | r+=encode_entries(an, sizeof(r), c);
|
8be18a | 2000-04-12 | Marcus Comstedt | | if(ns)
|
ead972 | 2003-01-20 | Martin Nilsson | | r+=encode_entries(ns, sizeof(r), c);
|
8be18a | 2000-04-12 | Marcus Comstedt | | if(ar)
|
ead972 | 2003-01-20 | Martin Nilsson | | r+=encode_entries(ar, sizeof(r), c);
|
8be18a | 2000-04-12 | Marcus Comstedt | | return r;
}
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | string low_mkquery(int id,
string dname,
int cl,
int type)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
8be18a | 2000-04-12 | Marcus Comstedt | | return low_low_mkquery((["id":id, "rd":1,
"qd":(["name":dname, "cl":cl, "type":type])]));
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
278999 | 2002-12-01 | H. William Welliver III | |
|
8be18a | 2000-04-12 | Marcus Comstedt | | string mkquery(string|mapping dnameorquery, int|void cl, int|void type)
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
8be18a | 2000-04-12 | Marcus Comstedt | | if(mappingp(dnameorquery))
return low_low_mkquery(dnameorquery);
else
return low_mkquery(random(65536),dnameorquery,cl,type);
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | string decode_domain(string msg, array(int) n)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(string) domains=({});
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
int pos=n[0];
int next=-1;
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(string) ret=({});
|
b95656 | 1998-05-20 | Henrik Grubbström (Grubba) | | while(pos < sizeof(msg))
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
switch(int len=msg[pos])
{
case 0:
if(next==-1) next=pos+1;
n[0]=next;
return ret*".";
case 1..63:
pos+=len+1;
ret+=({msg[pos-len..pos-1]});
continue;
default:
|
5b31d6 | 2000-04-11 | Marcus Comstedt | | if((~len)&0xc0)
|
484a74 | 2002-03-09 | Martin Nilsson | | error("Invalid message compression mode.\n");
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | if(next==-1) next=pos+2;
pos=((len&63)<<8) + msg[pos+1];
continue;
}
break;
}
}
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | string decode_string(string s, array(int) next)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
int len=s[next[0]];
next[0]+=len+1;
return s[next[0]-len..next[0]-1];
}
|
79d303 | 2004-04-13 | Jeff Hungerford | | int decode_byte(string s, array(int) next)
{
return s[next[0]++];
}
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | int decode_short(string s, array(int) next)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
sscanf(s[next[0]..next[0]+1],"%2c",int ret);
next[0]+=2;
return ret;
}
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | int decode_int(string s, array(int) next)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
a8c3f2 | 2002-01-13 | Marcus Comstedt | | sscanf(s[next[0]..next[0]+3],"%4c",int ret);
next[0]+=4;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | return ret;
}
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array decode_entries(string s,int num, array(int) next)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(string) ret=({});
|
ead972 | 2003-01-20 | Martin Nilsson | | for(int e=0;e<num && next[0]<sizeof(s);e++)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
mapping m=([]);
m->name=decode_domain(s,next);
sscanf(s[next[0]..next[0]+10],
"%2c%2c%4c%2c",
m->type,m->cl,m->ttl,m->len);
next[0]+=10;
int tmp=next[0];
switch(m->type)
{
case T_CNAME:
m->cname=decode_domain(s,next);
break;
case T_PTR:
m->ptr=decode_domain(s,next);
break;
case T_NS:
m->ns=decode_domain(s,next);
break;
case T_MX:
m->preference=decode_short(s,next);
m->mx=decode_domain(s,next);
break;
case T_HINFO:
m->cpu=decode_string(s,next);
m->os=decode_string(s,next);
break;
|
b6e344 | 2002-11-26 | H. William Welliver III | | case T_SRV:
m->priority=decode_short(s,next);
m->weight=decode_short(s,next);
m->port=decode_short(s,next);
m->target=decode_domain(s,next);
array x=m->name/".";
if(x[0])
{
if(x[0][0..0]=="_")
m->service=x[0][1..];
else
m->service=x[0];
}
if(x[1])
{
if(x[1][0..0]=="_")
m->proto=x[1][1..];
else
m->proto=x[1];
}
if(x[2])
{
if(x[2][0..0]=="_")
x[2]=x[2][1..];
m->name=x[2..]*".";
}
m->ttl=decode_int(s,next);
break;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | case T_A:
m->a=sprintf("%{.%d%}",values(s[next[0]..next[0]+m->len-1]))[1..];
break;
|
d00400 | 2003-04-22 | Martin Nilsson | | case T_AAAA:
|
e94d1a | 2003-04-23 | Marcus Comstedt | | m->aaaa=sprintf("%{:%02X%02X%}",
|
29dd3e | 2003-04-22 | Martin Nilsson | | values(s[next[0]..next[0]+m->len-1])/2)[1..];
|
d00400 | 2003-04-22 | Martin Nilsson | | break;
|
79d303 | 2004-04-13 | Jeff Hungerford | | case T_LOC:
m->version = decode_byte(s,next);
if (m->version == 0)
{
int aByte;
aByte = decode_byte(s,next);
m->size = pow((aByte>>4)&0xf , aByte&0xf)/100.0;
aByte = decode_byte(s,next);
m->h_perc = pow((aByte>>4)&0xf , aByte&0xf)/100.0;
aByte = decode_byte(s,next);
m->v_perc = pow((aByte>>4)&0xf , aByte&0xf)/100.0;
m->lat = ((decode_int(s,next)-(2<<30))/3600000.0);
m->long = ((decode_int(s,next)-(2<<30))/3600000.0);
m->alt = ((decode_int(s,next)/100.0)-100000.0);
}
break;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | case T_SOA:
m->mname=decode_domain(s,next);
m->rname=decode_domain(s,next);
m->serial=decode_int(s,next);
m->refresh=decode_int(s,next);
m->retry=decode_int(s,next);
m->expire=decode_int(s,next);
m->minimum=decode_int(s,next);
break;
}
next[0]=tmp+m->len;
ret+=({m});
}
return ret;
}
mapping decode_res(string s)
{
mapping m=([]);
sscanf(s,"%2c%c%c%2c%2c%2c%2c",
m->id,
m->c1,
m->c2,
m->qdcount,
m->ancount,
m->nscount,
m->arcount);
|
5b31d6 | 2000-04-11 | Marcus Comstedt | | m->rd=m->c1&1;
m->tc=(m->c1>>1)&1;
m->aa=(m->c1>>2)&1;
m->opcode=(m->c1>>3)&15;
m->qr=(m->c1>>7)&1;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
5b31d6 | 2000-04-11 | Marcus Comstedt | | m->rcode=m->c2&15;
m->cd=(m->c2>>4)&1;
m->ad=(m->c2>>5)&1;
m->ra=(m->c2>>7)&1;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
ead972 | 2003-01-20 | Martin Nilsson | | m->length=sizeof(s);
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(string) tmp=({});
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | int e;
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(int) next=({12});
|
5b31d6 | 2000-04-11 | Marcus Comstedt | | m->qd = allocate(m->qdcount);
for(int i=0; i<m->qdcount; i++) {
m->qd[i]=(["name":decode_domain(s,next)]);
sscanf(s[next[0]..next[0]+3],"%2c%2c",m->qd[i]->type, m->qd[i]->cl);
next[0]+=4;
}
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | m->an=decode_entries(s,m->ancount,next);
m->ns=decode_entries(s,m->nscount,next);
m->ar=decode_entries(s,m->arcount,next);
return m;
}
};
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
|
8be18a | 2000-04-12 | Marcus Comstedt | | class server
{
inherit protocol;
inherit Stdio.UDP : udp;
static void send_reply(mapping r, mapping q, mapping m)
{
if(!r)
r = (["rcode":4]);
r->id = q->id;
r->qr = 1;
r->opcode = q->opcode;
r->rd = q->rd;
r->qd = r->qd || q->qd;
string s = mkquery(r);
udp::send(m->ip, m->port, s);
}
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
|
cbe8c9 | 2003-04-07 | Martin Nilsson | |
|
c534f6 | 2002-12-23 | Henrik Grubbström (Grubba) | |
static mapping reply_query(mapping query, mapping udp_data)
|
8be18a | 2000-04-12 | Marcus Comstedt | | {
return 0;
}
static void handle_query(mapping q, mapping m)
{
mapping r = reply_query(q, m);
send_reply(r, q, m);
}
static void handle_response(mapping r, mapping m)
{
}
static private void rec_data(mapping m)
{
mixed err;
mapping q;
if (err = catch {
q=decode_res(m->data);
}) {
|
0bd6f5 | 2003-10-21 | Martin Nilsson | | werror("DNS: Failed to read UDP packet.\n%s\n",
describe_backtrace(err));
|
8be18a | 2000-04-12 | Marcus Comstedt | | if(m && m->data && sizeof(m->data)>=2)
send_reply((["rcode":1]),
mkmapping(({"id"}), array_sscanf(m->data, "%2c")), m);
}
else if(q->qr)
handle_response(q, m);
else
handle_query(q, m);
}
void create(int|void port)
{
if(!port)
port = 53;
if(!udp::bind(port))
|
484a74 | 2002-03-09 | Martin Nilsson | | error("DNS: failed to bind port "+port+".\n");
|
8be18a | 2000-04-12 | Marcus Comstedt | | udp::set_read_callback(rec_data);
}
}
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | #define RETRIES 12
#define RETRY_DELAY 5
|
7dc316 | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | class client
{
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | inherit protocol;
|
667644 | 1998-01-25 | Henrik Grubbström (Grubba) | | static private int is_ip(string ip)
{
|
18096f | 2003-08-22 | Martin Nilsson | |
return (replace(ip, "0123456789."/1, allocate(11,"")) == "");
|
667644 | 1998-01-25 | Henrik Grubbström (Grubba) | | }
static private mapping etc_hosts;
|
37ffea | 1998-04-06 | Fredrik Hübinette (Hubbe) | | #ifdef __NT__
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | array(string) get_tcpip_param(string val, void|string fallbackvalue)
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | {
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | array(string) res = ({});
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | foreach(({
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
"SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP"
}),string key)
|
37ffea | 1998-04-06 | Fredrik Hübinette (Hubbe) | | {
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | catch {
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | res += ({ RegGetValue(HKEY_LOCAL_MACHINE, key, val) });
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | };
|
37ffea | 1998-04-06 | Fredrik Hübinette (Hubbe) | | }
|
63ecf6 | 2000-06-28 | Fredrik Hübinette (Hubbe) | |
#if constant(RegGetKeyNames)
|
b3e440 | 2001-03-14 | Henrik Grubbström (Grubba) | |
catch {
foreach(RegGetKeyNames(HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\"
"Parameters\\Interfaces"), string key)
|
63ecf6 | 2000-06-28 | Fredrik Hübinette (Hubbe) | | {
catch {
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | res += ({ RegGetValue(HKEY_LOCAL_MACHINE,
|
3097d9 | 2001-02-05 | Tomas Nilsson | | "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\"
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | "Parameters\\Interfaces\\" + key, val) });
|
63ecf6 | 2000-06-28 | Fredrik Hübinette (Hubbe) | | };
}
|
b3e440 | 2001-03-14 | Henrik Grubbström (Grubba) | | };
|
83e190 | 2000-06-28 | Fredrik Hübinette (Hubbe) | | #endif
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | return sizeof(res) ? res : ({ fallbackvalue });
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | }
|
37ffea | 1998-04-06 | Fredrik Hübinette (Hubbe) | | #endif
|
667644 | 1998-01-25 | Henrik Grubbström (Grubba) | | static private string match_etc_hosts(string host)
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | {
if (!etc_hosts) {
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | array(string) paths;
|
9f6f4d | 1998-02-27 | Fredrik Hübinette (Hubbe) | | #ifdef __NT__
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | paths = get_tcpip_param("DataBasePath");
|
9f6f4d | 1998-02-27 | Fredrik Hübinette (Hubbe) | | #else
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | paths = ({ "/etc", "/amitcp/db" });
|
9f6f4d | 1998-02-27 | Fredrik Hübinette (Hubbe) | | #endif
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | |
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | etc_hosts = ([ "localhost":"127.0.0.1" ]);
|
37ffea | 1998-04-06 | Fredrik Hübinette (Hubbe) | |
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | foreach(paths, string path) {
string raw = Stdio.read_file(path + "/hosts");
if (raw && sizeof(raw)) {
foreach(raw/"\n"-({""}), string line) {
line = lower_case(replace((line/"#")[0], "\t", " "));
array arr = (line/" ") - ({ "" });
|
37ffea | 1998-04-06 | Fredrik Hübinette (Hubbe) | |
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | if (sizeof(arr) > 1) {
if (is_ip(arr[0])) {
foreach(arr[1..], string name) {
etc_hosts[name] = arr[0];
}
} else {
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | }
|
667644 | 1998-01-25 | Henrik Grubbström (Grubba) | | }
}
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | } else {
|
667644 | 1998-01-25 | Henrik Grubbström (Grubba) | | }
}
}
|
18096f | 2003-08-22 | Martin Nilsson | | return etc_hosts[lower_case(host)];
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | }
|
7dc316 | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
667644 | 1998-01-25 | Henrik Grubbström (Grubba) | |
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | array(string) nameservers = ({});
|
7f8292 | 1997-12-10 | David Hedbor | | array domains = ({});
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | void create(void|string|array(string) server, void|int|array(string) domain)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
if(!server)
{
|
d35779 | 1999-04-14 | Henrik Grubbström (Grubba) | | #if __NT__
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | domains = get_tcpip_param("Domain", "") +
get_tcpip_param("DhcpDomain", "") +
|
3c287a | 2000-12-08 | Henrik Grubbström (Grubba) | | map(get_tcpip_param("SearchList", ""),
lambda(string s) {
return replace(s, " ", ",")/",";
}) * ({});
|
89249d | 2000-08-22 | Johan Schön | |
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | nameservers = map(get_tcpip_param("NameServer", "") +
|
3c287a | 2000-12-08 | Henrik Grubbström (Grubba) | | get_tcpip_param("DhcpNameServer", ""),
lambda(string s) {
return replace(s, " ", ",")/",";
}) * ({});
|
9f6f4d | 1998-02-27 | Fredrik Hübinette (Hubbe) | | #else
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | string domain;
|
9f7040 | 1998-11-13 | Marcus Comstedt | | string resolv_conf;
|
8fe014 | 2000-10-17 | Henrik Grubbström (Grubba) | | foreach(({"/etc/resolv.conf", "/amitcp/db/resolv.conf"}),
string resolv_loc)
|
9f7040 | 1998-11-13 | Marcus Comstedt | | if ((resolv_conf = Stdio.read_file(resolv_loc)))
break;
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | |
if (!resolv_conf) {
|
7b8084 | 2002-09-17 | Martin Nilsson | | if (System->get_netinfo_property) {
|
d33477 | 2001-04-09 | Jonas Wallden | |
if (nameservers =
|
7b8084 | 2002-09-17 | Martin Nilsson | | System->get_netinfo_property(".",
|
d33477 | 2001-04-09 | Jonas Wallden | | "/locations/resolver",
"nameserver")) {
|
6241d6 | 2001-09-17 | Henrik Grubbström (Grubba) | | nameservers = map(nameservers, `-, "\n");
|
d33477 | 2001-04-09 | Jonas Wallden | | }
|
7b8084 | 2002-09-17 | Martin Nilsson | | if (domains = System->get_netinfo_property(".",
|
d33477 | 2001-04-09 | Jonas Wallden | | "/locations/resolver",
"domain")) {
|
6241d6 | 2001-09-17 | Henrik Grubbström (Grubba) | | domains = map(domains, `-, "\n");
|
d33477 | 2001-04-09 | Jonas Wallden | | }
} else {
resolv_conf = "nameserver 127.0.0.1";
}
|
8fe014 | 2000-10-17 | Henrik Grubbström (Grubba) | | #if 0
|
484a74 | 2002-03-09 | Martin Nilsson | | error( "Protocols.DNS.client(): No /etc/resolv.conf!\n" );
|
8fe014 | 2000-10-17 | Henrik Grubbström (Grubba) | | #endif /* 0 */
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | }
|
d33477 | 2001-04-09 | Jonas Wallden | | if (resolv_conf)
foreach(resolv_conf/"\n", string line)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
d33477 | 2001-04-09 | Jonas Wallden | | string rest;
sscanf(line,"%s#",line);
sscanf(line,"%*[\r \t]%s",line);
line=reverse(line);
sscanf(line,"%*[\r \t]%s",line);
line=reverse(line);
sscanf(line,"%s%*[ \t]%s",line,rest);
switch(line)
{
case "domain":
domain = sizeof(rest) && rest;
break;
case "search":
rest = replace(rest, "\t", " ");
domains += ((rest/" ") - ({""}));
break;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
d33477 | 2001-04-09 | Jonas Wallden | | case "nameserver":
if (!is_ip(rest)) {
string host = rest;
if (!(rest = match_etc_hosts(host))) {
|
0bd6f5 | 2003-10-21 | Martin Nilsson | | werror("Protocols.DNS.client(): "
"Can't resolv nameserver \"%s\"\n", host);
|
d33477 | 2001-04-09 | Jonas Wallden | | break;
}
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | }
|
d33477 | 2001-04-09 | Jonas Wallden | | if (sizeof(rest)) {
nameservers += ({ rest });
}
break;
}
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
9381ae | 2001-04-09 | Marcus Comstedt | |
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | if(domain)
domains = ({ domain }) + domains;
|
9f6f4d | 1998-02-27 | Fredrik Hübinette (Hubbe) | | #endif
|
e17773 | 2000-12-07 | Henrik Grubbström (Grubba) | | nameservers -= ({ "" });
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | if (!sizeof(nameservers)) {
|
d35779 | 1999-04-14 | Henrik Grubbström (Grubba) | |
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | nameservers = ({ "127.0.0.1" });
}
|
dc01b5 | 1999-06-01 | Henrik Grubbström (Grubba) | | domains -= ({ "" });
|
b9b6ec | 1998-01-25 | Henrik Grubbström (Grubba) | | domains = Array.map(domains, lambda(string d) {
if (d[-1] == '.') {
return d[..sizeof(d)-2];
}
return d;
});
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | | }
else
{
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | if(arrayp(server))
|
940cb7 | 1998-07-17 | Henrik Grubbström (Grubba) | | nameservers = server;
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | else
|
940cb7 | 1998-07-17 | Henrik Grubbström (Grubba) | | nameservers = ({ server });
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | |
if(arrayp(domain))
domains = domain;
else
|
522de1 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | if(stringp(domain))
|
4f3ea4 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | domains = ({ domain });
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
}
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
278999 | 2002-12-01 | H. William Welliver III | |
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | mapping do_sync_query(string s)
{
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | object udp = Stdio.UDP();
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | udp->bind(0);
mapping m;
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | int i;
for (i=0; i < RETRIES; i++) {
udp->send(nameservers[i % sizeof(nameservers)], 53, s);
|
d692ea | 2000-09-07 | Leif Stensson | |
catch
{
while (udp->wait(RETRY_DELAY))
{
catch {
m = udp->read();
if ((m->port == 53) &&
(m->data[0..1] == s[0..1]) &&
|
7b6964 | 2003-08-07 | Martin Nilsson | | has_value(nameservers, m->ip)) {
|
d692ea | 2000-09-07 | Leif Stensson | |
return decode_res(m->data);
}
};
}
};
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | }
return 0;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
7dc316 | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
bf895c | 2002-11-27 | Marek Habersack | |
|
7dc316 | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
bf895c | 2002-11-27 | Marek Habersack | |
|
7dc316 | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | |
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array gethostbyname(string s)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
27c685 | 1997-12-10 | David Hedbor | | mapping m;
|
7f8292 | 1997-12-10 | David Hedbor | | if(sizeof(domains) && s[-1] != '.' && sizeof(s/".") < 3) {
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | m = do_sync_query(mkquery(s, C_IN, T_A));
|
27c685 | 1997-12-10 | David Hedbor | | if(!m || !m->an || !sizeof(m->an))
|
7f8292 | 1997-12-10 | David Hedbor | | foreach(domains, string domain)
|
27c685 | 1997-12-10 | David Hedbor | | {
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | m = do_sync_query(mkquery(s+"."+domain, C_IN, T_A));
|
27c685 | 1997-12-10 | David Hedbor | | if(m && m->an && sizeof(m->an))
break;
}
} else {
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | m = do_sync_query(mkquery(s, C_IN, T_A));
|
27c685 | 1997-12-10 | David Hedbor | | }
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | |
if (!m) {
return ({ 0, ({}), ({}) });
}
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(string) names=({});
array(string) ips=({});
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | foreach(m->an, mapping x)
|
27c685 | 1997-12-10 | David Hedbor | | {
if(x->name)
names+=({x->name});
if(x->a)
ips+=({x->a});
}
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | return ({
sizeof(names)?names[0]:0,
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | ips,
names,
});
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
27e9f9 | 2002-12-01 | H. William Welliver III | |
|
b6e344 | 2002-11-26 | H. William Welliver III | |
|
bf895c | 2002-11-27 | Marek Habersack | |
|
b6e344 | 2002-11-26 | H. William Welliver III | |
array getsrvbyname(string service, string protocol, string|void name)
{
mapping m;
if(!service) error("no service name specified.");
if(!protocol) error("no service name specified.");
if(sizeof(domains) && !name) {
m = do_sync_query(
mkquery("_" + service +"._"+ protocol + "." + name, C_IN, T_SRV));
if(!m || !m->an || !sizeof(m->an))
foreach(domains, string domain)
{
m = do_sync_query(
mkquery("_" + service +"._"+ protocol + "." + domain, C_IN, T_SRV));
if(m && m->an && sizeof(m->an))
break;
}
} else {
m = do_sync_query(
mkquery("_" + service +"._"+ protocol + "." + name, C_IN, T_SRV));
}
if (!m) {
return ({});
}
array res=({});
foreach(m->an, mapping x)
{
res+=({({x->priority, x->weight, x->port, x->target})});
}
array y=({});
foreach(res, array t)
y+=({t[0]});
sort(y, res);
return res;
}
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | string arpa_from_ip(string ip)
{
|
e94d1a | 2003-04-23 | Marcus Comstedt | | if(has_value(ip,':')) {
string raw_ipv6 = make_raw_addr6(ip);
return reverse(sprintf("%@02X", values(raw_ipv6)))/1*"."+".IP6.ARPA";
} else
return reverse(ip/".")*"."+".IN-ADDR.ARPA";
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
string ip_from_arpa(string arpa)
{
|
e94d1a | 2003-04-23 | Marcus Comstedt | | array(string) parts = reverse(arpa/".");
if(sizeof(parts)<2) return "";
if(lower_case(parts[1]) == "ip6")
return map(parts[2..]/4, `*, "")*":";
else
return parts[2..]*".";
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | |
|
bf895c | 2002-11-27 | Marek Habersack | |
|
1f9e12 | 2002-11-27 | Marek Habersack | | array gethostbyaddr(string s)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | mapping m=do_sync_query(mkquery(arpa_from_ip(s), C_IN, T_PTR));
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | if (m) {
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(string) names=({});
array(string) ips=({});
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | |
foreach(m->an, mapping x)
|
27c685 | 1997-12-10 | David Hedbor | | {
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | if(x->ptr)
names+=({x->ptr});
if(x->name)
{
ips+=({ip_from_arpa(x->name)});
}
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | return ({
sizeof(names)?names[0]:0,
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | ips,
names,
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | });
} else {
|
18096f | 2003-08-22 | Martin Nilsson | | return ({ 0, ({}), ({}) });
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | }
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
7dc316 | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
bf895c | 2002-11-27 | Marek Habersack | |
|
7dc316 | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
a7c144 | 1999-10-30 | Mirar (Pontus Hagland) | |
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | string get_primary_mx(string host)
{
|
27c685 | 1997-12-10 | David Hedbor | | mapping m;
|
7f8292 | 1997-12-10 | David Hedbor | | if(sizeof(domains) && host[-1] != '.' && sizeof(host/".") < 3) {
|
27c685 | 1997-12-10 | David Hedbor | | m=do_sync_query(mkquery(host, C_IN, T_MX));
if(!m || !m->an || !sizeof(m->an))
|
7f8292 | 1997-12-10 | David Hedbor | | foreach(domains, string domain)
|
27c685 | 1997-12-10 | David Hedbor | | {
m=do_sync_query(mkquery(host+"."+domain, C_IN, T_MX));
if(m && m->an && sizeof(m->an))
break;
}
} else {
m=do_sync_query(mkquery(host, C_IN, T_MX));
}
|
eabcd5 | 1999-07-25 | Henrik Grubbström (Grubba) | | if (!m) {
return 0;
}
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | int minpref=29372974;
string ret;
foreach(m->an, mapping m2)
|
27c685 | 1997-12-10 | David Hedbor | | {
if(m2->preference<minpref)
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
27c685 | 1997-12-10 | David Hedbor | | ret=m2->mx;
minpref=m2->preference;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
27c685 | 1997-12-10 | David Hedbor | | }
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | return ret;
}
|
383392 | 1999-12-22 | Henrik Grubbström (Grubba) | |
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array(string) get_mx(string host)
|
383392 | 1999-12-22 | Henrik Grubbström (Grubba) | | {
mapping m;
if(sizeof(domains) && host[-1] != '.' && sizeof(host/".") < 3) {
m = do_sync_query(mkquery(host, C_IN, T_MX));
if(!m || !m->an || !sizeof(m->an)) {
foreach(domains, string domain)
{
m = do_sync_query(mkquery(host+"."+domain, C_IN, T_MX));
if(m && m->an && sizeof(m->an))
break;
}
}
} else {
m = do_sync_query(mkquery(host, C_IN, T_MX));
}
if (!m) {
return 0;
}
array a = m->an;
array(string) b = column( a, "mx");
sort( column( a, "preference"), b);
return b;
}
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | #define REMOVE_DELAY 120
#define GIVE_UP_DELAY (RETRIES * RETRY_DELAY + REMOVE_DELAY)*2
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
class async_client
{
inherit client;
|
854091 | 1999-07-21 | David Hedbor | | inherit Stdio.UDP : udp;
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | async_client next_client;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | class Request
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | string req;
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | string domain;
function callback;
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | int retries;
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | int timestamp;
|
4755b7 | 2000-02-18 | Henrik Grubbström (Grubba) | | array args;
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | };
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | mapping requests=([]);
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
static private void remove(object(Request) r)
{
if(!r) return;
sscanf(r->req,"%2c",int id);
m_delete(requests,id);
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | r->callback(r->domain,0,@r->args);
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | destruct(r);
}
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | void retry(object(Request) r, void|int nsno)
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
if(!r) return;
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | if (nsno >= sizeof(nameservers)) {
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | if(r->retries++ > RETRIES)
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | {
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | call_out(remove,REMOVE_DELAY,r);
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | | return;
} else {
nsno = 0;
}
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
625627 | 1998-01-11 | Henrik Grubbström (Grubba) | |
send(nameservers[nsno],53,r->req);
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | call_out(retry,RETRY_DELAY,r,nsno+1);
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
void do_query(string domain, int cl, int type,
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | function(string,mapping,mixed...:void) callback,
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | mixed ... args)
{
|
8ba41f | 2003-07-30 | Anders Johansson | | for(int e=next_client ? 100 : 256;e>=0;e--)
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | {
|
75c66d | 1998-07-17 | Fredrik Hübinette (Hubbe) | | int lid = random(65536);
if(!catch { requests[lid]++; })
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | {
|
75c66d | 1998-07-17 | Fredrik Hübinette (Hubbe) | | string req=low_mkquery(lid,domain,cl,type);
object r=Request();
r->req=req;
r->domain=domain;
r->callback=callback;
r->args=args;
r->timestamp=time();
requests[lid]=r;
udp::send(nameservers[0],53,r->req);
call_out(retry,RETRY_DELAY,r,1);
return;
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | }
}
|
75c66d | 1998-07-17 | Fredrik Hübinette (Hubbe) | |
if(!next_client)
next_client=async_client(nameservers,domains);
next_client->do_query(domain, cl, type, callback, @args);
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
529b30 | 1999-07-25 | David Hedbor | | static private void rec_data(mapping m)
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
6aa4b7 | 1998-06-13 | Henrik Grubbström (Grubba) | | mixed err;
if (err = catch {
|
7b6964 | 2003-08-07 | Martin Nilsson | | if(m->port != 53 || !has_value(nameservers, m->ip)) return;
|
6aa4b7 | 1998-06-13 | Henrik Grubbström (Grubba) | | sscanf(m->data,"%2c",int id);
object r=requests[id];
if(!r) return;
m_delete(requests,id);
r->callback(r->domain,decode_res(m->data),@r->args);
destruct(r);
}) {
|
0bd6f5 | 2003-10-21 | Martin Nilsson | | werror("DNS: Failed to read UDP packet. Connection refused?\n%s\n",
describe_backtrace(err));
|
6aa4b7 | 1998-06-13 | Henrik Grubbström (Grubba) | | }
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
static private void generic_get(string d,
mapping answer,
|
27c685 | 1997-12-10 | David Hedbor | | int multi,
|
901dde | 1998-09-13 | Henrik Grubbström (Grubba) | | int all,
int type,
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | string field,
string domain,
function callback,
mixed ... args)
{
if(!answer || !answer->an || !sizeof(answer->an))
{
|
7f8292 | 1997-12-10 | David Hedbor | | if(multi == -1 || multi >= sizeof(domains)) {
|
27c685 | 1997-12-10 | David Hedbor | |
callback(domain,0,@args);
} else {
|
901dde | 1998-09-13 | Henrik Grubbström (Grubba) | | do_query(domain+"."+domains[multi], C_IN, type,
generic_get, ++multi, all, type, field, domain,
callback, @args);
|
27c685 | 1997-12-10 | David Hedbor | | }
} else {
|
901dde | 1998-09-13 | Henrik Grubbström (Grubba) | | if (all) {
callback(domain, answer->an, @args);
} else {
foreach(answer->an, array an)
if(an[field])
{
callback(domain, an[field], @args);
return;
}
callback(domain,0,@args);
return;
}
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
}
void host_to_ip(string host, function callback, mixed ... args)
{
|
7f8292 | 1997-12-10 | David Hedbor | | if(sizeof(domains) && host[-1] != '.' && sizeof(host/".") < 3) {
|
27c685 | 1997-12-10 | David Hedbor | | do_query(host, C_IN, T_A,
|
901dde | 1998-09-13 | Henrik Grubbström (Grubba) | | generic_get, 0, 0, T_A, "a", host, callback, @args );
|
27c685 | 1997-12-10 | David Hedbor | | } else {
do_query(host, C_IN, T_A,
|
c9d48d | 1998-09-13 | Henrik Grubbström (Grubba) | | generic_get, -1, 0, T_A, "a",
|
27c685 | 1997-12-10 | David Hedbor | | host, callback, @args);
}
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
void ip_to_host(string ip, function callback, mixed ... args)
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | do_query(arpa_from_ip(ip), C_IN, T_PTR,
|
901dde | 1998-09-13 | Henrik Grubbström (Grubba) | | generic_get, -1, 0, T_PTR, "ptr",
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | ip, callback,
@args);
}
|
901dde | 1998-09-13 | Henrik Grubbström (Grubba) | | void get_mx_all(string host, function callback, mixed ... args)
{
mapping m;
if(sizeof(domains) && host[-1] != '.' && sizeof(host/".") < 3) {
do_query(host, C_IN, T_MX,
generic_get, 0, 1, T_MX, "mx", host, callback, @args);
} else {
do_query(host, C_IN, T_MX,
generic_get, -1, 1, T_MX, "mx", host, callback, @args);
}
}
void get_mx(string host, function callback, mixed ... args)
{
get_mx_all(host,
lambda(string domain, array(mapping) mx,
function callback, mixed ... args) {
array a;
if (mx) {
a = column(mx, "mx");
sort(column(mx, "preference"), a);
}
callback(a, @args);
|
79ee22 | 1998-09-18 | Henrik Grubbström (Grubba) | | }, callback, @args);
|
901dde | 1998-09-13 | Henrik Grubbström (Grubba) | | }
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | void create(void|string|array(string) server, void|string|array(string) domain)
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | | {
if(!udp::bind(0))
|
484a74 | 2002-03-09 | Martin Nilsson | | error( "DNS: failed to bind a port.\n" );
|
f971bd | 1997-09-15 | Fredrik Hübinette (Hubbe) | |
udp::set_read_callback(rec_data);
|
cd6ed0 | 1998-07-17 | Fredrik Hübinette (Hubbe) | | ::create(server,domain);
|
3d3ed7 | 1997-09-15 | Fredrik Hübinette (Hubbe) | | }
|
bb02cc | 1997-09-15 | Fredrik Hübinette (Hubbe) | | };
|
b6e344 | 2002-11-26 | H. William Welliver III | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
async_client global_async_client;
#define GAC(X) \
void async_##X( string host, function callback, mixed ... args ) \
{ \
if( !global_async_client ) \
global_async_client = async_client(); \
global_async_client->X(host,callback,@args); \
}
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GAC(ip_to_host);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GAC(host_to_ip);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GAC(get_mx_all);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GAC(get_mx);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
client global_client;
#define GC(X) \
mixed X( string host ) \
{ \
if( !global_client ) \
global_client = client(); \
return global_client->X(host); \
}
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GC(gethostbyname);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GC(gethostbyaddr);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GC(get_mx);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | |
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|
8ba41f | 2003-07-30 | Anders Johansson | | GC(get_primary_mx);
|
f3b89c | 2003-08-05 | Martin Nilsson | |
|