Show
Ignore:
Timestamp:
07/10/05 14:23:15 (8 years ago)
Author:
ahu
Message:

add TCP client timeouts, add way to limit the number of simultaneous TCP connections

Files:
1 modified

Legend:

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

    r435 r436  
    7979static int d_clientsock; 
    8080static vector<int> d_udpserversocks; 
    81 static vector<int> d_tcpserversocks; 
    82  
     81 
     82typedef vector<int> tcpserversocks_t; 
     83static tcpserversocks_t s_tcpserversocks; 
    8384 
    8485struct PacketID 
     
    352353    Utility::setNonBlocking(fd); 
    353354    listen(fd, 128); 
    354     d_tcpserversocks.push_back(fd); 
     355    s_tcpserversocks.push_back(fd); 
    355356    L<<Logger::Error<<"Listening for TCP queries on "<<inet_ntoa(sin.sin_addr)<<":"<<arg().asNum("local-port")<<endl; 
    356357  } 
     
    469470  struct sockaddr_in remote; 
    470471  char data[65535]; 
     472  time_t startTime; 
    471473}; 
    472474 
     
    497499    arg().set("delegation-only","Which domains we only accept delegations from")=""; 
    498500    arg().set("query-local-address","Source IP address for sending queries")="0.0.0.0"; 
     501    arg().set("client-tcp-timeout","Timeout in seconds when talking to TCP clients")="2"; 
     502    arg().set("max-tcp-clients","Maximum number of simultaneous TCP clients")="128"; 
    499503 
    500504    arg().setCmd("help","Provide a helpful message"); 
     
    575579    vector<TCPConnection> tcpconnections; 
    576580    counter=0; 
     581    time_t now; 
     582    unsigned int maxTcpClients=arg().asNum("max-tcp-clients"); 
    577583    for(;;) { 
    578584      while(MT->schedule()); // housekeeping, let threads do their thing 
     
    597603      int fdmax=d_clientsock; 
    598604 
    599       for(vector<TCPConnection>::const_iterator i=tcpconnections.begin();i!=tcpconnections.end();++i) { 
    600         FD_SET(i->fd, &readfds); 
    601         fdmax=max(fdmax,i->fd); 
    602       } 
     605      if(!tcpconnections.empty()) 
     606        now=time(0); 
     607 
     608      vector<TCPConnection> sweeped; 
     609      int tcpLimit=arg().asNum("client-tcp-timeout"); 
     610      for(vector<TCPConnection>::iterator i=tcpconnections.begin();i!=tcpconnections.end();++i) { 
     611        if(now < i->startTime + tcpLimit) { 
     612          FD_SET(i->fd, &readfds); 
     613          fdmax=max(fdmax,i->fd); 
     614          sweeped.push_back(*i); 
     615        } 
     616        else { 
     617          L<<Logger::Error<<"TCP timeout from client "<<inet_ntoa(i->remote.sin_addr)<<endl; 
     618          close(i->fd); 
     619        } 
     620      } 
     621      sweeped.swap(tcpconnections); 
     622 
    603623      for(vector<int>::const_iterator i=d_udpserversocks.begin(); i!=d_udpserversocks.end(); ++i) { 
    604624        FD_SET( *i, &readfds ); 
    605625        fdmax=max(fdmax,*i); 
    606626      } 
    607       for(vector<int>::const_iterator i=d_tcpserversocks.begin(); i!=d_tcpserversocks.end(); ++i) { 
    608         FD_SET( *i, &readfds ); 
    609         fdmax=max(fdmax,*i); 
    610       } 
     627      if(tcpconnections.size() < maxTcpClients)  
     628        for(tcpserversocks_t::const_iterator i=s_tcpserversocks.begin(); i!=s_tcpserversocks.end(); ++i) { 
     629          FD_SET(*i, &readfds ); 
     630          fdmax=max(fdmax,*i); 
     631        } 
     632 
    611633      for(map<int,PacketID>::const_iterator i=d_tcpclientreadsocks.begin(); i!=d_tcpclientreadsocks.end(); ++i) { 
    612634        // cerr<<"Adding TCP socket "<<i->first<<" to read select set"<<endl; 
     
    672694      } 
    673695 
    674       for(vector<int>::const_iterator i=d_tcpserversocks.begin(); i!=d_tcpserversocks.end(); ++i) {  
     696      for(tcpserversocks_t::const_iterator i=s_tcpserversocks.begin(); i!=s_tcpserversocks.end(); ++i) {  
    675697        if(FD_ISSET(*i ,&readfds)) { // do we have a new TCP connection 
    676698          struct sockaddr_in addr; 
     
    684706            tc.state=TCPConnection::BYTE0; 
    685707            tc.remote=addr; 
     708            tc.startTime=time(0); 
    686709            tcpconnections.push_back(tc); 
    687710          }