File: rpc2_addrinfo.c

package info (click to toggle)
rpc2 2.7%2Bdebian-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,852 kB
  • ctags: 2,661
  • sloc: ansic: 19,928; sh: 9,110; lex: 437; yacc: 416; makefile: 126; asm: 35
file content (651 lines) | stat: -rw-r--r-- 16,976 bytes parent folder | download | duplicates (2)
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
/* BLURB lgpl

                           Coda File System
                              Release 6

          Copyright (c) 2002-2003 Carnegie Mellon University
                  Additional copyrights listed below

This  code  is  distributed "AS IS" without warranty of any kind under
the  terms of the  GNU  Library General Public Licence  Version 2,  as
shown in the file LICENSE. The technical and financial contributors to
Coda are listed in the file CREDITS.

                        Additional copyrights
                           none currently
#*/

/*
 * This code provides getaddrinfo functionality, but we use our own structures
 * so that we can provide the following functions,
 *   RPC2_allocaddrinfo  - converts a sockaddr to addrinfo.
 *   RPC2_copyaddrinfo   - performs a deep copy operation.
 *   RPC2_formataddrinfo - addrinfo to string conversion.
 *   RPC2_cmpaddrinfo    - test if we have a member of a linked list of
 *			   addrinfo structures.
 *
 * Then there are some convenience functions for RPC2,
 *   rpc2_resolve	 - resolves host/port ident structure to addrinfo.
 *   rpc2_splitaddrinfo  - breaks an addrinfo into host/port ident.
 *   rpc2_simplifyHost   - used to for compatibility with old rpc2 apps.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#include "rpc2.private.h"

/* internal helper functions */

#ifndef HAVE_GETADDRINFO
#ifndef HAVE_GETIPNODEBYNAME

/* flags */
#define AI_ALL 0

/* errorcodes */
#define HOST_TRY_AGAIN TRY_AGAIN
#define HOST_NOADDRESS NO_ADDRESS
#define HOST_NORECOVERY NO_RECOVERY

static struct hostent *getipnodebyname(const char *name, int af, int flags,
				       int *error_num)
{
    struct hostent *he = NULL;
    int err;

    if (af == PF_INET) {
	he = gethostbyname(name);
	err = h_errno;
    } else
	err = HOST_NOADDRESS;

    if (!he && error_num)
	*error_num = err;

    return he;
}

static void freehostent(struct hostent *ip)
{
    return;
}
#endif /* !HAVE_GETIPNODEBYNAME */

static int addrinfo_init(int family, const void *addr, short port,
			 const struct RPC2_addrinfo *hints,
			 struct RPC2_addrinfo **res)
{
    int socktype = SOCK_STREAM, protocol = IPPROTO_TCP;
    struct sockaddr_storage ss;
    struct RPC2_addrinfo *ai;
    int addrlen = 0;
    memset(&ss, 0, sizeof(ss));

    switch (family) {
    case PF_INET:
	{
	    struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
	    sin->sin_family = PF_INET;
	    sin->sin_addr = *(struct in_addr *)addr;
	    sin->sin_port = port;
	    addrlen = sizeof(*sin);
	    break;
	}
#if defined(PF_INET6)
    case PF_INET6:
	{
	    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
	    sin6->sin6_family = PF_INET6;
	    sin6->sin6_addr = *(struct in6_addr *)addr;
	    sin6->sin6_port = port;
	    addrlen = sizeof(*sin6);
	    break;
	}
#endif
    default:
	return RPC2_EAI_FAMILY;
    }

    if (hints) {
	socktype = hints->ai_socktype;
	protocol = hints->ai_protocol;
    }

    ai = RPC2_allocaddrinfo((struct sockaddr *)&ss, addrlen,
			    socktype, protocol);
    if (!ai)
	return RPC2_EAI_MEMORY;

    ai->ai_next = *res;
    *res = ai;
    return 0;
}

static int getaddrinfo_noresolve(const char *node, short port,
				 const struct RPC2_addrinfo *hints,
				 struct RPC2_addrinfo **res)
{
    struct RPC2_addrinfo *ai;
    int family = hints ? hints->ai_family : PF_UNSPEC;
#if defined(PF_INET6)
    char addr[sizeof(struct in6_addr)];
#else
    char addr[sizeof(struct in_addr)];
#endif
    int err4, err6;

