2 * Mar 8, 2000 by Hajimu UMEMOTO <ume@mahoroba.org>
3 * $Id: getaddrinfo.c,v 1.2 2003/02/13 19:56:07 rjs3 Exp $
5 * This module is besed 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.
61 #include "saslauthd.h"
63 #include <sys/param.h>
64 #include <arpa/inet.h>
68 static struct addrinfo *
69 malloc_ai(int port, u_long addr, int socktype, int proto)
73 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) +
74 sizeof(struct sockaddr_in));
76 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
77 ai->ai_addr = (struct sockaddr *)(ai + 1);
78 /* XXX -- ssh doesn't use sa_len */
79 ai->ai_addrlen = sizeof(struct sockaddr_in);
80 #ifdef HAVE_SOCKADDR_SA_LEN
81 ai->ai_addr->sa_len = sizeof(struct sockaddr_in);
83 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
84 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
85 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
86 ai->ai_socktype = socktype;
87 ai->ai_protocol = proto;
95 gai_strerror(int ecode)
99 return "no address associated with hostname.";
101 return "memory allocation failure.";
103 return "ai_family not supported.";
105 return "servname not supported for ai_socktype.";
107 return "unknown error.";
112 freeaddrinfo(struct addrinfo *ai)
114 struct addrinfo *next;
116 if (ai->ai_canonname)
117 free(ai->ai_canonname);
121 } while ((ai = next) != NULL);
125 getaddrinfo(const char *hostname, const char *servname,
126 const struct addrinfo *hints, struct addrinfo **res)
128 struct addrinfo *cur, *prev = NULL;
131 int i, port = 0, socktype, proto;
133 if (hints && hints->ai_family != PF_INET && hints->ai_family != PF_UNSPEC)
136 socktype = (hints && hints->ai_socktype) ? hints->ai_socktype
138 if (hints && hints->ai_protocol)
139 proto = hints->ai_protocol;
154 if (isdigit((int)*servname))
155 port = htons(atoi(servname));
171 if ((se = getservbyname(servname, pe_proto)) == NULL)
177 if (hints && hints->ai_flags & AI_PASSIVE)
178 *res = malloc_ai(port, htonl(0x00000000), socktype, proto);
180 *res = malloc_ai(port, htonl(0x7f000001), socktype, proto);
186 if (inet_aton(hostname, &in)) {
187 *res = malloc_ai(port, in.s_addr, socktype, proto);
193 if (hints && hints->ai_flags & AI_NUMERICHOST)
196 if ((hp = gethostbyname(hostname)) &&
197 hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
198 for (i = 0; hp->h_addr_list[i]; i++) {
199 if ((cur = malloc_ai(port,
200 ((struct in_addr *)hp->h_addr_list[i])->s_addr,
201 socktype, proto)) == NULL) {
212 if (hints && hints->ai_flags & AI_CANONNAME && *res) {
213 /* NOT sasl_strdup for compatibility */
214 if (((*res)->ai_canonname = strdup(hp->h_name)) == NULL) {