Debian Codegraph @

compat/fake-rfc2553.c

raw
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
30 /*
31 * Pseudo-implementation of RFC2553 name / address resolution functions
32 *
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.
36 */
37 #include "tclInt.h"
38
39 TCL_DECLARE_MUTEX(netdbMutex)
40
41 #ifndef HAVE_GETNAMEINFO
42 #ifndef HAVE_STRLCPY
43 static size_t
44 strlcpy(char *dst, const char *src, size_t siz)
45 {
46 char *d = dst;
47 const char *s = src;
48 size_t n = siz;
49
50 /* Copy as many bytes as will fit */
51 if (n != 0 && --n != 0) {
52 do {
53 if ((*d++ = *s++) == 0)
54 break;
55 } while (--n != 0);
56 }
57
58 /* Not enough room in dst, add NUL and traverse rest of src */
59 if (n == 0) {
60 if (siz != 0)
61 *d = '\0'; /* NUL-terminate dst */
62 while (*s++)
63 ;
64 }
65
66 return(s - src - 1); /* count does not include NUL */
67 }
68 #endif
69
70 int fake_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
71 size_t hostlen, char *serv, size_t servlen, int flags)
72 {
73 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
74 struct hostent *hp;
75 char tmpserv[16];
76
77 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
78 return (EAI_FAMILY);
79 if (serv != NULL) {
80 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
81 if (strlcpy(serv, tmpserv, servlen) >= servlen)
82 return (EAI_MEMORY);
83 }
84
85 if (host != NULL) {
86 if (flags & NI_NUMERICHOST) {
87 size_t len;
88 Tcl_MutexLock(&netdbMutex);
89 len = strlcpy(host, inet_ntoa(sin->sin_addr), hostlen);
90 Tcl_MutexUnlock(&netdbMutex);
91 if (len >= hostlen) {
92 return (EAI_MEMORY);
93 } else {
94 return (0);
95 }
96 } else {
97 int ret;
98 Tcl_MutexLock(&netdbMutex);
99 hp = gethostbyaddr((char *)&sin->sin_addr,
100 sizeof(struct in_addr), AF_INET);
101 if (hp == NULL) {
102 ret = EAI_NODATA;
103 } else if (strlcpy(host, hp->h_name, hostlen)
104 >= hostlen) {
105 ret = EAI_MEMORY;
106 } else {
107 ret = 0;
108 }
109 Tcl_MutexUnlock(&netdbMutex);
110 return ret;
111 }
112 }
113 return (0);
114 }
115 #endif /* !HAVE_GETNAMEINFO */
116
117 #ifndef HAVE_GAI_STRERROR
118 const char *
119 fake_gai_strerror(int err)
120 {
121 switch (err) {
122 case EAI_NODATA:
123 return ("no address associated with name");
124 case EAI_MEMORY:
125 return ("memory allocation failure.");
126 case EAI_NONAME:
127 return ("nodename nor servname provided, or not known");
128 case EAI_FAMILY:
129 return ("ai_family not supported");
130 default:
131 return ("unknown/invalid error.");
132 }
133 }
134 #endif /* !HAVE_GAI_STRERROR */
135
136 #ifndef HAVE_FREEADDRINFO
137 void
138 fake_freeaddrinfo(struct addrinfo *ai)
139 {
140 struct addrinfo *next;
141
142 for(; ai != NULL;) {
143 next = ai->ai_next;
144 free(ai);
145 ai = next;
146 }
147 }
148 #endif /* !HAVE_FREEADDRINFO */
149
150 #ifndef HAVE_GETADDRINFO
151 static struct
152 addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
153 {
154 struct addrinfo *ai;
155
156 ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
157 if (ai == NULL)
158 return (NULL);
159
160 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
161
162 ai->ai_addr = (struct sockaddr *)(ai + 1);
163 /* XXX -- ssh doesn't use sa_len */
164 ai->ai_addrlen = sizeof(struct sockaddr_in);
165 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
166
167 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
168 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
169
170 /* XXX: the following is not generally correct, but does what we want */
171 if (hints->ai_socktype)
172 ai->ai_socktype = hints->ai_socktype;
173 else
174 ai->ai_socktype = SOCK_STREAM;
175
176 if (hints->ai_protocol)
177 ai->ai_protocol = hints->ai_protocol;
178
179 return (ai);
180 }
181
182 int
183 fake_getaddrinfo(const char *hostname, const char *servname,
184 const struct addrinfo *hints, struct addrinfo **res)
185 {
186 struct hostent *hp;
187 struct servent *sp;
188 struct in_addr in;
189 int i;
190 long int port;
191 u_long addr;
192
193 port = 0;
194 if (hints && hints->ai_family != AF_UNSPEC &&
195 hints->ai_family != AF_INET)
196 return (EAI_FAMILY);
197 if (servname != NULL) {
198 char *cp;
199
200 port = strtol(servname, &cp, 10);
201 if (port > 0 && port <= 65535 && *cp == '\0')
202 port = htons((unsigned short)port);
203 else if ((sp = getservbyname(servname, NULL)) != NULL)
204 port = sp->s_port;
205 else
206 port = 0;
207 }
208
209 if (hints && hints->ai_flags & AI_PASSIVE) {
210 addr = htonl(0x00000000);
211 if (hostname && inet_aton(hostname, &in) != 0)
212 addr = in.s_addr;
213 *res = malloc_ai(port, addr, hints);
214 if (*res == NULL)
215 return (EAI_MEMORY);
216 return (0);
217 }
218
219 if (!hostname) {
220 *res = malloc_ai(port, htonl(0x7F000001), hints);
221 if (*res == NULL)
222 return (EAI_MEMORY);
223 return (0);
224 }
225
226 if (inet_aton(hostname, &in)) {
227 *res = malloc_ai(port, in.s_addr, hints);
228 if (*res == NULL)
229 return (EAI_MEMORY);
230 return (0);
231 }
232
233 /* Don't try DNS if AI_NUMERICHOST is set */
234 if (hints && hints->ai_flags & AI_NUMERICHOST)
235 return (EAI_NONAME);
236
237 Tcl_MutexLock(&netdbMutex);
238 hp = gethostbyname(hostname);
239 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
240 struct addrinfo *cur, *prev;
241
242 cur = prev = *res = NULL;
243 for (i = 0; hp->h_addr_list[i]; i++) {
244 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
245
246 cur = malloc_ai(port, in->s_addr, hints);
247 if (cur == NULL) {
248 if (*res != NULL)
249 freeaddrinfo(*res);
250 Tcl_MutexUnlock(&netdbMutex);
251 return (EAI_MEMORY);
252 }
253 if (prev)
254 prev->ai_next = cur;
255 else
256 *res = cur;
257
258 prev = cur;
259 }
260 Tcl_MutexUnlock(&netdbMutex);
261 return (0);
262 }
263 Tcl_MutexUnlock(&netdbMutex);
264 return (EAI_NODATA);
265 }
266 #endif /* !HAVE_GETADDRINFO */