Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / tools / runidn / stub.c
blob1e476b00b5aee90792f8c02dc94b0c8e48bf4be1
1 /* $NetBSD: stub.c,v 1.4 2014/12/10 04:37:56 christos Exp $ */
3 #ifndef lint
4 static char *rcsid = "Id: stub.c,v 1.1 2003/06/04 00:27:13 marka Exp ";
5 #endif
7 /*
8 * Copyright (c) 2001 Japan Network Information Center. All rights reserved.
9 *
10 * By using this file, you agree to the terms and conditions set forth bellow.
12 * LICENSE TERMS AND CONDITIONS
14 * The following License Terms and Conditions apply, unless a different
15 * license is obtained from Japan Network Information Center ("JPNIC"),
16 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
17 * Chiyoda-ku, Tokyo 101-0047, Japan.
19 * 1. Use, Modification and Redistribution (including distribution of any
20 * modified or derived work) in source and/or binary forms is permitted
21 * under this License Terms and Conditions.
23 * 2. Redistribution of source code must retain the copyright notices as they
24 * appear in each source code file, this License Terms and Conditions.
26 * 3. Redistribution in binary form must reproduce the Copyright Notice,
27 * this License Terms and Conditions, in the documentation and/or other
28 * materials provided with the distribution. For the purposes of binary
29 * distribution the "Copyright Notice" refers to the following language:
30 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
32 * 4. The name of JPNIC may not be used to endorse or promote products
33 * derived from this Software without specific prior written approval of
34 * JPNIC.
36 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
37 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
39 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
46 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
49 #include <config.h>
51 #include <stdarg.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netdb.h>
57 #include <errno.h>
58 #ifdef HAVE_DLFCN_H
59 #include <dlfcn.h>
60 #endif
62 #include <idn/logmacro.h>
63 #include <idn/debug.h>
65 #include "stub.h"
67 #ifndef RTLD_NEXT
68 typedef struct {
69 const char *name;
70 void *handle;
71 } shared_obj_t;
73 static shared_obj_t shobj[] = {
74 #ifdef SOPATH_LIBC
75 { SOPATH_LIBC },
76 #endif
77 #ifdef SOPATH_LIBNSL
78 { SOPATH_LIBNSL },
79 #endif
80 { NULL },
82 #endif
84 static void *shared_obj_findsym(void *handle, const char *name);
85 static void *shared_obj_findsymx(void *handle, const char *name);
86 static void *get_func_addr(const char *name);
88 #ifndef RTLD_NEXT
89 static void *
90 shared_obj_open(const char *path) {
91 #ifdef HAVE_DLOPEN
92 return (dlopen(path, RTLD_LAZY));
93 #endif
94 FATAL(("stub: no way to load shared object file\n"));
95 return (NULL);
97 #endif
99 static void *
100 shared_obj_findsym(void *handle, const char *name) {
101 char namebuf[100];
102 void *addr;
103 static int need_leading_underscore = -1;
105 /* Prepend underscore. */
106 namebuf[0] = '_';
107 (void)strcpy(namebuf + 1, name);
108 name = namebuf;
110 if (need_leading_underscore < 0) {
111 /* First try without one. */
112 if ((addr = shared_obj_findsymx(handle, name + 1)) != NULL) {
113 need_leading_underscore = 0;
114 return (addr);
116 /* Then try with one. */
117 if ((addr = shared_obj_findsymx(handle, name)) != NULL) {
118 need_leading_underscore = 1;
119 return (addr);
121 } else if (need_leading_underscore) {
122 return (shared_obj_findsymx(handle, name));
123 } else {
124 return (shared_obj_findsymx(handle, name + 1));
126 return (NULL);
129 static void *
130 shared_obj_findsymx(void *handle, const char *name) {
131 #ifdef HAVE_DLSYM
132 return (dlsym(handle, name));
133 #endif
134 /* logging */
135 FATAL(("stub: no way to get symbol address\n"));
136 return (NULL);
139 static void *
140 get_func_addr(const char *name) {
141 #ifdef RTLD_NEXT
142 void *addr = shared_obj_findsym(RTLD_NEXT, name);
144 if (addr != NULL) {
145 TRACE(("stub: %s found in the subsequent objects\n", name));
146 return (addr);
148 #else
149 int i;
151 for (i = 0; shobj[i].name != NULL; i++) {
152 if (shobj[i].handle == NULL) {
153 TRACE(("stub: loading %s\n", shobj[i].name));
154 shobj[i].handle = shared_obj_open(shobj[i].name);
156 if (shobj[i].handle != NULL) {
157 void *addr = shared_obj_findsym(shobj[i].handle, name);
158 if (addr != NULL) {
159 TRACE(("stub: %s found in %s\n",
160 name, shobj[i].name));
161 return (addr);
165 #endif
166 TRACE(("stub: %s not found\n", name));
167 return (NULL);
170 #ifdef HAVE_GETHOSTBYNAME
171 struct hostent *
172 idn_stub_gethostbyname(const char *name) {
173 static struct hostent *(*fp)(const char *name);
175 if (fp == NULL)
176 fp = get_func_addr("gethostbyname");
177 if (fp != NULL)
178 return ((*fp)(name));
179 return (NULL);
181 #endif
183 #ifdef HAVE_GETHOSTBYNAME2
184 struct hostent *
185 idn_stub_gethostbyname2(const char *name, int af) {
186 static struct hostent *(*fp)(const char *name, int af);
188 if (fp == NULL)
189 fp = get_func_addr("gethostbyname2");
190 if (fp != NULL)
191 return ((*fp)(name, af));
192 return (NULL);
194 #endif
196 #ifdef HAVE_GETHOSTBYADDR
197 struct hostent *
198 idn_stub_gethostbyaddr(GHBA_ADDR_T addr, GHBA_ADDRLEN_T len, int type) {
199 static struct hostent *(*fp)(GHBA_ADDR_T name,
200 GHBA_ADDRLEN_T len, int type);
202 if (fp == NULL)
203 fp = get_func_addr("gethostbyaddr");
204 if (fp != NULL)
205 return ((*fp)(addr, len, type));
206 return (NULL);
208 #endif
210 #ifdef GETHOST_R_GLIBC_FLAVOR
212 #ifdef HAVE_GETHOSTBYNAME_R
214 idn_stub_gethostbyname_r(const char *name, struct hostent *result,
215 char *buffer, size_t buflen,
216 struct hostent **rp, int *errp)
218 static int (*fp)(const char *name, struct hostent *result,
219 char *buffer, size_t buflen,
220 struct hostent **rp, int *errp);
222 if (fp == NULL)
223 fp = get_func_addr("gethostbyname_r");
224 if (fp != NULL)
225 return ((*fp)(name, result, buffer, buflen, rp, errp));
226 return (ENOENT); /* ??? */
228 #endif
230 #ifdef HAVE_GETHOSTBYNAME2_R
232 idn_stub_gethostbyname2_r(const char *name, int af, struct hostent *result,
233 char *buffer, size_t buflen,
234 struct hostent **rp, int *errp)
236 static int (*fp)(const char *name, int af, struct hostent *result,
237 char *buffer, size_t buflen,
238 struct hostent **rp, int *errp);
240 if (fp == NULL)
241 fp = get_func_addr("gethostbyname2_r");
242 if (fp != NULL)
243 return ((*fp)(name, af, result, buffer, buflen, rp, errp));
244 return (ENOENT); /* ??? */
246 #endif
248 #ifdef HAVE_GETHOSTBYADDR_R
250 idn_stub_gethostbyaddr_r(GHBA_ADDR_T addr, GHBA_ADDRLEN_T len, int type,
251 struct hostent *result, char *buffer,
252 size_t buflen, struct hostent **rp, int *errp)
254 static int (*fp)(GHBA_ADDR_T addr, GHBA_ADDRLEN_T len, int type,
255 struct hostent *result, char *buffer,
256 size_t buflen, struct hostent **rp, int *errp);
258 if (fp == NULL)
259 fp = get_func_addr("gethostbyaddr_r");
260 if (fp != NULL)
261 return ((*fp)(addr, len, type, result,
262 buffer, buflen, rp, errp));
263 return (ENOENT); /* ??? */
265 #endif
267 #else /* GETHOST_R_GLIBC_FLAVOR */
269 #ifdef HAVE_GETHOSTBYNAME_R
270 struct hostent *
271 idn_stub_gethostbyname_r(const char *name, struct hostent *result,
272 char *buffer, int buflen, int *errp)
274 static struct hostent *(*fp)(const char *name, struct hostent *result,
275 char *buffer, int buflen, int *errp);
277 if (fp == NULL)
278 fp = get_func_addr("gethostbyname_r");
279 if (fp != NULL)
280 return ((*fp)(name, result, buffer, buflen, errp));
281 return (NULL);
283 #endif
285 #ifdef HAVE_GETHOSTBYADDR_R
286 struct hostent *
287 idn_stub_gethostbyaddr_r(GHBA_ADDR_T addr, int len, int type,
288 struct hostent *result, char *buffer,
289 int buflen, int *errp)
291 static struct hostent *(*fp)(GHBA_ADDR_T addr, int len, int type,
292 struct hostent *result, char *buffer,
293 int buflen, int *errp);
295 if (fp == NULL)
296 fp = get_func_addr("gethostbyaddr_r");
297 if (fp != NULL)
298 return ((*fp)(addr, len, type, result, buffer, buflen, errp));
299 return (NULL);
301 #endif
303 #endif /* GETHOST_R_GLIBC_FLAVOR */
305 #ifdef HAVE_GETIPNODEBYNAME
306 struct hostent *
307 idn_stub_getipnodebyname(const char *name, int af, int flags, int *errp) {
308 static struct hostent *(*fp)(const char *name, int af, int flags,
309 int *errp);
311 if (fp == NULL)
312 fp = get_func_addr("getipnodebyname");
313 if (fp != NULL)
314 return ((*fp)(name, af, flags, errp));
315 return (NULL);
317 #endif
319 #ifdef HAVE_GETIPNODEBYADDR
320 struct hostent *
321 idn_stub_getipnodebyaddr(const void *src, size_t len, int af, int *errp) {
322 static struct hostent *(*fp)(const void *src, size_t len, int af,
323 int *errp);
325 if (fp == NULL)
326 fp = get_func_addr("getipnodebyaddr");
327 if (fp != NULL)
328 return ((*fp)(src, len, af, errp));
329 return (NULL);
331 #endif
333 #ifdef HAVE_FREEHOSTENT
334 void
335 idn_stub_freehostent(struct hostent *hp) {
336 static void (*fp)(struct hostent *hp);
338 if (fp == NULL)
339 fp = get_func_addr("freehostent");
340 if (fp != NULL)
341 (*fp)(hp);
343 #endif
345 #ifdef HAVE_GETADDRINFO
347 idn_stub_getaddrinfo(const char *nodename, const char *servname,
348 const struct addrinfo *hints, struct addrinfo **res)
350 static int (*fp)(const char *nodename, const char *servname,
351 const struct addrinfo *hints, struct addrinfo **res);
353 if (fp == NULL)
354 fp = get_func_addr("getaddrinfo");
355 if (fp != NULL)
356 return ((*fp)(nodename, servname, hints, res));
357 return (EAI_FAIL);
359 #endif
361 #ifdef HAVE_FREEADDRINFO
362 void
363 idn_stub_freeaddrinfo(struct addrinfo *aip) {
364 static void (*fp)(struct addrinfo *aip);
366 if (fp == NULL)
367 fp = get_func_addr("freeaddrinfo");
368 if (fp != NULL)
369 (*fp)(aip);
371 #endif
373 #ifdef HAVE_GETNAMEINFO
375 idn_stub_getnameinfo(const struct sockaddr *sa, GNI_SALEN_T salen,
376 char *host, GNI_HOSTLEN_T hostlen,
377 char *serv, GNI_SERVLEN_T servlen, GNI_FLAGS_T flags) {
378 static int (*fp)(const struct sockaddr *sa, GNI_SALEN_T salen,
379 char *host, GNI_HOSTLEN_T hostlen,
380 char *serv, GNI_SERVLEN_T servlen,
381 GNI_FLAGS_T flags);
383 if (fp == NULL)
384 fp = get_func_addr("getnameinfo");
385 if (fp != NULL)
386 return ((*fp)(sa, salen, host, hostlen, serv, servlen, flags));
387 return (EAI_FAIL);
389 #endif