OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [resolv.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* resolv.c: DNS Resolver
2
 *
3
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
4
 *                     The Silver Hammer Group, Ltd.
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Library General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
*/
11
 
12
/*
13
 * Portions Copyright (c) 1985, 1993
14
 *    The Regents of the University of California.  All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without
17
 * modification, are permitted provided that the following conditions
18
 * are met:
19
 * 1. Redistributions of source code must retain the above copyright
20
 *    notice, this list of conditions and the following disclaimer.
21
 * 2. Redistributions in binary form must reproduce the above copyright
22
 *    notice, this list of conditions and the following disclaimer in the
23
 *    documentation and/or other materials provided with the distribution.
24
 * 4. Neither the name of the University nor the names of its contributors
25
 *    may be used to endorse or promote products derived from this software
26
 *    without specific prior written permission.
27
 *
28
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
 * SUCH DAMAGE.
39
 */
40
 
41
/*
42
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
43
 *
44
 * Permission to use, copy, modify, and distribute this software for any
45
 * purpose with or without fee is hereby granted, provided that the above
46
 * copyright notice and this permission notice appear in all copies, and that
47
 * the name of Digital Equipment Corporation not be used in advertising or
48
 * publicity pertaining to distribution of the document or software without
49
 * specific, written prior permission.
50
 *
51
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
52
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
53
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
54
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
55
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
56
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
57
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
58
 * SOFTWARE.
59
 */
60
 
61
/*
62
 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
63
 *
64
 * Permission to use, copy, modify, and distribute this software for any
65
 * purpose with or without fee is hereby granted, provided that the above
66
 * copyright notice and this permission notice appear in all copies.
67
 *
68
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
69
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
70
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
71
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
72
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
73
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
74
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
75
 * SOFTWARE.
76
 */
77
 
78
/*
79
 *
80
 *  5-Oct-2000 W. Greathouse  wgreathouse@smva.com
81
 *                              Fix memory leak and memory corruption.
82
 *                              -- Every name resolution resulted in
83
 *                                 a new parse of resolv.conf and new
84
 *                                 copy of nameservers allocated by
85
 *                                 strdup.
86
 *                              -- Every name resolution resulted in
87
 *                                 a new read of resolv.conf without
88
 *                                 resetting index from prior read...
89
 *                                 resulting in exceeding array bounds.
90
 *
91
 *                              Limit nameservers read from resolv.conf
92
 *
93
 *                              Add "search" domains from resolv.conf
94
 *
95
 *                              Some systems will return a security
96
 *                              signature along with query answer for
97
 *                              dynamic DNS entries.
98
 *                              -- skip/ignore this answer
99
 *
100
 *                              Include arpa/nameser.h for defines.
101
 *
102
 *                              General cleanup
103
 *
104
 * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
105
 *   partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
106
 *   functions added), IPv6 nameservers are also supported.
107
 *
108
 * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
109
 *   more IPv6 support (IPv6 support for gethostbyaddr();
110
 *   address family parameter and improved IPv6 support for get_hosts_byname
111
 *   and read_etc_hosts; getnameinfo() port from glibc; defined
112
 *   defined ip6addr_any and in6addr_loopback)
113
 *
114
 * 2-Feb-2002 Erik Andersen <andersee@debian.org>
115
 * Added gethostent(), sethostent(), and endhostent()
116
 *
117
 * 17-Aug-2002 Manuel Novoa III <mjn3@codepoet.org>
118
 *   Fixed __read_etc_hosts_r to return alias list, and modified buffer
119
 *   allocation accordingly.  See MAX_ALIASES and ALIAS_DIM below.
120
 *   This fixes the segfault in the Python 2.2.1 socket test.
121
 *
122
 * 04-Jan-2003 Jay Kulpinski <jskulpin@berkshire.rr.com>
123
 *   Fixed __decode_dotted to count the terminating null character
124
 *   in a host name.
125
 *
126
 * 02-Oct-2003 Tony J. White <tjw@tjw.org>
127
 *   Lifted dn_expand() and dependent ns_name_uncompress(), ns_name_unpack(),
128
 *   and ns_name_ntop() from glibc 2.3.2 for compatibility with ipsec-tools
129
 *   and openldap.
130
 *
131
 */
132
 
133
#define __FORCE_GLIBC
134
#include <features.h>
135
#include <string.h>
136
#include <stdio.h>
137
#include <signal.h>
138
#include <errno.h>
139
#include <sys/socket.h>
140
#include <sys/types.h>
141
#include <sys/time.h>
142
#include <netinet/in.h>
143
#include <arpa/inet.h>
144
#include <stdlib.h>
145
#include <unistd.h>
146
#include <resolv.h>
147
#include <netdb.h>
148
#include <ctype.h>
149
#include <arpa/nameser.h>
150
#include <sys/utsname.h>
151
#include <sys/un.h>
152
 
153
#define MAX_RECURSE 5
154
#define REPLY_TIMEOUT 10
155
#define MAX_RETRIES 3
156
#define MAX_SERVERS 3
157
#define MAX_SEARCH 4
158
 
159
#define MAX_ALIASES     5
160
 
161
/* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
162
#define         ALIAS_DIM               (2 + MAX_ALIASES + 1)
163
 
164
#undef DEBUG
165
/*#define DEBUG*/
166
 
167
#ifdef DEBUG
168
#define DPRINTF(X,args...) fprintf(stderr, X, ##args)
169
#else
170
#define DPRINTF(X,args...)
171
#endif /* DEBUG */
172
 
173
 
174
/* Global stuff (stuff needing to be locked to be thread safe)... */
175
extern int __nameservers;
176
extern char * __nameserver[MAX_SERVERS];
177
extern int __searchdomains;
178
extern char * __searchdomain[MAX_SEARCH];
179
 
180
#ifdef __UCLIBC_HAS_THREADS__
181
#include <pthread.h>
182
extern pthread_mutex_t __resolv_lock;
183
# define BIGLOCK        __pthread_mutex_lock(&__resolv_lock)
184
# define BIGUNLOCK      __pthread_mutex_unlock(&__resolv_lock);
185
#else
186
# define BIGLOCK
187
# define BIGUNLOCK
188
#endif
189
 
190
 
191
 
192
/* Structs */
193
struct resolv_header {
194
        int id;
195
        int qr,opcode,aa,tc,rd,ra,rcode;
196
        int qdcount;
197
        int ancount;
198
        int nscount;
199
        int arcount;
200
};
201
 
202
struct resolv_question {
203
        char * dotted;
204
        int qtype;
205
        int qclass;
206
};
207
 
208
struct resolv_answer {
209
        char * dotted;
210
        int atype;
211
        int aclass;
212
        int ttl;
213
        int rdlength;
214
        unsigned char * rdata;
215
        int rdoffset;
216
};
217
 
218
enum etc_hosts_action {
219
    GET_HOSTS_BYNAME = 0,
220
    GETHOSTENT,
221
    GET_HOSTS_BYADDR,
222
};
223
 
224
/* function prototypes */
225
extern int __get_hosts_byname_r(const char * name, int type,
226
                              struct hostent * result_buf,
227
                              char * buf, size_t buflen,
228
                              struct hostent ** result,
229
                              int * h_errnop);
230
extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
231
                              struct hostent * result_buf,
232
                              char * buf, size_t buflen,
233
                              struct hostent ** result,
234
                              int * h_errnop);
235
extern void __open_etc_hosts(FILE **fp);
236
extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
237
                            enum etc_hosts_action action,
238
                            struct hostent * result_buf,
239
                            char * buf, size_t buflen,
240
                            struct hostent ** result,
241
                            int * h_errnop);
