File: dns.c

package info (click to toggle)
dircproxy 1.0.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,108 kB
  • ctags: 738
  • sloc: ansic: 9,451; sh: 2,672; makefile: 113; perl: 70
file content (397 lines) | stat: -rw-r--r-- 9,683 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* dircproxy
 * Copyright (C) 2002 Scott James Remnant <scott@netsplit.com>.
 * All Rights Reserved.
 *
 * dns.c
 *  - non-blocking DNS lookups using callbacks
 *  - wrappers around /etc/services lookup functions
 *
 * The non-blocking stuff is a little complex, but it means that the main
 * loop can continue while waiting for DNS requests to complete.  Completion
 * of a DNS request is notified by the child death signal, so it will
 * interrupt the main loop to continue where you left off.
 * --
 * @(#) $Id: dns.c,v 1.14 2001/12/21 20:15:55 keybuk Exp $
 *
 * This file is distributed according to the GNU General Public
 * License.  For full details, read the top of 'main.c' or the
 * file called COPYING that was distributed with this code.
 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <netdb.h>

#include <dircproxy.h>
#include "sprintf.h"
#include "dns.h"

/* Structure used to hold information about a dns child */
struct dnschild {
  pid_t pid;
  int pipe;

  struct in_addr addr;

  void (*function)(void *, void *, struct in_addr *, const char *);
  void *boundto;
  void *data;

  struct dnschild *next;
};

/* Reply generated by a child */
struct dnsresult {
  int success;
  struct in_addr addr;
  char name[256];
};

/* forward declarations */
static struct dnsresult _dns_lookup(const char *, struct in_addr *);
static int _dns_startrequest(void *, void (*)(void *, void *,
                                              struct in_addr *, const char *),
                             void *, struct in_addr *, const char *);

/* Children */
static struct dnschild *dnschildren = 0;

/* Function that looks up DNS request */
static struct dnsresult _dns_lookup(const char *name, struct in_addr *addr) {
  struct dnsresult res;
  struct hostent *info;
  pid_t pid;

  pid = getpid();

  memset(&res, 0, sizeof(struct dnsresult));
  if (name) {
    debug("%d: Looking up IP for '%s'", pid, name);
    info = gethostbyname(name);
  } else if (addr) {
    debug("%d: Lookup up name for '%s'", pid, inet_ntoa(*addr));
    info = gethostbyaddr((char *)addr, sizeof(struct in_addr), AF_INET);
  } else {
    info = 0;
  }

  if (info) {
    res.success = 1;

    res.addr.s_addr = *((unsigned long *) info->h_addr);
    if (info->h_name) {
      strncpy(res.name, info->h_name, 255);
      res.name[255] = 0;
    }

    debug("%d: Got '%s' (%s)", pid, res.name, inet_ntoa(res.addr));
  } else {
    debug("%d: No result", pid);
  }

  return res;
}

/* Function that starts a non-blocking DNS request. */
static int _dns_startrequest(void *boundto,
                             void (*function)(void *, void *,
                                              struct in_addr *, const char *),
                             void *data, struct in_addr *addr, const char *name)
{
  struct dnschild *child;
  int p[2], wp[2];

  /* Pipe where the result will be placed on success */
  if (pipe(p)) {
    syscall_fail("pipe", "p", 0);
    function(boundto, data, 0, 0);
    return -1;
  }

  /* Pipe to indicate the child can go */
  if (pipe(wp)) {
    close(p[0]);
    close(p[1]);
    syscall_fail("pipe", "wp", 0);
    function(boundto, data, 0, 0);
    return -1;
  }

  /* Allocate and place the child on the list now, to avoid race conditions */
  child = (struct dnschild *)malloc(sizeof(struct dnschild));
  child->pipe = p[0];
  child->function = function;
  child->boundto = boundto;
  child->addr.s_addr = (addr ? addr->s_addr : 0);
  child->data = data;
  child->next = dnschildren;
  dnschildren = child;

  /* Fork */
  child->pid = fork();
  if (child->pid == -1) {
    /* Error */
    syscall_fail("fork", 0, 0);
    dnschildren = child->next;
    close(p[1]);
    close(p[0]);
    close(wp[1]);
    close(wp[0]);
    free(child);
    function(boundto, data, 0, 0);
    return -1;
    
  } else if (child->pid) {
    /* Parent */
    debug("New DNS process started, pid %d", child->pid);
    close(p[1]);
    close(wp[0]);

    /* Send go signal */
    write(wp[1], "go", 2);
    close(wp[1]);
    return 0;

  } else {
    struct dnsresult result;
    char gobuf[2];

    close(p[0]);
    close(wp[1]);
    
    /* Use ALARM to do timeouts */
    signal(SIGALRM, SIG_DFL);
    alarm(g.dns_timeout);

    /* Wait for a go signal */
    read(wp[0], gobuf, 2);
    close(wp[0]);
    
    /* Do the lookup */
    result = _dns_lookup(name, addr);
    if (result.success) {
      /* Succeded, write to our parent and die */
      write(p[1], (void *)&result, sizeof(struct dnsresult));
      exit(0);
    } else {
      /* Didn't succeed */
      exit(1);
    }
  }
}

/* Called to end a DNS request.  0 = Not handled, >0 = Ok, <0 = Error */
int dns_endrequest(pid_t pid, int status) {
  struct dnschild *lastchild, *child;
  struct dnsresult result;
  struct in_addr *addr;
  char *name;
  size_t len;

  /* Check to see whether this was a DNS child */
  child = dnschildren;
  lastchild = 0;
  while (child) {
    if (child->pid == pid)
      break;

    lastchild = child;
    child = child->next;
  }
  if (!child)
    return 0;

  /* Remove it from the list */
  if (lastchild) {
    lastchild->next = child->next;
  } else {
    dnschildren = child->next;
  }

  debug("%d: Was a DNS child, getting result", pid);

  /* Parameters to call function with */
  name = 0;
  addr = 0;
  
  /* Read from pipe if child returned normally */
  if (WIFEXITED(status)) {
    if (!WEXITSTATUS(status)) {
      len = read(child->pipe, (void *)&result, sizeof(struct dnsresult));
      if (len != sizeof(struct dnsresult)) {
        syscall_fail("read", 0, 0);
      } else if (result.success) {
        debug("%d: Got result", pid);

        addr = &(result.addr);
        name = result.name;
      } else {
        debug("%d: DNS lookup failed", pid);
      }
    } else {
      debug("%d: DNS lookup returned %d", pid, WEXITSTATUS(status));
    }
  } else if (WIFSIGNALED(status)) {
    debug("%d: DNS lookup terminated with signal %d", pid, WTERMSIG(status));
  } else {
    debug("%d: DNS lookyp terminated abnormally", pid);
  }

  /* If DNS failed, but we were looking up an IP address, fill that */
  if (!addr && child->addr.s_addr) {
    result.addr.s_addr = child->addr.s_addr;
    addr = &(result.addr);
  }

  /* If DNS failed but we have an IP, fill the name in with inet_ntoa() */
  if (addr && (!name || !strlen(name))) {
    char *ip;

    ip = inet_ntoa(*addr);
    strncpy(result.name, ip, 255);
    result.name[255] = 0;

    debug("%d: Changed name to '%s'", pid, result.name);
    name = result.name;
  }

  /* Call the function */
  child->function(child->boundto, child->data, addr, name);

  /* Clean up */
  close(child->pipe);
  free(child);

  return 1;
}

/* Kill off any children associated with an ircproxy */
int dns_delall(void *b) {
  struct dnschild *c, *l;
  int numdone;

  l = 0;
  c = dnschildren;
  numdone = 0;
  
  while (c) {
    if (c->boundto == b) {
      struct dnschild *n;

      n = c->next;
      debug("Killing DNS process %d", c->pid);
      kill(c->pid, SIGKILL);
      close(c->pipe);
      free(c);

      if (l) {
        c = l->next = n;
      } else {
        c = dnschildren = n;
      }
    } else {
      l = c;
      c = c->next;
    }
  }

  return numdone;
}

/* Kill off ALL dns children */
void dns_flush(void) {
  struct dnschild *c;

  c = dnschildren;
  while (c) {
    struct dnschild *n;

    n = c->next;
    debug("Killing DNS process %d", c->pid);
    kill(c->pid, SIGKILL);
    close(c->pipe);
    free(c);
    c = n;
  }

  dnschildren = 0;
}

/* Returns the IP address of a hostname */
int dns_addrfromhost(void *boundto, void *data, const char *name,
                     void (*function)(void *, void *,
                                      struct in_addr *, const char *)) {
  return _dns_startrequest(boundto, function, data, 0, name);
}

/* Returns the hostname of an IP address */
int dns_hostfromaddr(void *boundto, void *data, struct in_addr addr,
                     void (*function)(void *, void *,
                                      struct in_addr *, const char *)) {
  return _dns_startrequest(boundto, function, data, &addr, 0);
}

/* Fill a sockaddr_in from a hostname or hostname:port combo thing */
int dns_filladdr(void *boundto, const char *name,
                 const char *defaultport, int allowcolon,
                 struct sockaddr_in *result,
                 void (*function)(void *, void *,
                                  struct in_addr *, const char *),
                 void *data) {
  char *addr, *port;
  int ret = 0;

  memset(result, 0, sizeof(struct sockaddr_in));
  result->sin_family = AF_INET;
  if (defaultport)
    result->sin_port = dns_portfromserv(defaultport);

  addr = x_strdup(name);

  if (allowcolon) {
    port = strchr(addr, ':');

    if (port) {
      *(port++) = 0;

      if (strlen(port))
        result->sin_port = dns_portfromserv(port);
    }
  }

  ret = _dns_startrequest(boundto, function, data, 0, addr);

  free(addr);
  return ret;
}

/* Returns a network port number for a port as a string */
int dns_portfromserv(const char *serv) {
  struct servent *entry;

  entry = getservbyname(serv, "tcp");
  return (entry ? entry->s_port : htons(atoi(serv)));
}

/* Returns a service name for a network port number */
char *dns_servfromport(int port) {
  struct servent *entry;
  char *str;

  entry = getservbyport(port, "tcp");
  if (entry) {
    str = x_strdup(entry->s_name);
  } else {
    str = x_sprintf("%d", port);
  }

  return str;
}