summaryrefslogtreecommitdiff
path: root/src/lacaddress.c
blob: 0dba5ea1b9a1406e9759995d14b72a72ce4471f8 (plain)
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */

/*  Lac - Library for asynchronous communication
 *  Copyright (C) 2000, 2001, 2002, 2003  Søren Sandmann
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 */
#define LAC_IPV6_UNSAFE

#include "lac.h"

#include <string.h>

#include "lacdns-messages.h"
#include "lacdns-cache.h"
#include "lacdns-nameserver.h"
#include "lacdns-query.h"
#include "lacdns-config.h"

#include "lacinternals.h"

#include <stdlib.h>

#include "config.h"
#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#endif

/*
 * DNS RFC's:
 * 
 *      1034:   Domain names - concepts and facilities
 *               - standard
 *
 *      1035:   Domain names - implementation and specification
 *              - standard
 *
 *      1101:   DNS Encoding of Network Names and Other Types
 *              - textual representation for resource records
 *
 *      1183:   New DNS RR Definitions
 *              - defines RR type 17-21
 *              - experimental
 *
 *      1637:   DNS NSAP Resource Records
 *              - defines RR type 22
 *              - (Network Service Access Protocol)
 *              - experimental
 *
 *      1876:   A Means for Expressing Location Information
 *              in the Domain Name System
 *              - defines RR type 29
 *              - longitude/altitude
 *              - experimental
 *
 *      1982:   Serial Number Arithmetic
 *              - defines the meaning of the "serial" field in SOA
 *                records
 *              - proposed standard
 *
 *      1995:   Incremental Zone Transfers in DNS
 *              - proposed standard
 *
 *      1996:   A Mechanism for Prompt Notification of
 *              Zone Changes (DNS NOTIFY).
 *              - proposed standards
 *
 *      2136:   Dynamic Updates in the Domain Name System (DNS UPDATE)
 *      
 *      2137:   Secure Domain Name System Dynamic Update
 *
 *      2181:   Clarifications to the DNS Specification
 *              - IP packet header address usage from
 *                multi-homed servers,
 *              - TTLs in sets of records with the same name,
 *                class, and type,
 *              - correct handling of zone cuts,
 *              - three minor issues concerning SOA records and
 *                their use,
 *              - the precise definition of the Time to Live (TTL)
 *              - Use of the TC (truncated) header bit
 *              - the issue of what is an authoritative, or
 *                canonical, name,
 *              - and the issue of what makes a valid DNS label.
 *
 *      2308:   Negative Caching
 *
 *      2535:   Domain Name System Security Extensions
 *
 *      2845:   Secret Key Transaction Authentication for DNS (TSIG)
 */

struct LacAddress
{
    struct in_addr in_address;
};

/* lac_dns_error_quark */
GQuark
lac_dns_error_quark (void)
{
    static GQuark q = 0;
    
    if (q == 0)
	q = g_quark_from_static_string ("lac-dns-error-quark");
    
    return q;
}

/*
 * lac_address_new_lookup_all_from_name ()
 */
typedef struct AddressesLookupInfo {
    gchar *             domain;
    LacAddressesFunc    f;
    gpointer            data;
} AddressesLookupInfo;

static void
get_addresses_callback (const DnsQueryEvent *event, gpointer data)
{
    GPtrArray *addresses = NULL;
    GError *err = NULL;
    AddressesLookupInfo *info = data;
    const GList *list;

    switch (event->type)
    {
    case DNS_QUERY_EVENT_RESULTS:
	addresses = g_ptr_array_new ();
	
	for (list = event->results.results; list; list = list->next)
	{
	    RData *a = list->data;
	    LacAddress *addr =
		lac_address_new_from_a_b_c_d (
                    a->u.a.a, a->u.a.b, a->u.a.c, a->u.a.d);
	    
	    g_ptr_array_add (addresses, addr);
	}
	break;
	
    case DNS_QUERY_EVENT_NO_SUCH_NAME:
	err = g_error_new (
	    LAC_DNS_ERROR, LAC_DNS_ERROR_NO_SUCH_NAME,
	    "The name %s was not found", info->domain);
	break;
	
    case DNS_QUERY_EVENT_NO_DATA:
	err = g_error_new (
	    LAC_DNS_ERROR, LAC_DNS_ERROR_NO_DATA,
	    "No addresses found for %s", info->domain);
	break;
	
    case DNS_QUERY_EVENT_ERROR:
	err = g_error_copy (event->error.err);
	break;
    }

    info->f (addresses, info->data, err);

    g_free (info->domain);
    g_free (info);
    
    if (addresses)
    {
	gint i;
	for (i = 0; i < addresses->len; ++i)
	    lac_address_free (addresses->pdata[i]);
	g_ptr_array_free (addresses, TRUE);
    }
    
    if (err)
	g_error_free (err);
}