242
extern int __dns_lookup(const char * name, int type, int nscount,
243
        char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
244
 
245
extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
246
extern int __decode_dotted(const unsigned char * message, int offset,
247
        char * dest, int maxlen);
248
extern int __length_dotted(const unsigned char * message, int offset);
249
extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
250
extern int __decode_header(unsigned char * data, struct resolv_header * h);
251
extern int __encode_question(struct resolv_question * q,
252
        unsigned char * dest, int maxlen);
253
extern int __decode_question(unsigned char * message, int offset,
254
        struct resolv_question * q);
255
extern int __encode_answer(struct resolv_answer * a,
256
        unsigned char * dest, int maxlen);
257
extern int __decode_answer(unsigned char * message, int offset,
258
        struct resolv_answer * a);
259
extern int __length_question(unsigned char * message, int offset);
260
extern int __open_nameservers(void);
261
extern void __close_nameservers(void);
262
extern int __dn_expand(const u_char *, const u_char *, const u_char *,
263
        char *, int);
264
extern int __ns_name_uncompress(const u_char *, const u_char *,
265
                const u_char *, char *, size_t);
266
extern int __ns_name_ntop(const u_char *, char *, size_t);
267
extern int __ns_name_unpack(const u_char *, const u_char *, const u_char *,
268
               u_char *, size_t);
269
 
270
 
271
#ifdef L_encodeh
272
int __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
273
{
274
        if (maxlen < HFIXEDSZ)
275
                return -1;
276
 
277
        dest[0] = (h->id & 0xff00) >> 8;
278
        dest[1] = (h->id & 0x00ff) >> 0;
279
        dest[2] = (h->qr ? 0x80 : 0) |
280
                ((h->opcode & 0x0f) << 3) |
281
                (h->aa ? 0x04 : 0) |
282
                (h->tc ? 0x02 : 0) |
283
                (h->rd ? 0x01 : 0);
284
        dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
285
        dest[4] = (h->qdcount & 0xff00) >> 8;
286
        dest[5] = (h->qdcount & 0x00ff) >> 0;
287
        dest[6] = (h->ancount & 0xff00) >> 8;
288
        dest[7] = (h->ancount & 0x00ff) >> 0;
289
        dest[8] = (h->nscount & 0xff00) >> 8;
290
        dest[9] = (h->nscount & 0x00ff) >> 0;
291
        dest[10] = (h->arcount & 0xff00) >> 8;
292
        dest[11] = (h->arcount & 0x00ff) >> 0;
293
 
294
        return HFIXEDSZ;
295
}
296
#endif
297
 
298
#ifdef L_decodeh
299
int __decode_header(unsigned char *data, struct resolv_header *h)
300
{
301
        h->id = (data[0] << 8) | data[1];
302
        h->qr = (data[2] & 0x80) ? 1 : 0;
303
        h->opcode = (data[2] >> 3) & 0x0f;
304
        h->aa = (data[2] & 0x04) ? 1 : 0;
305
        h->tc = (data[2] & 0x02) ? 1 : 0;
306
        h->rd = (data[2] & 0x01) ? 1 : 0;
307
        h->ra = (data[3] & 0x80) ? 1 : 0;
308
        h->rcode = data[3] & 0x0f;
309
        h->qdcount = (data[4] << 8) | data[5];
310
        h->ancount = (data[6] << 8) | data[7];
311
        h->nscount = (data[8] << 8) | data[9];
312
        h->arcount = (data[10] << 8) | data[11];
313
 
314
        return HFIXEDSZ;
315
}
316
#endif
317
 
318
#ifdef L_encoded
319
/* Encode a dotted string into nameserver transport-level encoding.
320
   This routine is fairly dumb, and doesn't attempt to compress
321
   the data */
322
 
323
int __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
324
{
325
        int used = 0;
326
 
327
        while (dotted && *dotted) {
328
                char *c = strchr(dotted, '.');
329
                int l = c ? c - dotted : strlen(dotted);
330
 
331
                if (l >= (maxlen - used - 1))
332
                        return -1;
333
 
334
                dest[used++] = l;
335
                memcpy(dest + used, dotted, l);
336
                used += l;
337
 
338
                if (c)
339
                        dotted = c + 1;
340
                else
341
                        break;
342
        }
343
 
344
        if (maxlen < 1)
345
                return -1;
346
 
347
        dest[used++] = 0;
348
 
349
        return used;
350
}
351
#endif
352
 
353
#ifdef L_decoded
354
/* Decode a dotted string from nameserver transport-level encoding.
355
   This routine understands compressed data. */
356
 
357
int __decode_dotted(const unsigned char *data, int offset,
358
                                  char *dest, int maxlen)
359
{
360
        int l;
361
        int measure = 1;
362
        int total = 0;
363
        int used = 0;
364
 
365
        if (!data)
366
                return -1;
367
 
368
        while ((l=data[offset++])) {
369
                if (measure)
370
                    total++;
371
                if ((l & 0xc0) == (0xc0)) {
372
                        if (measure)
373
                                total++;
374
                        /* compressed item, redirect */
375
                        offset = ((l & 0x3f) << 8) | data[offset];
376
                        measure = 0;
377
                        continue;
378
                }
379
 
380
                if ((used + l + 1) >= maxlen)
381
                        return -1;
382
 
383
                memcpy(dest + used, data + offset, l);
384
                offset += l;
385
                used += l;
386
                if (measure)
387
                        total += l;
388
 
389
                if (data[offset] != 0)
390
                        dest[used++] = '.';
391
                else
392
                        dest[used++] = '\0';
393
        }
394
 
395
        /* The null byte must be counted too */
396
        if (measure) {
397
            total++;
398
        }
399
 
400
        DPRINTF("Total decode len = %d\n", total);
401
 
402
        return total;
403
}
404
#endif
405
 
406
#ifdef L_lengthd
407
 
408
int __length_dotted(const unsigned char *data, int offset)
409
{
410
        int orig_offset = offset;
411
        int l;
412
 
413
        if (!data)
414
                return -1;
415
 
416
        while ((l = data[offset++])) {
417
 
418
                if ((l & 0xc0) == (0xc0)) {
419
                        offset++;
420
                        break;
421
                }
422
 
423
                offset += l;
424
        }
425
 
426
        return offset - orig_offset;
427
}
428
#endif
429
 
430
#ifdef L_encodeq
431
int __encode_question(struct resolv_question *q,
432
                                        unsigned char *dest, int maxlen)
433
{
434
        int i;
435
 
436
        i = __encode_dotted(q->dotted, dest, maxlen);
437
        if (i < 0)
438
                return i;
439
 
440
        dest += i;
441
        maxlen -= i;
442
 
443
        if (maxlen < 4)
444
                return -1;
445
 
446
        dest[0] = (q->qtype & 0xff00) >> 8;
447
        dest[1] = (q->qtype & 0x00ff) >> 0;
448
        dest[2] = (q->qclass & 0xff00) >> 8;
449
        dest[3] = (q->qclass & 0x00ff) >> 0;
450
 
451
        return i + 4;
452
}
453
#endif
454
 
455
#ifdef L_decodeq
456
int __decode_question(unsigned char *message, int offset,
457
                                        struct resolv_question *q)
458
{
459
        char temp[256];
460
        int i;
461
 
462
        i = __decode_dotted(message, offset, temp, sizeof(temp));
463
        if (i < 0)
464
                return i;
465
 
466
        offset += i;
467
 
468
        q->dotted = strdup(temp);
469
        q->qtype = (message[offset + 0] << 8) | message[offset + 1];
470
        q->qclass = (message[offset + 2] << 8) | message[offset + 3];
471
 
472
        return i + 4;
473
}
474
#endif
475
 
476
#ifdef L_lengthq
477
int __length_question(unsigned char *message, int offset)
478
{
479
        int i;
480
 
481
        i = __length_dotted(message, offset);
482
        if (i < 0)
483
                return i;
484
 
485
        return i + 4;
486
}
487
#endif
488
 
489
#ifdef L_encodea
490
int __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
491
{
492
        int i;
493
 
494
        i = __encode_dotted(a->dotted, dest, maxlen);
495
        if (i < 0)
496
                return i;
497
 
498
        dest += i;
499
        maxlen -= i;
500
 
501
        if (maxlen < (RRFIXEDSZ+a->rdlength))
502
                return -1;
503
 
504
        *dest++ = (a->atype & 0xff00) >> 8;
505
        *dest++ = (a->atype & 0x00ff) >> 0;
506
        *dest++ = (a->aclass & 0xff00) >> 8;
507
        *dest++ = (a->aclass & 0x00ff) >> 0;
508
        *dest++ = (a->ttl & 0xff000000) >> 24;
509
        *dest++ = (a->ttl & 0x00ff0000) >> 16;
510
        *dest++ = (a->ttl & 0x0000ff00) >> 8;
511
        *dest++ = (a->ttl & 0x000000ff) >> 0;
512
        *dest++ = (a->rdlength & 0xff00) >> 8;
513
        *dest++ = (a->rdlength & 0x00ff) >> 0;
514
        memcpy(dest, a->rdata, a->rdlength);
515
 
516
        return i + RRFIXEDSZ + a->rdlength;
517
}
518
#endif
519
 
520
#ifdef L_decodea
521
int __decode_answer(unsigned char *message, int offset,
522
                                  struct resolv_answer *a)
523
{
524
        char temp[256];
525
        int i;
526
 
527
        i = __decode_dotted(message, offset, temp, sizeof(temp));
528
        if (i < 0)
529
                return i;
530
 
531
        message += offset + i;
532
 
533
        a->dotted = strdup(temp);
534
        a->atype = (message[0] << 8) | message[1];
535
        message += 2;
536
        a->aclass = (message[0] << 8) | message[1];
537
        message += 2;
538
        a->ttl = (message[0] << 24) |
539
                (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
540
        message += 4;
541
        a->rdlength = (message[0] << 8) | message[1];
542
        message += 2;
543
        a->rdata = message;
544
        a->rdoffset = offset + i + RRFIXEDSZ;
545
 
546
        DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
547
 
548
        return i + RRFIXEDSZ + a->rdlength;
549
}
550
#endif
551
 
552
#ifdef L_encodep
553
int __encode_packet(struct resolv_header *h,
554
        struct resolv_question **q,
555
        struct resolv_answer **an,
556
        struct resolv_answer **ns,
557
        struct resolv_answer **ar,
558
        unsigned char *dest, int maxlen)
559
{
560
        int i, total = 0;
561
        int j;
562
 
563
        i = __encode_header(h, dest, maxlen);
564
        if (i < 0)
565
                return i;
566
 
567
        dest += i;
568
        maxlen -= i;
569
        total += i;
570
 
571
        for (j = 0; j < h->qdcount; j++) {
572
                i = __encode_question(q[j], dest, maxlen);
573
                if (i < 0)
574
                        return i;
575
                dest += i;
576
                maxlen -= i;
577
                total += i;
578
        }
579
 
580
        for (j = 0; j < h->ancount; j++) {
581
                i = __encode_answer(an[j], dest, maxlen);
582
                if (i < 0)
583
                        return i;
584
                dest += i;
585
                maxlen -= i;
586
                total += i;
587
        }
588
        for (j = 0; j < h->nscount; j++) {
589
                i = __encode_answer(ns[j], dest, maxlen);
590
                if (i < 0)
591
                        return i;
592
                dest += i;
593
                maxlen -= i;
594
                total += i;
595
        }
596
        for (j = 0; j < h->arcount; j++) {
597
                i = __encode_answer(ar[j], dest, maxlen);
598
                if (i < 0)
599
                        return i;
600
                dest += i;
601
                maxlen -= i;
602
                total += i;
603
        }
604
 
605
        return total;
606
}
607
#endif
608
 
609
#ifdef L_decodep
610
int __decode_packet(unsigned char *data, struct resolv_header *h)
611
{
612
        return __decode_header(data, h);
613
}
614
#endif
615
 
616
#ifdef L_formquery
617
int __form_query(int id, const char *name, int type, unsigned char *packet,
618
                           int maxlen)
619
{
620
        struct resolv_header h;
621
        struct resolv_question q;
622
        int i, j;
623
 
624
        memset(&h, 0, sizeof(h));
625
        h.id = id;
626
        h.qdcount = 1;
627
 
628
        q.dotted = (char *) name;
629
        q.qtype = type;
630
        q.qclass = C_IN; /* CLASS_IN */
631
 
632
        i = __encode_header(&h, packet, maxlen);
633
        if (i < 0)
634
                return i;
635
 
636
        j = __encode_question(&q, packet + i, maxlen - i);
637
        if (j < 0)
638
                return j;
639
 
640
        return i + j;
641
}
642
#endif
643
 
644
#ifdef L_dnslookup
645
 
646
#ifdef __UCLIBC_HAS_THREADS__
647
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
648
# define LOCK   __pthread_mutex_lock(&mylock)
649
# define UNLOCK __pthread_mutex_unlock(&mylock);
650
#else
651
# define LOCK
652
# define UNLOCK
653
#endif
654
 
655
/* Just for the record, having to lock __dns_lookup() just for these two globals
656
 * is pretty lame.  I think these two variables can probably be de-global-ized,
657
 * which should eliminate the need for doing locking here...  Needs a closer
658
 * look anyways. */
659
static int ns=0, id=1;
660
 
661
int __dns_lookup(const char *name, int type, int nscount, char **nsip,
662
                           unsigned char **outpacket, struct resolv_answer *a)
663
{
664
        int i, j, len, fd, pos, rc;
665
        struct timeval tv;
666
        fd_set fds;
667
        struct resolv_header h;
668
        struct resolv_question q;
669
        int retries = 0;
670
        unsigned char * packet = malloc(PACKETSZ);
671
        char *dns, *lookup = malloc(MAXDNAME);
672
        int variant = 0;
673
        struct sockaddr_in sa;
674
#ifdef __UCLIBC_HAS_IPV6__
675
        int v6;
676
        struct sockaddr_in6 sa6;
677
#endif
678
 
679
        fd = -1;
680
 
681
        if (!packet || !lookup || !nscount)
682
            goto fail;
683
 
684
        DPRINTF("Looking up type %d answer for '%s'\n", type, name);
685
 
686
        LOCK;
687
        ns %= nscount;
688
        UNLOCK;
689
 
690
        while (retries++ < MAX_RETRIES) {
691
                if (fd != -1)
692
                        close(fd);
693
 
694
                memset(packet, 0, PACKETSZ);
695
 
696
                memset(&h, 0, sizeof(h));
697
 
698
                /* Mess with globals while under lock */
699
                LOCK;
700
                ++id;
701
                id &= 0xffff;
702
                h.id = id;
703
                dns = nsip[ns];
704
                UNLOCK;
705
 
706
                h.qdcount = 1;
707
                h.rd = 1;
708
 
709
                DPRINTF("encoding header\n", h.rd);
710
 
711
                i = __encode_header(&h, packet, PACKETSZ);
712
                if (i < 0)
713
                        goto fail;
714
 
715
                strncpy(lookup,name,MAXDNAME);
716
                BIGLOCK;
717
                if (variant < __searchdomains && strchr(lookup, '.') == NULL)
718
                {
719
                    strncat(lookup,".", MAXDNAME);
720
                    strncat(lookup,__searchdomain[variant], MAXDNAME);
721
                }
722
                BIGUNLOCK;
723
                DPRINTF("lookup name: %s\n", lookup);
724
                q.dotted = (char *)lookup;
725
                q.qtype = type;
726
                q.qclass = C_IN; /* CLASS_IN */
727
 
728
                j = __encode_question(&q, packet+i, PACKETSZ-i);
729
                if (j < 0)
730
                        goto fail;
731
 
732
                len = i + j;
733
 
734
                DPRINTF("On try %d, sending query to port %d of machine %s\n",
735
                                retries, NAMESERVER_PORT, dns);
736
 
737
#ifdef __UCLIBC_HAS_IPV6__
738
                v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
739
                fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
740
#else
741
                fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
742
#endif
743
                if (fd < 0) {
744
                    continue;
745
                }
746
 
747
                /* Connect to the UDP socket so that asyncronous errors are returned */
748
#ifdef __UCLIBC_HAS_IPV6__
749
                if (v6) {
750
                    sa6.sin6_family = AF_INET6;
751
                    sa6.sin6_port = htons(NAMESERVER_PORT);
752
                    /* sa6.sin6_addr is already here */
753
                    rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
754
                } else {
755
#endif
756
                    sa.sin_family = AF_INET;
757
                    sa.sin_port = htons(NAMESERVER_PORT);
758
                    sa.sin_addr.s_addr = inet_addr(dns);
759
                    rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
760
#ifdef __UCLIBC_HAS_IPV6__
761
                }
762
#endif
763
                if (rc < 0) {
764
                    if (errno == ENETUNREACH) {
765
                        /* routing error, presume not transient */
766
                        goto tryall;
767
                    } else
768
                        /* retry */
769
                        continue;
770
                }
771
 
772
                DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
773
                                len, h.id, h.qr);
774
 
775
                send(fd, packet, len, 0);
776
 
777
                FD_ZERO(&fds);
778
                FD_SET(fd, &fds);
779
                tv.tv_sec = REPLY_TIMEOUT;
780
                tv.tv_usec = 0;
781
                if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
782
                    DPRINTF("Timeout\n");
783
 
784
                        /* timed out, so retry send and receive,
785
                         * to next nameserver on queue */
786
                        goto again;
787
                }
788
 
789
                i = recv(fd, packet, 512, 0);
790
                if (i < HFIXEDSZ) {
791
                        /* too short ! */
792
                        goto again;
793
                }
794
 
795
                __decode_header(packet, &h);
796
 
797
                DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
798
 
799
                LOCK;
800
                if ((h.id != id) || (!h.qr)) {
801
                        UNLOCK;
802
                        /* unsolicited */
803
                        goto again;
804
                }
805
                UNLOCK;
806
 
807
 
808
                DPRINTF("Got response %s\n", "(i think)!");
809
                DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
810
                                h.qdcount, h.ancount, h.nscount, h.arcount);
811
                DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
812
                                h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
813
 
814
                if ((h.rcode) || (h.ancount < 1)) {
815
                        /* negative result, not present */
816
                        goto again;
817
                }
818
 
819
                pos = HFIXEDSZ;
820
 
821
                for (j = 0; j < h.qdcount; j++) {
822
                        DPRINTF("Skipping question %d at %d\n", j, pos);
823
                        i = __length_question(packet, pos);
824
                        DPRINTF("Length of question %d is %d\n", j, i);
825
                        if (i < 0)
826
                                goto again;
827
                        pos += i;
828
                }
829
                DPRINTF("Decoding answer at pos %d\n", pos);
830
 
831
                for (j=0;j<h.ancount;j++)
832
                {
833
                    i = __decode_answer(packet, pos, a);
834
 
835
                    if (i<0) {
836
                        DPRINTF("failed decode %d\n", i);
837
                        goto again;
838
                    }
839
                    /* For all but T_SIG, accept first answer */
840
                    if (a->atype != T_SIG)
841
                        break;
842
 
843
                    DPRINTF("skipping T_SIG %d\n", i);
844
                    free(a->dotted);
845
                    pos += i;
846
                }
847
 
848
                DPRINTF("Answer name = |%s|\n", a->dotted);
849
                DPRINTF("Answer type = |%d|\n", a->atype);
850
 
851
                close(fd);
852
 
853
                if (outpacket)
854
                        *outpacket = packet;
855
                else
856
                        free(packet);
857
                free(lookup);
858
                return (0);                              /* success! */
859
 
860
          tryall:
861
                /* if there are other nameservers, give them a go,
862
                   otherwise return with error */
863
                {
864
                    int sdomains;
865
 
866
                    BIGLOCK;
867
                    sdomains=__searchdomains;
868
                    BIGUNLOCK;
869
                    variant = 0;
870
                    if (retries >= nscount*(sdomains+1))
871
                        goto fail;
872
                }
873
 
874
          again:
875
                /* if there are searchdomains, try them or fallback as passed */
876
                {
877
                    int sdomains;
878
                    BIGLOCK;
879
                    sdomains=__searchdomains;
880
                    BIGUNLOCK;
881
 
882
                    if (variant < sdomains) {
883
                        /* next search */
884
                        variant++;
885
                    } else {
886
                        /* next server, first search */
887
                        LOCK;
888
                        ns = (ns + 1) % nscount;
889
                        UNLOCK;
890
                        variant = 0;
891
                    }
892
                }
893
        }
