chiark / gitweb /
Compiles but does not link.
[adns.git] / src / setup.c
index ecf92a2ee03ad57277309be91ccf973cfa746d25..3852603a545db86eea168e01598f709e29e5450e 100644 (file)
@@ -1,36 +1,36 @@
-/**/
-
-#include "adns-internal.h"
-
-static void vdebug(adns_state ads, const char *fmt, va_list al) {
-  if (!(ads->iflags & adns_if_debug)) return;
-  fputs("adns debug: ",stderr);
-  vfprintf(stderr,fmt,al);
-  fputc('\n',stderr);
-}
-
-void adns__debug(adns_state ads, const char *fmt, ...) {
-  va_list al;
-
-  va_start(al,fmt);
-  vdebug(ads,fmt,al);
-  va_end(al);
-}
-
-static void vdiag(adns_state ads, const char *fmt, va_list al) {
-  if (ads->iflags & adns_if_noerrprint) return;
-  fputs("adns: ",stderr);
-  vfprintf(stderr,fmt,al);
-  fputc('\n',stderr);
-}
-
-void adns__diag(adns_state ads, const char *fmt, ...) {
-  va_list al;
-
-  va_start(al,fmt);
-  vdiag(ads,fmt,al);
-  va_end(al);
-}
+/*
+ * setup.c
+ * - configuration file parsing
+ * - management of global state
+ */
+/*
+ *  This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
+ *  
+ *  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)
+ *  any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  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. 
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <netdb.h>
+#include <arpa/inet.h>
+
+#include "internal.h"
 
 static void addserver(adns_state ads, struct in_addr addr) {
   int i;
@@ -38,20 +38,18 @@ static void addserver(adns_state ads, struct in_addr addr) {
   
   for (i=0; i<ads->nservers; i++) {
     if (ads->servers[i].addr.s_addr == addr.s_addr) {
-      debug(ads,"duplicate nameserver %s ignored",inet_ntoa(addr));
+      adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr));
       return;
     }
   }
   
   if (ads->nservers>=MAXSERVERS) {
-    diag(ads,"too many nameservers, ignoring %s",inet_ntoa(addr));
+    adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr));
     return;
   }
 
   ss= ads->servers+ads->nservers;
   ss->addr= addr;
-  ss->state= server_disc;
-  ss->connw.head= ss->connw.tail= 0;
   ads->nservers++;
 }
 
@@ -75,22 +73,22 @@ static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *
     configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
     return;
   }
-  debug(ads,"using nameserver %s",inet_ntoa(ia));
+  adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia));
   addserver(ads,ia);
 }
 
 static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
   if (!buf) return;
-  diag(ads,"warning - `search' ignored FIXME");
+  adns__diag(ads,-1,0,"warning - `search' ignored fixme");
 }
 
 static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
-  diag(ads,"warning - `sortlist' ignored FIXME");
+  adns__diag(ads,-1,0,"warning - `sortlist' ignored fixme");
 }
 
 static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
   if (!buf) return;
-  diag(ads,"warning - `options' ignored FIXME");
+  adns__diag(ads,-1,0,"warning - `options' ignored fixme");
 }
 
 static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
@@ -110,9 +108,6 @@ static const struct configcommandinfo {
   {  0                                   }
 };
 
-static int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
-static int ctype_digit(int c) { return c>='0' && c<='9'; }
-
 static void readconfig(adns_state ads, const char *filename) {
   char linebuf[2000], *p, *q;
   FILE *file;
@@ -122,10 +117,11 @@ static void readconfig(adns_state ads, const char *filename) {
   file= fopen(filename,"r");
   if (!file) {
     if (errno == ENOENT) {
-      debug(ads,"configuration file `%s' does not exist",filename);
+      adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename);
       return;
     }
-    diag(ads,"cannot open configuration file `%s': %s",filename,strerror(errno));
+    adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
+              filename,strerror(errno));
     return;
   }
 
@@ -133,7 +129,7 @@ static void readconfig(adns_state ads, const char *filename) {
     l= strlen(linebuf);
     if (!l) continue;
     if (linebuf[l-1] != '\n' && !feof(file)) {
-      diag(ads,"%s:%d: line too long",filename,lno);
+      adns__diag(ads,-1,0,"%s:%d: line too long",filename,lno);
       while ((c= getc(file)) != EOF && c != '\n') { }
       if (c == EOF) break;
       continue;
@@ -149,14 +145,15 @@ static void readconfig(adns_state ads, const char *filename) {
         ccip->name && strncmp(ccip->name,p,q-p);
         ccip++);
     if (!ccip->name) {
-      diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p);
+      adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
+                filename,lno,q-p,p);
       continue;
     }
     while (ctype_whitespace(*q)) q++;
     ccip->fn(ads,filename,lno,q);
   }
   if (ferror(file)) {
-    diag(ads,"%s:%d: read error: %s",filename,lno,strerror(errno));
+    adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno));
   }
   fclose(file);
 }
@@ -165,8 +162,8 @@ static const char *instrum_getenv(adns_state ads, const char *envvar) {
   const char *value;
 
   value= getenv(envvar);
-  if (!value) debug(ads,"environment variable %s not set",envvar);
-  else debug(ads,"environment variable %s set to `%s'",envvar,value);
+  if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
+  else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value);
   return value;
 }
 
@@ -174,33 +171,42 @@ static void readconfigenv(adns_state ads, const char *envvar) {
   const char *filename;
 
   if (ads->iflags & adns_if_noenv) {
-    debug(ads,"not checking environment variable `%s'",envvar);
+    adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
     return;
   }
   filename= instrum_getenv(ads,envvar);
   if (filename) readconfig(ads,filename);
 }
+
+
+int adns__setnonblock(adns_state ads, int fd) {
+  int r;
   
-int adns_init(adns_state *ads_r, adns_initflags flags) {
+  r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
+  r |= O_NONBLOCK;
+  r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
+  return 0;
+}
+
+int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
   adns_state ads;
   const char *res_options, *adns_res_options;
   struct protoent *proto;
   int r;
   
   ads= malloc(sizeof(*ads)); if (!ads) return errno;
-  ads->tosend.head= ads->tosend.tail= 0;
-  ads->timew.head= ads->timew.tail= 0;
-  ads->childw.head= ads->childw.tail= 0;
-  ads->output.head= ads->output.tail= 0;
-  ads->nextid= 0x311f;
-  ads->udpsocket= -1;
-  ads->qbufavail= 0;
-  ads->qbuf= 0;
-  ads->tcpbufavail= ads->tcpbufused= ads->tcpbufdone= 0;
-  ads->tcpbuf= 0;
-  ads->iflags= flags;
-  ads->nservers= 0;
   ads->iflags= flags;
+  ads->diagfile= diagfile ? diagfile : stderr;
+  LIST_INIT(ads->timew);
+  LIST_INIT(ads->childw);
+  LIST_INIT(ads->output);
+  ads->nextid= 0x311f;
+  ads->udpsocket= ads->tcpsocket= -1;
+  adns__vbuf_init(&ads->tcpsend);
+  adns__vbuf_init(&ads->tcprecv);
+  ads->nservers= ads->tcpserver= 0;
+  ads->tcpstate= server_disconnected;
+  timerclear(&ads->tcptimeout);
 
   res_options= instrum_getenv(ads,"RES_OPTIONS");
   adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
@@ -227,7 +233,10 @@ int adns_init(adns_state *ads_r, adns_initflags flags) {
 
   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
   ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
-  if (!ads->udpsocket) { r= errno; goto x_closeudp; }
+  if (ads->udpsocket<0) { r= errno; goto x_free; }
+
+  r= adns__setnonblock(ads,ads->udpsocket);
+  if (r) { r= errno; goto x_closeudp; }
   
   *ads_r= ads;
   return 0;
@@ -240,5 +249,5 @@ int adns_init(adns_state *ads_r, adns_initflags flags) {
 }
 
 int adns_finish(adns_state ads) {
-  abort(); /* FIXME */
+  abort(); /* fixme */
 }