    err4 = err6 = RPC2_EAI_NONAME;
#if defined(PF_INET6)
    if (!hints || hints->ai_family != PF_INET6) /* PF_UNSPEC || PF_INET */
#endif
    {
	struct in_addr addr;
	if (!node) {
	    if (hints && hints->ai_flags & RPC2_AI_PASSIVE)
		 addr.s_addr = INADDR_ANY;
	    else addr.s_addr = INADDR_LOOPBACK;
	}
	else if (inet_pton(PF_INET, node, &addr) <= 0)
	    goto v4_not_found;

	err4 = addrinfo_init(PF_INET, &addr, port, hints, res);
    }
v4_not_found:

#if defined(PF_INET6)
    if (!hints || hints->ai_family != PF_INET) /* PF_UNSPEC || PF_INET6 */
    {
	struct in6_addr addr;
	if (!node) {
	    if (hints && hints->ai_flags & RPC2_AI_PASSIVE)
		 addr = in6addr_any;
	    else addr = in6addr_loopback;
	}
	else if (inet_pton(PF_INET6, node, &addr) <= 0)
	    goto v6_not_found;

	err6 = addrinfo_init(PF_INET6, &addr, port, hints, res);
    }
v6_not_found:
#endif

    /* did we find any usable address? */
    if (!err4 || !err6)
	return 0;

    /* perhaps we had an allocation error? */
    if (err4 == RPC2_EAI_MEMORY || err6 == RPC2_EAI_MEMORY)
	return RPC2_EAI_MEMORY;

    /* unable to get an ip-address from the passed arguments */
    return RPC2_EAI_NONAME;
}
#endif /* !HAVE_GETADDRINFO */

/* exported helper functions */

struct RPC2_addrinfo *RPC2_allocaddrinfo(const struct sockaddr *addr,
					 size_t addrlen, int socktype,
					 int protocol)
{
    struct RPC2_addrinfo *ai =
	(struct RPC2_addrinfo *)malloc(sizeof(*ai) + addrlen);

    if (!ai)
	return NULL;
    memset(ai, 0, sizeof(*ai));

    ai->ai_family = addr->sa_family;
    ai->ai_socktype = socktype;
    ai->ai_protocol = protocol;
    ai->ai_addrlen = addrlen;
    ai->ai_addr = (struct sockaddr *)&(ai[1]);
    memcpy(ai->ai_addr, addr, addrlen);
    return ai;
}

struct RPC2_addrinfo *RPC2_copyaddrinfo(const struct RPC2_addrinfo *ai)
{
    struct RPC2_addrinfo *head = NULL, *cur, *prev = NULL;
    const struct RPC2_addrinfo *src = ai;
    
    /* this loop probably a bit uglier than it should be because I'm
     * trying to keep the same order */
    while (src) {
        cur = RPC2_allocaddrinfo(src->ai_addr, src->ai_addrlen,
				 src->ai_socktype, src->ai_protocol);
        if (!cur) {
            RPC2_freeaddrinfo(head);
            head = NULL;
            break;
            /* assert(0 && "Allocation failed in rpc2_copyaddrinfo"); */
        }

        /* copy the guts of the addrinfo structure */
        cur->ai_flags = src->ai_flags;
        if (src->ai_canonname)
            cur->ai_canonname = strdup(src->ai_canonname);
        cur->ai_next = NULL;

        /* link into the previously allocated addrinfo */
        if (prev)
             prev->ai_next = cur;
        else head = cur;

        prev = cur;
        src = src->ai_next;
    }
    return head;
}