894
 
895
fail:
896
        if (fd != -1)
897
            close(fd);
898
        if (lookup)
899
            free(lookup);
900
        if (packet)
901
            free(packet);
902
        return -1;
903
}
904
#endif
905
 
906
#ifdef L_opennameservers
907
 
908
int __nameservers;
909
char * __nameserver[MAX_SERVERS];
910
int __searchdomains;
911
char * __searchdomain[MAX_SEARCH];
912
#ifdef __UCLIBC_HAS_THREADS__
913
pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
914
#endif
915
 
916
/*
917
 *      we currently read formats not quite the same as that on normal
918
 *      unix systems, we can have a list of nameservers after the keyword.
919
 */
920
 
921
int __open_nameservers()
922
{
923
        FILE *fp;
924
        int i;
925
#define RESOLV_ARGS 5
926
        char szBuffer[128], *p, *argv[RESOLV_ARGS];
927
        int argc;
928
 
929
        BIGLOCK;
930
        if (__nameservers > 0) {
931
            BIGUNLOCK;
932
            return 0;
933
        }
934
 
935
        if ((fp = fopen("/etc/resolv.conf", "r")) ||
936
                        (fp = fopen("/etc/config/resolv.conf", "r"))) {
937
 
938
                while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
939
 
940
                        for (p = szBuffer; *p && isspace(*p); p++)
941
                                /* skip white space */;
942
                        if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
943
                                continue;
944
                        argc = 0;
945
                        while (*p && argc < RESOLV_ARGS) {
946
                                argv[argc++] = p;
947
                                while (*p && !isspace(*p) && *p != '\n')
948
                                        p++;
949
                                while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
950
                                        *p++ = '\0';
951
                        }
952
 
953
                        if (strcmp(argv[0], "nameserver") == 0) {
954
                                for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
955
                                        __nameserver[__nameservers++] = strdup(argv[i]);
956
                                        DPRINTF("adding nameserver %s\n", argv[i]);
957
                                }
958
                        }
959
 
960
                        /* domain and search are mutually exclusive, the last one wins */
961
                        if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
962
                                while (__searchdomains > 0) {
963
                                        free(__searchdomain[--__searchdomains]);
964
                                        __searchdomain[__searchdomains] = NULL;
965
                                }
966
                                for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
967
                                        __searchdomain[__searchdomains++] = strdup(argv[i]);
968
                                        DPRINTF("adding search %s\n", argv[i]);
969
                                }
