6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details. You should have received a
15 * copy of the GNU General Public License along with this program; if
16 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
17 * Suite 330, Boston, MA 02111-1307, USA.
24 gcc -c -fPIC preload_open.c
25 ld -shared -o preload_open.so preload_open.o -ldl
27 export LD_PRELOAD=/etc/magic/preload_open.so
34 #include <sys/types.h>
42 #include <sys/types.h>
47 #include "overhosts.h"
49 void __attribute__ ((constructor)) overhosts_init(void);
51 struct hostent *(* ohreal_gethostbyname)(const char *name) = NULL;
52 struct hostent *(* ohreal_gethostbyname2)(const char *name, int af) = NULL;
53 int (*ohreal_getaddrinfo)(const char *node, const char *service,
54 const struct addrinfo *hints,
55 struct addrinfo **res) = NULL;
56 // void (* ohreal_freeaddrinfo)(struct addrinfo *res) = NULL;
58 static struct hostent ohreal_he = { "bernoulli",
62 (char *[]){ "\x7f\x00\x00\x01", NULL }
67 static struct addrinfo ohreal_ai = {
74 struct sockaddr *ai_addr;
76 struct addrinfo *ai_next;
81 void __attribute__ ((constructor)) overhosts_init(void)
83 ohreal_gethostbyname = dlsym((void *)-1, "gethostbyname");
84 // printf("PREINTT\n");
85 ohreal_gethostbyname2 = dlsym((void *)-1, "gethostbyname2");
86 ohreal_getaddrinfo = dlsym((void *)-1, "getaddrinfo");
90 static int is_an_alias(const char *name);
92 static int is_an_alias(const char *name)
97 if ((alias = getenv("OVERHOSTS_ALIASES")) == NULL) {
98 // printf("fuori1 [%s]\n", name);
105 // printf("in1 [%s][%s]\n", bf, name);
107 if (((p = strstr(alias, bf)) != NULL) || ((p = strstr(alias, name)) && p == alias)) {
111 // printf("in2 [%s][%c]\n", bf, *e);
112 if (*e == '\0' || *e == '|') {
113 // printf("fuori2 [%s]\n", name);
123 // struct hostent *gethostbyname(const char *name);
124 struct hostent *gethostbyname2(const char *name, int af);
126 struct hostent *gethostbyname2(const char *name, int af)
129 if (is_an_alias(name) && (real = getenv("OVERHOSTS_REAL")) != NULL) {
130 return (gethostbyname2(real, af));
133 return (gethostbyname2(name, af));
137 // struct hostent *gethostbyname(const char *name);
138 struct hostent *gethostbyname(const char *name);
140 struct hostent *gethostbyname(const char *name)
143 if (is_an_alias(name) && (real = getenv("OVERHOSTS_REAL")) != NULL) {
144 return (gethostbyname(real));
147 return (gethostbyname(name));
151 int getaddrinfo(const char *node, const char *service,
152 const struct addrinfo *hints,
153 struct addrinfo **res);
155 int getaddrinfo(const char *node, const char *service,
156 const struct addrinfo *hints,
157 struct addrinfo **res)
161 if (is_an_alias(node) && (real = getenv("OVERHOSTS_REAL")) != NULL) {
163 if (hints->ai_flags | AI_CANONNAME) {
164 printf("WARNING: AI_CANONAME set\n");
166 return ohreal_getaddrinfo(real, service,
170 return ohreal_getaddrinfo(node, service,