File: hostlist.c

package info (click to toggle)
puf 1.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 832 kB
  • ctags: 688
  • sloc: ansic: 4,523; sh: 3,369; makefile: 56
file content (348 lines) | stat: -rw-r--r-- 8,537 bytes parent folder | download | duplicates (2)
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
/* 
 * Copyright (C) 2000-2003 by Oswald Buddenhagen <puf@ossi.cjb.net>
 * based on puf 0.1.x (C) 1999,2000 by Anders Gavare <gavare@hotmail.com>
 *
 * You may modify and distribute this code under the terms of the GPL.
 * There is NO WARRANTY of any kind. See COPYING for details.
 *
 * hostlist.c - manage the hostname cache
 *
 */

#include "puf.h"

int always_primary_name;

static volatile host_t *hostlist;	/*  list of known hosts  */
static volatile int real_num_hosts;

whost_t *queue_dns_lookup;	/*  waiting for start of lookup  */
dnsproc_t *list_dns_busy, *list_dns_idle;	/*  helper processes  */


#define hhash(hp) calc_hash((u_char*)hp->name)

/*  create new hostname structure  */
static host_t *
add_host(const char *name, int len)
{
    host_t *h;
    int hash;

    if ((h = mmalloc(sizeof(host_t) + len))) {
	memcpy(h->name, name, len);
	hash = calc_nhash((u_char*)name, len - 1);
	h_add(hostlist, real_num_hosts, host_t, h, hash, free(h); return 0;, hhash);
    }
    return h;
}


/*  tolower() on whole string  */
static int 
lcase(char *d, const char *s)
{
    int l;

    for (l = 0; (d[l] = tolower((int)s[l])); l++);
    return l + 1;
}


/*  FIXME: maybe, we should use inet_aton (or inet_addr) before doing 
    the gethostbyname magic. this would save the forks for numerical
    input. also, gethostbyname reportedly fails addresses like
    http://2165339403/~f98anga/ on openbsd  */

/*  return a cached host entry for the given host  */
host_t *
host_lookup_fast(const char *name, int namlen)
{
    int hash = calc_nhash((u_char*)name, namlen);

#define hcmp(h) !memcmp(name, h->name, namlen + 1)
#define hfnd(h) return h;

    h_find(hostlist, real_num_hosts, host_t, hash, hcmp, hfnd);
    return 0;
}


/*  starts an asynchronous dns lookup for the given host  */
/*  the first name referred to determines the local directory 
    name for this host.  */
whost_t *
host_lookup_full(const char *name, int namlen)
{
    host_t *h;
    whost_t *nwh;

    dbg(DNS, ("host_lookup_full for '%.*s'\n", namlen, name));

    if (!(nwh = mmalloc(sizeof(*nwh))))
	return 0;

    if (!(h = add_host(name, namlen + 1))) {
	free(nwh);
	return 0;
    }

    h->info = (hinfo_t *)nwh;
    h->ready = 0;

    nwh->urls = 0;
    nwh->num_proxies = 0;
    nwh->host = h;
    cq_append(queue_dns_lookup, nwh); /* XXX optimize idle case */

    return nwh;
}

int 
start_lookup(dnsproc_t *proc)
{
    int l = strlen(proc->whost->host->name) + 1;
    dbg(DNS, ("starting dns lookup, helper %d\n", proc->pid));
    if (send(proc->fd, proc->whost->host->name, l, 0) != l) {
	prx(ERR, "cannot talk to DNS helper!\n");
	return 0;
    }
    return 1;
}