970
                        }
971
                }
972
                fclose(fp);
973
        } else {
974
            DPRINTF("failed to open %s\n", "resolv.conf");
975
        }
976
        DPRINTF("nameservers = %d\n", __nameservers);
977
        BIGUNLOCK;
978
        return 0;
979
}
980
#endif
981
 
982
 
983
#ifdef L_closenameservers
984
 
985
void __close_nameservers(void)
986
{
987
        BIGLOCK;
988
        while (__nameservers > 0) {
989
                free(__nameserver[--__nameservers]);
990
                __nameserver[__nameservers] = NULL;
991
        }
992
        while (__searchdomains > 0) {
993
                free(__searchdomain[--__searchdomains]);
994
                __searchdomain[__searchdomains] = NULL;
995
        }
996
        BIGUNLOCK;
997
}
998
#endif
999
 
1000
#ifdef L_gethostbyname
1001
 
1002
struct hostent *gethostbyname(const char *name)
1003
{
1004
        static struct hostent h;
1005
        static char buf[sizeof(struct in_addr) +
1006
                        sizeof(struct in_addr *)*2 +
1007
                        sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
1008
        struct hostent *hp;
1009
 
1010
        gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
1011
 
1012
        return hp;
1013
}
1014
#endif
1015
 
1016
#ifdef L_gethostbyname2
1017
 
1018
struct hostent *gethostbyname2(const char *name, int family)
1019
{
1020
#ifndef __UCLIBC_HAS_IPV6__
1021
        return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
1022
#else /* __UCLIBC_HAS_IPV6__ */
1023
        static struct hostent h;
1024
        static char buf[sizeof(struct in6_addr) +
1025
                        sizeof(struct in6_addr *)*2 +
1026
                        sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
1027
        struct hostent *hp;
1028
 
1029
        gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
1030
 
1031
        return hp;
1032
#endif /* __UCLIBC_HAS_IPV6__ */
1033
}
1034
#endif
1035
 
1036
#ifdef L_getnetbyname
1037
 
1038
struct netent * getnetbyname(const char * name)
1039
{
1040
        return NULL;
1041
}
1042
#endif
1043
 
1044
 
1045
#ifdef L_res_init
1046
struct __res_state _res;
1047
 
1048
int res_init(void)
1049
{
1050
        struct __res_state *rp = &(_res);
1051
 
1052
        __close_nameservers();
1053
        __open_nameservers();
1054
        rp->retrans = RES_TIMEOUT;
1055
        rp->retry = 4;
1056
        rp->options = RES_INIT;
1057
        rp->id = (u_int) random();
1058
        rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
1059
        rp->nsaddr.sin_family = AF_INET;
1060
        rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
1061
        rp->ndots = 1;
1062
        /** rp->pfcode = 0; **/
1063
        rp->_vcsock = -1;
1064
        /** rp->_flags = 0; **/
1065
        /** rp->qhook = NULL; **/
1066
        /** rp->rhook = NULL; **/
1067
        /** rp->_u._ext.nsinit = 0; **/
1068
 
1069
        BIGLOCK;
1070
        if(__searchdomains) {
1071
                int i;
1072
                for(i=0; i<__searchdomains; i++) {
1073
                        rp->dnsrch[i] = __searchdomain[i];
1074
                }
1075
        }
1076
 
1077
        if(__nameservers) {
1078
                int i;
1079
                struct in_addr a;
1080
                for(i=0; i<__nameservers; i++) {
1081
                        if (inet_aton(__nameserver[i], &a)) {
1082
                                rp->nsaddr_list[i].sin_addr = a;
1083
                                rp->nsaddr_list[i].sin_family = AF_INET;
1084
                                rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
1085
                        }
1086
                }
1087
        }
1088
        rp->nscount = __nameservers;
1089
        BIGUNLOCK;
1090
 
1091
        return(0);
1092
}
1093
 
1094
void res_close( void )
1095
{
1096
        return;
1097
}
1098
 
1099
#endif
1100
 
1101
 
1102
#ifdef L_res_query
1103
 
1104
#ifndef MIN
1105
#define MIN(x, y)       ((x) < (y) ? (x) : (y))
1106
#endif
1107
 
1108
int res_query(const char *dname, int class, int type,
1109
              unsigned char *answer, int anslen)
1110
{
1111
        int i;
1112
        unsigned char * packet = 0;
1113
        struct resolv_answer a;
1114
        int __nameserversXX;
1115
        char ** __nameserverXX;
1116
 
1117
        __open_nameservers();
1118
 
1119
        if (!dname || class != 1 /* CLASS_IN */)
1120
                return(-1);
1121
 
1122
        memset((char *) &a, '\0', sizeof(a));
1123
 
1124
        BIGLOCK;
1125
        __nameserversXX=__nameservers;
1126
        __nameserverXX=__nameserver;
1127
        BIGUNLOCK;
1128
        i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
1129
 
1130
        if (i < 0)
1131
                return(-1);
1132
 
1133
        free(a.dotted);
1134
 
1135
        if (a.atype == type) { /* CNAME*/
1136
                if (anslen && answer)
1137
                        memcpy(answer, a.rdata, MIN(anslen, a.rdlength));
1138
                if (packet)
1139
                        free(packet);
1140
                return(MIN(anslen, a.rdlength));
1141
        }
1142
        if (packet)
1143
                free(packet);
1144
        return 0;
1145
}
1146
#endif
1147
 
1148
#ifdef L_gethostbyaddr
1149
struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
1150
{
1151
        static struct hostent h;
1152
        static char buf[
1153
#ifndef __UCLIBC_HAS_IPV6__
1154
                sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1155
#else
1156
                sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1157
#endif /* __UCLIBC_HAS_IPV6__ */
1158
                sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
1159
        struct hostent *hp;
1160
 
1161
        gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
1162
 
1163
        return hp;
1164
}
1165
#endif
1166
 
1167
 
1168
#ifdef L_read_etc_hosts_r
1169
 
1170
void __open_etc_hosts(FILE **fp)
1171
{
1172
        if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
1173
                *fp = fopen("/etc/config/hosts", "r");
1174
        }
1175
        return;
1176
}
1177
 
1178
int __read_etc_hosts_r(FILE * fp, const char * name, int type,
1179
                     enum etc_hosts_action action,
1180
                     struct hostent * result_buf,
1181
                     char * buf, size_t buflen,
1182
                     struct hostent ** result,
1183
                     int * h_errnop)
1184
{
1185
        struct in_addr  *in=NULL;
1186
        struct in_addr  **addr_list=NULL;
1187
#ifdef __UCLIBC_HAS_IPV6__
1188
        struct in6_addr *in6=NULL;
1189
        struct in6_addr **addr_list6=NULL;
1190
#endif /* __UCLIBC_HAS_IPV6__ */
1191
        char                                    *cp;
1192
        char                                    **alias;
1193
        int                                             aliases, i;
1194
        int             ret=HOST_NOT_FOUND;
1195
 
1196
        if (buflen < sizeof(char *)*(ALIAS_DIM))
1197
                return ERANGE;
1198
        alias=(char **)buf;
1199
        buf+=sizeof(char **)*(ALIAS_DIM);
1200
        buflen-=sizeof(char **)*(ALIAS_DIM);
1201
 
1202
        if (action!=GETHOSTENT) {
1203
#ifdef __UCLIBC_HAS_IPV6__
1204
                char *p=buf;
1205
                size_t len=buflen;
1206
#endif /* __UCLIBC_HAS_IPV6__ */
1207
                *h_errnop=NETDB_INTERNAL;
1208
                if (buflen < sizeof(*in))
1209
                        return ERANGE;
1210
                in=(struct in_addr*)buf;
1211
                buf+=sizeof(*in);
1212
                buflen-=sizeof(*in);
1213
 
1214
                if (buflen < sizeof(*addr_list)*2)
1215
                        return ERANGE;
1216
                addr_list=(struct in_addr **)buf;
1217
                buf+=sizeof(*addr_list)*2;
1218
                buflen-=sizeof(*addr_list)*2;
1219
 
1220
#ifdef __UCLIBC_HAS_IPV6__
1221
                if (len < sizeof(*in6))
1222
                        return ERANGE;
1223
                in6=(struct in6_addr*)p;
1224
                p+=sizeof(*in6);
1225
                len-=sizeof(*in6);
1226
 
1227
                if (len < sizeof(*addr_list6)*2)
1228
                        return ERANGE;
1229
                addr_list6=(struct in6_addr**)p;
1230
                p+=sizeof(*addr_list6)*2;
1231
                len-=sizeof(*addr_list6)*2;
1232
 
1233
                if (len < buflen) {
1234
                        buflen=len;
1235
                        buf=p;
1236
                }
1237
#endif /* __UCLIBC_HAS_IPV6__ */
1238
 
1239
                if (buflen < 80)
1240
                        return ERANGE;
1241
 
1242
                __open_etc_hosts(&fp);
1243
                if (fp == NULL) {
1244
                        result=NULL;
1245
                        return errno;
1246
                }
1247
        }
1248
 
1249
        *h_errnop=HOST_NOT_FOUND;
1250
        while (fgets(buf, buflen, fp)) {
1251
                if ((cp = strchr(buf, '#')))
1252
                        *cp = '\0';
1253
                DPRINTF("Looking at: %s\n", buf);
1254
                aliases = 0;
1255
 
1256
                cp = buf;
1257
                while (*cp) {
1258
                        while (*cp && isspace(*cp))
1259
                                *cp++ = '\0';
1260
                        if (!*cp)
1261
                                continue;
1262
                        if (aliases < (2+MAX_ALIASES))
1263
                                alias[aliases++] = cp;
1264
                        while (*cp && !isspace(*cp))
1265
                                cp++;
1266
                }
1267
                alias[aliases] = 0;
1268
 
1269
                if (aliases < 2)
1270
                        continue; /* syntax error really */
1271
 
1272
                if (action==GETHOSTENT) {
1273
                        /* Return whatever the next entry happens to be. */
1274
                        break;
1275
                } else if (action==GET_HOSTS_BYADDR) {
1276
                        if (strcmp(name, alias[0]) != 0)
1277
                                continue;
1278
                } else {
1279
                        /* GET_HOSTS_BYNAME */
1280
                        for (i = 1; i < aliases; i++)
1281
                                if (strcasecmp(name, alias[i]) == 0)
1282
                                        break;
1283
                        if (i >= aliases)
1284
                                continue;
1285
                }
1286
 
1287
                if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
1288
                        DPRINTF("Found INET\n");
1289
                        addr_list[0] = in;
1290
                        addr_list[1] = 0;
1291
                        result_buf->h_name = alias[1];
1292
                        result_buf->h_addrtype = AF_INET;
1293
                        result_buf->h_length = sizeof(*in);
1294
                        result_buf->h_addr_list = (char**) addr_list;
1295
                        result_buf->h_aliases = alias + 2;
1296
                        *result=result_buf;
1297
                        ret=NETDB_SUCCESS;
1298
#ifdef __UCLIBC_HAS_IPV6__
1299
        } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
1300
                        DPRINTF("Found INET6\n");
1301
                        addr_list6[0] = in6;
1302
                        addr_list6[1] = 0;
1303
                        result_buf->h_name = alias[1];
1304
                        result_buf->h_addrtype = AF_INET6;
1305
                        result_buf->h_length = sizeof(*in6);
1306
                        result_buf->h_addr_list = (char**) addr_list6;
1307
                        result_buf->h_aliases = alias + 2;
1308
                        *result=result_buf;
1309
                        ret=NETDB_SUCCESS;
1310
#endif /* __UCLIBC_HAS_IPV6__ */
1311
                } else {
1312
                        DPRINTF("Error\n");
1313
                        ret=TRY_AGAIN;
1314
                        break; /* bad ip address */
1315
        }
1316
 
1317
                if (action!=GETHOSTENT) {
1318
                        fclose(fp);
1319
                }
1320
                return ret;
1321
        }
1322
        if (action!=GETHOSTENT) {
1323
                fclose(fp);
1324
        }
1325
        return ret;
1326
}
1327
#endif
1328
 
