Changeset 308

Show
Ignore:
Timestamp:
02/12/05 20:42:44 (8 years ago)
Author:
ahu
Message:

make sdig useable, remove warts from dnsparser

Location:
trunk/pdns/pdns
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/dnsparser.cc

    r303 r308  
    77{ 
    88public: 
    9   UnknownRecordContent(const struct dnsrecordheader& ah, PacketReader& pr)  
    10     : d_ah(ah) 
     9  UnknownRecordContent(const DNSRecord& dr, PacketReader& pr)  
     10    : d_dr(dr) 
    1111  { 
    12     pr.copyRecord(d_record, ah.d_clen); 
    13   } 
    14  
    15   string getType() const 
    16   { 
    17     return "#"+lexical_cast<string>(d_ah.d_type); 
    18   } 
    19  
    20  
     12    pr.copyRecord(d_record, dr.d_clen); 
     13  } 
    2114 
    2215  string getZoneRepresentation() const 
    2316  { 
    2417    ostringstream str; 
    25     if(d_ah.d_class==1) 
     18    if(d_dr.d_class==1) 
    2619      str<<"IN"; 
    2720    else 
    28       str<<"CLASS"<<d_ah.d_class; 
     21      str<<"CLASS"<<d_dr.d_class; 
    2922 
    3023    str<<"\t"; 
    3124 
    32     str<<"TYPE"<<d_ah.d_type<<"\t"; 
     25    str<<"TYPE"<<d_dr.d_type<<"\t"; 
    3326 
    3427    str<<"\\# "<<d_record.size()<<" "; 
     
    4437 
    4538private: 
    46   struct dnsrecordheader d_ah; 
     39  const DNSRecord& d_dr; 
    4740  vector<u_int8_t> d_record; 
    4841}; 
     
    5043 
    5144 
    52 DNSRecordContent* DNSRecordContent::mastermake(const struct dnsrecordheader& ah,  
     45DNSRecordContent* DNSRecordContent::mastermake(const DNSRecord &dr,  
    5346                                               PacketReader& pr) 
    5447{ 
    55   typemap_t::const_iterator i=typemap.find(make_pair(ah.d_class, ah.d_type)); 
     48  typemap_t::const_iterator i=typemap.find(make_pair(dr.d_class, dr.d_type)); 
    5649  if(i==typemap.end()) { 
    57     return new UnknownRecordContent(ah, pr); 
    58   } 
    59   return i->second(ah, pr); 
     50    return new UnknownRecordContent(dr, pr); 
     51  } 
     52  return i->second(dr, pr); 
    6053} 
    6154 
    6255 
    6356DNSRecordContent::typemap_t DNSRecordContent::typemap __attribute__((init_priority(1000))); 
     57DNSRecordContent::namemap_t DNSRecordContent::namemap __attribute__((init_priority(1000))); 
    6458 
    6559void MOADNSParser::init(const char *packet, unsigned int len) 
     
    112106     
    113107    dr.d_label=label; 
    114     dr.d_ah=ah;  
     108    dr.d_clen=ah.d_clen; 
    115109    d_answers.push_back(make_pair(dr, pr.d_pos)); 
    116110 
    117     dr.d_content=boost::shared_ptr<DNSRecordContent>(DNSRecordContent::mastermake(ah, pr)); 
     111    dr.d_content=boost::shared_ptr<DNSRecordContent>(DNSRecordContent::mastermake(dr, pr)); 
    118112    if(dr.d_content) { 
    119113      //      cout<<dr.d_label<<"\t"<<dr.d_content->getZoneRepresentation(); 
  • trunk/pdns/pdns/dnsparser.hh

    r303 r308  
    1212#include <arpa/nameser.h> 
    1313#include <boost/shared_ptr.hpp> 
     14#include <boost/lexical_cast.hpp> 
    1415 
    1516namespace { 
     
    3132 
    3233class MOADNSParser; 
     34 
     35 
    3336 
    3437class PacketReader 
     
    5558}; 
    5659 
     60class DNSRecord; 
     61 
    5762class DNSRecordContent 
    5863{ 
    5964public: 
    60   static DNSRecordContent* mastermake(const struct dnsrecordheader& ah, PacketReader& pr); 
     65  static DNSRecordContent* mastermake(const DNSRecord &dr, PacketReader& pr); 
    6166  virtual std::string getZoneRepresentation() const = 0; 
    62   virtual std::string getType() const=0; 
    6367  virtual ~DNSRecordContent() {} 
    6468 
    6569  std::string label; 
    6670  struct dnsrecordheader header; 
     71 
     72  typedef DNSRecordContent* makerfunc_t(const struct DNSRecord& dr, PacketReader& pr);   
     73  static void regist(uint16_t cl, uint16_t ty, makerfunc_t* f, const char* name) 
     74  { 
     75    typemap[make_pair(cl,ty)]=f; 
     76    namemap[make_pair(cl,ty)]=name; 
     77  } 
     78 
     79  static uint16_t TypeToNumber(const string& name) 
     80  { 
     81    for(namemap_t::const_iterator i=namemap.begin(); i!=namemap.end();++i) 
     82      if(i->second==name) 
     83        return i->first.second; 
     84 
     85    throw runtime_error("Unknown DNS type '"+name+"'"); 
     86 
     87  } 
     88 
     89  static const string NumberToType(uint16_t num) 
     90  { 
     91    if(!namemap.count(make_pair(1,num))) 
     92      throw runtime_error("Unknown DNS type with numerical id "+lexical_cast<string>(num)); 
     93    return namemap[make_pair(1,num)]; 
     94  } 
     95 
     96 
    6797protected: 
    68   typedef DNSRecordContent* makerfunc_t(const struct dnsrecordheader& ah, PacketReader& pr); 
     98 
    6999  typedef std::map<std::pair<u_int16_t, u_int16_t>, makerfunc_t* > typemap_t; 
    70100  static typemap_t typemap; 
     101  typedef std::map<std::pair<u_int16_t, u_int16_t>, string > namemap_t; 
     102  static namemap_t namemap; 
    71103}; 
    72104 
     
    77109  u_int16_t d_class; 
    78110  u_int32_t d_ttl; 
     111  u_int16_t d_clen; 
    79112  enum {Answer, Nameserver, Additional} d_place; 
    80113  boost::shared_ptr<DNSRecordContent> d_content; 
    81   struct dnsrecordheader d_ah; 
    82114}; 
     115 
    83116 
    84117class MOADNSParser 
     
    97130  string d_qname; 
    98131  u_int16_t d_qclass, d_qtype; 
     132  uint8_t d_rcode; 
    99133 
    100134  typedef vector<pair<DNSRecord, uint16_t > > answers_t; 
  • trunk/pdns/pdns/dnsrecords.cc

    r303 r308  
    44using namespace boost; 
    55 
    6  
    76class ARecordContent : public DNSRecordContent 
    87{ 
     
    109  static void report(void) 
    1110  { 
    12     typemap[make_pair(1,1)]=&make; 
    13   } 
    14  
    15   static DNSRecordContent* make(const struct dnsrecordheader& ah, PacketReader& pr)  
    16   { 
    17     if(ah.d_clen!=4) 
     11    regist(1,1,&make,"A"); 
     12   } 
     13 
     14  static DNSRecordContent* make(const DNSRecord& dr, PacketReader& pr)  
     15  { 
     16    if(dr.d_clen!=4) 
    1817      throw MOADNSException("Wrong size for A record"); 
    1918 
     
    2827  } 
    2928   
    30   string getType() const 
    31   { 
    32     return "A"; 
    33   } 
    3429 
    3530  string getZoneRepresentation() const 
     
    5752  } 
    5853 
    59   string getType() const 
    60   { 
    61     return "AAAA"; 
    62   } 
    63  
    64  
    65   static DNSRecordContent* make(const struct dnsrecordheader& ah, PacketReader& pr)  
    66   { 
    67     if(ah.d_clen!=16) 
     54  static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr)  
     55  { 
     56    if(dr.d_clen!=16) 
    6857      throw MOADNSException("Wrong size for AAAA record"); 
    6958 
     
    111100public: 
    112101 
    113   OneLabelRecordContent(const struct dnsrecordheader& ah, const string& nsname) : d_type(ah.d_type), d_nsname(nsname) {} 
    114  
    115   static void report(void) 
    116   { 
    117     typemap[make_pair(1,ns_t_ns)]=&make; 
    118     typemap[make_pair(1,ns_t_cname)]=&make; 
    119     typemap[make_pair(1,ns_t_ptr)]=&make; 
    120   } 
    121  
    122   static DNSRecordContent* make(const struct dnsrecordheader& ah, PacketReader &pr)  
    123   { 
    124     return new OneLabelRecordContent(ah, pr.getLabel()); 
    125   } 
    126  
    127   string getType() const  
    128   { 
    129     if(d_type==ns_t_ns) 
    130       return "NS"; 
    131     else if(d_type==ns_t_cname) 
    132       return "CNAME"; 
    133     if(d_type==ns_t_ptr) 
    134       return "PTR"; 
     102  OneLabelRecordContent(const DNSRecord &dr, const string& nsname) : d_type(dr.d_type), d_nsname(nsname) {} 
     103 
     104  static void report(void) 
     105  { 
     106    regist(1, ns_t_ns, &make, "NS"); 
     107    regist(1, ns_t_cname, &make, "CNAME"); 
     108    regist(1, ns_t_ptr, &make, "PTR"); 
     109  } 
     110 
     111  static DNSRecordContent* make(const DNSRecord &dr, PacketReader &pr)  
     112  { 
     113    return new OneLabelRecordContent(dr, pr.getLabel()); 
    135114  } 
    136115 
     
    166145  static void report(void) 
    167146  { 
    168     typemap[make_pair(1,6)]=&make; 
    169   } 
    170  
    171   string getType() const 
    172   { 
    173     return "SOA"; 
    174   } 
    175  
    176  
    177   static DNSRecordContent* make(const struct dnsrecordheader& ah, PacketReader& pr)  
     147    regist(1,ns_t_soa,&make,"SOA"); 
     148  } 
     149 
     150  static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr)  
    178151  { 
    179152    u_int16_t nowpos(pr.d_pos); 
     
    181154    string rname=pr.getLabel(); 
    182155 
    183     u_int16_t left=ah.d_clen - (pr.d_pos-nowpos); 
     156    u_int16_t left=dr.d_clen - (pr.d_pos-nowpos); 
    184157 
    185158    if(left!=sizeof(struct soatimes)) 
     
    224197  } 
    225198 
    226   string getType() const 
    227   { 
    228     return "MX"; 
    229   } 
    230  
    231  
    232   static void report(void) 
    233   { 
    234     typemap[make_pair(1,ns_t_mx)]=&make; 
    235   } 
    236  
    237   static DNSRecordContent* make(const struct dnsrecordheader& ah, PacketReader& pr)  
     199  static void report(void) 
     200  { 
     201    regist(1,ns_t_mx,&make,"MX"); 
     202  } 
     203 
     204  static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr)  
    238205  { 
    239206    u_int16_t preference=pr.get16BitInt(); 
     
    265232    SOARecordContent::report(); 
    266233    MXRecordContent::report(); 
     234    MXRecordContent::regist(1,255,0,"ANY"); 
    267235  } 
    268236} reporter __attribute__((init_priority(65535))); 
  • trunk/pdns/pdns/sdig.cc

    r303 r308  
    5151try 
    5252{ 
    53   DNSPacketGenerator dpg(argv[3], atoi(argv[4])); 
     53  DNSPacketGenerator dpg(argv[3], DNSRecordContent::TypeToNumber(argv[4])); 
    5454 
    5555  Socket sock(InterNetwork, Datagram); 
    56   IPEndpoint dest(argv[1], atoi(argv[2])); 
     56  IPEndpoint dest(argv[1] + (*argv[1]=='@'), atoi(argv[2])); 
    5757  sock.sendTo(dpg.getPacket(), dest); 
    5858   
     
    6161 
    6262  MOADNSParser mdp(reply); 
    63   cout<<"Reply to question for qname='"<<mdp.d_qname<<"', qtype="<<mdp.d_qtype<<endl; 
    64  
     63  cout<<"Reply to question for qname='"<<mdp.d_qname<<"', qtype="<<DNSRecordContent::NumberToType(mdp.d_qtype)<<endl; 
     64  cout<<"Rcode: "<<mdp.d_header.rcode<<", RA: "<<mdp.d_header.ra<<", RD: "<<mdp.d_header.rd; 
     65  cout<<", TC: "<<mdp.d_header.tc<<", AA: "<<mdp.d_header.aa<<", opcode: "<<mdp.d_header.opcode<<endl; 
    6566  for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i!=mdp.d_answers.end(); ++i) {           
    6667    shared_ptr<PacketReader> pr=mdp.getPacketReader(i->second); 
    67     DNSRecordContent* drc=DNSRecordContent::mastermake(i->first.d_ah, *pr); 
    68     cout<<i->first.d_place<<"\t"<<i->first.d_label<<"\tIN\t"<<drc->getType()<<"\t"<<i->first.d_ttl<<"\t"<<drc->getZoneRepresentation()<<endl; 
     68    DNSRecordContent* drc=DNSRecordContent::mastermake(i->first, *pr); 
     69    cout<<i->first.d_place<<"\t"<<i->first.d_label<<"\tIN\t"<<DNSRecordContent::NumberToType(i->first.d_type)<<"\t"<<i->first.d_ttl<<"\t"<<drc->getZoneRepresentation()<<endl; 
    6970  } 
    7071