Changeset 699

Show
Ignore:
Timestamp:
04/14/06 13:26:50 (4 years ago)
Author:
ahu
Message:

implement stunningly cool spoofing protection, plus spoofer in dnspbench

Location:
trunk/pdns/pdns
Files:
8 modified

Legend:

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

    r683 r699  
    379379    } 
    380380    else { 
     381      // should check for . here and replace by \. 
    381382      ret.append(&content.at(frompos), &content.at(frompos+labellen)); 
    382383      ret.append(1,'.'); 
  • trunk/pdns/pdns/dnspbench.cc

    r680 r699  
    4545  reportAllTypes(); 
    4646 
    47 #if 0 
    4847  Socket s(InterNetwork, Datagram); 
     48   
     49  IPEndpoint rem("127.0.0.1",1232), loc("213.156.2.1", 53); 
     50  s.bind(loc); 
    4951 
    5052  vector<uint8_t> vpacket; 
    51   string domain="www.ds9a.nl"; 
     53  string domain="ds9a.nl"; 
    5254  uint16_t type=1; 
    5355 
    54   for(unsigned int n=0; n < 1000000; ++n) { 
     56  for(unsigned int n=0; n < 65536; ++n) { 
    5557    DNSPacketWriter pw(vpacket, domain, type); 
    5658     
    5759    pw.getHeader()->rd=1; 
     60    pw.getHeader()->qr=1; 
    5861    pw.getHeader()->id=n; 
     62    ARecordContent arc("1.2.3.4"); 
     63    pw.startRecord("ds9a.nl", 1, 9999, 1, DNSPacketWriter::ANSWER); 
     64    arc.toPacket(pw); 
    5965    pw.commit(); 
    60     IPEndpoint rem("127.0.0.1",5300); 
     66 
    6167    string spacket((char*)(&*vpacket.begin()), vpacket.size()); 
    6268    s.sendTo(spacket, rem); 
    6369  } 
    6470 
    65  
    6671  return 0;  
    67 #endif 
     72#if 0 
    6873 
    6974  vector<uint8_t> packet; 
     
    8388  shared_ptr<DNSRecordContent> regen=DNSRecordContent::unserialize(argv[1], type, record); 
    8489  cerr<<"Out: "<<argv[1]<<" IN "<<argv[2]<<" "<<regen->getZoneRepresentation()<<endl; 
     90#endif 
    8591} 
    8692catch(exception& e) 
  • trunk/pdns/pdns/dnswriter.cc

    r690 r699  
    124124  typedef vector<pair<unsigned int, unsigned int> > parts_t; 
    125125  parts_t parts; 
    126   vstringtok(parts, label, "."); 
     126  vstringtok(parts, label, "."); // XXX FIXME this should deal with escaped . 
    127127   
    128128  // d_stuff is amount of stuff that is yet to be written out - the dnsrecordheader for example 
  • trunk/pdns/pdns/mtasker.cc

    r695 r699  
    159159*/ 
    160160 
    161 template<class EventKey, class EventVal>int MTasker<EventKey,EventVal>::waitEvent(const EventKey &key, EventVal *val, unsigned int timeout) 
     161template<class EventKey, class EventVal>int MTasker<EventKey,EventVal>::waitEvent(EventKey &key, EventVal *val, unsigned int timeout) 
    162162{ 
    163163  if(d_waiters.count(key)) { // there was already an exact same waiter 
     
    181181    *val=d_waitval; 
    182182  d_tid=w.tid; 
     183  key=d_eventkey; 
    183184  return d_waitstatus; 
    184185} 
     
    217218  ucontext_t *userspace=waiter->context; 
    218219  d_tid=waiter->tid;         // set tid  
    219    
     220  d_eventkey=waiter->key;        // pass waitEvent the exact key it was woken for 
    220221  d_waiters.erase(waiter);             // removes the waitpoint  
    221222  if(swapcontext(&d_kernel,userspace)) { // swaps back to the above point 'A' 
  • trunk/pdns/pdns/mtasker.hh

    r681 r699  
    5151  std::queue<int> d_zombiesQueue; 
    5252 
     53 
     54  typedef std::map<int, ucontext_t*> mthreads_t; 
     55  mthreads_t d_threads; 
     56  int d_tid; 
     57  int d_maxtid; 
     58  size_t d_stacksize; 
     59 
     60  EventVal d_waitval; 
     61  enum {Error=-1,TimeOut=0,Answer} d_waitstatus; 
     62 
     63public: 
    5364  struct Waiter 
    5465  { 
     
    5869    int tid; 
    5970  }; 
    60  
    61   //  typedef std::map<EventKey,Waiter> waiters_t; 
    6271 
    6372  typedef multi_index_container< 
     
    7180  waiters_t d_waiters; 
    7281 
    73   typedef std::map<int, ucontext_t*> mthreads_t; 
    74   mthreads_t d_threads; 
    75   int d_tid; 
    76   int d_maxtid; 
    77   size_t d_stacksize; 
    78  
    79   EventVal d_waitval; 
    80   enum {Error=-1,TimeOut=0,Answer} d_waitstatus; 
    81  
    82 public: 
    8382  //! Constructor 
    8483  /** Constructor with a small default stacksize. If any of your threads exceeds this stack, your application will crash.  
     
    9291 
    9392  typedef void tfunc_t(void *); //!< type of the pointer that starts a thread  
    94   int waitEvent(const EventKey &key, EventVal *val=0, unsigned int timeout=0); 
     93  int waitEvent(EventKey &key, EventVal *val=0, unsigned int timeout=0); 
    9594  void yield(); 
    9695  int sendEvent(const EventKey& key, const EventVal* val=0); 
     
    104103private: 
    105104  static void threadWrapper(MTasker *self, tfunc_t *tf, int tid, void* val); 
     105  EventKey d_eventkey;   // for waitEvent, contains exact key it was awoken for 
    106106}; 
    107107#include "mtasker.cc" 
  • trunk/pdns/pdns/pdns_recursor.cc

    r698 r699  
    128128static map<int,PacketID> d_tcpclientreadsocks, d_tcpclientwritesocks; 
    129129 
    130 MTasker<PacketID,string>* MT; 
     130typedef MTasker<PacketID,string> MT_t; 
     131MT_t* MT; 
    131132 
    132133int asendtcp(const string& data, Socket* sock)  
     
    183184    *d_len=packet.size(); 
    184185    memcpy(data,packet.c_str(),min(len,*d_len)); 
     186    if(pident.nearMisses > 100) { 
     187      L<<Logger::Error<<"Too many ("<<pident.nearMisses<<") bogus answers came in from "<<sockAddrToString((struct sockaddr_in*)toaddr, sizeof(pident.remote))<<", assuming spoof attempt."<<endl; 
     188      return -1; 
     189    } 
    185190  } 
    186191  return ret; 
     
    10001005                    L<<Logger::Warning<<"Discarding unexpected packet from "<<sockAddrToString((struct sockaddr_in*) &fromaddr, addrlen)<<endl; 
    10011006                  g_stats.unexpectedCount++; 
     1007                   
     1008                  for(MT_t::waiters_t::iterator mthread=MT->d_waiters.begin(); mthread!=MT->d_waiters.end(); ++mthread) { 
     1009                    if(!memcmp(&mthread->key.remote.sin_addr, &pident.remote.sin_addr, sizeof(pident.remote.sin_addr))) { 
     1010                      mthread->key.nearMisses++; 
     1011                    } 
     1012                  } 
    10021013                } 
    10031014              } 
  • trunk/pdns/pdns/rcpgenerator.cc

    r683 r699  
    9898} 
    9999 
    100  
    101 void RecordTextReader::xfrLabel(string& val, bool) 
     100void RecordTextReader::xfrLabel(string& val, bool)  
    102101{ 
    103102  skipSpaces(); 
  • trunk/pdns/pdns/syncres.hh

    r697 r699  
    318318struct PacketID 
    319319{ 
    320   PacketID() : sock(0), inNeeded(0), outPos(0) 
     320  PacketID() : sock(0), inNeeded(0), outPos(0), nearMisses(0) 
    321321  {} 
    322322 
     
    330330  string outMSG; // the outgoing message that needs to be sent 
    331331  string::size_type outPos;    // how far we are along in the outMSG 
     332 
     333  mutable uint32_t nearMisses; // number of near misses - host correct, id wrong 
    332334 
    333335  bool operator<(const PacketID& b) const