chiark / gitweb /
Integrated `select' handling bits from the background resolver project.
[mLib] / conn.c
diff --git a/conn.c b/conn.c
new file mode 100644 (file)
index 0000000..6ce7769
--- /dev/null
+++ b/conn.c
@@ -0,0 +1,193 @@
+/* -*-c-*-
+ *
+ * $Id: conn.c,v 1.1 1999/05/14 21:01:14 mdw Exp $
+ *
+ * Nonblocking connect handling
+ *
+ * (c) 1999 Mark Wooding
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * mLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: conn.c,v $
+ * Revision 1.1  1999/05/14 21:01:14  mdw
+ * Integrated `select' handling bits from the background resolver project.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "conn.h"
+#include "sel.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @conn_connect@ --- *
+ *
+ * Arguments:  @int fd@ = file descriptor to try to connect
+ *             @unsigned mode@ = what we can do to the file
+ *             @void *p@ = pointer to connection context
+ *
+ * Returns:    ---
+ *
+ * Use:                Handles select results for pending connections.
+ */
+
+static void conn_connect(int fd, unsigned mode, void *p)
+{
+  conn *c = p;
+  struct sockaddr_in sin;
+  int sinsz;
+
+  sinsz = sizeof(sin);
+  if (getpeername(fd, (struct sockaddr *)&sin, &sinsz) < 0) {
+    int err;
+    int errsz = sizeof(err);
+    if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
+      errno = err;
+    c->func(-1, c->p);
+    close(fd);
+  } else
+    c->func(fd, c->p);
+  sel_rmfile(&c->writer);
+}
+
+/* --- @conn_init@ --- *
+ *
+ * Arguments:  @conn *c@ = pointer to connection block
+ *             @sel_state *s@ = pointer to select state to attach to
+ *             @unsigned long saddr@ = source IP address
+ *             @unsigned short sport@ = source port
+ *             @unsigned long daddr@ = destination IP address
+ *             @unsigned short dport@ = destination port
+ *             @void (*func)(int fd, void *p) = handler function
+ *             @void *p@ = argument for the handler function
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets up a nonblocking connect job.
+ */
+
+void conn_init(conn *c, sel_state *s,
+              unsigned long saddr,
+              unsigned short sport,
+              unsigned long daddr,
+              unsigned long dport,
+              void (*func)(int /*fd*/, void */*p*/),
+              void *p)
+{
+  int fd;
+
+  /* --- Make a socket to do the connecting with --- */
+
+  c->writer.fd = -1;
+  if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
+    goto fail;
+
+  /* --- Make the socket nonblocking --- */
+
+  {
+    int f;
+
+    if ((f = fcntl(fd, F_GETFL)) < 0 ||
+       fcntl(fd, F_SETFL, f | O_NONBLOCK))
+      goto fail_close;
+  }
+
+  /* --- Set up the source address and bind it to the socket --- */
+
+  {
+    struct sockaddr_in sin;
+
+    memset(&sin, 0, sizeof(sin));
+    sin.sin_family = AF_INET;
+    sin.sin_addr.s_addr = saddr;
+    sin.sin_port = sport;
+    if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
+      goto fail_close;
+  }
+
+  /* --- Finally, set up the destination and try the connect --- */
+
+  {
+    struct sockaddr_in sin;
+
+    memset(&sin, 0, sizeof(sin));
+    sin.sin_family = AF_INET;
+    sin.sin_addr.s_addr = daddr;
+    sin.sin_port = dport;
+    if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+      if (errno != EINPROGRESS)
+       goto fail_close;
+      c->func = func;
+      c->p = p;
+      sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
+      sel_addfile(&c->writer);
+    } else
+      func(fd, p);
+  }
+
+  /* --- Everything is set up now --- */
+
+  return;
+
+  /* --- Something went pear-shaped --- */
+
+fail_close:
+  close(fd);
+fail:
+  func(-1, p);
+}
+
+/* --- @conn_kill@ --- *
+ *
+ * Arguments:  @conn *c@ = pointer to connection to dispose of
+ *
+ * Returns:    ---
+ *
+ * Use:                Disposes of a connection when it's not wanted any more.
+ */
+
+void conn_kill(conn *c)
+{
+  if (c->writer.fd != -1) {
+    close(c->writer.fd);
+    sel_rmfile(&c->writer);
+    c->writer.fd = -1;
+  }
+}
+
+/*----- That's all, folks -------------------------------------------------*/