[go: up one dir, main page]

File: dns.c

package info (click to toggle)
dillo 0.8.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,168 kB
  • ctags: 3,987
  • sloc: ansic: 32,778; sh: 3,401; makefile: 251; perl: 17
file content (546 lines) | stat: -rw-r--r-- 13,895 bytes parent folder | download
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 * File: dns.c
 *
 * Copyright (C) 1999, 2000, 2001 Jorge Arellano Cid <jcid@dillo.org>
 *
 * 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 of the License, or
 * (at your option) any later version.
 */

/*
 * Non blocking pthread-handled Dns scheme
 */

#include <pthread.h>
#include <glib.h>

#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>

#include "msg.h"
#include "dns.h"
#include "list.h"

#define DEBUG_LEVEL 5
#include "debug.h"


/*
 * Note: comment the following line for debugging or gprof profiling.
 */
#define G_DNS_THREADED

/*
 * Uncomment the following line for libc5 optimization
 */
/* #define LIBC5 */


/* Maximum dns resolving threads */
#ifdef G_DNS_THREADED
#  define G_DNS_MAX_SERVERS 4
#else
#  define G_DNS_MAX_SERVERS 1
#endif


typedef struct {
   gint channel;         /* Index of this channel [0 based] */
   gboolean in_use;      /* boolean to tell if server is doing a lookup */
   gboolean ip_ready;    /* boolean: is IP lookup done? */
   GSList *addr_list;    /* IP address */
   char *hostname;       /* Adress to resolve */
   guint timeout_id;     /* gtk timeout function ID */
#ifdef G_DNS_THREADED
   pthread_t th1;        /* Thread id */
#endif
} DnsServer;

typedef struct {
   char *hostname;          /* host name for cache */
   GSList *addr_list;       /* address of host */
} GDnsCache;

typedef struct {
   gint channel;    /* -2 if waiting, otherwise index to dns_server[] */
   char *hostname;  /* The one we're resolving */
   ChainLink *Info; /* CCC info */
} GDnsQueue;


/*
 * Forward declarations
 */
static gboolean Dns_timeout_client(gpointer data);

/*
 * Local Data
 */
static DnsServer dns_server[G_DNS_MAX_SERVERS];
static gint num_servers;
static GDnsCache *dns_cache;
static gint dns_cache_size, dns_cache_size_max;
static GDnsQueue *dns_queue;
static gint dns_queue_size, dns_queue_size_max;
static gboolean ipv6_enabled;


/* ----------------------------------------------------------------------
 *  Dns queue functions
 */
static void Dns_queue_add(ChainLink *Info, gint channel, const char *hostname)
{
   a_List_add(dns_queue, dns_queue_size, dns_queue_size_max);
   dns_queue[dns_queue_size].Info = Info;
   dns_queue[dns_queue_size].channel = channel;
   dns_queue[dns_queue_size].hostname = g_strdup(hostname);
   dns_queue_size++;
}

/*
 * Find hostname index in dns_queue
 * (if found, returns queue index; -1 if not)
 */
static gint Dns_queue_find(const char *hostname)
{
   gint i;

   for (i = 0; i < dns_queue_size; i++)
      if (!strcmp(hostname, dns_queue[i].hostname))
         return i;

   return -1;
}

/*
 * Given an index, remove an entry from the dns_queue
 */
static void Dns_queue_remove(int index)
{
   gint i;

   DEBUG_MSG(2, "Dns_queue_remove: deleting client [%d] [queue_size=%d]\n",
             index, dns_queue_size);

   if (index < dns_queue_size) {
      g_free(dns_queue[index].hostname);
      --dns_queue_size;         /* you'll find out why ;-) */
      for (i = index; i < dns_queue_size; i++)
         dns_queue[i] = dns_queue[i + 1];
   }
}

/*
 * Debug function
 *
void Dns_queue_print()
{
   gint i;

   MSG("Queue: [");
   for (i = 0; i < dns_queue_size; i++)
      MSG("%d:%s ", dns_queue[i].channel, dns_queue[i].hostname);
   MSG("]\n");
}
 */