int 
finish_lookup(dnsproc_t *proc)
{
    whost_t *wh;
    host_t *h;
    hinfo_t *hi;
    int i, na, cp, nhs, hapi;
    host_t *hs[16];
    u_char buf[1024];

    dbg(DNS, ("finishing dns lookup, helper %d\n", proc->pid));
    if (recv(proc->fd, buf, sizeof(buf), 0) < (int)sizeof(int)) {
	prx(ERR, "cannot talk to DNS helper!\n");
	return 0;
    }
    wh = proc->whost;
    if (!wh->host)
	return 1;
    dbg(DNS, (" request originator was %s\n", wh->host->name));
    na = (int)(buf[0]);
    if (!na) {
	prx(ERR, "DNS lookup for '%s' failed!\n", wh->host->name);
	goto badhost;
    }
    if (na < 0) {
	prx(ERR, "DNS lookup for '%s' timed out!\n", wh->host->name);
	goto badhost;
    }
    if (((int *)buf)[1] != sizeof(struct in_addr)) {
	prx(ERR, "cannot handle address returned for '%s'!\n", wh->host->name);
	goto badhost;
    }

    for (cp = 2 * sizeof(int) + na * sizeof(struct in_addr),
	 nhs = 0, hi = 0, hapi = 0;
	 buf[cp]; cp += buf[cp] + 1)
    {
	if (nhs >= (int)(sizeof(hs)/sizeof(hs[0])) - 1) {
	    prx(WRN, "lookup of '%s' yielded too many aliases\n", wh->host->name);
	    break;
	}

	if (!memcmp(wh->host->name, buf + cp + 1, buf[cp])) {
	    dbg(DNS, (" entry %d is origin\n", nhs));
	    hapi = 1;
	    hs[nhs++] = wh->host;
	} else if ((h = host_lookup_fast((const char*)(buf + cp + 1), buf[cp] - 1))) {
	    if (h->ready) {
		dbg(DNS, (" entry %d (%s) is already resolved\n",
			  nhs, h->name));
		if (hi && h->info != hi) {
		    prx(WRN, "inconsistent DNS info for '%s'\n", h->name);
		    continue;
		}
		hi = h->info;
	    } else
		dbg(DNS, (" entry %d (%s) has resolution pending\n",
			  nhs, h->name));
	    hs[nhs++] = h;
	} else {
	    dbg(DNS, (" entry %d (%s) is new\n", nhs, buf + cp + 1));
	    if (!(h = add_host((const char *)(buf + cp + 1), buf[cp]))) {
		if (!nhs)
		    goto badhost;
		continue;
	    } else {
		h->ready = 0;
		h->info = 0;
		hs[nhs++] = h;
	    }
	}
    }

    if (!hapi)
	hs[nhs++] = wh->host;

    /*  possibly create new hostinfo structure ...  */
  if (!hi && (hi = mmalloc(sizeof(hinfo_t) + na * sizeof(haddr_t)))) {
    /*  ... and initialize it  */
    hi->name = hs[0]->name;
    hi->lname = always_primary_name ? hs[0]->name : wh->host->name;
    hi->is_http11 = 1;
    hi->cur_ip = 0;
    hi->num_ips = na;
    /*  copy list of ip-addresses and initialize retry counters  */
    for (i = 0; i < na; i++) {
	hi->ips[i].addr = ((struct in_addr *)(((int *)buf) + 2))[i];
	hi->ips[i].retry_time = 0;
	hi->ips[i].last_errt = 0;
	hi->ips[i].attempt = 0;
    }
  }

    for (i = 0; i < nhs; i++)
	if (!hs[i]->ready) {
	    wh = (whost_t *)hs[i]->info;
	    hs[i]->info = hi;
	    hs[i]->ready = 1;
	    if (wh)
		finish_whost(wh);
	}

    return 1;

badhost:
    wh->host->info = 0;
    wh->host->ready = 1;
    finish_whost(wh);
    return 1;
}

#include <setjmp.h>

jmp_buf alrmjmp;

static void
sigalrm(int n)
{
    (void)n;
    longjmp(alrmjmp, 1);
}

