noip.c: Hack ioctl(2) as well.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 2 May 2016 12:28:24 +0000 (13:28 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 2 May 2016 23:12:08 +0000 (00:12 +0100)
It appears that Java's network machinery does lots of probing of network
addresses with ioctl(2), and /some/ of these ioctls don't work well with
Unix-domain sockets.  If we see one of these, then make a temporary
Internet socket and do the ioctl on that instead.

This really is quite unpleasant, but it seems to work well enough to
make Gradle work, for example.

noip.c

diff --git a/noip.c b/noip.c
index 2a09c16..b9043f0 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -34,6 +34,7 @@
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
+#include <stdarg.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -125,7 +126,8 @@ static aclnode *connect_real,  **connect_tail = &connect_real;
   _(recvfrom, ssize_t, (int, void *buf, size_t, int,                   \
                        struct sockaddr *from, socklen_t *fromlen))     \
   _(sendmsg, ssize_t, (int, const struct msghdr *, int))               \
-  _(recvmsg, ssize_t, (int, struct msghdr *, int))
+  _(recvmsg, ssize_t, (int, struct msghdr *, int))                     \
+  _(ioctl, int, (int, unsigned long, ...))
 
 /* Function pointers to set up. */
 #define DECL(imp, ret, args) static ret (*real_##imp) args;
@@ -1367,6 +1369,36 @@ int setsockopt(int sk, int lev, int opt, const void *p, socklen_t len)
   return (real_setsockopt(sk, lev, opt, p, len));
 }
 
+int ioctl(int fd, unsigned long op, ...)
+{
+  va_list ap;
+  void *arg;
+  int sk;
+  int rc;
+
+  va_start(ap, op);
+  arg = va_arg(ap, void *);
+
+  switch (op) {
+    case SIOCGIFADDR:
+    case SIOCGIFBRDADDR:
+    case SIOCGIFDSTADDR:
+    case SIOCGIFNETMASK:
+      PRESERVING_ERRNO({
+       if (fixup_real_ip_socket(fd, AF_INET, &sk)) goto real;
+      });
+      rc = real_ioctl(sk, op, arg);
+      PRESERVING_ERRNO({ close(sk); });
+      break;
+    default:
+    real:
+      rc = real_ioctl(fd, op, arg);
+      break;
+  }
+  va_end(ap);
+  return (rc);
+}
+
 /*----- Initialization ----------------------------------------------------*/
 
 /* Clean up the socket directory, deleting stale sockets. */