/*
 *  Given a CCC Info, remove its client
 */
static void Dns_abort_by_ccc(ChainLink *Info)
{
   gint i;

   for (i = 0; i < dns_queue_size; i++)
      if (dns_queue[i].Info == Info) {
         Dns_queue_remove(i);
         break;
      }
}

/*
 *  Add an IP/hostname pair to Dns-cache
 */
static void Dns_cache_add(char *hostname, GSList *addr_list)
{
   a_List_add(dns_cache, dns_cache_size, dns_cache_size_max);
   dns_cache[dns_cache_size].hostname = g_strdup(hostname);
   dns_cache[dns_cache_size].addr_list = addr_list;
   ++dns_cache_size;
   DEBUG_MSG(1, "Cache objects: %d\n", dns_cache_size);
}


/*
 *  Initializer function
 */
void a_Dns_init(void)
{
   gint i;

   DEBUG_MSG(5, "dillo_dns_init: Here we go!\n");
   dns_queue_size = 0;
   dns_queue_size_max = 16;
   dns_queue = g_new(GDnsQueue, dns_queue_size_max);

   dns_cache_size = 0;
   dns_cache_size_max = 16;
   dns_cache = g_new(GDnsCache, dns_cache_size_max);

   num_servers = G_DNS_MAX_SERVERS;

   /* Initialize servers data */
   for (i = 0; i < num_servers; ++i) {
      dns_server[i].channel = i;
      dns_server[i].in_use = FALSE;
      dns_server[i].ip_ready = FALSE;
      dns_server[i].addr_list = NULL;
      dns_server[i].hostname = NULL;
      dns_server[i].timeout_id = 0;
#ifdef G_DNS_THREADED
      dns_server[i].th1 = (pthread_t) -1;
#endif
   }

   /* IPv6 test */
   ipv6_enabled = FALSE;
#ifdef ENABLE_IPV6
   {
      /* If the IPv6 address family is not available there is no point
         wasting time trying to connect to v6 addresses. */
      int fd = socket(AF_INET6, SOCK_STREAM, 0);
      if (fd >= 0) {
         close(fd);
         ipv6_enabled = TRUE;
      }
   }
#endif
}

/*
 * Allocate a host structure and add it to the list
 */
static GSList *Dns_note_hosts(GSList *list, int af, struct hostent *host)
{
   int i;

   if (host->h_length > DILLO_ADDR_MAX)
      return list;
   for (i = 0; host->h_addr_list[i]; i++) {
      DilloHost *dh = g_new0(DilloHost, 1);
      dh->af = af;
      dh->alen = host->h_length;
      memcpy(&dh->data[0], host->h_addr_list[i], host->h_length);
      list = g_slist_append(list, dh);
   }
   return list;
}

/*
 *  Server function (runs on its own thread)
 */
static void *Dns_server(void *data)
{
   struct hostent *host;
   gint channel = GPOINTER_TO_INT(data);
#ifdef LIBC5
   gint h_err;
   char buff[1024];
   struct hostent sh;
#endif
   GSList *hosts = NULL;

#ifdef G_DNS_THREADED
   /* Set this thread to detached state */
   pthread_detach(dns_server[channel].th1);
#endif

   DEBUG_MSG(3, "Dns_server: starting...\n ch: %d host: %s\n",
             channel, dns_server[channel].hostname);

#ifdef ENABLE_IPV6
   if (ipv6_enabled) {
      host = gethostbyname2(dns_server[channel].hostname, AF_INET6);
      if (host)
         hosts = Dns_note_hosts(hosts, AF_INET6, host);
   }
#endif

#ifdef LIBC5
   host = gethostbyname_r(dns_server[channel].hostname, &sh, buff,
                          sizeof(buff), &h_err);
#else
   host = gethostbyname(dns_server[channel].hostname);
#endif

   if (host)
      hosts = Dns_note_hosts(hosts, AF_INET, host);

   /* write hostname to client */
   DEBUG_MSG(5, "Dns_server [%d]: %s is %p\n", channel,
             dns_server[channel].hostname, hosts);
   dns_server[channel].addr_list = hosts;
   dns_server[channel].ip_ready = TRUE;

   return NULL;                 /* (avoids a compiler warning) */
}


