]> andersk Git - openssh.git/blame - openbsd-compat/fake-rfc2553.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / openbsd-compat / fake-rfc2553.c
CommitLineData
f5827134 1/*
2 * Copyright (C) 2000-2003 Damien Miller. All rights reserved.
3 * Copyright (C) 1999 WIDE Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
48e671d5 30/*
52d58495 31 * Pseudo-implementation of RFC2553 name / address resolution functions
48e671d5 32 *
33 * But these functions are not implemented correctly. The minimum subset
5e24e4a5 34 * is implemented for ssh use only. For example, this routine assumes
48e671d5 35 * that ai_family is AF_INET. Don't use it for another purpose.
48e671d5 36 */
37
4ed465ec 38#include "includes.h"
f8688ddd 39
40#include <stdlib.h>
e204f3ee 41#include <string.h>
4ed465ec 42
e3220bb2 43#include <netinet/in.h>
44#include <arpa/inet.h>
45
52d58495 46#ifndef HAVE_GETNAMEINFO
47int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
48 size_t hostlen, char *serv, size_t servlen, int flags)
49{
50 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
51 struct hostent *hp;
52 char tmpserv[16];
53
2ade01eb 54 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
55 return (EAI_FAMILY);
52d58495 56 if (serv != NULL) {
57 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
58 if (strlcpy(serv, tmpserv, servlen) >= servlen)
59 return (EAI_MEMORY);
60 }
61
62 if (host != NULL) {
63 if (flags & NI_NUMERICHOST) {
64 if (strlcpy(host, inet_ntoa(sin->sin_addr),
65 hostlen) >= hostlen)
66 return (EAI_MEMORY);
67 else
68 return (0);
69 } else {
70 hp = gethostbyaddr((char *)&sin->sin_addr,
71 sizeof(struct in_addr), AF_INET);
72 if (hp == NULL)
73 return (EAI_NODATA);
74
75 if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
76 return (EAI_MEMORY);
77 else
78 return (0);
79 }
80 }
81 return (0);
82}
83#endif /* !HAVE_GETNAMEINFO */
84
48e671d5 85#ifndef HAVE_GAI_STRERROR
1a086f97 86#ifdef HAVE_CONST_GAI_STRERROR_PROTO
87const char *
88#else
9901cb37 89char *
1a086f97 90#endif
9901cb37 91gai_strerror(int err)
48e671d5 92{
9901cb37 93 switch (err) {
94 case EAI_NODATA:
95 return ("no address associated with name");
96 case EAI_MEMORY:
97 return ("memory allocation failure.");
02e2a074 98 case EAI_NONAME:
99 return ("nodename nor servname provided, or not known");
2ade01eb 100 case EAI_FAMILY:
101 return ("ai_family not supported");
9901cb37 102 default:
103 return ("unknown/invalid error.");
5daf7064 104 }
48e671d5 105}
106#endif /* !HAVE_GAI_STRERROR */
107
108#ifndef HAVE_FREEADDRINFO
9901cb37 109void
110freeaddrinfo(struct addrinfo *ai)
48e671d5 111{
5daf7064 112 struct addrinfo *next;
113
0509a052 114 for(; ai != NULL;) {
115 next = ai->ai_next;
5daf7064 116 free(ai);
9901cb37 117 ai = next;
118 }
48e671d5 119}
120#endif /* !HAVE_FREEADDRINFO */
121
122#ifndef HAVE_GETADDRINFO
9901cb37 123static struct
124addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
48e671d5 125{
5daf7064 126 struct addrinfo *ai;
127
d6bd2b5a 128 ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
129 if (ai == NULL)
130 return (NULL);
5daf7064 131
9901cb37 132 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
5daf7064 133
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;
48e671d5 138
5daf7064 139 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
140 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
141
b1848832 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;
145 else
146 ai->ai_socktype = SOCK_STREAM;
147
148 if (hints->ai_protocol)
149 ai->ai_protocol = hints->ai_protocol;
150
9901cb37 151 return (ai);
48e671d5 152}
153
9901cb37 154int
155getaddrinfo(const char *hostname, const char *servname,
156 const struct addrinfo *hints, struct addrinfo **res)
48e671d5 157{
5daf7064 158 struct hostent *hp;
ecc03386 159 struct servent *sp;
9c13d877 160 struct in_addr in;
ecc03386 161 int i;
162 long int port;
62086365 163 u_long addr;
5daf7064 164
ecc03386 165 port = 0;
2ade01eb 166 if (hints && hints->ai_family != AF_UNSPEC &&
167 hints->ai_family != AF_INET)
168 return (EAI_FAMILY);
ecc03386 169 if (servname != NULL) {
170 char *cp;
171
172 port = strtol(servname, &cp, 10);
173 if (port > 0 && port <= 65535 && *cp == '\0')
174 port = htons(port);
175 else if ((sp = getservbyname(servname, NULL)) != NULL)
176 port = sp->s_port;
177 else
178 port = 0;
179 }
5daf7064 180
181 if (hints && hints->ai_flags & AI_PASSIVE) {
62086365 182 addr = htonl(0x00000000);
183 if (hostname && inet_aton(hostname, &in) != 0)
184 addr = in.s_addr;
9901cb37 185 *res = malloc_ai(port, addr, hints);
d6bd2b5a 186 if (*res == NULL)
187 return (EAI_MEMORY);
9901cb37 188 return (0);
5daf7064 189 }
190
191 if (!hostname) {
9901cb37 192 *res = malloc_ai(port, htonl(0x7f000001), hints);
d6bd2b5a 193 if (*res == NULL)
194 return (EAI_MEMORY);
9901cb37 195 return (0);
5daf7064 196 }
197
dc2a6d09 198 if (inet_aton(hostname, &in)) {
9901cb37 199 *res = malloc_ai(port, in.s_addr, hints);
d6bd2b5a 200 if (*res == NULL)
201 return (EAI_MEMORY);
9901cb37 202 return (0);
5daf7064 203 }
204
02e2a074 205 /* Don't try DNS if AI_NUMERICHOST is set */
206 if (hints && hints->ai_flags & AI_NUMERICHOST)
207 return (EAI_NONAME);
208
5daf7064 209 hp = gethostbyname(hostname);
210 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
9901cb37 211 struct addrinfo *cur, *prev;
212
d6bd2b5a 213 cur = prev = *res = NULL;
5daf7064 214 for (i = 0; hp->h_addr_list[i]; i++) {
9901cb37 215 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
216
217 cur = malloc_ai(port, in->s_addr, hints);
d6bd2b5a 218 if (cur == NULL) {
219 if (*res != NULL)
220 freeaddrinfo(*res);
221 return (EAI_MEMORY);
222 }
5daf7064 223 if (prev)
224 prev->ai_next = cur;
225 else
226 *res = cur;
227
228 prev = cur;
229 }
9901cb37 230 return (0);
5daf7064 231 }
232
9901cb37 233 return (EAI_NODATA);
48e671d5 234}
235#endif /* !HAVE_GETADDRINFO */
This page took 4.683428 seconds and 5 git commands to generate.