int RPC2_cmpaddrinfo(const struct RPC2_addrinfo *node,
		     const struct RPC2_addrinfo *host)
{
    if (!node && !host)
        return 1;

    for(; node != NULL; node = node->ai_next) {
        if (node->ai_family != host->ai_family)
            continue;
        if (node->ai_addrlen != host->ai_addrlen)
            continue;

	switch (node->ai_family) {
	case PF_INET:
	    {
		struct sockaddr_in *sinN, *sinH;
		sinN = (struct sockaddr_in *)node->ai_addr;
		sinH = (struct sockaddr_in *)host->ai_addr;

		if (host->ai_addrlen == sizeof(struct sockaddr_in) &&
		    sinN->sin_port == sinH->sin_port		   &&
		    memcmp(&sinN->sin_addr, &sinH->sin_addr,
			   sizeof(sinN->sin_addr)) == 0)
		    return 1;
		break;
	    }
#if defined(PF_INET6)
	case PF_INET6:
	    {
		struct sockaddr_in6 *sin6N, *sin6H;
		sin6N = (struct sockaddr_in6 *)node->ai_addr;
		sin6H = (struct sockaddr_in6 *)host->ai_addr;

		if (host->ai_addrlen == sizeof(struct sockaddr_in6) &&
		    sin6N->sin6_port == sin6H->sin6_port	    &&
		    memcmp(&sin6N->sin6_addr, &sin6H->sin6_addr,
			   sizeof(sin6N->sin6_addr)) == 0)
		    return 1;
		break;
	    }
#endif
	default:
	    break;
	}
    }
    return 0;
}

void RPC2_formataddrinfo(const struct RPC2_addrinfo *ai,
			 char *buf, size_t buflen)
{
    int n, port = 0;
    void *addr = NULL;
    char *p = buf;

    buflen--;

    if (!ai) {
	strncpy(buf, "(no addrinfo)", buflen);
	buf[buflen] = '\0';
	return;
    }

    switch (ai->ai_family) {
    case PF_INET:
	addr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
	port = ((struct sockaddr_in *)ai->ai_addr)->sin_port;
	break;
#if defined(PF_INET6)
    case PF_INET6:
	addr = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
	port = ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port;
	*(p++) = '[';
	break;
#endif
    }

    /* only use the hostname if it is available and we can comfortably pack
     * it with the port number in the buffer */
    if (ai->ai_canonname && strlen(ai->ai_canonname) < (buflen - 12))
    {
	strncpy(buf, ai->ai_canonname, buflen);
	p = buf;
    }
    else if (!addr || !inet_ntop(ai->ai_family, addr, p, buf + buflen - p))
    {
	strncpy(buf, "(untranslatable)", buflen);
	p = buf;
    }
    buf[buflen] = '\0'; /* just in case inet_ntop didn't \0 terminate */

    n = strlen(buf);
    if (port && n < buflen - 3)
	snprintf(&buf[n], buflen - n,"%s:%u", p != buf ? "]" : "", ntohs(port));
    buf[buflen] = '\0';
}


/* standard getaddrinfo functionality */

int RPC2_getaddrinfo(const char *node, const char *service,
		     const struct RPC2_addrinfo *hints,
		     struct RPC2_addrinfo **res)
{
#ifdef TESTING
    fprintf(stderr, "Doing A record lookup for %s %s\n", node, service);
#endif

#ifdef HAVE_GETADDRINFO
    struct addrinfo *ai = NULL, *head;
    struct RPC2_addrinfo **new, *list = NULL;
    int ret;

    ret = getaddrinfo(node, service, (const struct addrinfo *)hints, &ai);
    /* map getaddrinfo error codes into RPC2_getaddrinfo errorcodes */
    switch (ret) {
    case 0:		 break;
    case EAI_FAMILY:	 return RPC2_EAI_FAMILY;
    case EAI_SOCKTYPE:	 return RPC2_EAI_SOCKTYPE;
    case EAI_BADFLAGS:	 return RPC2_EAI_BADFLAGS;
    case EAI_NONAME:	 return RPC2_EAI_NONAME;
    case EAI_SERVICE:	 return RPC2_EAI_SERVICE;
    case EAI_MEMORY:	 return RPC2_EAI_MEMORY;
    case EAI_FAIL:	 return RPC2_EAI_FAIL;
    case EAI_AGAIN:	 return RPC2_EAI_AGAIN;
    case EAI_SYSTEM:	 return RPC2_EAI_SYSTEM;
    default:		 return RPC2_EAI_FAIL;
    }

