2 * Copyright (C) 2000-2003 Damien Miller. All rights reserved.
3 * Copyright (C) 1999 WIDE Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Pseudo-implementation of RFC2553 name / address resolution functions
33 * But these functions are not implemented correctly. The minimum subset
34 * is implemented for ssh use only. For example, this routine assumes
35 * that ai_family is AF_INET. Don't use it for another purpose.
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #ifndef HAVE_GETNAMEINFO
47 int getnameinfo(const struct sockaddr
*sa
, size_t salen
, char *host
,
48 size_t hostlen
, char *serv
, size_t servlen
, int flags
)
50 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
54 if (sa
->sa_family
!= AF_UNSPEC
&& sa
->sa_family
!= AF_INET
)
57 snprintf(tmpserv
, sizeof(tmpserv
), "%d", ntohs(sin
->sin_port
));
58 if (strlcpy(serv
, tmpserv
, servlen
) >= servlen
)
63 if (flags
& NI_NUMERICHOST
) {
64 if (strlcpy(host
, inet_ntoa(sin
->sin_addr
),
70 hp
= gethostbyaddr((char *)&sin
->sin_addr
,
71 sizeof(struct in_addr
), AF_INET
);
75 if (strlcpy(host
, hp
->h_name
, hostlen
) >= hostlen
)
83 #endif /* !HAVE_GETNAMEINFO */
85 #ifndef HAVE_GAI_STRERROR
86 #ifdef HAVE_CONST_GAI_STRERROR_PROTO
95 return ("no address associated with name");
97 return ("memory allocation failure.");
99 return ("nodename nor servname provided, or not known");
101 return ("ai_family not supported");
103 return ("unknown/invalid error.");
106 #endif /* !HAVE_GAI_STRERROR */
108 #ifndef HAVE_FREEADDRINFO
110 freeaddrinfo(struct addrinfo
*ai
)
112 struct addrinfo
*next
;
120 #endif /* !HAVE_FREEADDRINFO */
122 #ifndef HAVE_GETADDRINFO
124 addrinfo
*malloc_ai(int port
, u_long addr
, const struct addrinfo
*hints
)
128 ai
= malloc(sizeof(*ai
) + sizeof(struct sockaddr_in
));
132 memset(ai
, '\0', sizeof(*ai
) + sizeof(struct sockaddr_in
));
134 ai
->ai_addr
= (struct sockaddr
*)(ai
+ 1);
135 /* XXX -- ssh doesn't use sa_len */
136 ai
->ai_addrlen
= sizeof(struct sockaddr_in
);
137 ai
->ai_addr
->sa_family
= ai
->ai_family
= AF_INET
;
139 ((struct sockaddr_in
*)(ai
)->ai_addr
)->sin_port
= port
;
140 ((struct sockaddr_in
*)(ai
)->ai_addr
)->sin_addr
.s_addr
= addr
;
142 /* XXX: the following is not generally correct, but does what we want */
143 if (hints
->ai_socktype
)
144 ai
->ai_socktype
= hints
->ai_socktype
;
146 ai
->ai_socktype
= SOCK_STREAM
;
148 if (hints
->ai_protocol
)
149 ai
->ai_protocol
= hints
->ai_protocol
;
155 getaddrinfo(const char *hostname
, const char *servname
,
156 const struct addrinfo
*hints
, struct addrinfo
**res
)
166 if (hints
&& hints
->ai_family
!= AF_UNSPEC
&&
167 hints
->ai_family
!= AF_INET
)
169 if (servname
!= NULL
) {
172 port
= strtol(servname
, &cp
, 10);
173 if (port
> 0 && port
<= 65535 && *cp
== '\0')
175 else if ((sp
= getservbyname(servname
, NULL
)) != NULL
)
181 if (hints
&& hints
->ai_flags
& AI_PASSIVE
) {
182 addr
= htonl(0x00000000);
183 if (hostname
&& inet_aton(hostname
, &in
) != 0)
185 *res
= malloc_ai(port
, addr
, hints
);
192 *res
= malloc_ai(port
, htonl(0x7f000001), hints
);
198 if (inet_aton(hostname
, &in
)) {
199 *res
= malloc_ai(port
, in
.s_addr
, hints
);
205 /* Don't try DNS if AI_NUMERICHOST is set */
206 if (hints
&& hints
->ai_flags
& AI_NUMERICHOST
)
209 hp
= gethostbyname(hostname
);
210 if (hp
&& hp
->h_name
&& hp
->h_name
[0] && hp
->h_addr_list
[0]) {
211 struct addrinfo
*cur
, *prev
;
213 cur
= prev
= *res
= NULL
;
214 for (i
= 0; hp
->h_addr_list
[i
]; i
++) {
215 struct in_addr
*in
= (struct in_addr
*)hp
->h_addr_list
[i
];
217 cur
= malloc_ai(port
, in
->s_addr
, hints
);
235 #endif /* !HAVE_GETADDRINFO */