Handle tids connections from rudimentary event loop in main().
authorJennifer Richards <jennifer@painless-security.com>
Tue, 17 May 2016 23:03:07 +0000 (19:03 -0400)
committerJennifer Richards <jennifer@painless-security.com>
Tue, 17 May 2016 23:03:07 +0000 (19:03 -0400)
Has debugging code in place, not ready for release.

include/trust_router/tid.h
tid/example/tids_main.c
tid/tids.c

index 4d245b8..d87e0e0 100644 (file)
@@ -148,6 +148,10 @@ TR_EXPORT TIDS_INSTANCE *tids_create (void);
 TR_EXPORT int tids_start (TIDS_INSTANCE *tids, TIDS_REQ_FUNC *req_handler,
                          tids_auth_func *auth_handler, const char *hostname, 
                          unsigned int port, void *cookie);
+TR_EXPORT int tids_get_listener (TIDS_INSTANCE *tids, TIDS_REQ_FUNC *req_handler,
+                         tids_auth_func *auth_handler, const char *hostname, 
+                         unsigned int port, void *cookie);
+TR_EXPORT int tids_accept(TIDS_INSTANCE *tids, int listen);
 TR_EXPORT int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp);
 TR_EXPORT int tids_send_err_response (TIDS_INSTANCE *tids, TID_REQ *req, const char *err_msg);
 TR_EXPORT void tids_destroy (TIDS_INSTANCE *tids);
index 89b5d52..29fa9b5 100644 (file)
@@ -38,6 +38,7 @@
 #include <talloc.h>
 #include <sqlite3.h>
 #include <argp.h>
+#include <poll.h>
 
 #include <tr_debug.h>
 #include <tid_internal.h>
@@ -345,9 +346,10 @@ int main (int argc,
           char *argv[]) 
 {
   TIDS_INSTANCE *tids;
-  int rc = 0;
   TR_NAME *gssname = NULL;
   struct cmdline_args opts={NULL};
+  int tids_socket=-1;
+  struct pollfd *poll_fds=NULL;
 
   /* parse the command line*/
   argp_parse(&argp, argc, argv, 0, 0, &opts);
@@ -380,10 +382,32 @@ int main (int argc,
 
   tids->ipaddr = opts.ip_address;
 
-  /* Start-up the server, won't return unless there is an error. */
-  rc = tids_start(tids, &tids_req_handler , auth_handler, opts.hostname, TID_PORT, gssname);
-  
-  tr_crit("Error in tids_start(), rc = %d. Exiting.", rc);
+  /* get listener for tids port */
+  tids_socket = tids_get_listener(tids, &tids_req_handler , auth_handler, opts.hostname, TID_PORT, gssname);
+
+  poll_fds=malloc(sizeof(*poll_fds));
+  if (poll_fds == NULL) {
+    tr_crit("Could not allocate event polling list, exiting.");
+    return 1;
+  }
+
+  poll_fds[0].fd=tids_socket;
+  poll_fds[0].events=POLLIN; /* poll on ready for reading */
+  poll_fds[0].revents=0; 
+
+  /* main event loop */
+  while (1) {
+    if(poll(poll_fds, 1, 1000) > 0) {
+      if (poll_fds[0].revents & POLLIN) {
+        if (0 != tids_accept(tids, tids_socket)) {
+          tr_err("Error handling tids request.");
+        }
+      }
+    } else {
+      printf("Idle..."); fflush(stdout);
+    }
+
+  }
 
   /* Clean-up the TID server instance */
   tids_destroy(tids);
index eb081dd..2ebe887 100644 (file)
@@ -35,6 +35,7 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
@@ -372,6 +373,81 @@ TIDS_INSTANCE *tids_create (void)
   return tids;
 }
 
+/* Get a listener for tids requests, returns its socket fd. Accept
+ * connections with tids_accept() */
+int tids_get_listener(TIDS_INSTANCE *tids, 
+                      TIDS_REQ_FUNC *req_handler,
+                      tids_auth_func *auth_handler,
+                      const char *hostname,
+                      unsigned int port,
+                      void *cookie)
+{
+  int listen = -1;
+
+  tids->tids_port = port;
+  if (0 > (listen = tids_listen(tids, port))) {
+    char errbuf[256];
+    if (0 == strerror_r(errno, errbuf, 256)) {
+      tr_debug("tids_get_listener: Error opening port %d: %s.", port, errbuf);
+    } else {
+      tr_debug("tids_get_listener: Unknown error openining port %d.", port);
+    }
+  } 
+
+  if (listen > 0) {
+    /* opening port succeeded */
+    tr_debug("tids_get_listener: Opened port %d.", port);
+    
+    /* make this socket non-blocking */
+    if (0 != fcntl(listen, F_SETFD, O_NONBLOCK)) {
+      tr_debug("tids_get_listener: Error setting O_NONBLOCK.");
+      close(listen);
+      listen=-1;
+    }
+  }
+
+  if (listen > 0) {
+    /* store the caller's request handler & cookie */
+    tids->req_handler = req_handler;
+    tids->auth_handler = auth_handler;
+    tids->hostname = hostname;
+    tids->cookie = cookie;
+  }
+
+  return listen;
+}
+
+/* Accept and process a connection on a port opened with tids_get_listener() */
+int tids_accept(TIDS_INSTANCE *tids, int listen)
+{
+  int conn=-1;
+  int pid=-1;
+
+  if (0 > (conn = accept(listen, NULL, NULL))) {
+    perror("Error from TIDS Server accept()");
+    return 1;
+  }
+
+  if (0 > (pid = fork())) {
+    perror("Error on fork()");
+    return 1;
+  }
+
+  if (pid == 0) {
+    close(listen);
+    tids_handle_connection(tids, conn);
+    close(conn);
+    exit(0); /* exit to kill forked child process */
+  } else {
+    close(conn);
+  }
+
+  /* clean up any processes that have completed  (TBD: move to main loop?) */
+  while (waitpid(-1, 0, WNOHANG) > 0);
+
+  return 0;
+}
+
 /* Process tids requests forever. Should not return except on error. */
 int tids_start (TIDS_INSTANCE *tids, 
                TIDS_REQ_FUNC *req_handler,