+/*
+ * overhosts.c
+ *
+ * Copyright (C) 2012
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details. You should have received a
+ * copy of the GNU General Public License along with this program; if
+ * not, write to the Free Software Foundation, Inc, 59 Temple Place -
+ * Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+/*
+ compile like this:
+
+ gcc -c -fPIC preload_open.c
+ ld -shared -o preload_open.so preload_open.o -ldl
+
+ export LD_PRELOAD=/etc/magic/preload_open.so
+ myapp ARGS
+
+*/
+
+
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <string.h>
+#include <netdb.h>
+
+#include "overhosts.h"
+
+void __attribute__ ((constructor)) overhosts_init(void);
+
+struct hostent *(* ohreal_gethostbyname)(const char *name) = NULL;
+struct hostent *(* ohreal_gethostbyname2)(const char *name, int af) = NULL;
+int (*ohreal_getaddrinfo)(const char *node, const char *service,
+ const struct addrinfo *hints,
+ struct addrinfo **res) = NULL;
+// void (* ohreal_freeaddrinfo)(struct addrinfo *res) = NULL;
+#if 0
+static struct hostent ohreal_he = { "bernoulli",
+ (char *[]){ NULL },
+ AF_INET,
+ 4,
+ (char *[]){ "\x7f\x00\x00\x01", NULL }
+};
+#endif
+
+/*
+static struct addrinfo ohreal_ai = {
+
+ int ai_flags;
+ int ai_family;
+ int ai_socktype;
+ int ai_protocol;
+ size_t ai_addrlen;
+ struct sockaddr *ai_addr;
+ char *ai_canonname;
+ struct addrinfo *ai_next;
+ };
+
+*/
+
+void __attribute__ ((constructor)) overhosts_init(void)
+{
+ ohreal_gethostbyname = dlsym((void *)-1, "gethostbyname");
+ // printf("PREINTT\n");
+ ohreal_gethostbyname2 = dlsym((void *)-1, "gethostbyname2");
+ ohreal_getaddrinfo = dlsym((void *)-1, "getaddrinfo");
+ // printf("INITT\n");
+}
+
+static int is_an_alias(const char *name);
+
+static int is_an_alias(const char *name)
+{
+ char *alias, *p, *e;
+ char bf[4096];
+
+ if ((alias = getenv("OVERHOSTS_ALIASES")) == NULL) {
+ // printf("fuori1 [%s]\n", name);
+ return 0;
+ }
+
+ strcpy(bf, "|");
+ strcat(bf, name);
+
+ // printf("in1 [%s][%s]\n", bf, name);
+
+ if (((p = strstr(alias, bf)) != NULL) || ((p = strstr(alias, name)) && p == alias)) {
+ e = p+strlen(name);
+ if (p > alias)
+ e++;
+ // printf("in2 [%s][%c]\n", bf, *e);
+ if (*e == '\0' || *e == '|') {
+ // printf("fuori2 [%s]\n", name);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+
+
+// struct hostent *gethostbyname(const char *name);
+struct hostent *gethostbyname2(const char *name, int af);
+
+struct hostent *gethostbyname2(const char *name, int af)
+{
+ char *real;
+ if (is_an_alias(name) && (real = getenv("OVERHOSTS_REAL")) != NULL) {
+ return (gethostbyname2(real, af));
+ }
+ else {
+ return (gethostbyname2(name, af));
+ }
+}
+
+// struct hostent *gethostbyname(const char *name);
+struct hostent *gethostbyname(const char *name);
+
+struct hostent *gethostbyname(const char *name)
+{
+ char *real;
+ if (is_an_alias(name) && (real = getenv("OVERHOSTS_REAL")) != NULL) {
+ return (gethostbyname(real));
+ }
+ else {
+ return (gethostbyname(name));
+ }
+}
+
+int getaddrinfo(const char *node, const char *service,
+ const struct addrinfo *hints,
+ struct addrinfo **res);
+
+int getaddrinfo(const char *node, const char *service,
+ const struct addrinfo *hints,
+ struct addrinfo **res)
+{
+ char *real;
+
+ if (is_an_alias(node) && (real = getenv("OVERHOSTS_REAL")) != NULL) {
+
+ if (hints->ai_flags | AI_CANONNAME) {
+ printf("WARNING: AI_CANONAME set\n");
+ }
+ return ohreal_getaddrinfo(real, service,
+ hints, res);
+ }
+ else {
+ return ohreal_getaddrinfo(node, service,
+ hints, res);
+ }
+}