Reads some config file directives; simple test program created.
[adns] / src / adns.c
CommitLineData
a17d3a1d 1/**/
2
964344fc 3#include <stdarg.h>
4#include <stdio.h>
5#include <errno.h>
6#include <string.h>
7#include <stdlib.h>
8
a17d3a1d 9#include <arpa/nameser.h>
964344fc 10#include <sys/socket.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
a17d3a1d 13
14#include "adns-internal.h"
15
16#define LIST_UNLINK(list,node) \
17 do { \
18 if ((node)->back) (node)->back->next= (node)->next; \
19 else (list).head= (node)->next; \
20 if ((node)->next) (node)->next->back= (node)->back; \
21 else (list).tail= (node)->back; \
22 } while(0)
23
24#define LIST_LINK_TAIL(list,node) \
25 do { \
26 (node)->back= 0; \
27 (node)->next= (list).tail; \
964344fc 28 if ((list).tail) (list).tail->back= (node); else (list).head= (node); \
a17d3a1d 29 (list).tail= (node); \
30 } while(0)
31
964344fc 32static void vdebug(adns_state ads, const char *fmt, va_list al) {
33 if (!(ads->iflags & adns_if_debug)) return;
34 fputs("adns debug: ",stderr);
35 vfprintf(stderr,fmt,al);
36 fputc('\n',stderr);
37}
38
39static void debug(adns_state ads, const char *fmt, ...) {
40 va_list al;
41
42 va_start(al,fmt);
43 vdebug(ads,fmt,al);
44 va_end(al);
45}
46
47static void vdiag(adns_state ads, const char *fmt, va_list al) {
48 if (ads->iflags & adns_if_noerrprint) return;
49 fputs("adns: ",stderr);
50 vfprintf(stderr,fmt,al);
51 fputc('\n',stderr);
52}
53
54static void diag(adns_state ads, const char *fmt, ...) {
55 va_list al;
56
57 va_start(al,fmt);
58 vdiag(ads,fmt,al);
59 va_end(al);
60}
61
62static void addserver(adns_state ads, struct in_addr addr) {
a17d3a1d 63 if (ads->nservers>=MAXSERVERS) {
964344fc 64 diag(ads,"too many nameservers, ignoring %s",inet_ntoa(addr));
a17d3a1d 65 } else {
66 ads->servers[ads->nservers].addr= addr;
67 ads->servers[ads->nservers].tcpsocket= -1;
68 ads->nservers++;
69 }
70}
71
964344fc 72static void configparseerr(adns_state ads, const char *fn, int lno,
73 const char *fmt, ...) {
74 va_list al;
75
76 if (ads->iflags & adns_if_noerrprint) return;
77 if (lno==-1) fprintf(stderr,"adns: %s: ",fn);
78 else fprintf(stderr,"adns: %s:%d: ",fn,lno);
79 va_start(al,fmt);
80 vfprintf(stderr,fmt,al);
81 va_end(al);
82 fputc('\n',stderr);
83}
84
85static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) {
86 struct in_addr ia;
87
88 if (!inet_aton(buf,&ia)) {
89 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
90 return;
91 }
92 debug(ads,"using nameserver %s",inet_ntoa(ia));
93 addserver(ads,ia);
94}
95
96static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
97 if (!buf) return;
98 diag(ads,"warning - `search' ignored FIXME");
99}
100
101static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
102 diag(ads,"warning - `sortlist' ignored FIXME");
103}
104
105static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
106 if (!buf) return;
107 diag(ads,"warning - `options' ignored FIXME");
108}
109
110static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
111 ads->nservers= 0;
112}
113
114static const struct configcommandinfo {
115 const char *name;
116 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
117} configcommandinfos[]= {
118 { "nameserver", ccf_nameserver },
119 { "domain", ccf_search },
120 { "search", ccf_search },
121 { "sortlist", ccf_sortlist },
122 { "options", ccf_options },
123 { "clearnameservers", ccf_clearnss },
124 { 0 }
125};
126
127static int whitespace(int c) {
128 return c==' ' || c=='\n' || c=='\t';
129}
130
131static void readconfig(adns_state ads, const char *filename) {
132 char linebuf[2000], *p, *q;
133 FILE *file;
134 int lno, l, c;
135 const struct configcommandinfo *ccip;
136
137 file= fopen(filename,"r");
138 if (!file) {
139 if (errno == ENOENT) {
140 debug(ads,"configuration file `%s' does not exist",filename);
141 return;
142 }
143 diag(ads,"cannot open configuration file `%s': %s",filename,strerror(errno));
144 return;
145 }
146
147 for (lno=1; fgets(linebuf,sizeof(linebuf),file); lno++) {
148 l= strlen(linebuf);
149 if (!l) continue;
150 if (linebuf[l-1] != '\n' && !feof(file)) {
151 diag(ads,"%s:%d: line too long",filename,lno);
152 while ((c= getc(file)) != EOF && c != '\n') { }
153 if (c == EOF) break;
154 continue;
155 }
156 while (l>0 && whitespace(linebuf[l-1])) l--;
157 linebuf[l]= 0;
158 p= linebuf;
159 while (whitespace(*p)) p++;
160 if (*p == '#' || *p == '\n') continue;
161 q= p;
162 while (*q && !whitespace(*q)) q++;
163 for (ccip=configcommandinfos;
164 ccip->name && strncmp(ccip->name,p,q-p);
165 ccip++);
166 if (!ccip->name) {
167 diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p);
168 continue;
169 }
170 while (whitespace(*q)) q++;
171 ccip->fn(ads,filename,lno,q);
172 }
173 if (ferror(file)) {
174 diag(ads,"%s:%d: read error: %s",filename,lno,strerror(errno));
175 }
176 fclose(file);
177}
178
179static const char *instrum_getenv(adns_state ads, const char *envvar) {
180 const char *value;
181
182 value= getenv(envvar);
183 if (!value) debug(ads,"environment variable `%s' not set",envvar);
184 else debug(ads,"environment variable `%s' set to `%s'",envvar,value);
185 return value;
a17d3a1d 186}
187
964344fc 188static void readconfigenv(adns_state ads, const char *envvar) {
a17d3a1d 189 const char *filename;
190
964344fc 191 if (ads->iflags & adns_if_noenv) {
192 debug(ads,"not checking environment variable `%s'",envvar);
193 return;
194 }
195 filename= instrum_getenv(ads,envvar);
196 if (filename) readconfig(ads,filename);
a17d3a1d 197}
198
964344fc 199int adns_init(adns_state *ads_r, adns_initflags flags) {
a17d3a1d 200 adns_state ads;
964344fc 201 const char *res_options, *adns_res_options;
a17d3a1d 202
203 ads= malloc(sizeof(*ads)); if (!ads) return errno;
964344fc 204 ads->input.head= ads->input.tail= 0;
a17d3a1d 205 ads->timew.head= ads->timew.tail= 0;
964344fc 206 ads->childw.head= ads->childw.tail= 0;
207 ads->output.head= ads->output.tail= 0;
a17d3a1d 208 ads->udpsocket= -1;
209 ads->qbufavail= 0;
210 ads->qbuf= 0;
211 ads->tcpbufavail= ads->tcpbufused= ads->tcpbufdone= 0;
212 ads->tcpbuf= 0;
964344fc 213 ads->iflags= flags;
a17d3a1d 214 ads->nservers= 0;
964344fc 215 ads->iflags= flags;
216
217 res_options= instrum_getenv(ads,"RES_OPTIONS");
218 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
219 ccf_options(ads,"RES_OPTIONS",-1,res_options);
220 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
a17d3a1d 221
222 readconfig(ads,"/etc/resolv.conf");
223 readconfigenv(ads,"RES_CONF");
224 readconfigenv(ads,"ADNS_RES_CONF");
964344fc 225
226 ccf_options(ads,"RES_OPTIONS",-1,res_options);
227 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
228
229 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
230 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
231
a17d3a1d 232 if (!ads->nservers) {
964344fc 233 struct in_addr ia;
234 if (ads->iflags & adns_if_debug)
a17d3a1d 235 fprintf(stderr,"adns: no nameservers, using localhost\n");
964344fc 236 ia.s_addr= INADDR_LOOPBACK;
237 addserver(ads,ia);
a17d3a1d 238 }
239
240 *ads_r= ads;
241 return 0;
242}
243
964344fc 244static void query_fail(adns_state ads, adns_query qu, adns_status stat) {
245 struct adns_answer *ans;
a17d3a1d 246
247 ans= qu->answer;
248 if (!ans) ans= malloc(sizeof(*qu->answer));
249 if (ans) {
250 ans->status= stat;
251 ans->cname= 0;
252 ans->type= qu->type;
253 ans->nrrs= 0;
254 }
255 qu->answer= ans;
964344fc 256 LIST_LINK_TAIL(ads->input,qu);
a17d3a1d 257}
258
964344fc 259void adns_interest(adns_state ads,
260 fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
261 int *maxfd, struct timeval **tv_io, struct timeval *tvbuf) {
262 abort(); /* FIXME */
a17d3a1d 263}
264
265int adns_submit(adns_state ads,
266 const char *owner,
267 adns_rrtype type,
268 int flags,
269 void *context,
270 adns_query *query_r) {
271 adns_query qu;
272 adns_status stat;
964344fc 273 int ol;
a17d3a1d 274
275 stat= 0;
276 ol= strlen(owner);
964344fc 277 if (ol>MAXDNAME+1) { stat= adns_s_invaliddomain; ol= 0; }
a17d3a1d 278 if (ol>0 && owner[ol-1]=='.') { flags &= ~adns_f_search; ol--; }
279 qu= malloc(sizeof(*qu)+ol+1); if (!qu) return errno;
280 qu->next= qu->back= qu->parent= qu->child= 0;
281 qu->type= type;
282 qu->answer= 0;
283 qu->flags= flags;
284 qu->context= context;
964344fc 285 qu->udpretries= 0;
a17d3a1d 286 qu->server= 0;
287 memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
288 if (stat) {
289 query_fail(ads,qu,stat);
290 } else {
291 LIST_LINK_TAIL(ads->input,qu);
964344fc 292 adns_interest(ads,0,0,0,0,0,0);
a17d3a1d 293 }
294 *query_r= qu;
964344fc 295
296 abort(); /* FIXME */
a17d3a1d 297}