yaid.c: Common function for fixing connected sockets.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 20 Oct 2012 12:51:57 +0000 (13:51 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 21 Oct 2012 15:08:58 +0000 (16:08 +0100)
Previously I'd forgotten to drag `out-of-band' data inline, which will
cause a select(2) spin, and to make the client connection be nonblocking.
Put all of the relevant stuff in a utility function.

yaid.c

diff --git a/yaid.c b/yaid.c
index 1355019..bdfdd93 100644 (file)
--- a/yaid.c
+++ b/yaid.c
@@ -165,6 +165,26 @@ static void disconnect_client(struct client *c)
   xfree(c);
 }
 
+static int fix_up_socket(int fd, const char *what)
+{
+  int yes = 1;
+
+  if (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)) {
+    logmsg(0, LOG_ERR, "failed to set %s connection nonblocking: %s",
+          what, strerror(errno));
+    return (-1);
+  }
+
+  if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &yes, sizeof(yes))) {
+    logmsg(0, LOG_ERR,
+          "failed to disable `out-of-band' data on %s connection: %s",
+          what, strerror(errno));
+    return (-1);
+  }
+
+  return (0);
+}
+
 static void done_client_write(int err, void *p)
 {
   struct client *c = p;
@@ -354,12 +374,7 @@ static void proxy_query(struct client *c)
           c->l->ao->name, strerror(errno));
     goto err_0;
   }
-
-  if (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)) {
-    logmsg(&c->q, LOG_ERR, "failed to set %s proxy socket nonblocking: %s",
-          c->l->ao->name, strerror(errno));
-    goto err_1;
-  }
+  if (fix_up_socket(fd, "proxy")) goto err_1;
 
   s = c->q.u.nat;
   s.port = 113;
@@ -548,6 +563,7 @@ static void accept_client(int fd, unsigned mode, void *p)
     }
     return;
   }
+  if (fix_up_socket(sk, "incoming client")) { close(sk); return; }
 
   c = xmalloc(sizeof(*c));
   c->l = l;