]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Fix infinite loop when all DNS request slots are in use
authorattilamolnar <attilamolnar@hush.com>
Wed, 20 Mar 2013 22:43:51 +0000 (23:43 +0100)
committerattilamolnar <attilamolnar@hush.com>
Wed, 20 Mar 2013 22:43:51 +0000 (23:43 +0100)
This is not the best way to detect this scenario, a better detection mechanism will replace this in the future

src/dns.cpp

index bede73a6fada8736bc5fa94f01de32cf27789e8a..63bde0eccb46e5819b213d33f71df21f8d4cf32a 100644 (file)
@@ -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);