    head = ai;
    new = &list;
    while (ai) {
	*new = RPC2_allocaddrinfo(ai->ai_addr, ai->ai_addrlen,
				  ai->ai_socktype, ai->ai_protocol);
	if (!*new) {
	    RPC2_freeaddrinfo(list);
	    list = NULL;
	    break;
	}

	(*new)->ai_flags = ai->ai_flags;
	if (ai->ai_canonname)
	    (*new)->ai_canonname = strdup(ai->ai_canonname);

	new = &(*new)->ai_next;
	ai = ai->ai_next;
    }
    freeaddrinfo(head);

    if (!list)
	return RPC2_EAI_MEMORY;

    *new = *res;
    *res = list;
    return 0;
#else
    struct hostent *he;
    int err, family, i, resolved = 0;
    short port = 0;
    char *end;

    /* map the service name (or string formatted port number) into a usable
     * port number in network byte order */

    if (service) {
	port = htons(strtol(service, &end, 10));
	if (*service == '\0' || *end != '\0') {
	    const char *proto = (hints && hints->ai_protocol == IPPROTO_UDP) ?
		"udp" : "tcp";
	    struct servent *se = getservbyname(service, proto);
	    if (!se)
		return RPC2_EAI_SERVICE;
	    port = se->s_port;
	}
    }

    if (!node || (hints && hints->ai_flags & RPC2_AI_NUMERICHOST))
	return getaddrinfo_noresolve(node, port, hints, res);

#if defined (PF_INET6)
    family = (hints && hints->ai_family == PF_INET) ? PF_INET : PF_INET6;
    he = getipnodebyname(node, family, (family == PF_INET6) ? AI_ALL : 0, &err);
#else
    family = PF_INET;
    he = getipnodebyname(node, family, 0, &err);

#endif

    if (!he) {
	switch (err) {
	case HOST_TRY_AGAIN: return RPC2_EAI_AGAIN;
	case HOST_NOADDRESS:
	case HOST_NOT_FOUND: return RPC2_EAI_NONAME;
	case HOST_NORECOVERY:
	default:	     return RPC2_EAI_FAIL;
	}
    }

    for (i = 0; he->h_addr_list[i]; i++) {
	int err = 
	    addrinfo_init(he->h_addrtype, he->h_addr_list[i], port, hints, res);
	if (err)
	    continue;

	if (hints && hints->ai_flags & RPC2_AI_CANONNAME)
	    (*res)->ai_canonname = strdup(he->h_name);

	resolved++;
    }
    freehostent(he);

    return resolved ? 0 : (i ? RPC2_EAI_MEMORY : RPC2_EAI_NONAME);
#endif
}

void RPC2_freeaddrinfo(struct RPC2_addrinfo *res)
{
    while (res) {
	struct RPC2_addrinfo *ai = res;
	res = res->ai_next;

	if (ai->ai_canonname)
	    free(ai->ai_canonname);
	free(ai);
    }
}

const char *RPC2_gai_strerror(const int errcode)
{
#ifdef HAVE_GAI_STRERROR
    return gai_strerror(errcode);
#else
    const char *str;
    switch (errcode) {
    case RPC2_EAI_FAMILY:     str = "Family not supported"; break;
    case RPC2_EAI_SOCKTYPE:   str = "Socktype not supported"; break;
    case RPC2_EAI_BADFLAGS:   str = "Invalid flags"; break;
    case RPC2_EAI_NONAME:     str = "Node or service not found"; break;
    case RPC2_EAI_SERVICE:    str = "Service not available"; break;
    case RPC2_EAI_MEMORY:     str = "Allocation failure"; break;
    case RPC2_EAI_FAIL:       str = "Permanent lookup failure"; break;
    case RPC2_EAI_AGAIN:      str = "Temporary lookup failure"; break;
    case RPC2_EAI_SYSTEM:     str = "System error"; break;
    default:		      str = "Unknown failure"; break;
    }
    return str;
#endif
}


/* rpc2 specific helpers */

void rpc2_printaddrinfo(const struct RPC2_addrinfo *ai, FILE *f)
{
    char buf[RPC2_ADDRSTRLEN];
    RPC2_formataddrinfo(ai, buf, RPC2_ADDRSTRLEN);
    fputs("Addrinfo = ", f);
    fputs(buf, f);
}

struct RPC2_addrinfo *rpc2_resolve(RPC2_HostIdent *Host, RPC2_PortIdent *Port)
{
    struct RPC2_addrinfo hint, *result = NULL;
    char buf[11];
    char *node = NULL, *service = NULL;
    int retval;

