Debian Codegraph @

src/resolver.c

raw
1 /*
2 * stunnel TLS offloading and load-balancing proxy
3 * Copyright (C) 1998-2026 Michal Trojnara <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses>.
17 *
18 * Linking stunnel statically or dynamically with other modules is making
19 * a combined work based on stunnel. Thus, the terms and conditions of
20 * the GNU General Public License cover the whole combination.
21 *
22 * In addition, as a special exception, the copyright holder of stunnel
23 * gives you permission to combine stunnel with free software programs or
24 * libraries that are released under the GNU LGPL and with code included
25 * in the standard release of OpenSSL under the OpenSSL License (or
26 * modified versions of such code, with unchanged license). You may copy
27 * and distribute such a system following the terms of the GNU GPL for
28 * stunnel and the licenses of the other code concerned.
29 *
30 * Note that people who make modified versions of stunnel are not obligated
31 * to grant this special exception for their modified versions; it is their
32 * choice whether to do so. The GNU General Public License gives permission
33 * to release a modified version without this exception; this exception
34 * also makes it possible to release a modified version which carries
35 * forward this exception.
36 */
37
38 #include "prototypes.h"
39
40 #ifdef _MSC_VER
41 /*
42 * Disable MSVC C4996 warning:
43 * warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead
44 * warning C4996: 'inet_ntoa': Use inet_ntop() or InetNtop() instead
45 * warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead
46 */
47 #pragma warning(disable: 4996)
48 #endif
49
50 /**************************************** prototypes */
51
52 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
53
54 typedef int (CALLBACK * GETADDRINFO) (const char *,
55 const char *, const struct addrinfo *, struct addrinfo **);
56 typedef void (CALLBACK * FREEADDRINFO) (struct addrinfo *);
57 typedef int (CALLBACK * GETNAMEINFO) (const struct sockaddr *, socklen_t,
58 char *, size_t, char *, size_t, int);
59 extern GETADDRINFO s_getaddrinfo;
60 extern FREEADDRINFO s_freeaddrinfo;
61 extern GETNAMEINFO s_getnameinfo;
62
63 NOEXPORT int get_ipv6(LPTSTR);
64
65 #endif /* defined(USE_WIN32) && !defined(_WIN32_WCE) */
66
67 NOEXPORT void addrlist2addr(SOCKADDR_UNION *, SOCKADDR_LIST *);
68 NOEXPORT void addrlist_reset(SOCKADDR_LIST *);
69
70 #ifndef HAVE_GETADDRINFO
71
72 #ifndef EAI_MEMORY
73 #define EAI_MEMORY 1
74 #endif
75 #ifndef EAI_NONAME
76 #define EAI_NONAME 2
77 #endif
78 #ifndef EAI_SERVICE
79 #define EAI_SERVICE 8
80 #endif
81
82 /* rename some potentially locally shadowed declarations */
83 #define getaddrinfo local_getaddrinfo
84 #define freeaddrinfo local_freeaddrinfo
85
86 #ifndef HAVE_STRUCT_ADDRINFO
87 struct addrinfo {
88 int ai_flags;
89 int ai_family;
90 int ai_socktype;
91 int ai_protocol;
92 int ai_addrlen;
93 struct sockaddr *ai_addr;
94 char *ai_canonname;
95 struct addrinfo *ai_next;
96 };
97 #endif
98
99 #ifndef AI_PASSIVE
100 #define AI_PASSIVE 1
101 #endif
102
103 NOEXPORT int getaddrinfo(const char *, const char *,
104 const struct addrinfo *, struct addrinfo **);
105 NOEXPORT int alloc_addresses(struct hostent *, const struct addrinfo *,
106 u_short port, struct addrinfo **, struct addrinfo **);
107 NOEXPORT void freeaddrinfo(struct addrinfo *);
108
109 #endif /* !defined HAVE_GETADDRINFO */
110
111 /**************************************** resolver initialization */
112
113 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
114 GETADDRINFO s_getaddrinfo;
115 FREEADDRINFO s_freeaddrinfo;
116 GETNAMEINFO s_getnameinfo;
117 #endif
118
119 void resolver_init(void) {
120 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
121 if(get_ipv6(TEXT("ws2_32.dll"))) /* IPv6 in Windows XP or higher */
122 return;
123 if(get_ipv6(TEXT("wship6.dll"))) /* experimental IPv6 for Windows 2000 */
124 return;
125 /* fall back to the built-in emulation */
126 #endif
127 }
128
129 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
130 #ifdef __GNUC__
131 #pragma GCC diagnostic push
132 #pragma GCC diagnostic ignored "-Wpragmas"
133 #pragma GCC diagnostic ignored "-Wcast-function-type"
134 #endif /* __GNUC__ */
135 NOEXPORT int get_ipv6(LPTSTR file) {
136 HINSTANCE handle;
137
138 handle=LoadLibrary(file);
139 if(!handle)
140 return 0;
141 s_getaddrinfo=(GETADDRINFO)GetProcAddress(handle, "getaddrinfo");
142 s_freeaddrinfo=(FREEADDRINFO)GetProcAddress(handle, "freeaddrinfo");
143 s_getnameinfo=(GETNAMEINFO)GetProcAddress(handle, "getnameinfo");
144 if(!s_getaddrinfo || !s_freeaddrinfo || !s_getnameinfo) {
145 s_getaddrinfo=NULL;
146 s_freeaddrinfo=NULL;
147 s_getnameinfo=NULL;
148 FreeLibrary(handle);
149 return 0;
150 }
151 return 1; /* IPv6 detected -> OK */
152 }
153 #ifdef __GNUC__
154 #pragma GCC diagnostic pop
155 #endif /* __GNUC__ */
156 #endif
157
158 int use_ipv6(void) {
159 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
160 return s_getaddrinfo != NULL;
161 #else /* defined(USE_WIN32) && !defined(_WIN32_WCE) */
162 #if defined(USE_IPV6)
163 return 1;
164 #else /* defined(USE_IPV6) */
165 return 0;
166 #endif /* defined(USE_IPV6) */
167 #endif /* defined(USE_WIN32) && !defined(_WIN32_WCE) */
168 }
169
170 /**************************************** stunnel resolver API */
171
172 unsigned name2addr(SOCKADDR_UNION *addr, char *name, int passive) {
173 SOCKADDR_LIST *addr_list;
174 unsigned retval;
175
176 addr_list=str_alloc(sizeof(SOCKADDR_LIST));
177 addrlist_clear(addr_list, passive);
178 retval=name2addrlist(addr_list, name);
179 if(retval)
180 addrlist2addr(addr, addr_list);
181 str_free(addr_list->addr);
182 str_free(addr_list);
183 return retval;
184 }
185
186 unsigned hostport2addr(SOCKADDR_UNION *addr,
187 char *host_name, const char *port_name, int passive) {
188 SOCKADDR_LIST *addr_list;
189 unsigned num;
190
191 addr_list=str_alloc(sizeof(SOCKADDR_LIST));
192 addrlist_clear(addr_list, passive);
193 num=hostport2addrlist(addr_list, host_name, port_name);
194 if(num)
195 addrlist2addr(addr, addr_list);
196 str_free(addr_list->addr);
197 str_free(addr_list);
198 return num;
199 }
200
201 NOEXPORT void addrlist2addr(SOCKADDR_UNION *addr, SOCKADDR_LIST *addr_list) {
202 unsigned i;
203
204 for(i=0; i<addr_list->num; ++i) { /* find the first IPv4 address */
205 if(addr_list->addr[i].in.sin_family==AF_INET) {
206 memcpy(addr, &addr_list->addr[i], sizeof(SOCKADDR_UNION));
207 return;
208 }
209 }
210 #ifdef USE_IPV6
211 for(i=0; i<addr_list->num; ++i) { /* find the first IPv6 address */
212 if(addr_list->addr[i].in.sin_family==AF_INET6) {
213 memcpy(addr, &addr_list->addr[i], sizeof(SOCKADDR_UNION));
214 return;
215 }
216 }
217 #endif
218 /* copy the first address resolved (currently AF_UNIX) */
219 memcpy(addr, &addr_list->addr[0], sizeof(SOCKADDR_UNION));
220 }
221
222 unsigned name2addrlist(SOCKADDR_LIST *addr_list, char *name) {
223 char *tmp, *host_name, *port_name;
224 unsigned num;
225
226 /* first check if this is a UNIX socket */
227 #ifdef HAVE_STRUCT_SOCKADDR_UN
228 if(*name=='/') {
229 if(offsetof(struct sockaddr_un, sun_path)+strlen(name)+1
230 > sizeof(struct sockaddr_un)) {
231 s_log(LOG_ERR, "Unix socket path is too long");
232 return 0; /* no results */
233 }
234 addr_list->addr=str_realloc_detached(addr_list->addr,
235 (addr_list->num+1)*sizeof(SOCKADDR_UNION));
236 addr_list->addr[addr_list->num].un.sun_family=AF_UNIX;
237 strcpy(addr_list->addr[addr_list->num].un.sun_path, name);
238 ++(addr_list->num);
239 return 1; /* ok - return the number of new addresses */
240 }
241 #endif
242
243 /* setup host_name and port_name */
244 tmp=str_dup(name);
245 port_name=strrchr(tmp, ':');
246 if(port_name) {
247 host_name=tmp;
248 *port_name++='\0';
249 } else { /* no ':' - use default host IP */
250 host_name=NULL;
251 port_name=tmp;
252 }
253
254 /* fill addr_list structure */
255 num=hostport2addrlist(addr_list, host_name, port_name);
256 str_free(tmp);
257 return num; /* ok - return the number of new addresses */
258 }
259
260 unsigned hostport2addrlist(SOCKADDR_LIST *addr_list,
261 char *host_name, const char *port_name) {
262 struct addrinfo hints, *res, *cur;
263 int err, retry=0;
264 unsigned num;
265
266 memset(&hints, 0, sizeof hints);
267 #if defined(USE_IPV6) || defined(USE_WIN32)
268 hints.ai_family=AF_UNSPEC;
269 #else
270 hints.ai_family=AF_INET;
271 #endif
272 hints.ai_socktype=SOCK_STREAM;
273 hints.ai_protocol=IPPROTO_TCP;
274 hints.ai_flags=0;
275 if(addr_list->passive)
276 hints.ai_flags|=AI_PASSIVE;
277 #ifdef AI_ADDRCONFIG
278 hints.ai_flags|=AI_ADDRCONFIG;
279 #endif
280 for(;;) {
281 res=NULL;
282 err=getaddrinfo(host_name, port_name, &hints, &res);
283 if(!err) /* success */
284 break;
285 if(err==EAI_SERVICE) {
286 s_log(LOG_ERR, "Unknown TCP service \"%s\"", port_name);
287 return 0; /* error */
288 }
289 if(err==EAI_AGAIN && ++retry<=3) {
290 s_log(LOG_DEBUG, "getaddrinfo: EAI_AGAIN received: retrying");
291 s_poll_sleep(1, 0);
292 continue;
293 }
294 #ifdef AI_ADDRCONFIG
295 if(hints.ai_flags&AI_ADDRCONFIG) {
296 hints.ai_flags&=~AI_ADDRCONFIG;
297 continue; /* retry for unconfigured network interfaces */
298 }
299 #endif
300 s_log(LOG_ERR, "Error resolving \"%s\": %s",
301 host_name ? host_name :
302 (addr_list->passive ? DEFAULT_ANY : DEFAULT_LOOPBACK),
303 s_gai_strerror(err));
304 return 0; /* error */
305 }
306
307 /* find the number of newly resolved addresses */
308 num=0;
309 for(cur=res; cur; cur=cur->ai_next) {
310 if(cur->ai_addrlen>(int)sizeof(SOCKADDR_UNION)) {
311 s_log(LOG_ERR, "INTERNAL ERROR: ai_addrlen value too big");
312 freeaddrinfo(res);
313 return 0; /* no results */
314 }
315 ++num;
316 }
317
318 /* append the newly resolved addresses to addr_list->addr */
319 addr_list->addr=str_realloc_detached(addr_list->addr,
320 (addr_list->num+num)*sizeof(SOCKADDR_UNION));
321 for(cur=res; cur; cur=cur->ai_next)
322 memcpy(&addr_list->addr[(addr_list->num)++], cur->ai_addr,
323 (size_t)cur->ai_addrlen);
324
325 freeaddrinfo(res);
326 return num; /* ok - return the number of new addresses */
327 }
328
329 /* initialize the structure */
330 void addrlist_clear(SOCKADDR_LIST *addr_list, int passive) {
331 addrlist_reset(addr_list);
332 addr_list->names=NULL;
333 addr_list->passive=passive;
334 }
335
336 /* prepare the structure to resolve new hosts */
337 NOEXPORT void addrlist_reset(SOCKADDR_LIST *addr_list) {
338 addr_list->num=0;
339 addr_list->addr=NULL;
340 addr_list->start=0;
341 addr_list->parent=addr_list; /* allow a copy to locate its parent */
342 }
343
344 unsigned addrlist_dup(SOCKADDR_LIST *dst, const SOCKADDR_LIST *src) {
345 memcpy(dst, src, sizeof(SOCKADDR_LIST));
346 if(src->num) { /* already resolved */
347 dst->addr=str_alloc_detached(src->num*sizeof(SOCKADDR_UNION));
348 memcpy(dst->addr, src->addr, src->num*sizeof(SOCKADDR_UNION));
349 } else { /* delayed resolver */
350 addrlist_resolve(dst);
351 }
352 return dst->num;
353 }
354
355 unsigned addrlist_resolve(SOCKADDR_LIST *addr_list) {
356 unsigned num=0, rnd=0;
357 NAME_LIST *host;
358
359 addrlist_reset(addr_list);
360 for(host=addr_list->names; host; host=host->next)
361 num+=name2addrlist(addr_list, host->name);
362 if(num<2) {
363 addr_list->start=0;
364 } else {
365 /* randomize the initial value of round-robin counter */
366 /* ignore the error value and the distribution bias */
367 RAND_bytes((unsigned char *)&rnd, sizeof rnd);
368 addr_list->start=rnd%num;
369 }
370 return num;
371 }
372
373 char *s_ntop(SOCKADDR_UNION *addr, socklen_t addrlen) {
374 int err;
375 char *host, *port, *retval;
376
377 if(addrlen==sizeof(u_short)) /* see UNIX(7) manual for details */
378 return str_dup("unnamed socket");
379 host=str_alloc(256);
380 port=str_alloc(256); /* needs to be long enough for AF_UNIX path */
381 err=getnameinfo(&addr->sa, addrlen,
382 host, 256, port, 256, NI_NUMERICHOST|NI_NUMERICSERV);
383 if(err) {
384 s_log(LOG_ERR, "getnameinfo: %s", s_gai_strerror(err));
385 retval=str_dup("unresolvable address");
386 } else if(!*port && !*host)
387 retval=str_dup("unnamed socket");
388 else
389 retval=str_printf("%s%s%s", host, *host && *port ? ":" : "", port);
390 str_free(host);
391 str_free(port);
392 return retval;
393 }
394
395 socklen_t addr_len(const SOCKADDR_UNION *addr) {
396 switch(addr->sa.sa_family) {
397 case AF_UNSPEC: /* 0 */
398 return 0;
399 case AF_INET: /* 2 (almost universally) */
400 return sizeof(struct sockaddr_in);
401 #ifdef USE_IPV6
402 case AF_INET6:
403 return sizeof(struct sockaddr_in6);
404 #endif
405 #ifdef HAVE_STRUCT_SOCKADDR_UN
406 case AF_UNIX:
407 return sizeof(struct sockaddr_un);
408 #endif
409 }
410 s_log(LOG_ERR, "INTERNAL ERROR: Unknown sa_family: %d",
411 addr->sa.sa_family);
412 return sizeof(SOCKADDR_UNION);
413 }
414
415 /**************************************** my getaddrinfo() */
416 /* implementation is limited to functionality needed by stunnel */
417
418 #ifndef HAVE_GETADDRINFO
419 NOEXPORT int getaddrinfo(const char *node, const char *service,
420 const struct addrinfo *hints, struct addrinfo **res) {
421 struct hostent *h;
422 #ifndef _WIN32_WCE
423 struct servent *p;
424 #endif
425 u_short port;
426 struct addrinfo *ai;
427 int retval;
428 char *tmpstr;
429
430 if(!node)
431 node=(hints->ai_flags & AI_PASSIVE) ? DEFAULT_ANY : DEFAULT_LOOPBACK;
432 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
433 if(s_getaddrinfo)
434 return s_getaddrinfo(node, service, hints, res);
435 #endif
436 /* decode service name */
437 port=htons((u_short)strtol(service, &tmpstr, 10));
438 if(tmpstr==service || *tmpstr) { /* not a number */
439 #ifdef _WIN32_WCE
440 return EAI_NONAME;
441 #else /* defined(_WIN32_WCE) */
442 p=getservbyname(service, "tcp");
443 if(!p)
444 return EAI_NONAME;
445 port=(u_short)p->s_port;
446 #endif /* defined(_WIN32_WCE) */
447 }
448
449 /* allocate addrlist structure */
450 ai=str_alloc(sizeof(struct addrinfo));
451 if(hints)
452 memcpy(ai, hints, sizeof(struct addrinfo));
453
454 /* try to decode numerical address */
455 #if defined(USE_IPV6) && !defined(USE_WIN32)
456 ai->ai_family=AF_INET6;
457 ai->ai_addrlen=sizeof(struct sockaddr_in6);
458 ai->ai_addr=str_alloc((size_t)ai->ai_addrlen);
459 ai->ai_addr->sa_family=AF_INET6;
460 if(inet_pton(AF_INET6, node,
461 &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr)>0) {
462 #else
463 ai->ai_family=AF_INET;
464 ai->ai_addrlen=sizeof(struct sockaddr_in);
465 ai->ai_addr=str_alloc(ai->ai_addrlen);
466 ai->ai_addr->sa_family=AF_INET;
467 ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr=inet_addr(node);
468 if(((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr+1) {
469 /* (signed)((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr!=-1 */
470 #endif
471 ((struct sockaddr_in *)ai->ai_addr)->sin_port=port;
472 *res=ai;
473 return 0; /* numerical address resolved */
474 }
475 str_free(ai->ai_addr);
476 str_free(ai);
477
478 /* not numerical: need to call resolver library */
479 *res=NULL;
480 ai=NULL;
481 CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_INET]);
482 #ifdef HAVE_GETHOSTBYNAME2
483 h=gethostbyname2(node, AF_INET6);
484 if(h) /* some IPv6 addresses found */
485 alloc_addresses(h, hints, port, res, &ai); /* ignore the error */
486 #endif
487 h=gethostbyname(node); /* get list of addresses */
488 if(h)
489 retval=ai ?
490 alloc_addresses(h, hints, port, &ai->ai_next, &ai) :
491 alloc_addresses(h, hints, port, res, &ai);
492 else if(!*res)
493 retval=EAI_NONAME; /* no results */
494 else
495 retval=0;
496 #ifdef HAVE_ENDHOSTENT
497 endhostent();
498 #endif
499 CRYPTO_THREAD_unlock(stunnel_locks[LOCK_INET]);
500 if(retval) { /* error: free allocated memory */
501 freeaddrinfo(*res);
502 *res=NULL;
503 }
504 return retval;
505 }
506
507 NOEXPORT int alloc_addresses(struct hostent *h, const struct addrinfo *hints,
508 u_short port, struct addrinfo **head, struct addrinfo **tail) {
509 int i;
510 struct addrinfo *ai;
511
512 /* copy addresses */
513 for(i=0; h->h_addr_list[i]; i++) {
514 ai=str_alloc(sizeof(struct addrinfo));
515 if(hints)
516 memcpy(ai, hints, sizeof(struct addrinfo));
517 ai->ai_next=NULL; /* just in case */
518 if(*tail) { /* list not empty: add a node */
519 (*tail)->ai_next=ai;
520 *tail=ai;
521 } else { /* list empty: create it */
522 *head=ai;
523 *tail=ai;
524 }
525 ai->ai_family=h->h_addrtype;
526 #if defined(USE_IPV6)
527 if(h->h_addrtype==AF_INET6) {
528 ai->ai_addrlen=sizeof(struct sockaddr_in6);
529 ai->ai_addr=str_alloc((size_t)ai->ai_addrlen);
530 memcpy(&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
531 h->h_addr_list[i], (size_t)h->h_length);
532 } else
533 #endif
534 {
535 ai->ai_addrlen=sizeof(struct sockaddr_in);
536 ai->ai_addr=str_alloc((size_t)ai->ai_addrlen);
537 memcpy(&((struct sockaddr_in *)ai->ai_addr)->sin_addr,
538 h->h_addr_list[i], (size_t)h->h_length);
539 }
540 ai->ai_addr->sa_family=(u_short)h->h_addrtype;
541 /* offsets of sin_port and sin6_port should be the same */
542 ((struct sockaddr_in *)ai->ai_addr)->sin_port=port;
543 }
544 return 0; /* success */
545 }
546
547 NOEXPORT void freeaddrinfo(struct addrinfo *current) {
548 struct addrinfo *next;
549
550 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
551 if(s_freeaddrinfo) {
552 s_freeaddrinfo(current);
553 return;
554 }
555 #endif
556 while(current) {
557 str_free(current->ai_addr);
558 str_free(current->ai_canonname);
559 next=current->ai_next;
560 str_free(current);
561 current=next;
562 }
563 }
564 #endif /* !defined HAVE_GETADDRINFO */
565
566 /* due to a problem with Mingw32 I decided to define my own gai_strerror() */
567 const char *s_gai_strerror(int err) {
568 switch(err) {
569 #ifdef EAI_BADFLAGS
570 case EAI_BADFLAGS:
571 return "Invalid value for ai_flags (EAI_BADFLAGS)";
572 #endif
573 case EAI_NONAME:
574 return "Neither nodename nor servname known (EAI_NONAME)";
575 #ifdef EAI_AGAIN
576 case EAI_AGAIN:
577 return "Temporary failure in name resolution (EAI_AGAIN)";
578 #endif
579 #ifdef EAI_FAIL
580 case EAI_FAIL:
581 return "Non-recoverable failure in name resolution (EAI_FAIL)";
582 #endif
583 #ifdef EAI_NODATA
584 #if EAI_NODATA!=EAI_NONAME
585 case EAI_NODATA:
586 return "No address associated with nodename (EAI_NODATA)";
587 #endif /* EAI_NODATA!=EAI_NONAME */
588 #endif /* defined EAI_NODATA */
589 #ifdef EAI_FAMILY
590 case EAI_FAMILY:
591 return "ai_family not supported (EAI_FAMILY)";
592 #endif
593 #ifdef EAI_SOCKTYPE
594 case EAI_SOCKTYPE:
595 return "ai_socktype not supported (EAI_SOCKTYPE)";
596 #endif
597 #ifdef EAI_SERVICE
598 case EAI_SERVICE:
599 return "servname is not supported for ai_socktype (EAI_SERVICE)";
600 #endif
601 #ifdef EAI_ADDRFAMILY
602 case EAI_ADDRFAMILY:
603 return "Address family for nodename not supported (EAI_ADDRFAMILY)";
604 #endif /* EAI_ADDRFAMILY */
605 case EAI_MEMORY:
606 return "Memory allocation failure (EAI_MEMORY)";
607 #ifdef EAI_SYSTEM
608 case EAI_SYSTEM:
609 return "System error returned in errno (EAI_SYSTEM)";
610 #endif /* EAI_SYSTEM */
611 default:
612 return "Unknown error";
613 }
614 }
615
616 /**************************************** my getnameinfo() */
617 /* implementation is limited to functionality needed by stunnel */
618
619 #ifndef HAVE_GETNAMEINFO
620 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
621 char *host, size_t hostlen, char *serv, size_t servlen, int flags) {
622
623 #if defined(USE_WIN32) && !defined(_WIN32_WCE)
624 if(s_getnameinfo)
625 return s_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
626 #endif
627 if(host && hostlen) {
628 #if defined(USE_IPV6) && !defined(USE_WIN32)
629 inet_ntop(sa->sa_family, sa->sa_family==AF_INET6 ?
630 (void *)&((struct sockaddr_in6 *)sa)->sin6_addr :
631 (void *)&((struct sockaddr_in *)sa)->sin_addr,
632 host, hostlen);
633 #else /* USE_IPV6 */
634 /* inet_ntoa is not mt-safe */
635 CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_INET]);
636 strncpy(host, inet_ntoa(((struct sockaddr_in *)sa)->sin_addr),
637 hostlen);
638 CRYPTO_THREAD_unlock(stunnel_locks[LOCK_INET]);
639 host[hostlen-1]='\0';
640 #endif /* USE_IPV6 */
641 }
642 if(serv && servlen)
643 sprintf(serv, "%u", ntohs(((struct sockaddr_in *)sa)->sin_port));
644 /* sin_port is in the same place both in sockaddr_in and sockaddr_in6 */
645 /* ignore servlen since it's long enough in stunnel code */
646 return 0;
647 }
648 #endif
649
650 /* end of resolver.c */