#ifndef G_DNS_THREADED
/*
 *  Blocking server-function (it doesn't use threads)
 */
static void Dns_blocking_server(void)
{
   gint channel = 0;
   struct hostent *host = NULL;
   GSList *hosts = NULL;

   DEBUG_MSG(3, "Dns_blocking_server: starting...\n");
   DEBUG_MSG(3, "Dns_blocking_server: dns_server[%d].hostname = %s\n",
             channel, dns_server[channel].hostname);

#ifdef ENABLE_IPV6
   if (ipv6_enabled) {
      host = gethostbyname2(dns_server[channel].hostname, AF_INET6);
      if (host)
         hosts = Dns_note_hosts(hosts, AF_INET6, host);
   }
#endif

   if (!host) {
      host = gethostbyname(dns_server[channel].hostname);
      if (host == NULL) {
         DEBUG_MSG(3, "--> Dns_blocking_server: gethostbyname NULL return\n");
      } else {
         DEBUG_MSG(3, "--> Dns_blocking_server - good return\n");
         hosts = Dns_note_hosts(hosts, AF_INET, host);
      }
   }

   /* write IP to server data channel */
   DEBUG_MSG(3, "Dns_blocking_server: IP of %s is %p\n",
             dns_server[channel].hostname, hosts);
   dns_server[channel].addr_list = hosts;
   dns_server[channel].ip_ready = TRUE;

   DEBUG_MSG(3, "Dns_blocking_server: leaving...\n");
}
#endif

/*
 *  Request function (spawn a server and let it handle the request)
 */
static void Dns_server_req(gint channel, const char *hostname)
{
   dns_server[channel].in_use = TRUE;
   dns_server[channel].ip_ready = FALSE;

   g_free(dns_server[channel].hostname);
   dns_server[channel].hostname = g_strdup(hostname);

   /* Let's set a timeout client to poll the server channel (5 times/sec) */
   dns_server[channel].timeout_id =
      gtk_timeout_add(200, (GtkFunction)Dns_timeout_client,
                      GINT_TO_POINTER(dns_server[channel].channel));

#ifdef G_DNS_THREADED
   /* Spawn thread */
   pthread_create(&dns_server[channel].th1, NULL, Dns_server,
                  GINT_TO_POINTER(dns_server[channel].channel));
#else
   Dns_blocking_server();
#endif
}

/*
 * Return the IP for the given hostname.
 * Side effect: a thread can be spawned later.
 */
static void Dns_lookup(ChainLink *Info, const char *hostname)
{
   gint i, channel;

   g_return_if_fail (hostname != NULL);

   /* check for cache hit. */
   for (i = 0; i < dns_cache_size; i++)
      if (!strcmp(hostname, dns_cache[i].hostname))
         break;

   /* if it hits already resolved, call the Callback inmediately. */
   if (i < dns_cache_size) {
      a_Dns_ccc(OpSend, 1, FWD, Info, dns_cache[i].addr_list, NULL);
      a_Dns_ccc(OpEnd, 1, FWD, Info, NULL, NULL);
      return;
   }
   if ((i = Dns_queue_find(hostname)) != -1) {
      /* hit in queue, but answer hasn't come back yet. */
      Dns_queue_add(Info, dns_queue[i].channel, hostname);
   } else {
      /* Never requested before -- we must solve it! */

      /* Find a channel we can send the request to */
      for (channel = 0; channel < num_servers; channel++)
         if (!dns_server[channel].in_use)
            break;
      if (channel < num_servers) {
         /* Found a free channel! */
         Dns_queue_add(Info, channel, hostname);
         Dns_server_req(channel, hostname);
      } else {
         /* We'll have to wait for a thread to finish... */
         Dns_queue_add(Info, -2, hostname);
      }
   }
}

/*
 * Give answer to all queued callbacks on this channel
 */