    if (Host) {
        /* We can easily convert a numeric ip-address to an addrinfo
         * structure without having to resolve the whole thing. */
        if (Host->Tag == RPC2_HOSTBYINETADDR) {
            RPC2_HostIdent host = *Host;
            rpc2_simplifyHost(&host, Port);
            return host.Value.AddrInfo;
        }

        if (Host->Tag == RPC2_HOSTBYADDRINFO)
            return RPC2_copyaddrinfo(Host->Value.AddrInfo);
    }

    /* Here we know that Host is either NULL, or RPC2_HOSTBYNAME
     * (or RPC2_DUMMYHOST) and we'll have to use the resolver */

    if (Port) {
        switch (Port->Tag) {
        case RPC2_PORTBYNAME:
            service = Port->Value.Name;
            break;

        case RPC2_PORTBYINETNUMBER:
            snprintf(buf, 11, "%u", ntohs(Port->Value.InetPortNumber));
            service = buf;
            break;

	default:
	    break;
        }
    }

    /* Resolve host */
    memset(&hint, 0, sizeof(struct RPC2_addrinfo));
    hint.ai_family = rpc2_ipv6ready ? PF_UNSPEC : PF_INET;
    hint.ai_socktype = SOCK_DGRAM;

    if (Host && Host->Tag == RPC2_HOSTBYNAME)
        node = Host->Value.Name;
    else /* no host specified, we must be resolving for a listener */
        hint.ai_flags = RPC2_AI_PASSIVE;

    /* getaddrinfo doesn't allow both node and service to be NULL */
    if (!node && !service)
        service = "0";

    retval = RPC2_getaddrinfo(node, service, &hint, &result);
    if (retval) {
        say(0, RPC2_DebugLevel, "rpc2_resolve: getaddrinfo failed\n");
        return NULL;
    }
    return result;
}

void rpc2_splitaddrinfo(RPC2_HostIdent *Host, RPC2_PortIdent *Port,
			const struct RPC2_addrinfo *ai)
{
    if (Host) {
        if (rpc2_ipv6ready) {
            Host->Tag = RPC2_HOSTBYADDRINFO;
            Host->Value.AddrInfo = RPC2_copyaddrinfo(ai);
        } else {
            assert(ai->ai_family == PF_INET);
            Host->Tag = RPC2_HOSTBYINETADDR;
            Host->Value.InetAddress = 
                ((struct sockaddr_in *)ai->ai_addr)->sin_addr;
        }
    }
    if (Port) {
        Port->Tag = RPC2_PORTBYINETNUMBER;
        switch (ai->ai_family) {
        case PF_INET:
            Port->Value.InetPortNumber =
                ((struct sockaddr_in *)ai->ai_addr)->sin_port;
            break;
#if defined(PF_INET6)
        case PF_INET6:
            Port->Value.InetPortNumber =
                ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port;
            break;
#endif
        default:
            Port->Tag = RPC2_DUMMYPORT;
        }
    }
}

void rpc2_simplifyHost(RPC2_HostIdent *Host, RPC2_PortIdent *Port)
{
    struct sockaddr_in sin;
    struct RPC2_addrinfo *ai;

    if (Host->Tag == RPC2_HOSTBYADDRINFO)
        return;

    assert(Host->Tag == RPC2_HOSTBYINETADDR);
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = PF_INET;
    sin.sin_addr = Host->Value.InetAddress;

    if (Port) {
        struct servent *se;

        switch(Port->Tag) {
        case RPC2_PORTBYINETNUMBER:
            sin.sin_port = Port->Value.InetPortNumber;
            break;

        case RPC2_PORTBYNAME:
            se = getservbyname(Port->Value.Name, "udp");
            if (se)
                sin.sin_port = se->s_port;
            break;

	default:
	    break;
        }
    }

    ai = RPC2_allocaddrinfo((struct sockaddr *)&sin, sizeof(sin),
			    SOCK_DGRAM, IPPROTO_UDP);
    assert(ai != NULL);

    Host->Tag = RPC2_HOSTBYADDRINFO;
    Host->Value.AddrInfo = ai;
}