dnsproc_t *
fork_dnsproc()
{
    dnsproc_t *proc;
    struct hostent *he;
    int hl, i, na, pid, fds[2];
    u_int cp;
    sigset_t ss, oss; 
    char buf[1024];

    dbg(DNS, ("forking new dns helper\n"));

    if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) &&
	(!free_fd(0) ||
	 (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) &&
	  (!free_fd(0) ||
           socketpair(PF_UNIX, SOCK_STREAM, 0, fds)))))
	return 0;

    sigfillset(&ss);
    sigprocmask(SIG_SETMASK, &ss, &oss);
    if ((pid = fork())) {
	sigprocmask(SIG_SETMASK, &oss, 0);
	close(fds[1]);
	if (pid < 0) {
            dbg(DNS, ("  failed\n"));
	    close(fds[0]);
	    return 0;
	} else {
            dbg(DNS, ("  pid = %d\n", pid));
	    if (!(proc = mmalloc(sizeof(*proc)))) {
		kill(pid, SIGTERM);
		waitpid(pid, 0, 0);
		close(fds[0]);
		return 0;
	    }
	    proc->fd = fds[0];
	    proc->pid = pid;
	    return proc;
	}
    }
    signal(SIGTERM, SIG_DFL);
    signal(SIGINT, SIG_DFL);
    sigprocmask(SIG_SETMASK, &oss, 0);
    close(fds[0]);

    for (;;) {
	if (!setjmp(alrmjmp)) {
            dbg(DNS, ("dns helper %d: awaiting request\n", getpid()));
	    if (read(fds[1], buf, sizeof(buf)) <= 0) {
	      if (getppid() != 1)
		prx(ERR, "DNS helper: cannot read control socket!\n");
		exit(1);
	    }
            dbg(DNS, ("dns helper %d: looking up '%s'\n", getpid(), buf));
	    signal(SIGALRM, (void (*)(int))sigalrm);
	    alarm(timeout_dns);
	    he = gethostbyname(buf);
	    alarm(0);
	    if (!he) {
                dbg(DNS, ("dns helper %d: lookup failed\n", getpid()));
		buf[0] = 0;
		if (write(fds[1], buf, sizeof(int)) != sizeof(int)) {
		  if (getppid() != 1)
		    prx(ERR, "DNS helper: cannot write control socket!\n");
		    exit(1);
		}
	    } else {
		/*  count ip-addresses for this name  */
		for (na = 0, cp = 2 * sizeof(int); he->h_addr_list[na]; 
		     na++, cp += he->h_length)
		    memcpy(buf + cp, he->h_addr_list[na], he->h_length);
                dbg(DNS, ("dns helper %d: lookup successful, %d addresses\n", getpid(), na));
		buf[0] = na;
		((int *)buf)[1] = he->h_length;

		/*  copy name and aliases  */
		hl = lcase(buf + cp + 1, he->h_name);
		buf[cp] = hl;
		cp += hl + 1;
		for (i = 0; he->h_aliases[i]; i++) {
		    hl = lcase(buf + cp + 1, he->h_aliases[i]);
		    buf[cp] = hl;
		    cp += hl + 1;
		}
		buf[cp++] = 0;

		if (write(fds[1], buf, cp) != (int)cp) {
		  if (getppid() != 1)
		    prx(ERR, "DNS helper: cannot write control socket!\n");
		    exit(1);
		}
	    }
	} else {
            dbg(DNS, ("dns helper %d: lookup timed out\n", getpid()));
	    buf[0] = -1;
	    if (write(fds[1], buf, sizeof(int)) != sizeof(int)) {
	      if (getppid() != 1)
		prx(ERR, "DNS helper: cannot write control socket!\n");
		exit(1);
	    }
	}
    }
}

void
reap_dnsproc(dnsproc_t *proc)
{
    dbg(DNS, ("reaping dns helper %d\n", proc->pid));
    kill(proc->pid, SIGTERM);
    waitpid(proc->pid, 0, 0);
    close(proc->fd);
    free(proc);
}