1329
 
1330
#ifdef L_gethostent
1331
 
1332
#ifdef __UCLIBC_HAS_THREADS__
1333
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
1334
# define LOCK   __pthread_mutex_lock(&mylock)
1335
# define UNLOCK __pthread_mutex_unlock(&mylock);
1336
#else
1337
# define LOCK
1338
# define UNLOCK
1339
#endif
1340
 
1341
static int __stay_open;
1342
static FILE * __gethostent_fp;
1343
 
1344
void endhostent (void)
1345
{
1346
    LOCK;
1347
    __stay_open = 0;
1348
    if (__gethostent_fp) {
1349
        fclose(__gethostent_fp);
1350
    }
1351
    UNLOCK;
1352
}
1353
 
1354
void sethostent (int stay_open)
1355
{
1356
    LOCK;
1357
    __stay_open = stay_open;
1358
    UNLOCK;
1359
}
1360
 
1361
 
1362
struct hostent *gethostent (void)
1363
{
1364
    static struct hostent h;
1365
    static char buf[
1366
#ifndef __UCLIBC_HAS_IPV6__
1367
            sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1368
#else
1369
            sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1370
#endif /* __UCLIBC_HAS_IPV6__ */
1371
                sizeof(char *)*(ALIAS_DIM) +
1372
            80/*namebuffer*/ + 2/* margin */];
1373
    struct hostent *host;
1374
 
1375
    LOCK;
1376
    if (__gethostent_fp == NULL) {
1377
        __open_etc_hosts(&__gethostent_fp);
1378
        if (__gethostent_fp == NULL) {
1379
            UNLOCK;
1380
            return((struct hostent *)NULL);
1381
        }
1382
    }
1383
 
1384
    __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
1385
                   &h, buf, sizeof(buf), &host, &h_errno);
1386
    if (__stay_open==0) {
1387
        fclose(__gethostent_fp);
1388
    }
1389
    UNLOCK;
1390
    return(host);
1391
}
1392
#endif
1393
 
1394
#ifdef L_get_hosts_byname_r
1395
 
1396
int __get_hosts_byname_r(const char * name, int type,
1397
                            struct hostent * result_buf,
1398
                            char * buf, size_t buflen,
1399
                            struct hostent ** result,
1400
                            int * h_errnop)
1401
{
1402
        return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
1403
}
1404
#endif
1405
 
1406
#ifdef L_get_hosts_byaddr_r
1407
 
1408
int __get_hosts_byaddr_r(const char * addr, int len, int type,
1409
                            struct hostent * result_buf,
1410
                            char * buf, size_t buflen,
1411
                            struct hostent ** result,
1412
                            int * h_errnop)
1413
{
1414
#ifndef __UCLIBC_HAS_IPV6__
1415
        char    ipaddr[INET_ADDRSTRLEN];
1416
#else
1417
        char    ipaddr[INET6_ADDRSTRLEN];
1418
#endif /* __UCLIBC_HAS_IPV6__ */
1419
 
1420
    switch (type) {
1421
        case AF_INET:
1422
                if (len != sizeof(struct in_addr))
1423
                        return 0;
1424
                break;
1425
#ifdef __UCLIBC_HAS_IPV6__
1426
        case AF_INET6:
1427
                if (len != sizeof(struct in6_addr))
1428
                        return 0;
1429
                break;
1430
#endif /* __UCLIBC_HAS_IPV6__ */
1431
        default:
1432
                return 0;
1433
        }
1434
 
1435
        inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
1436
 
1437
        return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
1438
                    result_buf, buf, buflen, result, h_errnop));
1439
}
1440
#endif
1441
 
1442
#ifdef L_getnameinfo
1443
 
1444
#ifndef min
1445
# define min(x,y) (((x) > (y)) ? (y) : (x))
1446
#endif /* min */
1447
 
1448
int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
1449
             socklen_t hostlen, char *serv, socklen_t servlen,
1450
             unsigned int flags)
1451
{
1452
        int serrno = errno;
1453
        int ok = 0;
1454
        struct hostent *h = NULL;
1455
    char domain[256];
1456
 
1457
        if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
1458
                return EAI_BADFLAGS;
1459
 
1460
        if (sa == NULL || addrlen < sizeof (sa_family_t))
1461
                return EAI_FAMILY;
1462
 
1463
        switch (sa->sa_family) {
1464
        case AF_LOCAL:
1465
                break;
1466
        case AF_INET:
1467
                if (addrlen < sizeof (struct sockaddr_in))
1468
                        return EAI_FAMILY;
1469
                break;
1470
#ifdef __UCLIBC_HAS_IPV6__
1471
        case AF_INET6:
1472
                if (addrlen < sizeof (struct sockaddr_in6))
1473
                        return EAI_FAMILY;
1474
                break;
1475
#endif /* __UCLIBC_HAS_IPV6__ */
1476
        default:
1477
                return EAI_FAMILY;
1478
        }
1479
 
1480
        if (host != NULL && hostlen > 0)
1481
                switch (sa->sa_family) {
1482
                case AF_INET:
1483
#ifdef __UCLIBC_HAS_IPV6__
1484
                case AF_INET6:
1485
#endif /* __UCLIBC_HAS_IPV6__ */
1486
                        if (!(flags & NI_NUMERICHOST)) {
1487
#ifdef __UCLIBC_HAS_IPV6__
1488
                                if (sa->sa_family == AF_INET6)
1489
                                        h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
1490
                                                sizeof(struct in6_addr), AF_INET6);
1491
                                else
1492
#endif /* __UCLIBC_HAS_IPV6__ */
1493
                    h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
1494
                                          sizeof(struct in_addr), AF_INET);
1495
 
1496
                                if (h) {
1497
                                        char *c;
1498
                                        if ((flags & NI_NOFQDN)
1499
                                            && (getdomainname (domain, sizeof(domain)) == 0)
1500
                                            && (c = strstr (h->h_name, domain))
1501
                                            && (c != h->h_name) && (*(--c) == '.')) {
1502
                                                strncpy (host, h->h_name,
1503
                                                        min(hostlen, (size_t) (c - h->h_name)));
1504
                                                host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
1505
                                                ok = 1;
1506
                                        } else {
1507
                                                strncpy (host, h->h_name, hostlen);
1508
                                                ok = 1;
1509
                                        }
1510
                                 }
1511
                        }