void
lac_address_new_lookup_all_from_name (const gchar      *name,
                                      LacAddressesFunc  f,
                                      gpointer          data)
{
    AddressesLookupInfo *info;
    LacAddress *address;
    GError *err = NULL;

    if (!dns_config_initialize (&err))
    {
        f (NULL, data, err);

        g_error_free (err);
        return;
    }
    
    /* check if name is on the "a.b.c.d" form */
    address = lac_address_new_from_string (name);
    if (address)
    {
	GPtrArray *addresses;

        addresses = g_ptr_array_new ();
	g_ptr_array_add (addresses, address);

        f (addresses, data, NULL);

        lac_address_free (address);
        g_ptr_array_free (addresses, TRUE);

        return;
    }

    info = g_new (AddressesLookupInfo, 1);
    info->domain = g_strdup (name);
    info->f = f;
    info->data = data;
    
    dns_query (name, A_TYPE, get_addresses_callback, info);
}

/*
 * lac_address_lookup_from_name()
 */
typedef struct {
    LacAddressFunc      f;
    gpointer            data;
} GetAddressInfo;

static void
dns_addr_callback (const GPtrArray *addresses,
                   gpointer         data,
                   const GError    *err)
{
    GetAddressInfo *info = data;

    if (err)
        info->f (NULL, info->data, err);
    else
        info->f (addresses->pdata[0], info->data, err);

    g_free (info);
}

void
lac_address_new_lookup_from_name (const gchar    *name,
                                  LacAddressFunc  f,
                                  gpointer        data)
{
    GetAddressInfo *info;
    
    g_return_if_fail (name != NULL);
    g_return_if_fail (f != NULL);

    info = g_new (GetAddressInfo, 1);
    info->f = f;
    info->data = data;

    lac_address_new_lookup_all_from_name (name, dns_addr_callback, info);
}

/*
 * lac_address_lookup_name()
 */
typedef struct NameLookupInfo {
    LacAddress  *address;
    LacNameFunc  f;
    gpointer     data;
} NameLookupInfo;

static void
get_name_callback (const DnsQueryEvent *event, gpointer data)
{
    NameLookupInfo *info = data;
    gchar  *name =         NULL;
    GError *err  =         NULL;
    gchar *free_me =       NULL;
    RData *ptr;

    switch (event->type)
    {
    case DNS_QUERY_EVENT_RESULTS:
	ptr = event->results.results->data;
	free_me = name = domain_name_to_string (ptr->u.ptr.ptr_name);
	break;
	
    case DNS_QUERY_EVENT_NO_SUCH_NAME:
        free_me = lac_address_to_string (info->address);
	err = g_error_new (
	    LAC_DNS_ERROR, LAC_DNS_ERROR_NO_SUCH_NAME,
	    "No names found for %s", free_me);
	break;
	
    case DNS_QUERY_EVENT_NO_DATA:
        free_me = lac_address_to_string (info->address);
	err = g_error_new (
	    LAC_DNS_ERROR, LAC_DNS_ERROR_NO_DATA,
	    "No names found for %s", free_me);
	break;
	
    case DNS_QUERY_EVENT_ERROR:
	err = g_error_copy (event->error.err);
	break;
    }

    info->f (name, info->data, err);

    lac_address_free (info->address);
    g_free (info);
    
    if (free_me)
        g_free (free_me);
}

static gchar *
address_to_in_addr_arpa (const LacAddress *addr)
{
    gint a, b, c, d;
    
    g_return_val_if_fail (addr != NULL, NULL);

    lac_address_get_a_b_c_d (addr, &a, &b, &c, &d);

    return g_strdup_printf ("%d.%d.%d.%d.in-addr.arpa.", d, c, b, a);
}

