pike.git / lib / modules / Protocols.pmod / DNS.pmod

version» Context lines:

pike.git/lib/modules/Protocols.pmod/DNS.pmod:1: - // $Id: DNS.pmod,v 1.98 2008/07/22 15:34:15 grubba Exp $ + // $Id: DNS.pmod,v 1.99 2008/07/23 15:11:21 grubba Exp $   // Not yet finished -- Fredrik Hubinette      //! Domain Name System - //! RFC 1035 + //! RFC 1034, RFC 1035 and RFC 2308 + //!      #pike __REAL_VERSION__      final constant NOERROR=0;   final constant FORMERR=1;   final constant SERVFAIL=2;   final constant NXDOMAIN=3;   final constant NOTIMPL=4; -  + final constant REFUSED=5;   final constant NXRRSET=8;      final constant QUERY=0;      //! Resource classes   enum ResourceClass   {    //! Class Internet    C_IN=1,   
pike.git/lib/modules/Protocols.pmod/DNS.pmod:331:    return ret;    }       int decode_int(string s, array(int) next)    {    sscanf(s[next[0]..next[0]+3],"%4c",int ret);    next[0]+=4;    return ret;    }    +  //! Decode a set of entries from an answer. +  //! +  //! @param s +  //! Encoded entries. +  //! +  //! @param num +  //! Number of entires in @[s]. +  //! +  //! @param next +  //! Array with a single element containing the start position in @[s] +  //! on entry and the continuation position on return. +  //! +  //! @returns +  //! Returns an array of mappings describing the decoded entires: +  //! @array +  //! @elem mapping 0.. +  //! Mapping describing a single entry: +  //! @mapping +  //! @member string "name" +  //! Name the entry concerns. +  //! @member EntryType "type" +  //! Type of entry. +  //! @member ResourceClass "cl" +  //! Resource class. Typically @[C_IN]. +  //! @member int "ttl" +  //! Time to live for the entry in seconds. +  //! @member int "len" +  //! Length in bytes of the encoded data section. +  //! @endmapping +  //! Depending on the type of entry the mapping may contain +  //! different additional fields: +  //! @int +  //! @value T_CNAME +  //! @mapping +  //! @member string "cname" +  //! @endmapping +  //! @value T_PTR +  //! @mapping +  //! @member string "ptr" +  //! @endmapping +  //! @value T_NS +  //! @mapping +  //! @member string "ns" +  //! @endmapping +  //! @value T_MX +  //! @mapping +  //! @member int "preference" +  //! @member string "mx" +  //! @endmapping +  //! @value T_HINFO +  //! @mapping +  //! @member string "cpu" +  //! @member string "os" +  //! @endmapping +  //! @value T_SRV +  //! RFC 2052 and RFC 2782. +  //! @mapping +  //! @member int "priority" +  //! @member int "weight" +  //! @member int "port" +  //! @member string "target" +  //! @member string "service" +  //! @member string "proto" +  //! @member string "name" +  //! @endmapping +  //! @value T_A +  //! @mapping +  //! @member string "a" +  //! IPv4-address in dotted-decimal format. +  //! @endmapping +  //! @value T_AAAA +  //! @mapping +  //! @member string "aaaa" +  //! IPv6-address in colon-separated hexadecimal format. +  //! @endmapping +  //! @value T_LOC +  //! @mapping +  //! @member int "version" +  //! Version, currently only version @expr{0@} (zero) is +  //! supported. +  //! @member int "size" +  //! @member int "h_perc" +  //! @member int "v_perc" +  //! @member int "lat" +  //! @member int "long" +  //! @member int "alt" +  //! @endmapping +  //! @value T_SOA +  //! @mapping +  //! @member string "mname" +  //! @member string "rname" +  //! @member int "serial" +  //! @member int "refresh" +  //! @member int "retry" +  //! @member int "expire" +  //! +  //! @member int "minimum" +  //! Note: For historical reasons this entry is named +  //! @expr{"minimum"@}, but it contains the TTL for +  //! negative answers (RFC 2308). +  //! @endmapping +  //! @value T_NAPTR +  //! @mapping +  //! @member int "order" +  //! @member int "preference" +  //! @member string "flags" +  //! @member string "service" +  //! @member string "regexp" +  //! @member string "replacement" +  //! @endmapping +  //! @value T_TXT +  //! @mapping +  //! @member string "txt" +  //! @endmapping +  //! @endint +  //! @endarray    array decode_entries(string s,int num, array(int) next)    {    array(string) ret=({});    for(int e=0;e<num && next[0]<sizeof(s);e++)    {    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);
pike.git/lib/modules/Protocols.pmod/DNS.pmod:364:    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;    case T_SRV: +  // RFC 2052 and RFC 2782.    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(sizeof(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->name=x[2..]*".";    break;    case T_A:    m->a=sprintf("%{.%d%}",values(s[next[0]..next[0]+m->len-1]))[1..];    break;    case T_AAAA:    m->aaaa=sprintf("%{:%02X%02X%}",    values(s[next[0]..next[0]+m->len-1])/2)[1..];    break;    case T_LOC:    m->version = decode_byte(s,next);
pike.git/lib/modules/Protocols.pmod/DNS.pmod:485:    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;    }   };      //! Implements a Domain Name Service (DNS) server.   class server   { +  //!    inherit protocol; -  +     inherit Stdio.UDP : udp;       protected void send_reply(mapping r, mapping q, mapping m)    {    // FIXME: Needs to handle truncation somehow.    if(!r)    r = (["rcode":4]);    r->id = q->id;    r->qr = 1;    r->opcode = q->opcode;
pike.git/lib/modules/Protocols.pmod/DNS.pmod:516:    //!    //! @param udp_data    //! Raw UDP data.    //!    //! Overload this function to implement the proper lookup.    //!    //! @returns    //! Returns @expr{0@} (zero) on failure, or a result mapping on success:    //! @mapping    //! @member int "rcode" -  //! @member array(mapping(string:string|int))|void "qd" +  //! @member array(mapping(string:string|int))|void "an"    //! @array    //! @elem mapping(string:string|int) entry    //! @mapping    //! @member string|array(string) "name"    //! @member int "type"    //! @member int "cl"    //! @endmapping    //! @endarray -  //! @member array|void "an" +  //! @member array|void "qd"    //! @member array|void "ns"    //! @member array|void "ar"    //! @endmapping    protected mapping reply_query(mapping query, mapping udp_data)    {    // Override this function.    //    // Return mapping may contain:    // aa, ra, {ad, cd,} rcode, an, ns, ar