xref: /freebsd/crypto/krb5/src/lib/krb5/os/hostaddr.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/hostaddr.c - Return list of krb5 addresses for a hostname */
3 /*
4  * Copyright 1990,1991,2008 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 #include "k5-int.h"
28 #include "os-proto.h"
29 
30 #include "fake-addrinfo.h"
31 
32 krb5_error_code
k5_os_hostaddr(krb5_context context,const char * name,krb5_address *** ret_addrs)33 k5_os_hostaddr(krb5_context context, const char *name,
34                krb5_address ***ret_addrs)
35 {
36     krb5_error_code     retval;
37     krb5_address        **addrs = NULL;
38     size_t              i, j;
39     int                 r;
40     struct addrinfo hints, *ai = NULL, *aip;
41 
42     if (!name)
43         return KRB5_ERR_BAD_HOSTNAME;
44 
45     memset (&hints, 0, sizeof (hints));
46     hints.ai_flags = AI_NUMERICHOST | AI_ADDRCONFIG;
47     /* We don't care what kind at this point, really, but without
48        this, we can get back multiple sockaddrs per address, for
49        SOCK_DGRAM, SOCK_STREAM, and SOCK_RAW.  I haven't checked if
50        that's what the spec indicates.  */
51     hints.ai_socktype = SOCK_DGRAM;
52 
53     r = getaddrinfo (name, 0, &hints, &ai);
54     if (r && AI_NUMERICHOST != 0) {
55         hints.ai_flags &= ~AI_NUMERICHOST;
56         r = getaddrinfo (name, 0, &hints, &ai);
57     }
58     if (r)
59         return KRB5_ERR_BAD_HOSTNAME;
60 
61     for (i = 0, aip = ai; aip; aip = aip->ai_next) {
62         switch (aip->ai_addr->sa_family) {
63         case AF_INET:
64         case AF_INET6:
65             i++;
66         default:
67             /* Ignore addresses of unknown families.  */
68             ;
69         }
70     }
71 
72     addrs = k5calloc(i + 1, sizeof(*addrs), &retval);
73     if (addrs == NULL)
74         goto errout;
75 
76     for (j = 0; j < i + 1; j++)
77         addrs[j] = 0;
78 
79     for (i = 0, aip = ai; aip; aip = aip->ai_next) {
80         const void *ptr;
81         size_t addrlen;
82         int atype;
83 
84         switch (aip->ai_addr->sa_family) {
85         case AF_INET:
86             addrlen = sizeof (struct in_addr);
87             ptr = &sa2sin(aip->ai_addr)->sin_addr;
88             atype = ADDRTYPE_INET;
89             break;
90         case AF_INET6:
91             addrlen = sizeof (struct in6_addr);
92             ptr = &sa2sin6(aip->ai_addr)->sin6_addr;
93             atype = ADDRTYPE_INET6;
94             break;
95         default:
96             continue;
97         }
98         addrs[i] = (krb5_address *) malloc(sizeof(krb5_address));
99         if (!addrs[i]) {
100             retval = ENOMEM;
101             goto errout;
102         }
103         addrs[i]->magic = KV5M_ADDRESS;
104         addrs[i]->addrtype = atype;
105         addrs[i]->length = addrlen;
106         addrs[i]->contents = k5memdup(ptr, addrlen, &retval);
107         if (addrs[i]->contents == NULL)
108             goto errout;
109         i++;
110     }
111 
112     *ret_addrs = addrs;
113     if (ai)
114         freeaddrinfo(ai);
115     return 0;
116 
117 errout:
118     if (addrs) {
119         for (i = 0; addrs[i]; i++) {
120             free (addrs[i]->contents);
121             free (addrs[i]);
122         }
123         krb5_free_addresses(context, addrs);
124     }
125     if (ai)
126         freeaddrinfo(ai);
127     return retval;
128 
129 }
130