2 * Mar 8, 2000 by Hajimu UMEMOTO <ume@mahoroba.org>
3 * $Id: getaddrinfo.c,v 1.8 2003/03/19 18:25:28 rjs3 Exp $
5 * This module is based on ssh-1.2.27-IPv6-1.5 written by
6 * KIKUCHI Takahiro <kick@kyoto.wide.ad.jp>
9 * Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
23 * 3. The name "Carnegie Mellon University" must not be used to
24 * endorse or promote products derived from this software without
25 * prior written permission. For permission or any other legal
26 * details, please contact
27 * Office of Technology Transfer
28 * Carnegie Mellon University
30 * Pittsburgh, PA 15213-3890
31 * (412) 268-4387, fax: (412) 268-7395
32 * tech-transfer@andrew.cmu.edu
34 * 4. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by Computing Services
37 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
39 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
40 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
41 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
42 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
44 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
45 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 * fake library for ssh
50 * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
51 * These funtions are defined in rfc2133.
53 * But these functions are not implemented correctly. The minimum subset
54 * is implemented for ssh use only. For exapmle, this routine assumes
55 * that ai_family is AF_INET. Don't use it for another purpose.
57 * In the case not using 'configure --enable-ipv6', this getaddrinfo.c
58 * will be used if you have broken getaddrinfo or no getaddrinfo.
63 #include <sys/param.h>
65 # include <arpa/inet.h>
66 # endif /* macintosh */
\r
71 /* : Windows socket library is missing inet_aton, emulate it with
72 : inet_addr. inet_aton return 0 if the address is uncorrect, a non zero
75 inet_aton (const char *cp, struct in_addr *inp)
77 if (cp == NULL || inp == NULL) {
81 /* : handle special case */
82 if (strcmp (cp, "255.255.255.255") == 0) {
83 inp->s_addr = (unsigned int) 0xFFFFFFFF;
87 inp->s_addr = inet_addr (cp);
92 static struct addrinfo *
93 malloc_ai(int port, unsigned long addr, int socktype, int proto)
97 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) +
98 sizeof(struct sockaddr_in));
100 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
101 ai->ai_addr = (struct sockaddr *)(ai + 1);
102 /* XXX -- ssh doesn't use sa_len */
103 ai->ai_addrlen = sizeof(struct sockaddr_in);
104 #ifdef HAVE_SOCKADDR_SA_LEN
105 ai->ai_addr->sa_len = sizeof(struct sockaddr_in);
107 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
108 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
109 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
110 ai->ai_socktype = socktype;
111 ai->ai_protocol = proto;
119 gai_strerror(int ecode)
123 return "no address associated with hostname.";
125 return "memory allocation failure.";
127 return "ai_family not supported.";
129 return "servname not supported for ai_socktype.";
131 return "unknown error.";
136 freeaddrinfo(struct addrinfo *ai)
138 struct addrinfo *next;
140 if (ai->ai_canonname)
141 free(ai->ai_canonname);
145 } while ((ai = next) != NULL);
149 getaddrinfo(const char *hostname, const char *servname,
150 const struct addrinfo *hints, struct addrinfo **res)
152 struct addrinfo *cur, *prev = NULL;
155 int i, port = 0, socktype, proto;
157 if (hints && hints->ai_family != PF_INET && hints->ai_family != PF_UNSPEC)
160 socktype = (hints && hints->ai_socktype) ? hints->ai_socktype
162 if (hints && hints->ai_protocol)
163 proto = hints->ai_protocol;
178 if (isdigit((int)*servname))
179 port = htons((short) atoi(servname));
195 /* xxx thread safety ? */
196 if ((se = getservbyname(servname, pe_proto)) == NULL)
202 if (hints && hints->ai_flags & AI_PASSIVE)
203 *res = malloc_ai(port, htonl(0x00000000), socktype, proto);
205 *res = malloc_ai(port, htonl(0x7f000001), socktype, proto);
212 if (inet_aton(hostname, &in))
214 if ((in.s_addr = inet_addr(hostname)) != -1)
217 *res = malloc_ai(port, in.s_addr, socktype, proto);
223 if (hints && hints->ai_flags & AI_NUMERICHOST)
226 /* xxx thread safety? / gethostbyname_r */
227 if ((hp = gethostbyname(hostname)) &&
228 hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
229 for (i = 0; hp->h_addr_list[i]; i++) {
230 if ((cur = malloc_ai(port,
231 ((struct in_addr *)hp->h_addr_list[i])->s_addr,
232 socktype, proto)) == NULL) {
243 if (hints && hints->ai_flags & AI_CANONNAME && *res) {
244 /* NOT sasl_strdup for compatibility */
245 if (((*res)->ai_canonname = strdup(hp->h_name)) == NULL) {