2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
5 * lib/krb5/os/hostaddr.c
7 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
10 * Export of this software from the United States of America may
11 * require a specific license from the United States Government.
12 * It is the responsibility of any person or organization contemplating
13 * export to obtain such a license before exporting.
15 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
16 * distribute this software and its documentation for any purpose and
17 * without fee is hereby granted, provided that the above copyright
18 * notice appear in all copies and that both that copyright notice and
19 * this permission notice appear in supporting documentation, and that
20 * the name of M.I.T. not be used in advertising or publicity pertaining
21 * to distribution of the software without specific, written prior
22 * permission. Furthermore if you modify this software you must label
23 * your software as modified software and not distribute it in such a
24 * fashion that it might be confused with the original M.I.T. software.
25 * M.I.T. makes no representations about the suitability of
26 * this software for any purpose. It is provided "as is" without express
27 * or implied warranty.
29 * This routine returns a list of krb5 addresses given a hostname.
35 #include "fake-addrinfo.h"
38 krb5_os_hostaddr(krb5_context context
, const char *name
, krb5_address
***ret_addrs
)
40 krb5_error_code retval
;
43 struct addrinfo hints
, *ai
, *aip
;
46 return KRB5_ERR_BAD_HOSTNAME
;
49 memset (&hints
, 0, sizeof (hints
));
50 hints
.ai_flags
= AI_NUMERICHOST
;
51 /* We don't care what kind at this point, really, but without
52 this, we can get back multiple sockaddrs per address, for
53 SOCK_DGRAM, SOCK_STREAM, and SOCK_RAW. I haven't checked if
54 that's what the spec indicates. */
55 hints
.ai_socktype
= SOCK_DGRAM
;
57 r
= getaddrinfo (name
, 0, &hints
, &ai
);
58 if (r
&& AI_NUMERICHOST
!= 0) {
59 hints
.ai_flags
&= ~AI_NUMERICHOST
;
60 r
= getaddrinfo (name
, 0, &hints
, &ai
);
63 krb5_set_error_message(context
, KRB5_ERR_BAD_HOSTNAME
,
65 "Hostname cannot be canonicalized for '%s': %s"),
67 return KRB5_ERR_BAD_HOSTNAME
;
70 for (i
= 0, aip
= ai
; aip
; aip
= aip
->ai_next
) {
71 switch (aip
->ai_addr
->sa_family
) {
78 /* Ignore addresses of unknown families. */
83 addrs
= malloc ((i
+1) * sizeof(*addrs
));
87 for (j
= 0; j
< i
+ 1; j
++)
90 for (i
= 0, aip
= ai
; aip
; aip
= aip
->ai_next
) {
95 switch (aip
->ai_addr
->sa_family
) {
97 addrlen
= sizeof (struct in_addr
);
99 ptr
= &sa2sin(aip
->ai_addr
)->sin_addr
;
100 atype
= ADDRTYPE_INET
;
102 #ifdef KRB5_USE_INET6
104 addrlen
= sizeof (struct in6_addr
);
106 ptr
= &sa2sin6(aip
->ai_addr
)->sin6_addr
;
107 atype
= ADDRTYPE_INET6
;
113 addrs
[i
] = (krb5_address
*) malloc(sizeof(krb5_address
));
118 addrs
[i
]->magic
= KV5M_ADDRESS
;
119 addrs
[i
]->addrtype
= atype
;
120 addrs
[i
]->length
= addrlen
;
121 addrs
[i
]->contents
= malloc(addrs
[i
]->length
);
122 if (!addrs
[i
]->contents
) {
126 memcpy (addrs
[i
]->contents
, ptr
, addrs
[i
]->length
);
136 /* Solaris Kerberos */
138 krb5_free_addresses(context
, addrs
);