1512
 
1513
                        if (!ok) {
1514
                                if (flags & NI_NAMEREQD) {
1515
                                        errno = serrno;
1516
                                        return EAI_NONAME;
1517
                                } else {
1518
                                        const char *c;
1519
#ifdef __UCLIBC_HAS_IPV6__
1520
                                        if (sa->sa_family == AF_INET6) {
1521
                                                const struct sockaddr_in6 *sin6p;
1522
 
1523
                                                sin6p = (const struct sockaddr_in6 *) sa;
1524
 
1525
                                                c = inet_ntop (AF_INET6,
1526
                                                        (const void *) &sin6p->sin6_addr, host, hostlen);
1527
#if 0
1528
/* Does scope id need to be supported? */
1529
                                                uint32_t scopeid;
1530
                                                scopeid = sin6p->sin6_scope_id;
1531
                                                if (scopeid != 0) {
1532
                                                        /* Buffer is >= IFNAMSIZ+1.  */
1533
                                                        char scopebuf[IFNAMSIZ + 1];
1534
                                                        char *scopeptr;
1535
                                                        int ni_numericscope = 0;
1536
                                                        size_t real_hostlen = __strnlen (host, hostlen);
1537
                                                        size_t scopelen = 0;
1538
 
1539
                                                        scopebuf[0] = SCOPE_DELIMITER;
1540
                                                        scopebuf[1] = '\0';
1541
                                                        scopeptr = &scopebuf[1];
1542
 
1543
                                                        if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
1544
                                                            || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
1545
                                                                if (if_indextoname (scopeid, scopeptr) == NULL)
1546
                                                                        ++ni_numericscope;
1547
                                                                else
1548
                                                                        scopelen = strlen (scopebuf);
1549
                                                        } else {
1550
                                                                ++ni_numericscope;
1551
                            }
1552
 
1553
                                                        if (ni_numericscope)
1554
                                                                scopelen = 1 + snprintf (scopeptr,
1555
                                                                        (scopebuf
1556
                                                                        + sizeof scopebuf
1557
                                                                        - scopeptr),
1558
                                                                        "%u", scopeid);
1559
 
1560
                                                        if (real_hostlen + scopelen + 1 > hostlen)
1561
                                                                return EAI_SYSTEM;
1562
                                                        memcpy (host + real_hostlen, scopebuf, scopelen + 1);
1563
                                                }
1564
#endif
1565
                                        } else
1566
#endif /* __UCLIBC_HAS_IPV6__ */
1567
                                                c = inet_ntop (AF_INET,
1568
                                                        (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
1569
                                                        host, hostlen);
1570
 
1571
                                        if (c == NULL) {
1572
                                                errno = serrno;
1573
                                                return EAI_SYSTEM;
1574
                                        }
1575
                                }
1576
                                ok = 1;
1577
                        }
1578
                        break;
1579
 
1580
                case AF_LOCAL:
1581
                        if (!(flags & NI_NUMERICHOST)) {
1582
                                struct utsname utsname;
1583
 
1584
                                if (!uname (&utsname)) {
1585
                                        strncpy (host, utsname.nodename, hostlen);
1586
                                        break;
1587
                                };
1588
                        };
1589
 
1590
                        if (flags & NI_NAMEREQD) {
1591
                                errno = serrno;
1592
                                return EAI_NONAME;
1593
                        }
1594
 
1595
                        strncpy (host, "localhost", hostlen);
1596
                        break;
1597
 
1598
                default:
1599
                        return EAI_FAMILY;
1600
        }
1601
 
1602
        if (serv && (servlen > 0)) {
1603
                switch (sa->sa_family) {
1604
                case AF_INET:
1605
#ifdef __UCLIBC_HAS_IPV6__
1606
                case AF_INET6:
1607
#endif /* __UCLIBC_HAS_IPV6__ */
1608
                        if (!(flags & NI_NUMERICSERV)) {
1609
                                struct servent *s;
1610
                                s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
1611
                                      ((flags & NI_DGRAM) ? "udp" : "tcp"));
1612
                                if (s) {
1613
                                        strncpy (serv, s->s_name, servlen);
1614
                                        break;
1615
                                }
1616
                        }
1617
                        snprintf (serv, servlen, "%d",
1618
                                ntohs (((const struct sockaddr_in *) sa)->sin_port));
1619
                        break;
1620
 
1621
                case AF_LOCAL:
1622
                        strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
1623
                        break;
1624
                }
1625
        }
1626
        if (host && (hostlen > 0))
1627
                host[hostlen-1] = 0;
1628
        if (serv && (servlen > 0))
1629
                serv[servlen-1] = 0;
1630
        errno = serrno;
1631
        return 0;
1632
}
1633
#endif
1634
 
1635
 
1636
#ifdef L_gethostbyname_r
1637
 
1638
int gethostbyname_r(const char * name,
1639
                            struct hostent * result_buf,
1640
                            char * buf, size_t buflen,
1641
                            struct hostent ** result,
1642
                            int * h_errnop)
1643
{
1644
        struct in_addr *in;
1645
        struct in_addr **addr_list;
1646
        unsigned char *packet;
1647
        struct resolv_answer a;
1648
        int i;
1649
        int nest = 0;
1650
        int __nameserversXX;
1651
        char ** __nameserverXX;
1652
 
1653
        __open_nameservers();
1654
 
1655
        *result=NULL;
1656
        if (!name)
1657
                return EINVAL;
1658
 
1659
        /* do /etc/hosts first */
1660
        if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
1661
                                  buf, buflen, result, h_errnop))==0)
1662
                return i;
1663
        switch (*h_errnop) {
1664
                case HOST_NOT_FOUND:
1665
                case NO_ADDRESS:
1666
                        break;
1667
                case NETDB_INTERNAL:
1668
                        if (errno == ENOENT) {
1669
                            break;
1670
                        }
1671
                        /* else fall through */
1672
                default:
1673
                        return i;
1674
        }
1675
 
1676
        DPRINTF("Nothing found in /etc/hosts\n");
1677
 
1678
        *h_errnop = NETDB_INTERNAL;
1679
        if (buflen < sizeof(*in))
1680
                return ERANGE;
1681
        in=(struct in_addr*)buf;
1682
        buf+=sizeof(*in);
1683
        buflen-=sizeof(*in);
1684
 
1685
        if (buflen < sizeof(*addr_list)*2)
1686
                return ERANGE;
1687
        addr_list=(struct in_addr**)buf;
1688
        buf+=sizeof(*addr_list)*2;
1689
        buflen-=sizeof(*addr_list)*2;
1690
 
1691
        addr_list[0] = in;
1692
        addr_list[1] = 0;
1693
 
1694
        if (buflen<256)
1695
                return ERANGE;
1696
        strncpy(buf, name, buflen);
1697
 
1698
        /* First check if this is already an address */
1699
        if (inet_aton(name, in)) {
1700
            result_buf->h_name = buf;
1701
            result_buf->h_addrtype = AF_INET;
1702
            result_buf->h_length = sizeof(*in);
1703
            result_buf->h_addr_list = (char **) addr_list;
1704
            *result=result_buf;
1705
            *h_errnop = NETDB_SUCCESS;
1706
            return NETDB_SUCCESS;
1707
        }
1708
 
1709
        for (;;) {
1710
 
1711
        BIGLOCK;
1712
        __nameserversXX=__nameservers;
1713
        __nameserverXX=__nameserver;
1714
        BIGUNLOCK;
1715
                i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
1716
 
1717
                if (i < 0) {
1718
                        *h_errnop = HOST_NOT_FOUND;
1719
                        DPRINTF("__dns_lookup\n");
1720
                        return TRY_AGAIN;
1721
                }
1722
 
1723
                strncpy(buf, a.dotted, buflen);
1724
                free(a.dotted);
1725
 
1726
                if (a.atype == T_CNAME) {               /* CNAME */
1727
                        DPRINTF("Got a CNAME in gethostbyname()\n");
1728
                        i = __decode_dotted(packet, a.rdoffset, buf, buflen);
1729
                        free(packet);
1730
 
1731
                        if (i < 0) {
1732
                                *h_errnop = NO_RECOVERY;
1733
                                DPRINTF("__decode_dotted\n");
1734
                                return -1;
1735
                        }
1736
                        if (++nest > MAX_RECURSE) {
1737
                                *h_errnop = NO_RECOVERY;
1738
                                DPRINTF("recursion\n");
1739
                                return -1;
1740
                        }
1741
                        continue;
1742
                } else if (a.atype == T_A) {    /* ADDRESS */
1743
                        memcpy(in, a.rdata, sizeof(*in));
1744
                        result_buf->h_name = buf;
1745
                        result_buf->h_addrtype = AF_INET;
1746
                        result_buf->h_length = sizeof(*in);
1747
                        result_buf->h_addr_list = (char **) addr_list;
1748
                        free(packet);
1749
                        break;
1750
                } else {
1751
                        free(packet);
1752
                        *h_errnop=HOST_NOT_FOUND;
1753
                        return TRY_AGAIN;
1754
                }
1755
        }
