summaryrefslogtreecommitdiff
path: root/ecos/packages/net/ns/dns/current/include/dns_impl.inl
blob: 8950ddadb44cc80b1357001fcb78372421eacb15 (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
#ifndef CYGONCE_NS_DNS_DNS_IMPL_H
#define CYGONCE_NS_DNS_DNS_IMPL_H
//=============================================================================
//
//      dns_impl.inl
//
//      DNS client code implementation.
//
//=============================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
// -------------------------------------------                              
// This file is part of eCos, the Embedded Configurable Operating System.   
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under    
// the terms of the GNU General Public License as published by the Free     
// Software Foundation; either version 2 or (at your option) any later      
// version.                                                                 
//
// eCos 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 General Public License    
// for more details.                                                        
//
// You should have received a copy of the GNU General Public License        
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
//
// As a special exception, if other files instantiate templates or use      
// macros or inline functions from this file, or you compile this file      
// and link it with other works to produce a work based on this file,       
// this file does not by itself cause the resulting work to be covered by   
// the GNU General Public License. However the source code for this file    
// must still be made available in accordance with section (3) of the GNU   
// General Public License v2.                                               
//
// This exception does not invalidate any other reasons why a work based    
// on this file might be covered by the GNU General Public License.         
// -------------------------------------------                              
// ####ECOSGPLCOPYRIGHTEND####                                              
//=============================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):   andrew.lunn
// Contributors:andrew.lunn, jskov
// Date:        2001-09-18
// Description: The code is kept in this separate file to allow it to be
//              used from RedBoot.
//
//####DESCRIPTIONEND####
//
//=============================================================================

#include <cyg/ns/dns/dns_priv.h>

/* Validate a hostname is legal as defined in RFC 1035 */
static int
valid_hostname(const char *hostname)
{
    const char * label;
    const char * label_end;
  
    if (!hostname) {
        return false;
    }
    label = hostname;
    while (*label) {
        if (!isalpha(*label))
            return false;
        label_end = strchr(label, (int)'.') - 1;
        if (label_end == (char *)-1) {
            label_end = strchr(label, (int)'\0') - 1;
        }
        while (label != label_end) {
            if (!isalnum(*label) && (*label != '-')) {
                return false;
            }
            label++;
        }
        label = label_end+1;            /* Move onto the . or the null. */
        if (*label == '.') {            /* Move over the . if there is one */
            label++;
        }
    }
    return true;
}

/* Build a qname structure. The structure consists of pairs of
   <len><label> where <label> is eg ma in tux.ma.tech.ascom.ch. len is
   the length of the label. */
static int 
build_qname(unsigned char *ptr, const char *hostname)
{
    const char *label = hostname;
    char *end_label;
    int total_len = 0;
    int len;
  
    while (*label) {
        end_label = strchr(label, (int)'.') - 1;
        if (end_label == (char *)-1) {
            end_label = strchr(label, (int)'\0') - 1;
        }
        len = end_label - label + 1;
        if (len > 63) {
            return -1;
        }
        *ptr++ = (char) len;            /* Put the length of the label */
        memcpy(ptr, label, len);        /* and now the label */
        ptr += len;
    
        total_len += len +1;
        label = end_label+1;            /* Move onto the . or the null. */
        if (*label == '.') {            /* Move over the . if there is one */
            label++;
        }
    }
    *ptr = 0;                           /* Add the last length of zero
                                           to mark the end */
    return (total_len+1);
}

/* Given a pointer to a qname, find the length of the qname. This is
   the number of bytes needed to represent the qname, not the length
   of the name. A qname is terminated by either a 00, or a pointer
   into another qname. This pointer has the top two bits set. */
static int 
qname_len(unsigned char * qname)
{
    unsigned char * ptr = qname;
    
    while ((*ptr != 0) && ((*ptr & 0xc0) != 0xc0)) {
        ptr += *ptr + 1;
    }
    /* Pointers are two bytes */
    if ((*ptr & 0xc0) == 0xc0) {
        ptr++;
    }
    ptr++;                              /* Skip over the trailing byte */

    return (ptr - qname);
}

/* Build a real name from a qname. Alloc the memory needed and return
   it. Return NULL on error */
char *
real_name(unsigned char *msg, unsigned char *qname)
{
    unsigned char * ptr = qname;
    char * name;
    char * label;
    int len = 0;
    int offset;

    /* First pass works out the length of the name */
    while (*ptr != 0) {
        if ((*ptr & 0xc0) == 0xc0) {
            /* pointer to somewhere else. Follow the pointer */
            offset = ((*ptr & 0x3f) << 8) | *(ptr+1);
            ptr = msg + offset;
        } else {
            len += *ptr + 1;
            ptr += *ptr + 1;
        }
    }

    /* Now allocate the memory needed and copy the host name into it */
    name = alloc_string(len+1);
    if (name) {
        label = name;
        ptr = qname;
        while (*ptr != 0) {
            if ((*ptr & 0xc0) == 0xc0) {
                /* pointer to somewhere else. Follow the pointer */
                offset = ((*ptr & 0x3f) << 8) | *(ptr+1);
                ptr = msg + offset;
            } else {
                len = *ptr;
                memcpy(label, (ptr+1), len);
                label += len;
                *label++ = '.';
                ptr += *ptr + 1;
            }
        }
        *label = '\0';
    }
    return name;
}

/* Build a query message which can be sent to the server. If something
   goes wrong return -1, otherwise the length of the query message */
static int 
build_query(const unsigned char * msg, const char * hostname, short rr_type)
{
    struct dns_header *dns_hdr;
    unsigned char *ptr;
    int len;

    /* Fill out the header */
    dns_hdr = (struct dns_header *) msg;
    dns_hdr->id = (cyg_uint16)htons(id++);
    dns_hdr->rd = true;
    dns_hdr->opcode = DNS_QUERY;
    dns_hdr->qdcount = htons(1);
  
    /* Now the question we want to ask */
    ptr = (unsigned char *)&dns_hdr[1];

    len = build_qname(ptr, hostname);

    if (len < 0) {
        h_errno = NO_RECOVERY;
        return -1;
    }
    ptr += len;

    /* Set the type and class fields */
    *ptr++ = (rr_type >> 8) & 0xff;
    *ptr++ = rr_type & 0xff;
    *ptr++ = (DNS_CLASS_IN >> 8) & 0xff;
    *ptr++ = DNS_CLASS_IN & 0xff;

    len = ptr - msg;

    return len;
}

/* Check if the hostname is actually of dot format. If so convert it 
   and return host entity structure. If not, return NULL. */
static struct hostent *
dot_hostname(const char *hostname)
{
    struct hostent *hent = NULL;
    struct in_addr addr;

    if (inet_aton(hostname, &addr)) {
        hent = alloc_hent();
        if (hent) {
            memcpy(hent->h_addr_list[0], &addr, sizeof(struct in_addr));
            hent->h_addrtype = AF_INET;
            hent->h_length = sizeof(struct in_addr);
            hent->h_name = alloc_string(strlen(hostname)+1);
            if (!hent->h_name) {
                free_hent(hent);
                return NULL;
            }
            strcpy(hent->h_name, hostname);
        }
    }
    return hent;
}

#ifdef CYGPKG_NET_INET6
/* Check if the hostname is actually colon format of IPv6. If so convert it 
   and return host entity structure. If not, return NULL. */
static struct hostent *
colon_hostname(const char *hostname) 
{   
  struct sockaddr_in6 addr;
  struct hostent *hent = NULL;

  if (inet_pton(AF_INET6, hostname, (void *)&addr)) {
    hent = alloc_hent();
    if (hent) {
      memcpy(hent->h_addr_list[0], &addr, sizeof(addr));
      hent->h_addrtype = AF_INET6;
      hent->h_length = sizeof(addr);
      hent->h_name = alloc_string(strlen(hostname)+1);
      if (!hent->h_name) {
        free_hent(hent);
        return NULL;
      }
      strcpy(hent->h_name, hostname);
    }
  }
  return hent;
}
#endif

/* Decode the answer from the server. Returns NULL if failed, or 
   a hostent structure containing different data depending on the
   query type:
    DNS_TYPE_A: 
      h_name:         a pointer to the hostname
      h_addr_list[0]: a pointer to in_addr structure
    DNS_TYPE_PTR:
      h_name:         a pointer to the hostname
      h_addr_list[0]: a pointer to an empty in_addr structure. Caller
                      must fill out!
*/
static struct hostent *
parse_answer(unsigned char * msg, short rr_type)
{
    static struct hostent *hent;
    struct dns_header *dns_hdr;
    struct resource_record rr, *rr_p = NULL;
    unsigned char *qname = NULL;
    unsigned char *ptr;

    dns_hdr = (struct dns_header *)msg;

    if (DNS_REPLY_NAME_ERROR == dns_hdr->rcode) {
        h_errno = HOST_NOT_FOUND;
        return NULL;
    }

    if ((dns_hdr->qr != 1) || 
        (dns_hdr->opcode != DNS_QUERY) ||
        (dns_hdr->rcode != DNS_REPLY_NOERR)) {
        h_errno = NO_RECOVERY;
        return NULL;
    }
  
    dns_hdr->ancount = ntohs(dns_hdr->ancount);
    dns_hdr->qdcount = ntohs(dns_hdr->qdcount);
    ptr = (unsigned char *)&dns_hdr[1];

    /* Skip over the query section */
    if (dns_hdr->qdcount > 0) {
        while (dns_hdr->qdcount) {
            ptr += qname_len(ptr);
            ptr += 4;                   /* skip type & class */
            dns_hdr->qdcount--;
        }
    }  
    /* Skip over the answers resource records to find an answer of the
       correct type. */
    while (dns_hdr->ancount) {
        qname = ptr;
        ptr += qname_len(ptr);
        rr_p = (struct resource_record *)ptr;
        memcpy(&rr, ptr, sizeof(rr));
        if ((rr.rr_type == htons(rr_type)) && 
            (rr.class == htons(DNS_CLASS_IN))) {
            break;
        }
        ptr += sizeof(struct resource_record) - sizeof(rr.rdata) + ntohs(rr.rdlength);
        dns_hdr->ancount--;
    }

    /* If we found one. decode it */
    if (dns_hdr->ancount > 0) {
        hent = alloc_hent();
        if (!hent) 
            return NULL;
        switch (rr_type) {
        case DNS_TYPE_A:
            hent->h_name = real_name(msg, qname);
            if (!hent->h_name) {
                free_hent(hent);
                return NULL;
            }
            memcpy(hent->h_addr_list[0], rr_p->rdata, sizeof(struct in_addr));
            hent->h_addrtype = AF_INET;
            hent->h_length = sizeof(struct in_addr);
            return hent;
        case DNS_TYPE_PTR:
            hent->h_name = real_name(msg, (unsigned char *)rr_p->rdata);
            if (!hent->h_name) {
                free_hent(hent);
                return NULL;
            }
            hent->h_addrtype = AF_INET;
            hent->h_length = sizeof(struct in_addr);
            return hent;
        default:
            free_hent(hent);
        }
    }
    h_errno = NO_DATA;
    return NULL;
}

/* Given an address, find out the hostname. */
struct hostent *
gethostbyaddr(const char *addr, int len, int type)
{
    unsigned char msg[MAXDNSMSGSIZE];
    char hostname[40];
    struct hostent * hent;

    CYG_REPORT_FUNCNAMETYPE( "gethostbyaddr", "returning %08x" );
    CYG_REPORT_FUNCARG3( "addr=%08x, len=%d, type=%d", addr, len, type );

    if ( !addr || 0 == len) {
        CYG_REPORT_RETVAL( NULL );
        return NULL;
    }

    CYG_CHECK_DATA_PTR( addr, "addr is not a valid pointer!" );

    /* Has the socket to the DNS server been opened? */
    if (s < 0) {
        CYG_REPORT_RETVAL( NULL );
        return NULL;
    }

    /* See if there is an answer to an old query. If so free the memory
       it uses. */
    free_stored_hent();

    /* Only IPv4 addresses accepted */
    if ((type != AF_INET) || (len != sizeof(struct in_addr))) {
        CYG_REPORT_RETVAL( NULL );
        return NULL;
    }

    cyg_drv_mutex_lock(&dns_mutex);

    /* Build the 'hostname' we want to lookup. */
    sprintf(hostname, "%d.%d.%d.%d.IN-ADDR.ARPA.",
            (unsigned char)addr[3],
            (unsigned char)addr[2],
            (unsigned char)addr[1],
            (unsigned char)addr[0]);
  
    memset(msg, 0, sizeof(msg));
  
    /* Build a PTR type request using the hostname */
    len = build_query(msg, hostname, DNS_TYPE_PTR);
    if (len < 0) {
        cyg_drv_mutex_unlock(&dns_mutex);
        CYG_REPORT_RETVAL( NULL );
        return NULL;
    }

    /* Send the request and wait for an answer */
    len = send_recv(msg, len, sizeof(msg));
    if (len < 0) {
        cyg_drv_mutex_unlock(&dns_mutex);
        CYG_REPORT_RETVAL( NULL );
        return NULL;
    }

    /* Fill in the missing address */
    hent = parse_answer(msg, DNS_TYPE_PTR);
    if (hent) {
        memcpy(hent->h_addr_list[0], addr, sizeof(struct in_addr));
        store_hent(hent);
    }
    cyg_drv_mutex_unlock(&dns_mutex);

    CYG_REPORT_RETVAL( hent );
    return hent;
}

/* Build message, send, receive and decode */
static struct hostent *
do_query(const char * hostname) 
{
    unsigned char msg[MAXDNSMSGSIZE];
    int len;
        
    memset(msg, 0, sizeof(msg));
    len = build_query(msg, hostname, DNS_TYPE_A);
    if (len < 0) {
        return NULL;
    }
  
    /* Send the query and wait for an answer */
    len = send_recv(msg, len, sizeof(msg));
    if (len < 0) {
        return NULL;
    }
  
    /* Decode the answer */
    return parse_answer(msg, DNS_TYPE_A);
}


/* Given a hostname find out the IP address */
struct hostent *
gethostbyname(const char * hostname)
{
    char name[256];
    char * dot;
    struct hostent *hent;

    CYG_REPORT_FUNCNAMETYPE( "gethostbyname", "returning %08x" );
    CYG_REPORT_FUNCARG1( "hostname=%08x", hostname );

    if ( !hostname ) {
        CYG_REPORT_RETVAL( NULL );
        return NULL;
    }

    CYG_CHECK_DATA_PTR( hostname, "hostname is not a valid pointer!" );

    /* Has the socket to the DNS server been opened? */
    if (s < 0) {
        CYG_REPORT_RETVAL( NULL );
        return NULL;
    }

    /* See if there is an answer to an old query. If so free the memory
       it uses */
    free_stored_hent();
  
    if (!valid_hostname(hostname)) {
         /* It could be a dot address */
         if ((hent = dot_hostname(hostname)) != NULL) {
              store_hent(hent);
              CYG_REPORT_RETVAL( hent );
              return hent;
         }
#ifdef CYGPKG_NET_INET6
         /* It could be a colon seperated IPv6 address */
         if ((hent = colon_hostname(hostname)) != NULL) {
              store_hent(hent);
              CYG_REPORT_RETVAL( hent );
              return hent;
         }
#endif
         CYG_REPORT_RETVAL( hent );
         return hent;
    }
    cyg_drv_mutex_lock(&dns_mutex);

    if (domainname) {
        if ((strlen(hostname) + strlen(domainname)) > 254) {
            h_errno = NO_RECOVERY;
            cyg_drv_mutex_unlock(&dns_mutex);
            CYG_REPORT_RETVAL( NULL );
            return NULL;
        }
        strcpy(name, hostname);
        strcat(name, ".");
        strcat(name, domainname);
    }

        /* If the hostname ends with . it a FQDN. Don't bother adding the
    domainname. If it does not contain a . , try appending with the
    domainname first. If it does have a . , try without a domain name
    first. */

    dot = strrchr(hostname,'.');
    if (dot) {
        if (*(dot+1) == '\0') {
            /* FQDN */
            hent = do_query(hostname);
        } else {
          /* Dot somewhere */
          hent = do_query(hostname);
          if (domainname && (hent == NULL)) { 
            hent = do_query(name);
          }
        }
    } else {
    /* No Dot. Try adding domainname first */
        hent = NULL;
        if (domainname) {
            hent = do_query(name);
        }
        if (hent == NULL) {
            hent = do_query(hostname);
        }
    }
    
    cyg_drv_mutex_unlock(&dns_mutex); 
    store_hent(hent);
    CYG_REPORT_RETVAL( hent );
    return hent;
}

/* Set the domain names, as used by the DNS server */
int
setdomainname(const char *name, size_t len)
{
    char * ptr;
    int length;

    CYG_REPORT_FUNCNAMETYPE( "setdomainname", "returning %d" );
    CYG_REPORT_FUNCARG2( "name=%08x, len=%d", name, len );

    if ((len < 0) || (len > 255)) {
        h_errno = NO_RECOVERY;
        CYG_REPORT_RETVAL( -1 );
        return -1;
    }
    if (len != 0) {
        CYG_CHECK_DATA_PTR( name, "name is not a valid pointer!" );
        length = strlen(name);
        if (length > len) {
            h_errno = NO_RECOVERY;
            CYG_REPORT_RETVAL( -1 );
            return -1;
        }
        if (name[length-1] != '.') {
                length++;
        }       
        ptr = alloc_string(length+1);
        if (!ptr) {         
            h_errno = NO_RECOVERY;
            CYG_REPORT_RETVAL( -1 );
            return -1;
        } 
        memcpy(ptr, name, len);
        if (name[length-1] != '.') {
            ptr[length-1] = '.';
            ptr[length] = '\0';
        } else {
            ptr[len]=0;
        }
    } else {
        ptr = NULL;
    }
  
    if (domainname) {
        free_string(domainname);
    }
    domainname = ptr;
    CYG_REPORT_RETVAL( 0 );
    return 0;
}

/* Return the domain name as used by the DNS server */
int
getdomainname(char *name, size_t len)
{
    CYG_REPORT_FUNCNAMETYPE( "getdomainname", "returning %d" );
    CYG_REPORT_FUNCARG2( "name=%08x, len=%d", name, len );

    if ( !name || 0 == len) {
        CYG_REPORT_RETVAL( -1 );
        return -1;
    }

    CYG_CHECK_DATA_PTR( name, "name is not a valid pointer!" );

    /* No domainname set, return a 0 */
    if (!domainname) {
        if (len == 0) {
            h_errno = HOST_NOT_FOUND;
            CYG_REPORT_RETVAL( -1 );
            return -1;
        }
        name[0]='\0';
    } else {
        if ((strlen(domainname) + 1) > len) {
            h_errno = NO_RECOVERY;
            CYG_REPORT_RETVAL( -1 );
            return -1;
        }
        strncpy(name, domainname, len);
    }
    CYG_REPORT_RETVAL( 0 );
    return 0;
}

int h_errno = DNS_SUCCESS;

const char*
hstrerror(int err)
{
    CYG_REPORT_FUNCNAMETYPE( "hstrerror", "returning %08x" );
    CYG_REPORT_FUNCARG1( "err=%d", err );

    switch (err) {
    case DNS_SUCCESS:
        return "No error";
    case HOST_NOT_FOUND:
        return "No such host is known";
    case TRY_AGAIN:
        return "Timeout";
    case NO_RECOVERY:
        return "Server failure or invalid input";
    case NO_DATA:
        return "No data for type";
    default:
        return "Uknown error";
    }
}

//-----------------------------------------------------------------------------
#endif // CYGONCE_NS_DNS_DNS_IMPL_H
// End of dns_impl.h