CVS: bopm/src firedns.c,1.20,1.21

[email protected] (David Leadbeater)
Newsgroups gmane.network.irc.bopm
Message-ID <[email protected]>
Update of /data/cvs/bopm/src
In directory nubian.blitzed.org:/usr/home/dg/bopm/src

Modified Files:
	firedns.c 
Log Message:
Fix the ID/Unknown error bug (finally), this needs testing..


Index: firedns.c
===================================================================
RCS file: /data/cvs/bopm/src/firedns.c,v
retrieving revision 1.20
retrieving revision 1.21
diff --unified=6 -r1.20 -r1.21
--- firedns.c	22 Jun 2003 13:19:39 -0000	1.20
+++ firedns.c	12 Jan 2004 18:12:48 -0000	1.21
@@ -594,62 +594,43 @@
    }
 
    /* query not found */
    if(c == NULL)
       return &result;
 
-   /* query found-- pull from list: */
-   list_remove(CONNECTIONS, node);
-   node_free(node);
+   /* query found -- we remove in cleanup */
 
    l = recv(c->fd,&h,sizeof(struct s_header),0);
-   close(c->fd);
-   fdns_fdinuse--;
    result.info = (void *) c->info;
    strncpy(result.lookup, c->lookup, 256);
 
    if(l == -1)
    {
       fdns_errno = FDNS_ERR_NETWORK;
-      MyFree(c);
-      return &result;
+      goto cleanup;
    }
 
    if (l < 12)
-   {
-      MyFree(c);
-      return &result;
-   }
+      goto cleanup;
    if (c->id[0] != h.id[0] || c->id[1] != h.id[1])
-   {
-      /* ID mismatch */
-      MyFree(c);
-      return &result;
-   }
+      /* ID mismatch: we keep the connection, as this could be an answer to
+         a previous lookup.. */
+      return NULL;
    if ((h.flags1 & FLAGS1_MASK_QR) == 0)
-   {
-      MyFree(c);
-      return &result;
-   }
+      goto cleanup;
    if ((h.flags1 & FLAGS1_MASK_OPCODE) != 0)
-   {
-      MyFree(c);
-      return &result;
-   }
+      goto cleanup;
    if ((h.flags2 & FLAGS2_MASK_RCODE) != 0)
    {
       fdns_errno = (h.flags2 & FLAGS2_MASK_RCODE);
-      MyFree(c);
-      return &result;
+      goto cleanup;
    }
    h.ancount = ntohs(h.ancount);
    if (h.ancount < 1)
-   { /* no sense going on if we don't have any answers */
-      MyFree(c);
-      return &result;
-   }
+   /* no sense going on if we don't have any answers */
+      goto cleanup;
    /* skip queries */
    i = 0;
    q = 0;
    l -= 12;
    h.qdcount = ntohs(h.qdcount);
    while (q < h.qdcount && i < l)
@@ -691,16 +672,13 @@
             }
             else
                i += h.payload[i] + 1; /* skip length and label */
          }
       }
       if (l - i < 10)
-      {
-         MyFree(c);
-         return &result;
-      }
+         goto cleanup;
       rr = (struct s_rr_middle *)&h.payload[i];
       src = (char *) rr;
       dst = (char *) &rrbacking;
       for (bytes = sizeof(rrbacking); bytes; bytes--)
          *dst++ = *src++;
       rr = &rrbacking;
@@ -718,24 +696,30 @@
          i += rr->rdlength;
          continue;
       }
       break;
    }
 
-   MyFree(c);
-
    if (curanswer == h.ancount)
-      return &result;
+      goto cleanup;
    if (i + rr->rdlength > l)
-      return &result;
+      goto cleanup;
    if (rr->rdlength > 1023)
-      return &result;
+      goto cleanup;
 
    fdns_errno = FDNS_ERR_NONE;
    memcpy(result.text,&h.payload[i],rr->rdlength);
    result.text[rr->rdlength] = '\0';
+
+   /* Clean-up */
+cleanup:
+   list_remove(CONNECTIONS, node);
+   node_free(node);
+   close(c->fd);
+   fdns_fdinuse--;
+   MyFree(c);
 
    return &result;
 }
 
 void firedns_cycle(void)
 {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.