1756
 
1757
        *result=result_buf;
1758
        *h_errnop = NETDB_SUCCESS;
1759
        return NETDB_SUCCESS;
1760
}
1761
#endif
1762
 
1763
#ifdef L_gethostbyname2_r
1764
 
1765
int gethostbyname2_r(const char *name, int family,
1766
                            struct hostent * result_buf,
1767
                            char * buf, size_t buflen,
1768
                            struct hostent ** result,
1769
                            int * h_errnop)
1770
{
1771
#ifndef __UCLIBC_HAS_IPV6__
1772
        return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
1773
#else /* __UCLIBC_HAS_IPV6__ */
1774
        struct in6_addr *in;
1775
        struct in6_addr **addr_list;
1776
        unsigned char *packet;
1777
        struct resolv_answer a;
1778
        int i;
1779
        int nest = 0;
1780
        int __nameserversXX;
1781
        char ** __nameserverXX;
1782
 
1783
        if (family == AF_INET)
1784
                return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
1785
 
1786
        if (family != AF_INET6)
1787
                return EINVAL;
1788
 
1789
        __open_nameservers();
1790
 
1791
        *result=NULL;
1792
        if (!name)
1793
                return EINVAL;
1794
 
1795
        /* do /etc/hosts first */
1796
        if ((i=__get_hosts_byname_r(name, family, result_buf,
1797
                                  buf, buflen, result, h_errnop))==0)
1798
                return i;
1799
        switch (*h_errnop) {
1800
                case HOST_NOT_FOUND:
1801
                case NO_ADDRESS:
1802
                        break;
1803
                default:
1804
                        return i;
1805
        }
1806
 
1807
        DPRINTF("Nothing found in /etc/hosts\n");
1808
 
1809
        *h_errnop = NETDB_INTERNAL;
1810
        if (buflen < sizeof(*in))
1811
                return ERANGE;
1812
        in=(struct in6_addr*)buf;
1813
        buf+=sizeof(*in);
1814
        buflen-=sizeof(*in);
1815
 
1816
        if (buflen < sizeof(*addr_list)*2)
1817
                return ERANGE;
1818
        addr_list=(struct in6_addr**)buf;
1819
        buf+=sizeof(*addr_list)*2;
1820
        buflen-=sizeof(*addr_list)*2;
1821
 
1822
        addr_list[0] = in;
1823
        addr_list[1] = 0;
1824
 
1825
        if (buflen<256)
1826
                return ERANGE;
1827
        strncpy(buf, name, buflen);
1828
 
1829
        /* First check if this is already an address */
1830
        if (inet_pton(AF_INET6, name, in)) {
1831
            result_buf->h_name = buf;
1832
            result_buf->h_addrtype = AF_INET6;
1833
            result_buf->h_length = sizeof(*in);
1834
            result_buf->h_addr_list = (char **) addr_list;
1835
            *result=result_buf;
1836
            *h_errnop = NETDB_SUCCESS;
1837
            return NETDB_SUCCESS;
1838
        }
1839
 
1840
        for (;;) {
1841
        BIGLOCK;
1842
        __nameserversXX=__nameservers;
1843
        __nameserverXX=__nameserver;
1844
        BIGUNLOCK;
1845
 
1846
                i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
1847
 
1848
                if (i < 0) {
1849
                        *h_errnop = HOST_NOT_FOUND;
1850
                        return TRY_AGAIN;
1851
                }
1852
 
1853
                strncpy(buf, a.dotted, buflen);
1854
                free(a.dotted);
1855
 
1856
                if (a.atype == T_CNAME) {               /* CNAME */
1857
                        DPRINTF("Got a CNAME in gethostbyname()\n");
1858
                        i = __decode_dotted(packet, a.rdoffset, buf, buflen);
1859
                        free(packet);
1860
 
1861
                        if (i < 0) {
1862
                                *h_errnop = NO_RECOVERY;
1863
                                return -1;
1864
                        }
1865
                        if (++nest > MAX_RECURSE) {
1866
                                *h_errnop = NO_RECOVERY;
1867
                                return -1;
1868
                        }
1869
                        continue;
1870
                } else if (a.atype == T_AAAA) { /* ADDRESS */
1871
                        memcpy(in, a.rdata, sizeof(*in));
1872
                        result_buf->h_name = buf;
1873
                        result_buf->h_addrtype = AF_INET6;
1874
                        result_buf->h_length = sizeof(*in);
1875
                        result_buf->h_addr_list = (char **) addr_list;
1876
                        free(packet);
1877
                        break;
1878
                } else {
1879
                        free(packet);
1880
                        *h_errnop=HOST_NOT_FOUND;
1881
                        return TRY_AGAIN;
1882
                }
1883
        }
1884
 
1885
        *result=result_buf;
1886
        *h_errnop = NETDB_SUCCESS;
1887
        return NETDB_SUCCESS;
1888
#endif /* __UCLIBC_HAS_IPV6__ */
1889
}
1890
#endif
1891
 
1892
#ifdef L_gethostbyaddr_r
1893
int gethostbyaddr_r (const void *addr, socklen_t len, int type,
1894
                            struct hostent * result_buf,
1895
                            char * buf, size_t buflen,
1896
                            struct hostent ** result,
1897
                            int * h_errnop)
1898
 
1899
{
1900
        struct in_addr *in;
1901
        struct in_addr **addr_list;
1902
#ifdef __UCLIBC_HAS_IPV6__
1903
        char *qp;
1904
        size_t plen;
1905
        struct in6_addr *in6;
1906
        struct in6_addr **addr_list6;
1907
#endif /* __UCLIBC_HAS_IPV6__ */
1908
        unsigned char *packet;
1909
        struct resolv_answer a;
1910
        int i;
1911
        int nest = 0;
1912
        int __nameserversXX;
1913
        char ** __nameserverXX;
1914
 
1915
        *result=NULL;
1916
        if (!addr)
1917
                return EINVAL;
1918
 
1919
        switch (type) {
1920
                case AF_INET:
1921
                        if (len != sizeof(struct in_addr))
1922
                                return EINVAL;
1923
                        break;
1924
#ifdef __UCLIBC_HAS_IPV6__
1925
                case AF_INET6:
1926
                        if (len != sizeof(struct in6_addr))
1927
                                return EINVAL;
1928
                        break;
1929
#endif /* __UCLIBC_HAS_IPV6__ */
1930
                default:
1931
                        return EINVAL;
1932
        }
1933
 
1934
        /* do /etc/hosts first */
1935
        if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
1936
                                  buf, buflen, result, h_errnop))==0)
1937
                return i;
1938
        switch (*h_errnop) {
1939
                case HOST_NOT_FOUND:
1940
                case NO_ADDRESS:
1941
                        break;
1942
                default:
1943
                        return i;
1944
        }
1945
 
1946
        __open_nameservers();
1947
 
1948
#ifdef __UCLIBC_HAS_IPV6__
1949
        qp=buf;
1950
        plen=buflen;
1951
#endif /* __UCLIBC_HAS_IPV6__ */
1952
 
1953
        *h_errnop = NETDB_INTERNAL;
1954
        if (buflen < sizeof(*in))
1955
                return ERANGE;
1956
        in=(struct in_addr*)buf;
1957
        buf+=sizeof(*in);
1958
        buflen-=sizeof(*in);
1959
 
1960
        if (buflen < sizeof(*addr_list)*2)
1961
                return ERANGE;
1962
        addr_list=(struct in_addr**)buf;
1963
        buf+=sizeof(*addr_list)*2;
1964
        buflen-=sizeof(*addr_list)*2;
1965
 
1966
#ifdef __UCLIBC_HAS_IPV6__
1967
        if (plen < sizeof(*in6))
1968
                return ERANGE;
1969
        in6=(struct in6_addr*)qp;
1970
        qp+=sizeof(*in6);
1971
        plen-=sizeof(*in6);
1972
 
1973
        if (plen < sizeof(*addr_list6)*2)
1974
                return ERANGE;
1975
        addr_list6=(struct in6_addr**)qp;
1976
        qp+=sizeof(*addr_list6)*2;
1977
        plen-=sizeof(*addr_list6)*2;
1978
 
1979
        if (plen < buflen) {
1980
                buflen=plen;
1981
                buf=qp;
1982
        }
1983
#endif /* __UCLIBC_HAS_IPV6__ */
1984
 
1985
        if (buflen<256)
1986
                return ERANGE;
1987
 
1988
        if(type == AF_INET) {
1989
                unsigned char *tmp_addr = (unsigned char *)addr;
1990
 
1991
                memcpy(&in->s_addr, addr, len);
1992
 
1993
                addr_list[0] = in;
1994
 
1995
                sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
1996
                        tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
1997
#ifdef __UCLIBC_HAS_IPV6__
1998
        } else {
1999
                memcpy(in6->s6_addr, addr, len);
2000
 
2001
                addr_list6[0] = in6;
2002
                qp = buf;
2003
 
2004
                for (i = len - 1; i >= 0; i--) {
2005
                        qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
2006
                                (in6->s6_addr[i] >> 4) & 0xf);
2007
        }
2008
        strcpy(qp, "ip6.int");
2009
#endif /* __UCLIBC_HAS_IPV6__ */
2010
        }
2011
 
2012
        addr_list[1] = 0;
2013
 
