1 /* $NetBSD: stub.c,v 1.4 2014/12/10 04:37:56 christos Exp $ */
4 static char *rcsid
= "Id: stub.c,v 1.1 2003/06/04 00:27:13 marka Exp ";
8 * Copyright (c) 2001 Japan Network Information Center. All rights reserved.
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
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.
54 #include <sys/types.h>
55 #include <sys/socket.h>
62 #include <idn/logmacro.h>
63 #include <idn/debug.h>
73 static shared_obj_t shobj
[] = {
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
);
90 shared_obj_open(const char *path
) {
92 return (dlopen(path
, RTLD_LAZY
));
94 FATAL(("stub: no way to load shared object file\n"));
100 shared_obj_findsym(void *handle
, const char *name
) {
103 static int need_leading_underscore
= -1;
105 /* Prepend underscore. */
107 (void)strcpy(namebuf
+ 1, name
);
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;
116 /* Then try with one. */
117 if ((addr
= shared_obj_findsymx(handle
, name
)) != NULL
) {
118 need_leading_underscore
= 1;
121 } else if (need_leading_underscore
) {
122 return (shared_obj_findsymx(handle
, name
));
124 return (shared_obj_findsymx(handle
, name
+ 1));
130 shared_obj_findsymx(void *handle
, const char *name
) {
132 return (dlsym(handle
, name
));
135 FATAL(("stub: no way to get symbol address\n"));
140 get_func_addr(const char *name
) {
142 void *addr
= shared_obj_findsym(RTLD_NEXT
, name
);
145 TRACE(("stub: %s found in the subsequent objects\n", name
));
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
);
159 TRACE(("stub: %s found in %s\n",
160 name
, shobj
[i
].name
));
166 TRACE(("stub: %s not found\n", name
));
170 #ifdef HAVE_GETHOSTBYNAME
172 idn_stub_gethostbyname(const char *name
) {
173 static struct hostent
*(*fp
)(const char *name
);
176 fp
= get_func_addr("gethostbyname");
178 return ((*fp
)(name
));
183 #ifdef HAVE_GETHOSTBYNAME2
185 idn_stub_gethostbyname2(const char *name
, int af
) {
186 static struct hostent
*(*fp
)(const char *name
, int af
);
189 fp
= get_func_addr("gethostbyname2");
191 return ((*fp
)(name
, af
));
196 #ifdef HAVE_GETHOSTBYADDR
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
);
203 fp
= get_func_addr("gethostbyaddr");
205 return ((*fp
)(addr
, len
, type
));
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
);
223 fp
= get_func_addr("gethostbyname_r");
225 return ((*fp
)(name
, result
, buffer
, buflen
, rp
, errp
));
226 return (ENOENT
); /* ??? */
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
);
241 fp
= get_func_addr("gethostbyname2_r");
243 return ((*fp
)(name
, af
, result
, buffer
, buflen
, rp
, errp
));
244 return (ENOENT
); /* ??? */
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
);
259 fp
= get_func_addr("gethostbyaddr_r");
261 return ((*fp
)(addr
, len
, type
, result
,
262 buffer
, buflen
, rp
, errp
));
263 return (ENOENT
); /* ??? */
267 #else /* GETHOST_R_GLIBC_FLAVOR */
269 #ifdef HAVE_GETHOSTBYNAME_R
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
);
278 fp
= get_func_addr("gethostbyname_r");
280 return ((*fp
)(name
, result
, buffer
, buflen
, errp
));
285 #ifdef HAVE_GETHOSTBYADDR_R
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
);
296 fp
= get_func_addr("gethostbyaddr_r");
298 return ((*fp
)(addr
, len
, type
, result
, buffer
, buflen
, errp
));
303 #endif /* GETHOST_R_GLIBC_FLAVOR */
305 #ifdef HAVE_GETIPNODEBYNAME
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
,
312 fp
= get_func_addr("getipnodebyname");
314 return ((*fp
)(name
, af
, flags
, errp
));
319 #ifdef HAVE_GETIPNODEBYADDR
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
,
326 fp
= get_func_addr("getipnodebyaddr");
328 return ((*fp
)(src
, len
, af
, errp
));
333 #ifdef HAVE_FREEHOSTENT
335 idn_stub_freehostent(struct hostent
*hp
) {
336 static void (*fp
)(struct hostent
*hp
);
339 fp
= get_func_addr("freehostent");
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
);
354 fp
= get_func_addr("getaddrinfo");
356 return ((*fp
)(nodename
, servname
, hints
, res
));
361 #ifdef HAVE_FREEADDRINFO
363 idn_stub_freeaddrinfo(struct addrinfo
*aip
) {
364 static void (*fp
)(struct addrinfo
*aip
);
367 fp
= get_func_addr("freeaddrinfo");
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
,
384 fp
= get_func_addr("getnameinfo");
386 return ((*fp
)(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
));