X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fdns.cpp;h=63bde0eccb46e5819b213d33f71df21f8d4cf32a;hb=153bd37b598373f4f52024747ee3bc2cbb76f629;hp=aa340a1de480ba4bef68110098efd489d8e49c98;hpb=2bdbb2878efb600af81513147983aeac7bd62331;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/dns.cpp b/src/dns.cpp index aa340a1de..63bde0ecc 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -258,8 +258,28 @@ DNSRequest* DNS::AddQuery(DNSHeader *header, int &id, const char* original) return NULL; /* Create an id */ + unsigned int tries = 0; do { id = ServerInstance->GenRandomInt(DNS::MAX_REQUEST_ID); + if (++tries == DNS::MAX_REQUEST_ID*5) + { + // If we couldn't find an empty slot this many times, do a sequential scan as a last + // resort. If an empty slot is found that way, go on, otherwise throw an exception + id = -1; + for (int i = 0; i < DNS::MAX_REQUEST_ID; i++) + { + if (!requests[i]) + { + id = i; + break; + } + } + + if (id == -1) + throw ModuleException("DNS: All ids are in use"); + + break; + } } while (requests[id]); DNSRequest* req = new DNSRequest(this, id, original); @@ -371,10 +391,6 @@ DNS::DNS() /* Clear the requests class table */ memset(requests,0,sizeof(requests)); - /* Set the id of the next request to 0 - */ - currid = 0; - /* DNS::Rehash() sets this to a valid ptr */ this->cache = NULL;