chiark / gitweb /
noipv6 command
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 6 Feb 2011 12:43:43 +0000 (12:43 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 6 Feb 2011 12:43:43 +0000 (12:43 +0000)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
defs.Linux [new file with mode: 0644]
gaise.c [new file with mode: 0644]
noipv6.m4 [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..3257201
--- /dev/null
@@ -0,0 +1,4 @@
+*~
+*.lo
+*.so
+noipv6
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..178a886
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,76 @@
+prefix=/usr/local
+bindir=${prefix}/bin
+libdir=${prefix}/lib
+mandir=${prefix}/man
+man1dir=${mandir}/man1
+pkglibdir=${libdir}/gaise
+INSTALL=install -c
+
+include defs.$(shell uname -s)
+
+VERSION=0.1
+
+all: ${MODULE} noipv6
+
+noipv6: noipv6.m4 Makefile
+       m4 -Dpkglibdir="${pkglibdir}" \
+               -DVERSION="${VERSION}" \
+               -D__module__=${MODULE} \
+               -D__variable__=${VARIABLE} \
+               noipv6.m4 > noipv6.tmp
+       mv noipv6.tmp noipv6
+       chmod 755 noipv6
+
+$(MODULE): gaise.lo
+       $(CC) $(CFLAGS) $(SHAREFLAGS) -o $@ $^ $(LIBS)
+
+install: installdirs
+       $(INSTALL) -m 755 noipv6 $(bindir)/noipv6
+       $(INSTALL) -m 644 ${MODULE} $(libdir)/gaise/${MODULE} 
+       #$(INSTALL) -m 644 noipv6.1 $(man1dir)/noipv6
+
+install-strip: install
+
+uninstall:
+       rm -f $(bindir)/noipv6
+       rm -f $(libdir)/gaise/${MODULE}
+       rm -f $(man1dir)/noipv6.1
+       -rmdir $(libdir)/gaise
+
+installdirs:
+       mkdir -p $(libdir)
+       mkdir -p $(libdir)/gaise
+       mkdir -p $(bindir)
+       mkdir -p $(man1dir)
+
+clean:
+       rm -f *.so
+       rm -f *.lo
+       rm -f *.dylib
+       rm -f noipv6
+
+dist:
+       rm -rf gaise-${VERSION}
+       mkdir gaise-${VERSION}
+       cp COPYING Makefile README *.c noipv6.m4 noipv6.1 gaise-${VERSION} 
+       cp defs.Linux gaise-${VERSION}
+       mkdir gaise-${VERSION}/debian
+       cp debian/changelog gaise-${VERSION}/debian
+       cp debian/control debian/copyright gaise-${VERSION}/debian
+       cp debian/rules gaise-${VERSION}/debian
+       chmod +x gaise-${VERSION}/debian/rules
+       tar cf gaise-${VERSION}.tar gaise-${VERSION}
+       gzip -9vf gaise-${VERSION}.tar
+       rm -rf gaise-${VERSION}
+
+distcheck: dist
+       tar xfz gaise-${VERSION}.tar.gz
+       cd gaise-${VERSION} && make check
+       cd gaise-${VERSION} && make install prefix=distcheck/usr/local
+       rm -rf gaise-${VERSION}
+
+%.lo : %.c
+       $(CC) $(CPPFLAGS) $(CFLAGS) -fpic -c $< -o $@
+
+echo-version:
+       @echo "$(VERSION)"
diff --git a/defs.Linux b/defs.Linux
new file mode 100644 (file)
index 0000000..6936d05
--- /dev/null
@@ -0,0 +1,6 @@
+MODULE=gaise.so
+VARIABLE=LD_PRELOAD
+CC=gcc -Wall -W
+CFLAGS=-g
+SHAREFLAGS=-shared
+LIBS=-ldl -lc
diff --git a/gaise.c b/gaise.c
new file mode 100644 (file)
index 0000000..5d5116f
--- /dev/null
+++ b/gaise.c
@@ -0,0 +1,48 @@
+#define _GNU_SOURCE
+#include <netdb.h>
+#include <dlfcn.h>
+
+#ifndef LIBC_GETADDRINFO
+# define LIBC_GETADDRINFO "getaddrinfo"
+#endif
+
+static void addrinfo_remove(int familyToRemove,
+                            struct addrinfo **aip) {
+  struct addrinfo *ai;
+
+  while((ai = *aip)) {
+    if(ai->ai_family == familyToRemove) {
+      *aip = ai->ai_next;
+      ai->ai_next = NULL;
+      freeaddrinfo(ai);
+    } else {
+      aip = &ai->ai_next;
+    }
+  }
+}
+
+int __gaise_getaddrinfo(const char *node, const char *service,
+                        const struct addrinfo *hints,
+                        struct addrinfo **res) {
+  int (*real_getaddrinfo)(const char *node, const char *service,
+                          const struct addrinfo *hints,
+                          struct addrinfo **res);
+  int rc;
+  real_getaddrinfo = dlsym(RTLD_NEXT, LIBC_GETADDRINFO);
+  rc = real_getaddrinfo(node, service, hints, res);
+  if(!rc)
+    addrinfo_remove(AF_INET6, res);
+  return rc;
+}
+
+#if __linux__
+int getaddrinfo(const char *node, const char *service,
+                const struct addrinfo *hints,
+                struct addrinfo **res)
+  __attribute__((weak, alias("__gaise_getaddrinfo")));
+
+int __getaddrinfo(const char *node, const char *service,
+                  const struct addrinfo *hints,
+                  struct addrinfo **res)
+  __attribute__((weak, alias("__gaise_getaddrinfo")));
+#endif
diff --git a/noipv6.m4 b/noipv6.m4
new file mode 100644 (file)
index 0000000..3deece2
--- /dev/null
+++ b/noipv6.m4
@@ -0,0 +1,36 @@
+#! /bin/sh
+
+set -e
+
+case "$1" in
+-h | --help )
+  echo "Gaise VERSION"
+  echo
+  echo "Usage:"
+  echo
+  echo "  noipv6 [--] COMMAND ARGS..."
+  echo
+  exit 0
+  ;;
+-V | --version )
+  echo "Gaise VERSION"
+  exit 0
+  ;;
+-- )
+  shift
+  ;;
+-* )
+  echo "unknown option '$1'" 1>&2
+  exit 1
+  ;;
+esac
+
+if test "x$__variable__" = "x"; then
+  __variable__=pkglibdir/__module__
+else
+  __variable__="pkglibdir/__module__:$__VARIABLE__"
+fi
+export __variable__
+DYLD_FORCE_FLAT_NAMESPACE=yes
+export DYLD_FORCE_FLAT_NAMESPACE
+exec "$@"