2014
        for (;;) {
2015
 
2016
        BIGLOCK;
2017
        __nameserversXX=__nameservers;
2018
        __nameserverXX=__nameserver;
2019
        BIGUNLOCK;
2020
                i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
2021
 
2022
                if (i < 0) {
2023
                        *h_errnop = HOST_NOT_FOUND;
2024
                        return TRY_AGAIN;
2025
                }
2026
 
2027
                strncpy(buf, a.dotted, buflen);
2028
                free(a.dotted);
2029
 
2030
                if (a.atype == T_CNAME) {               /* CNAME */
2031
                        DPRINTF("Got a CNAME in gethostbyaddr()\n");
2032
                        i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2033
                        free(packet);
2034
 
2035
                        if (i < 0) {
2036
                                *h_errnop = NO_RECOVERY;
2037
                                return -1;
2038
                        }
2039
                        if (++nest > MAX_RECURSE) {
2040
                                *h_errnop = NO_RECOVERY;
2041
                                return -1;
2042
                        }
2043
                        continue;
2044
                } else if (a.atype == T_PTR) {  /* ADDRESS */
2045
                        i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2046
                        free(packet);
2047
 
2048
                        result_buf->h_name = buf;
2049
                        result_buf->h_addrtype = type;
2050
 
2051
                        if(type == AF_INET) {
2052
                                result_buf->h_length = sizeof(*in);
2053
#ifdef __UCLIBC_HAS_IPV6__
2054
                        } else {
2055
                                result_buf->h_length = sizeof(*in6);
2056
#endif /* __UCLIBC_HAS_IPV6__ */
2057
                }
2058
 
2059
                        result_buf->h_addr_list = (char **) addr_list;
2060
                        break;
2061
                } else {
2062
                        free(packet);
2063
                        *h_errnop = NO_ADDRESS;
2064
                        return TRY_AGAIN;
2065
                }
2066
        }
2067
 
2068
        *result=result_buf;
2069
        *h_errnop = NETDB_SUCCESS;
2070
        return NETDB_SUCCESS;
2071
}
2072
#endif
2073
 
2074
#ifdef L_res_comp
2075
/*
2076
 * Expand compressed domain name 'comp_dn' to full domain name.
2077
 * 'msg' is a pointer to the begining of the message,
2078
 * 'eomorig' points to the first location after the message,
2079
 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2080
 * Return size of compressed name or -1 if there was an error.
2081
 */
2082
int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
2083
          char *dst, int dstsiz)
2084
{
2085
        int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
2086
 
2087
        if (n > 0 && dst[0] == '.')
2088
                dst[0] = '\0';
2089
        return (n);
2090
}
2091
#endif /* L_res_comp */
2092
 
2093
#ifdef L_ns_name
2094
/*
2095
 * printable(ch)
2096
 *      Thinking in noninternationalized USASCII (per the DNS spec),
2097
 *      is this character visible and not a space when printed ?
2098
 * return:
2099
 *      boolean.
2100
 */
2101
static int printable(int ch)
2102
{
2103
        return (ch > 0x20 && ch < 0x7f);
2104
}
2105
 
2106
/*
2107
 * special(ch)
2108
 *      Thinking in noninternationalized USASCII (per the DNS spec),
2109
 *      is this characted special ("in need of quoting") ?
2110
 * return:
2111
 *      boolean.
2112
 */
2113
static int special(int ch)
2114
{
2115
        switch (ch) {
2116
        case 0x22: /* '"' */
2117
        case 0x2E: /* '.' */
2118
        case 0x3B: /* ';' */
2119
        case 0x5C: /* '\\' */
2120
        /* Special modifiers in zone files. */
2121
        case 0x40: /* '@' */
2122
        case 0x24: /* '$' */
2123
                return (1);
2124
        default:
2125
                return (0);
2126
        }
2127
}
2128
 
2129
/*
2130
 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
2131
 *      Expand compressed domain name to presentation format.
2132
 * return:
2133
 *      Number of bytes read out of `src', or -1 (with errno set).
2134
 * note:
2135
 *      Root domain returns as "." not "".
2136
 */
2137
int __ns_name_uncompress(const u_char *msg, const u_char *eom,
2138
                const u_char *src, char *dst, size_t dstsiz)
2139
{
2140
        u_char tmp[NS_MAXCDNAME];
2141
        int n;
2142
 
2143
        if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
2144
                return (-1);
2145
        if (ns_name_ntop(tmp, dst, dstsiz) == -1)
2146
                return (-1);
2147
        return (n);
2148
}
2149
 
2150
 
2151
/*
2152
 * ns_name_ntop(src, dst, dstsiz)
2153
 *      Convert an encoded domain name to printable ascii as per RFC1035.
2154
 * return:
2155
 *      Number of bytes written to buffer, or -1 (with errno set)
2156
 * notes:
2157
 *      The root is returned as "."
2158
 *      All other domains are returned in non absolute form
2159
 */
2160
int __ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
2161
        const u_char *cp;
2162
        char *dn, *eom;
2163
        u_char c;
2164
        u_int n;
2165
        const char digits[] = "0123456789";
2166
 
2167
        cp = src;
2168
        dn = dst;
2169
        eom = dst + dstsiz;
2170
 
2171
        while ((n = *cp++) != 0) {
2172
                if ((n & NS_CMPRSFLGS) != 0) {
2173
                        /* Some kind of compression pointer. */
2174
                        __set_errno (EMSGSIZE);
2175
                        return (-1);
2176
                }
2177
                if (dn != dst) {
2178
                        if (dn >= eom) {
2179
                                __set_errno (EMSGSIZE);
2180
                                return (-1);
2181
                        }
2182
                        *dn++ = '.';
2183
                }
2184
                if (dn + n >= eom) {
2185
                        __set_errno (EMSGSIZE);
2186
                        return (-1);
2187
                }
2188
                for ((void)NULL; n > 0; n--) {
2189
                        c = *cp++;
2190
                        if (special(c)) {
2191
                                if (dn + 1 >= eom) {
2192
                                        __set_errno (EMSGSIZE);
2193
                                        return (-1);
2194
                                }
2195
                                *dn++ = '\\';
2196
                                *dn++ = (char)c;
2197
                        } else if (!printable(c)) {
2198
                                if (dn + 3 >= eom) {
2199
                                        __set_errno (EMSGSIZE);
2200
                                        return (-1);
2201
                                }
2202
                                *dn++ = '\\';
2203
                                *dn++ = digits[c / 100];
2204
                                *dn++ = digits[(c % 100) / 10];
2205
                                *dn++ = digits[c % 10];
2206
                        } else {
2207
                                if (dn >= eom) {
2208
                                        __set_errno (EMSGSIZE);
2209
                                        return (-1);
2210
                                }
2211
                                *dn++ = (char)c;
2212
                        }
2213
                }
2214
        }
2215
        if (dn == dst) {
2216
                if (dn >= eom) {
2217
                        __set_errno (EMSGSIZE);
2218
                        return (-1);
2219
                }
2220
                *dn++ = '.';
2221
        }
2222
        if (dn >= eom) {
2223
                __set_errno (EMSGSIZE);
2224
                return (-1);
2225
        }
2226
        *dn++ = '\0';
2227
        return (dn - dst);
2228
}
2229
 
2230
/*
2231
 * ns_name_unpack(msg, eom, src, dst, dstsiz)
2232
 *      Unpack a domain name from a message, source may be compressed.
2233
 * return:
2234
 *      -1 if it fails, or consumed octets if it succeeds.
2235
 */
2236
int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
2237
               u_char *dst, size_t dstsiz)
2238
{
2239
        const u_char *srcp, *dstlim;
2240
        u_char *dstp;
2241
        int n, len, checked;
2242
 
2243
        len = -1;
2244
        checked = 0;
2245
        dstp = dst;
2246
        srcp = src;
2247
        dstlim = dst + dstsiz;
2248
        if (srcp < msg || srcp >= eom) {
2249
                __set_errno (EMSGSIZE);
2250
                return (-1);
2251
        }
2252
        /* Fetch next label in domain name. */
2253
        while ((n = *srcp++) != 0) {
2254
                /* Check for indirection. */
2255
                switch (n & NS_CMPRSFLGS) {
2256
                case 0:
2257
                        /* Limit checks. */
2258
                        if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
2259
                                __set_errno (EMSGSIZE);
2260
                                return (-1);
2261
                        }
2262
                        checked += n + 1;
2263
                        *dstp++ = n;
2264
                        memcpy(dstp, srcp, n);
2265
                        dstp += n;
2266
                        srcp += n;
2267
                        break;
2268
 
2269
                case NS_CMPRSFLGS:
2270
                        if (srcp >= eom) {
2271
                                __set_errno (EMSGSIZE);
2272
                                return (-1);
2273
                        }
2274
                        if (len < 0)
2275
                                len = srcp - src + 1;
2276
                        srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
2277
                        if (srcp < msg || srcp >= eom) {  /* Out of range. */
2278
                                __set_errno (EMSGSIZE);
2279
                                return (-1);
2280
                        }
2281
                        checked += 2;
2282
                        /*
2283
                         * Check for loops in the compressed name;
2284
                         * if we've looked at the whole message,
2285
                         * there must be a loop.
2286
                         */
2287
                        if (checked >= eom - msg) {
2288
                                __set_errno (EMSGSIZE);
2289
                                return (-1);
2290
                        }
2291
                        break;
2292
 
2293
                default:
2294
                        __set_errno (EMSGSIZE);
2295
                        return (-1);                    /* flag error */
2296
                }
2297
        }
2298
        *dstp = '\0';
2299
        if (len < 0)
2300
                len = srcp - src;
2301
        return (len);
2302
}
2303
#endif /* L_ns_name */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.