summaryrefslogtreecommitdiff
path: root/ecos/packages/net/common/current/src/getaddrinfo.c
blob: 2f2735ecb4297b2caf97ff103945b2b98a4d3ff8 (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
//==========================================================================
//
//      lib/getaddrinfo.c
//
//      getaddrinfo(), freeaddrinfo(), gai_strerror(), getnameinfo()
//
//==========================================================================
// ####ECOSPDCOPYRIGHTBEGIN####                                    
// -------------------------------------------                     
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
//
// Permission is granted to use, copy, modify and redistribute this
// file.                                                           
//
// -------------------------------------------                     
// ####ECOSPDCOPYRIGHTEND####                                      
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    gthomas
// Contributors: gthomas, andrew.lunn@ascom.ch
// Date:         2002-03-05
// Purpose:      
// Description:  
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================

#include <sys/param.h>
#include <sys/socket.h>           // PF_xxx
#include <netinet/in.h>           // IPPROTO_xx
#include <net/netdb.h>
#include <netdb.h>                // DNS support routines
#include <errno.h>
#include <cyg/infra/cyg_ass.h>

#include <pkgconf/system.h>
#ifdef CYGPKG_NS_DNS
#include <pkgconf/ns_dns.h>
#ifdef CYGPKG_NS_DNS_BUILD
#include <cyg/ns/dns/dns.h>
#endif
#endif

extern int  sprintf(char *, const char *, ...);
extern long strtol(const char *, char **, int);
extern void *malloc(size_t);
extern void *calloc(int, size_t);
extern void free(void *);

// Allocate a new addrinfo structure and if passed an existing
// addrinfo copy all the port, protocol info into the new structure
// and then link the new onto the old.

struct addrinfo * alloc_addrinfo(struct addrinfo * ai) {
    
    struct addrinfo * nai;
    struct sockaddr * sa;

    nai = (struct addrinfo *)malloc(sizeof(struct addrinfo));
    if (!nai) {
        return NULL;
    }
    sa = (struct sockaddr *) malloc(sizeof(struct sockaddr));
    if (!sa) {
        free (nai);
        return NULL;
    }
    memset(sa,0,sizeof(*sa));

    if (ai) {
        memcpy(nai,ai,sizeof(struct addrinfo));
        ai->ai_next = nai;
    } else {
        memset(nai,0,sizeof(*nai));
    }
    nai->ai_addr = sa;
    nai->ai_addrlen = sizeof(*sa);

    return nai;
}

// getaddrinfo has not been passed a hostname. So it should use the
// loopback or the any address.
static int
no_node_addr(struct addrinfo *ai, const struct addrinfo *hints, int port) {

    switch (hints->ai_family) {
    case AF_INET: {
        struct sockaddr_in *sa = (struct sockaddr_in *) ai->ai_addr;
        if (hints->ai_flags & AI_PASSIVE) {
            sa->sin_addr.s_addr = htonl(INADDR_ANY);
        } else {
            sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        }
        sa->sin_len = sizeof(*sa);
        sa->sin_port = htons(port);
        sa->sin_family = AF_INET;
        ai->ai_family = AF_INET;
        break;
    }
#ifdef CYGPKG_NET_INET6
    case AF_INET6: {
        struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->ai_addr;
        if (hints->ai_flags & AI_PASSIVE) {
            memcpy(&sa->sin6_addr, &in6addr_any, sizeof(sa->sin6_addr));
        } else {
            memcpy(&sa->sin6_addr, &in6addr_loopback, sizeof(sa->sin6_addr));
        }
        sa->sin6_len = sizeof(*sa);
        sa->sin6_port = htons(port);
        sa->sin6_family = AF_INET6;
        ai->ai_family = AF_INET6;
        break;
    }
#endif
    case PF_UNSPEC: {
        struct sockaddr_in *sa = (struct sockaddr_in *) ai->ai_addr;
        if (hints->ai_flags & AI_PASSIVE) {
            sa->sin_addr.s_addr = htonl(INADDR_ANY);
        } else {
            sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        }
        sa->sin_len = sizeof(*sa);
        sa->sin_port = htons(port);
        sa->sin_family = AF_INET;
        ai->ai_family = AF_INET;
#ifdef CYGPKG_NET_INET6 
        {
            struct sockaddr_in6 *sa6;
            ai=alloc_addrinfo(ai);
            if (ai == NULL) {
                return EAI_MEMORY;
            }
            sa6 = (struct sockaddr_in6 *) ai->ai_addr;
            if (hints->ai_flags & AI_PASSIVE) {
                memcpy(&sa6->sin6_addr, &in6addr_any, sizeof(sa6->sin6_addr));
            } else {
                memcpy(&sa6->sin6_addr, &in6addr_loopback, sizeof(sa6->sin6_addr));
            }
            sa6->sin6_len = sizeof(*sa);
            sa6->sin6_port = htons(port);
            sa6->sin6_family = AF_INET6;
            ai->ai_family = AF_INET6;
        }
#endif
        break;
    }
    }
   return EAI_NONE;
}


// We have been asked to convert only numeric addresses so as to not
// need a DNS server query.
static int
numeric_node_addr(struct addrinfo *ai, const char *node, 
               const struct addrinfo *hints, int port) {
    
    switch (hints->ai_family) {
    case AF_INET: {
        struct sockaddr_in *sa = (struct sockaddr_in *) ai->ai_addr;
        if (!inet_pton(AF_INET, node, (void *)&sa->sin_addr.s_addr)) {
            return EAI_FAIL;
        }
        sa->sin_port = htons(port);
        sa->sin_family = AF_INET;
        sa->sin_len = sizeof(*sa);
        ai->ai_family = AF_INET;
        break;
    }
#ifdef CYGPKG_NET_INET6
    case AF_INET6: {
        struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->ai_addr;
        if (!inet_pton(AF_INET6, node, (void *)&sa->sin6_addr.s6_addr)) {
            return EAI_FAIL;
        }
        sa->sin6_port = htons(port);
        sa->sin6_family = AF_INET6;
        sa->sin6_len = sizeof(*sa);
        ai->ai_family = AF_INET6;
        break;
    }
#endif
    case PF_UNSPEC: {
        struct sockaddr_in *sa = (struct sockaddr_in *) ai->ai_addr;
        sa->sin_len = sizeof(*sa);
        sa->sin_port = htons(port);
        sa->sin_family = AF_INET;
        ai->ai_family = AF_INET;
        if (inet_pton(AF_INET, node, (void *)&sa->sin_addr.s_addr)) {
          return EAI_NONE;
        }
#ifdef CYGPKG_NET_INET6
        {
            struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->ai_addr;
            sa->sin6_len = sizeof(*sa);
            sa->sin6_port = htons(port);
            sa->sin6_family = AF_INET6;
            ai->ai_family = AF_INET6;
            if (inet_pton(AF_INET6, node, (void *)&sa->sin6_addr.s6_addr)) {
                return EAI_NONE;
            }
        }
#endif
        return EAI_FAIL;
        break;
    }
    }
    return EAI_NONE;
}

// We have a host name. Use the DNS client to perform a lookup. If the
// DNS client is not part of the configuration try using the numeric
// convertion.
static int
with_node_addr(struct addrinfo *ai, const char *node, 
               const struct addrinfo *hints, int port) {
    
#ifdef CYGPKG_NS_DNS_BUILD
    struct sockaddr addrs[CYGNUM_NS_DNS_GETADDRINFO_ADDRESSES];
    int nresults;
    int i;
    char ** canon = NULL;
    
    if (hints->ai_flags & AI_CANONNAME) {
        canon = &ai->ai_canonname;
    }
    nresults = cyg_dns_getaddrinfo(node, 
                                   addrs, CYGNUM_NS_DNS_GETADDRINFO_ADDRESSES, 
                                   hints->ai_family, canon);
    if (nresults < 0) {
        return -nresults;
    }
    
    for (i=0; i < nresults; i++) {
        if (i != 0) {
            ai = alloc_addrinfo(ai);
            if (ai == NULL) {
                return EAI_MEMORY;
            }
        }
        memcpy(ai->ai_addr, &addrs[i], sizeof(addrs[i]));        
        ai->ai_family = addrs[i].sa_family;
        ai->ai_addrlen = addrs[i].sa_len;
        switch (ai->ai_family) {
        case AF_INET: {
            struct sockaddr_in *sa = (struct sockaddr_in *) ai->ai_addr;
            sa->sin_port = htons(port);
            break;
        }
#ifdef CYGPKG_NET_INET6
        case AF_INET6: {
            struct sockaddr_in6 *sa = (struct sockaddr_in6 *) ai->ai_addr;
            sa->sin6_port = htons(port);
            break;
        }
#endif
        }
    }
    return EAI_NONE;
#else
    return (numeric_node_addr(ai, node, hints, port));
#endif
}

int   
getaddrinfo(const char *nodename, const char *servname,
            const struct addrinfo *hints, struct addrinfo **res)
{
    struct addrinfo dflt_hints;
    struct protoent *proto = (struct protoent *)NULL;
    struct addrinfo *ai;
    char *protoname;
    char *endptr;
    int port = 0;
    int err;

    if (hints == (struct addrinfo *)NULL) {
        dflt_hints.ai_flags = 0;  // No special flags
        dflt_hints.ai_family = PF_UNSPEC;
        dflt_hints.ai_socktype = 0;
        dflt_hints.ai_protocol = 0;
        hints = &dflt_hints;
    }
    // Prevalidate parameters
    if ((nodename == (char *)NULL) && (servname == (char *)NULL)) {
        return EAI_NONAME;
    }
    switch (hints->ai_family) {
    case PF_UNSPEC:
    case PF_INET:
        break;
#ifdef CYGPKG_NET_INET6
    case PF_INET6:
        break;
#endif
    default:
        return EAI_FAMILY;
    }
    // Allocate the first/primary result
    *res = ai = alloc_addrinfo(NULL);
    if (ai == (struct addrinfo *)NULL) {
        return EAI_MEMORY;
    }
    // Handle request
    if (hints->ai_protocol != 0) {
        proto = getprotobynumber(hints->ai_protocol);
    }
    
    // Note: this does not handle the case where a given service can be
    // handled via multiple protocols, e.g. http/tcp & http/udp
    if (servname != (char *)NULL) {
        switch (hints->ai_socktype) {
        case 0:
        case SOCK_STREAM:
            protoname = "tcp";
            ai->ai_socktype = SOCK_STREAM;
            break;
        case SOCK_DGRAM:
            protoname = "udp";
            ai->ai_socktype = SOCK_DGRAM;
            break;
        default:
            freeaddrinfo(ai);
            return EAI_SOCKTYPE;
        }
        // See if this is just a port #
        if (((port = strtol(servname, &endptr, 0)) >= 0) &&
            (endptr > servname)) {
            ai->ai_socktype = hints->ai_socktype;
            if (hints->ai_socktype == 0) {
                // Need to have complete binding type/port
                freeaddrinfo(ai);
                return EAI_SERVICE;
            }
        } else {
            struct servent *serv = (struct servent *)NULL;
            
            serv = getservbyname(servname, protoname);
            if (serv == (struct servent *)NULL) {
                if (hints->ai_socktype == 0) {
                    protoname = "udp";
                    ai->ai_socktype = SOCK_DGRAM;    
                    serv = getservbyname(servname, protoname);
                }
            }
            if (serv == (struct servent *)NULL) {
                freeaddrinfo(ai);
                return EAI_SERVICE;
            }
            port = ntohs(serv->s_port);  
        }
        proto = getprotobyname(protoname);
        if (hints->ai_protocol && (hints->ai_protocol != proto->p_proto)) {
            freeaddrinfo(ai);
            return EAI_SOCKTYPE;
        }
        ai->ai_protocol = proto->p_proto;
    }
    
    if (nodename) {
        if (hints->ai_flags & AI_NUMERICHOST) {
            err = numeric_node_addr(ai, nodename, hints, port);
        } else {
            err = with_node_addr(ai, nodename, hints, port );
        }
    } else {
        err = no_node_addr(ai, hints, port);
    }
    
    if (err != EAI_NONE) {
        freeaddrinfo(ai);
        return err;
    }

    if ((hints->ai_flags & AI_CANONNAME) && !nodename) {
        ai->ai_canonname = malloc(strlen("localhost")+1);
        if (ai->ai_canonname) {
            strcpy(ai->ai_canonname, "localhost");
        } else {
            freeaddrinfo(ai);
            return EAI_MEMORY;
        }
    }
    
    /* The DNS code may have filled in the official address. If not
       and we have been asked for it, return an error */
    if ((hints->ai_flags & AI_CANONNAME) & !ai->ai_canonname) {
        freeaddrinfo(ai);
        return EAI_FAIL;
    }
    return EAI_NONE;  // No errors
}

// The canonname will probably point to the same memory in each
// addrinfo in the linked list. Don't free it multiple times.
void  
freeaddrinfo(struct addrinfo *ai)
{
    struct addrinfo *next = ai;
    char * last_canonname = NULL;

    while ((ai = next) != (struct addrinfo *)NULL) {
        if ((ai->ai_canonname) && 
            (ai->ai_canonname != last_canonname)) { 
            free(ai->ai_canonname);
            last_canonname = ai->ai_canonname;
        }
        if (ai->ai_addr) free(ai->ai_addr);
        next = ai->ai_next;
        free(ai);
    }
}

char 
*gai_strerror(int err)
{
    switch (err) {
    case EAI_NONE:
        return "EAI - no error";
    case EAI_AGAIN:
        return "EAI - temporary failure in name resolution";
    case EAI_BADFLAGS:
        return "EAI - invalid flags";
    case EAI_FAIL:
        return "EAI - non-recoverable failure in name resolution";
    case EAI_FAMILY:
        return "EAI - family not supported";
    case EAI_MEMORY:
        return "EAI - memory allocation failure";
    case EAI_NONAME:
        return "EAI - hostname nor servname provided, or not known";
    case EAI_SERVICE:
        return "EAI - servname not supported for socket type";
    case EAI_SOCKTYPE:
        return "EAI - socket type not supported";
    case EAI_SYSTEM:
        return "EAI - system error";
    case EAI_BADHINTS:
        return "EAI - inconsistent hints";
    case EAI_PROTOCOL:
        return "EAI - bad protocol";
    default:
        return "EAI - unknown error";
    }
}

// Set of flags implemented
#define NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM)

int 
getnameinfo (const struct sockaddr *sa, socklen_t salen, 
             char *host, socklen_t hostlen, 
             char *serv, socklen_t servlen, 
             unsigned int flags)
{
    int port;
    char *s;
    struct servent *se;
    int error;
    int numeric = (flags & NI_NUMERICHOST);

    if ((flags & ~NI_MASK) != 0) {
        return EAI_BADFLAGS;
    }
    switch (sa->sa_family) {
    case PF_INET:
#ifdef CYGPKG_NET_INET6
    case PF_INET6:
#endif
        if (host != (char *)NULL) {
            if ( !numeric) {
                error = EAI_NONAME;
#ifdef CYGPKG_NS_DNS_BUILD
                error = -cyg_dns_getnameinfo(sa, host,hostlen);
#endif
                if ((error == EAI_NONAME) && (flags & NI_NAMEREQD)) {
                    return EAI_NONAME;
                }
                // If lookup failed, try it as numeric address
                numeric = !(error == EAI_NONE);
            }
            if (numeric) {
                s = _inet_ntop((struct sockaddr *)sa, host, hostlen);
                if (!s) {
                    return EAI_FAIL;
                }
            }
            if (!numeric && flags & NI_NOFQDN) {
                s = index(host, '.');
                if (s) {
                    *s = '\0';
                }
            }
        }
    
        if (serv != (char *)NULL) {
            port = _inet_port((struct sockaddr *)sa);
            if (!port) {
                return EAI_FAIL;
            }
            se = (struct servent *)NULL;
            if ((flags & NI_NUMERICSERV) == 0) {
                if ((flags & NI_DGRAM) == 0) {
                    se = getservbyport(htons(port), "tcp");
                }
                if (se == (struct servent *)NULL) {
                    se = getservbyport(htons(port), "udp");
                }
            }
            if (se != (struct servent *)NULL) {
                diag_snprintf(serv,servlen, "%s/%s", se->s_name, se->s_proto);
            } else {
                diag_snprintf(serv,servlen, "%d", port);
            }
        }
        break;
    default:
        return EAI_FAMILY;
    }
    return EAI_NONE;
}