void
lac_address_lookup_name (const LacAddress  *address,
                         LacNameFunc        f,
                         gpointer           data)
{
    NameLookupInfo *info;
    gchar *domain_str;
    GError *err = NULL;

    if (!dns_config_initialize (&err))
    {
        f (NULL, data, err);

        g_error_free (err);
        return;
    }

    domain_str = address_to_in_addr_arpa (address);

    info = g_new (NameLookupInfo, 1);
    info->address = lac_address_copy (address);
    info->f = f;
    info->data = data;

    dns_query (domain_str, PTR_TYPE, get_name_callback, info);
    g_free (domain_str);
}

/*
 * "blocking" versions
 */

/*
 * lac_address_new_from_name_wait()
 */
typedef struct {
    LacAddress *result;
    GError *    err;
    GMainLoop * loop;
} GetHostByNameInfo;

static void
ghbn_callback (const GPtrArray *addresses, gpointer data, const GError *err)
{
    GetHostByNameInfo *ghbn_info = data;

    if (addresses)
    {
        ghbn_info->result = lac_address_copy (addresses->pdata[0]);
        ghbn_info->err    = NULL;
    }
    else
    {
        ghbn_info->result = NULL;
        ghbn_info->err    = g_error_copy (err);
    }

    if (g_main_loop_is_running (ghbn_info->loop))
        g_main_loop_quit (ghbn_info->loop);
}

/*
 * Lookup an address from a name. This function waits for the name
 * to arrive. Note that this function runs a recursive main loop,
 * so for example GTK+ applications will still process expose
 * events etc. while waiting inside this function. Of course
 * this also means that timeouts and idles etc. will still possibly
 * run.
 */
LacAddress *
lac_address_new_from_name_wait (const gchar       *name,
                                GError           **err)
{
    GetHostByNameInfo ghbn_info;
    
    ghbn_info.loop = g_main_loop_new (NULL, FALSE);
    
    lac_address_new_lookup_all_from_name (
        name, ghbn_callback, &ghbn_info);

    g_main_loop_run (ghbn_info.loop);

    g_main_loop_unref (ghbn_info.loop);

    if (ghbn_info.err)
    {
        g_propagate_error (err, ghbn_info.err);
        return NULL;
    }
    else
    {
        return ghbn_info.result;
    }
}

/*
 * lac_address_lookup_name_wait
 */
typedef struct {
    gchar *     result;
    GError *    err;
    GMainLoop * loop;
} GetHostByAddrInfo;

static void
ghba_callback (const gchar *name, gpointer data, const GError *err)
{
    GetHostByAddrInfo *ghba_info = data;

    if (name)
    {
        ghba_info->result = g_strdup (name);
        ghba_info->err = NULL;
    }
    else
    {
        ghba_info->result = NULL;
        ghba_info->err = g_error_copy (err);
    }

    if (g_main_loop_is_running (ghba_info->loop))
        g_main_loop_quit (ghba_info->loop);
}

gchar *
lac_address_lookup_name_wait (const LacAddress  *addr,
                              GError           **err)
{
    GetHostByAddrInfo ghba_info;

    ghba_info.loop = g_main_loop_new (NULL, FALSE);
    
    lac_address_lookup_name (addr, ghba_callback, &ghba_info);

    g_main_loop_run (ghba_info.loop);

    g_main_loop_unref (ghba_info.loop);
    
    if (ghba_info.err)
    {
        g_propagate_error (err, ghba_info.err);
        return NULL;
    }
    else
    {
        return ghba_info.result;
    }
}

LacAddress *
lac_address_allocate (void)
{
    LacAddress *addr = g_new0 (LacAddress, 1);
    
    return addr;
}

LacAddress *
lac_address_copy (const LacAddress *addr)
{
    LacAddress *copy = lac_address_allocate ();
    *copy = *addr;
    return copy;
}

void
lac_address_free (LacAddress *addr)
{
    g_return_if_fail (addr != NULL);
    
    g_free (addr);
}

LacAddress *
lac_address_new_from_a_b_c_d (gchar a,
                              gchar b,
                              gchar c,
                              gchar d)
{
    LacAddress *addr = lac_address_allocate ();
    gchar *addr_bytes = (guchar *)&(addr->in_address.s_addr);
    
    addr_bytes[0] = a;
    addr_bytes[1] = b;
    addr_bytes[2] = c;
    addr_bytes[3] = d;
    
    return addr;
}

