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

version» Context lines:

pike.git/lib/modules/Protocols.pmod/DNS.pmod:1:   // Not yet finished -- Fredrik Hubinette   // RFC 1035    - //! $Id: DNS.pmod,v 1.63 2002/09/17 18:58:14 nilsson Exp $ + //! $Id: DNS.pmod,v 1.64 2002/11/26 21:59:50 bill Exp $      #pike __REAL_VERSION__      constant NOERROR=0;   constant FORMERR=1;   constant SERVFAIL=2;   constant NXDOMAIN=3;   constant NOTIMPL=4;   constant NXRRSET=8;   
pike.git/lib/modules/Protocols.pmod/DNS.pmod:26:   constant T_MB=7;   constant T_MG=8;   constant T_MR=9;   constant T_NULL=10;   constant T_PTR=12;   constant T_HINFO=13;   constant T_MINFO=14;   constant T_MX=15;   constant T_TXT=16;   constant T_AAAA=28; + constant T_SRV=33;    -  +    class protocol   {    string mklabel(string s)    {    if(strlen(s)>63)    error("Too long component in domain name.\n");    return sprintf("%c%s",strlen(s),s);    }       static private string mkname(string|array(string) labels, int pos,
pike.git/lib/modules/Protocols.pmod/DNS.pmod:82:    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:    return sprintf("%1c%s%1c%s", strlen(entry->cpu||""), entry->cpu||"",    strlen(entry->os||""), entry->os||"");    case T_MINFO:    string rmailbx = mkname(entry->rmailbx, pos, c);    return rmailbx + mkname(entry->emailbx, pos+strlen(rmailbx), c); +  case T_SRV: +  return sprintf("%2c%2c%2c", entry->priority, entry->weight, entry->port) + +  mkname(entry->target||"", pos+6, c);    case T_A:    case T_AAAA:    return sprintf("%@1c", (array(int))((entry->a||"0.0.0.0")/".")[0..3]);    case T_SOA:    string mname = mkname(entry->mname, pos, c);    return mname + mkname(entry->rname, pos+strlen(mname), c) +    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||({})),
pike.git/lib/modules/Protocols.pmod/DNS.pmod:244:    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; +  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;    case T_A:    case T_AAAA:    m->a=sprintf("%{.%d%}",values(s[next[0]..next[0]+m->len-1]))[1..];    break;    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);
pike.git/lib/modules/Protocols.pmod/DNS.pmod:641:    //! DNS server. The result is an array with three elements,    //! @array    //! @elem string hostname    //! Hostname.    //! @elem array(string) ip    //! IP number(s).    //! @elem array(string) aliases    //! DNS name(s).    //! @endarray    //! -  +     array gethostbyname(string s)    {    mapping m;    if(sizeof(domains) && s[-1] != '.' && sizeof(s/".") < 3) {    m = do_sync_query(mkquery(s, C_IN, T_A));    if(!m || !m->an || !sizeof(m->an))    foreach(domains, string domain)    {    m = do_sync_query(mkquery(s+"."+domain, C_IN, T_A));    if(m && m->an && sizeof(m->an))
pike.git/lib/modules/Protocols.pmod/DNS.pmod:678:    if(x->a)    ips+=({x->a});    }    return ({    sizeof(names)?names[0]:0,    ips,    names,    });    }    +  //! Querys the service record from the default or given +  //! DNS server. The result is an array of arrays with the +  //! following six elements for each record. The array is +  //! sorted according to the priority of each record. +  //! +  //! Each element of the array returned represents a service +  //! record. Each service record contains the following: +  //! @array +  //! @elem int priority +  //! Priority +  //! @elem int weight +  //! Weight in event of multiple records with same priority. +  //! @elem int port +  //! port number +  //! @elem string target +  //! target dns name +  //! @endarray +  //! +  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) { +  // we haven't provided a target domain to search on. +  // we probably shouldn't do a searchdomains search, +  // as it might return ambiguous results. +  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) { // no entries. +  return ({}); +  } +  +  array res=({}); +  +  foreach(m->an, mapping x) +  { +  res+=({({x->priority, x->weight, x->port, x->target})}); +  } +  +  // now we sort the array by priority as a convenience +  array y=({}); +  foreach(res, array t) +  y+=({t[0]}); +  sort(y, res); +  +  return res; +  } +     string arpa_from_ip(string ip)    {    return reverse(ip/".")*"."+".IN-ADDR.ARPA";    }       string ip_from_arpa(string arpa)    {    return reverse(arpa/".")[2..]*".";    }   
pike.git/lib/modules/Protocols.pmod/DNS.pmod:970:       void create(void|string|array(string) server, void|string|array(string) domain)    {    if(!udp::bind(0))    error( "DNS: failed to bind a port.\n" );       udp::set_read_callback(rec_data);    ::create(server,domain);    }   }; +