From: Mark Wooding Date: Sat, 20 Oct 2012 12:51:57 +0000 (+0100) Subject: yaid.c: Common function for fixing connected sockets. X-Git-Tag: 1.0.0~15 X-Git-Url: https://git.distorted.org.uk/~mdw/yaid/commitdiff_plain/95df134c16f744e4ca2f65a0e4823c4a96b8f5b7 yaid.c: Common function for fixing connected sockets. 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. --- diff --git a/yaid.c b/yaid.c index 1355019..bdfdd93 100644 --- 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;