/* FIXME: is this correct when there are more than three dots?
 */
LacAddress *
lac_address_new_from_string (const gchar *str)
{
    LacAddress *addr = NULL;
    gchar **strs;
    gint numbers[4];
    gint i;
    
    g_return_val_if_fail (str != NULL, NULL);
    
    strs = g_strsplit (str, ".", 4);
    
    for (i = 0; i < 4; ++i)
    {
        gchar *endptr;
        
        if (!strs[i])
            goto err;
        numbers[i] = strtol (strs[i], &endptr, 10);
        if (*endptr != '\0' || endptr == strs[i])
            goto err;
    }
    if (strs[4])
        goto err;
    
    addr = lac_address_new_from_a_b_c_d (
        numbers[0], numbers[1], numbers[2], numbers[3]);
    
 err:
    g_strfreev (strs);
    
    return addr;
}

void
lac_address_new_lookup_from_localhost (LacAddressFunc f,
                                       gpointer       data)
{
    gchar *name;
    
    name = lac_gethostname ();
    lac_address_new_lookup_from_name (name, f, data);
    g_free (name);
}

typedef struct {
    LacAddress *result;
    GError     *err;
    GMainLoop  *loop;
} NewFromLocalHostInfo;

static void
localhost_callback (const LacAddress *address,
                    gpointer          data,
                    const GError     *err)
{
    NewFromLocalHostInfo *info = data;

    if (address)
    {
        info->result = lac_address_copy (address);
        info->err = NULL;
    }
    else
    {
        info->result = NULL;
        info->err = g_error_copy (err);
    }

    if (g_main_loop_is_running (info->loop))
        g_main_loop_quit (info->loop);
}

LacAddress *
lac_address_new_from_localhost_wait (GError      **err)
{
    NewFromLocalHostInfo info;

    info.loop = g_main_loop_new (NULL, FALSE);

    lac_address_new_lookup_from_localhost (localhost_callback, &info);

    g_main_loop_run (info.loop);

    g_main_loop_unref (info.loop);
    
    if (info.err)
    {
        g_propagate_error (err, info.err);
        return NULL;
    }
    else
    {
        return info.result;
    }
}

guint
lac_address_hash (gconstpointer addr)
{
    return (guint)(((LacAddress *)addr)->in_address.s_addr);
}

gint
lac_address_compare (gconstpointer addr1, gconstpointer addr2)
{
    const LacAddress *a1 = addr1;
    const LacAddress *a2 = addr2;
    
    if (a1->in_address.s_addr == a2->in_address.s_addr)
	return 0;
    if (a1->in_address.s_addr > a2->in_address.s_addr)
	return 1;
    return -1;
}

gboolean
lac_address_equal (gconstpointer addr1, gconstpointer addr2)
{
    const LacAddress *a1 = addr1;
    const LacAddress *a2 = addr2;
    
    return a1->in_address.s_addr == a2->in_address.s_addr;
}

gchar *
lac_address_to_string (const LacAddress *addr) 
{
    guchar *addr_bytes;
    struct in_addr in_addr;
    
    g_return_val_if_fail (addr != NULL, NULL);

    lac_address_get_in_addr (addr, &in_addr);
    
    addr_bytes = (guchar *)&(in_addr.s_addr);
    
    return g_strdup_printf ("%d.%d.%d.%d",
                            addr_bytes[0], addr_bytes[1],
                            addr_bytes[2], addr_bytes[3]);
}

void
lac_address_get_a_b_c_d (const LacAddress *addr,
                         gint             *a,
                         gint             *b,
                         gint             *c,
                         gint             *d)
{
    const guchar *addr_bytes;

    g_return_if_fail (addr != NULL);
    
    addr_bytes = (const guchar *)&(addr->in_address.s_addr);

    if (a)
        *a = addr_bytes[0];
    if (b)
        *b = addr_bytes[1];
    if (c)
        *c = addr_bytes[2];
    if (d)
        *d = addr_bytes[3];
}

void
lac_address_get_in_addr        (const LacAddress *addr,
                                struct in_addr   *in_addr)
{
    *in_addr = addr->in_address;
}

void
lac_address_set_in_addr        (LacAddress     *addr,
                                struct in_addr *in_addr)
{
    addr->in_address = *in_addr;
}