Factor out identical tids_listen/trps_listen functions into shared copy
[trust_router.git] / tid / tids.c
index c1c9bcb..0cd5cf9 100644 (file)
@@ -32,7 +32,6 @@
  *
  */
 
-#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -41,7 +40,7 @@
 #include <errno.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
-#include <netinet/in.h>
+#include <netdb.h>
 #include <jansson.h>
 #include <talloc.h>
 #include <poll.h>
@@ -49,6 +48,7 @@
 #include <gsscon.h>
 #include <tr_debug.h>
 #include <tr_msg.h>
+#include <tr_socket.h>
 
 static TID_RESP *tids_create_response (TIDS_INSTANCE *tids, TID_REQ *req) 
 {
@@ -84,83 +84,6 @@ cleanup:
   return resp;
 }
 
-static int tids_listen(TIDS_INSTANCE *tids, int port, int *fd_out, size_t max_fd) 
-{
-  int rc = 0;
-  int conn = -1;
-  int optval = 1;
-  struct addrinfo *ai=NULL;
-  struct addrinfo *ai_head=NULL;
-  struct addrinfo hints={.ai_flags=AI_PASSIVE,
-                         .ai_family=AF_UNSPEC,
-                         .ai_socktype=SOCK_STREAM,
-                         .ai_protocol=IPPROTO_TCP};
-  char *port_str=NULL;
-  size_t n_opened=0;
-
-  tr_debug("tids_listen: started!");
-  port_str=talloc_asprintf(NULL, "%d", port);
-  if (port_str==NULL) {
-    tr_debug("tids_listen: unable to allocate port.");
-    return -1;
-  }
-
-  tr_debug("getaddrinfo()=%d", getaddrinfo(NULL, port_str, &hints, &ai_head));
-  talloc_free(port_str);
-  tr_debug("tids_listen: got address info");
-
-  /* TODO: listen on all ports */
-  for (ai=ai_head,n_opened=0; (ai!=NULL)&&(n_opened<max_fd); ai=ai->ai_next) {
-    if (0 > (conn = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))) {
-      tr_debug("tids_listen: unable to open socket.");
-      continue;
-    }
-
-    optval=1;
-    if (0!=setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)))
-      tr_debug("tids_listen: unable to set SO_REUSEADDR."); /* not fatal? */
-
-    if (ai->ai_family==AF_INET6) {
-      /* don't allow IPv4-mapped IPv6 addresses (per RFC4942, not sure
-       * if still relevant) */
-      if (0!=setsockopt(conn, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))) {
-        tr_debug("tids_listen: unable to set IPV6_V6ONLY. Skipping interface.");
-        close(conn);
-        continue;
-      }
-    }
-
-    rc=bind(conn, ai->ai_addr, ai->ai_addrlen);
-    if (rc<0) {
-      tr_debug("tids_listen: unable to bind to socket.");
-      close(conn);
-      continue;
-    }
-
-    if (0>listen(conn, 512)) {
-      tr_debug("tids_listen: unable to listen on bound socket.");
-      close(conn);
-      continue;
-    }
-
-    /* ok, this one worked. Save it */
-    fd_out[n_opened++]=conn;
-  }
-  freeaddrinfo(ai_head);
-
-  if (n_opened==0) {
-    tr_debug("tids_listen: no addresses available for listening.");
-    return -1;
-  }
-
-  tr_debug("tids_listen: TRP Server listening on port %d on %d socket%s",
-           port,
-           n_opened,
-           (n_opened==1)?"":"s");
-
-  return n_opened;
-}
-
 /* returns EACCES if authorization is denied */
 static int tids_auth_cb(gss_name_t clientName, gss_buffer_t displayName,
                        void *data)
@@ -430,7 +353,8 @@ int tids_get_listener(TIDS_INSTANCE *tids,
   size_t ii=0;
 
   tids->tids_port = port;
-  n_fd=tids_listen(tids, port, fd_out, max_fd);
+  n_fd=listen_on_all_addrs(port, fd_out, max_fd);
+
   if (n_fd<=0)
     tr_err("tids_get_listener: Error opening port %d");
   else {