API: Reject unknown flags
[adns] / src / setup.c
index 09f044b..fd20c43 100644 (file)
@@ -5,14 +5,15 @@
  */
 /*
  *  This file is part of adns, which is
- *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
+ *    Copyright (C) 1997-2000,2003,2006,2014  Ian Jackson
+ *    Copyright (C) 2014  Mark Wooding
  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
  *    Copyright (C) 1991 Massachusetts Institute of Technology
  *  (See the file INSTALL for full details.)
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2, or (at your option)
+ *  the Free Software Foundation; either version 3, or (at your option)
  *  any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
@@ -21,8 +22,7 @@
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software Foundation,
- *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
+ *  along with this program; if not, write to the Free Software Foundation.
  */
 
 #include <stdlib.h>
@@ -567,6 +567,10 @@ static int init_begin(adns_state *ads_r, adns_initflags flags,
   adns_state ads;
   pid_t pid;
   
+  if (flags & ~(adns_initflags)(0x4fff))
+    /* 0x4000 is reserved for `harmless' future expansion */
+    return ENOSYS;
+
   ads= malloc(sizeof(*ads)); if (!ads) return errno;
 
   ads->iflags= flags;
@@ -580,7 +584,7 @@ static int init_begin(adns_state *ads_r, adns_initflags flags,
   LIST_INIT(ads->intdone);
   ads->forallnext= 0;
   ads->nextid= 0x311f;
-  ads->nudp= 0;
+  ads->nudpsockets= 0;
   ads->tcpsocket= -1;
   adns__vbuf_init(&ads->tcpsend);
   adns__vbuf_init(&ads->tcprecv);
@@ -604,7 +608,7 @@ static int init_finish(adns_state ads) {
   struct sockaddr_in sin;
   struct protoent *proto;
   struct udpsocket *udp;
-  int i, j;
+  int i;
   int r;
   
   if (!ads->nservers) {
@@ -618,16 +622,16 @@ static int init_finish(adns_state ads) {
   }
 
   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
-  ads->nudp= 0;
+  ads->nudpsockets= 0;
   for (i=0; i<ads->nservers; i++) {
     if (adns__udpsocket_by_af(ads, ads->servers[i].addr.sa.sa_family))
       continue;
-    assert(ads->nudp < MAXUDP);
-    udp= &ads->udpsocket[ads->nudp];
+    assert(ads->nudpsockets < MAXUDP);
+    udp= &ads->udpsockets[ads->nudpsockets];
     udp->af= ads->servers[i].addr.sa.sa_family;
     udp->fd= socket(udp->af,SOCK_DGRAM,proto->p_proto);
     if (udp->fd < 0) { r= errno; goto x_free; }
-    ads->nudp++;
+    ads->nudpsockets++;
     r= adns__setnonblock(ads,udp->fd);
     if (r) { r= errno; goto x_closeudp; }
   }
@@ -635,7 +639,7 @@ static int init_finish(adns_state ads) {
   return 0;
 
  x_closeudp:
-  for (j=0; j<ads->nudp; j++) close(ads->udpsocket[j].fd);
+  for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd);
  x_free:
   free(ads);
   return r;
@@ -752,7 +756,7 @@ void adns_finish(adns_state ads) {
     else if (ads->intdone.head) adns__cancel(ads->output.head);
     else break;
   }
-  for (i=0; i<ads->nudp; i++) close(ads->udpsocket[i].fd);
+  for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd);
   if (ads->tcpsocket >= 0) close(ads->tcpsocket);
   adns__vbuf_free(&ads->tcpsend);
   adns__vbuf_free(&ads->tcprecv);