static void Dns_serve_channel(gint channel)
{
   gint i;
   ChainLink *Info;
   DnsServer *srv = &dns_server[channel];

   for (i = 0; i < dns_queue_size; i++) {
      if (dns_queue[i].channel == channel) {
         Info = dns_queue[i].Info;
         if ( srv->addr_list == NULL ) {
            a_Dns_ccc(OpAbort, 1, FWD, Info, NULL, NULL);
         } else {
            a_Dns_ccc(OpSend, 1, FWD, Info, srv->addr_list, NULL);
            a_Dns_ccc(OpEnd, 1, FWD, Info, NULL, NULL);
         }
         Dns_queue_remove(i);
         --i;
      }
   }
   /* set current channel free */
   srv->in_use = FALSE;
}

/*
 * Assign free channels to waiting clients (-2)
 */
static void Dns_assign_channels(void)
{
   gint ch, i, j;

   for (ch = 0; ch < num_servers; ++ch) {
      if (dns_server[ch].in_use == FALSE) {
         /* Find the next query in the queue (we're a FIFO) */
         for (i = 0; i < dns_queue_size; i++)
            if (dns_queue[i].channel == -2)
               break;

         if (i < dns_queue_size) {
            /* assign this channel to every queued request
             * with the same hostname*/
            for (j = i; j < dns_queue_size; j++)
               if (dns_queue[j].channel == -2 &&
                   !strcmp(dns_queue[j].hostname, dns_queue[i].hostname))
                  dns_queue[j].channel = ch;
            Dns_server_req(ch, dns_queue[i].hostname);
         } else
            return;
      }
   }
}

/*
 * This is a Gtk+ timeout function that
 * reads the DNS results and resumes the stopped jobs.
 */
static gboolean Dns_timeout_client(gpointer data)
{
   gint channel = GPOINTER_TO_INT(data);
   DnsServer *srv = &dns_server[channel];

   if ( srv->ip_ready ){
      if (srv->addr_list != NULL) {
         /* DNS succeeded, let's cache it */
         Dns_cache_add(srv->hostname, srv->addr_list);
      }
      Dns_serve_channel(channel);
      Dns_assign_channels();
      srv->timeout_id = 0;
      return FALSE;  /* Done! */
   }

   /* IP not already solved, keep on trying... */
   return TRUE;
}

/*
 * CCC function for the DNS module
 * ( Data1 = hostname | IP; Data2 = {Not used here} )
 */
void a_Dns_ccc(int Op, int Branch, int Dir, ChainLink *Info,
               void *Data1, void *Data2)
{
   gchar *Hostname;

   if ( Branch == 1 ){
      if (Dir == BCK) {
         /* Solve hostname */
         switch (Op) {
         case OpStart:
            Hostname = g_strdup(Data1 ? (gchar *)Data1 : "");
            Info->LocalKey = Hostname;
            Dns_lookup(Info, Hostname);
            break;
         case OpAbort:
            Dns_abort_by_ccc(Info);
            g_free(Info->LocalKey);
            g_free(Info);
            break;
         }
      } else {  /* FWD */
         /* Solve hostname answer */
         switch (Op) {
         case OpSend:
            a_Chain_fcb(OpSend, Info, Data1, NULL);
            break;
         case OpEnd:
            Hostname = Info->LocalKey;
            a_Chain_fcb(OpEnd, Info, NULL, NULL);
            g_free(Hostname);
            break;
         case OpAbort:
            Hostname = Info->LocalKey;
            a_Chain_fcb(OpAbort, Info, NULL, NULL);
            g_free(Hostname);
            break;
         }
      }
   }
}

/*
 *  Dns memory-deallocation
 *  (Call this one at exit time)
 *  The Dns_queue is deallocated at execution time (no need to do that here)
 *  'dns_cache' is the only one that grows dinamically
 */
void a_Dns_freeall(void)
{
   gint i;

   for ( i = 0; i < dns_cache_size; ++i ){
      g_free(dns_cache[i].hostname);
   }
   g_free(dns_cache);
}