]> andersk Git - openssh.git/blame - openbsd-compat/fake-rfc2553.c
- deraadt@cvs.openbsd.org 2006/03/19 18:51:18
[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"
39
52d58495 40#ifndef HAVE_GETNAMEINFO
41int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
42 size_t hostlen, char *serv, size_t servlen, int flags)
43{
44 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
45 struct hostent *hp;
46 char tmpserv[16];
47
48 if (serv != NULL) {
49 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
50 if (strlcpy(serv, tmpserv, servlen) >= servlen)
51 return (EAI_MEMORY);
52 }
53
54 if (host != NULL) {
55 if (flags & NI_NUMERICHOST) {
56 if (strlcpy(host, inet_ntoa(sin->sin_addr),
57 hostlen) >= hostlen)
58 return (EAI_MEMORY);
59 else
60 return (0);
61 } else {
62 hp = gethostbyaddr((char *)&sin->sin_addr,
63 sizeof(struct in_addr), AF_INET);
64 if (hp == NULL)
65 return (EAI_NODATA);
66
67 if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
68 return (EAI_MEMORY);
69 else
70 return (0);
71 }
72 }
73 return (0);
74}
75#endif /* !HAVE_GETNAMEINFO */
76
48e671d5 77#ifndef HAVE_GAI_STRERROR
1a086f97 78#ifdef HAVE_CONST_GAI_STRERROR_PROTO
79const char *
80#else
9901cb37 81char *
1a086f97 82#endif
9901cb37 83gai_strerror(int err)
48e671d5 84{
9901cb37 85 switch (err) {
86 case EAI_NODATA:
87 return ("no address associated with name");
88 case EAI_MEMORY:
89 return ("memory allocation failure.");
02e2a074 90 case EAI_NONAME:
91 return ("nodename nor servname provided, or not known");
9901cb37 92 default:
93 return ("unknown/invalid error.");
5daf7064 94 }
48e671d5 95}
96#endif /* !HAVE_GAI_STRERROR */
97
98#ifndef HAVE_FREEADDRINFO
9901cb37 99void
100freeaddrinfo(struct addrinfo *ai)
48e671d5 101{
5daf7064 102 struct addrinfo *next;
103
0509a052 104 for(; ai != NULL;) {
105 next = ai->ai_next;
5daf7064 106 free(ai);
9901cb37 107 ai = next;
108 }
48e671d5 109}
110#endif /* !HAVE_FREEADDRINFO */
111
112#ifndef HAVE_GETADDRINFO
9901cb37 113static struct
114addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
48e671d5 115{
5daf7064 116 struct addrinfo *ai;
117
d6bd2b5a 118 ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
119 if (ai == NULL)
120 return (NULL);
5daf7064 121
9901cb37 122 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
5daf7064 123
124 ai->ai_addr = (struct sockaddr *)(ai + 1);
125 /* XXX -- ssh doesn't use sa_len */
126 ai->ai_addrlen = sizeof(struct sockaddr_in);
127 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
48e671d5 128
5daf7064 129 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
130 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
131
b1848832 132 /* XXX: the following is not generally correct, but does what we want */
133 if (hints->ai_socktype)
134 ai->ai_socktype = hints->ai_socktype;
135 else
136 ai->ai_socktype = SOCK_STREAM;
137
138 if (hints->ai_protocol)
139 ai->ai_protocol = hints->ai_protocol;
140
9901cb37 141 return (ai);
48e671d5 142}
143
9901cb37 144int
145getaddrinfo(const char *hostname, const char *servname,
146 const struct addrinfo *hints, struct addrinfo **res)
48e671d5 147{
5daf7064 148 struct hostent *hp;
ecc03386 149 struct servent *sp;
9c13d877 150 struct in_addr in;
ecc03386 151 int i;
152 long int port;
62086365 153 u_long addr;
5daf7064 154
ecc03386 155 port = 0;
156 if (servname != NULL) {
157 char *cp;
158
159 port = strtol(servname, &cp, 10);
160 if (port > 0 && port <= 65535 && *cp == '\0')
161 port = htons(port);
162 else if ((sp = getservbyname(servname, NULL)) != NULL)
163 port = sp->s_port;
164 else
165 port = 0;
166 }
5daf7064 167
168 if (hints && hints->ai_flags & AI_PASSIVE) {
62086365 169 addr = htonl(0x00000000);
170 if (hostname && inet_aton(hostname, &in) != 0)
171 addr = in.s_addr;
9901cb37 172 *res = malloc_ai(port, addr, hints);
d6bd2b5a 173 if (*res == NULL)
174 return (EAI_MEMORY);
9901cb37 175 return (0);
5daf7064 176 }
177
178 if (!hostname) {
9901cb37 179 *res = malloc_ai(port, htonl(0x7f000001), hints);
d6bd2b5a 180 if (*res == NULL)
181 return (EAI_MEMORY);
9901cb37 182 return (0);
5daf7064 183 }
184
dc2a6d09 185 if (inet_aton(hostname, &in)) {
9901cb37 186 *res = malloc_ai(port, in.s_addr, hints);
d6bd2b5a 187 if (*res == NULL)
188 return (EAI_MEMORY);
9901cb37 189 return (0);
5daf7064 190 }
191
02e2a074 192 /* Don't try DNS if AI_NUMERICHOST is set */
193 if (hints && hints->ai_flags & AI_NUMERICHOST)
194 return (EAI_NONAME);
195
5daf7064 196 hp = gethostbyname(hostname);
197 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
9901cb37 198 struct addrinfo *cur, *prev;
199
d6bd2b5a 200 cur = prev = *res = NULL;
5daf7064 201 for (i = 0; hp->h_addr_list[i]; i++) {
9901cb37 202 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
203
204 cur = malloc_ai(port, in->s_addr, hints);
d6bd2b5a 205 if (cur == NULL) {
206 if (*res != NULL)
207 freeaddrinfo(*res);
208 return (EAI_MEMORY);
209 }
5daf7064 210 if (prev)
211 prev->ai_next = cur;
212 else
213 *res = cur;
214
215 prev = cur;
216 }
9901cb37 217 return (0);
5daf7064 218 }
219
9901cb37 220 return (EAI_NODATA);
48e671d5 221}
222#endif /* !HAVE_GETADDRINFO */
This page took 0.362514 seconds and 5 git commands to generate.