node_main.c
changeset 26 6d615203d963
parent 24 8307d28329ae
--- a/node_main.c	Thu Jun 26 03:31:59 2008 +0300
+++ b/node_main.c	Sun Jul 06 23:33:24 2008 +0300
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <arpa/inet.h>
-#include <sys/socket.h>
 #include <stdlib.h>
 #include <signal.h>
 #include <string.h>
@@ -15,6 +14,7 @@
 #include <event2/bufferevent.h>
 
 #include "common.h"
+#include "socket.h"
 #include "render.h"
 #include "render_struct.h"
 #include "render_net.h"
@@ -242,22 +242,21 @@
 
 int main (int argc, char** argv) {
     struct event_base *ev_base;
+    struct config_endpoint endpoint;
     int ssock;
-    struct sockaddr_in addr;
-    
+
     // parse arguments
     int opt;
-    const char *port_name = NULL;
+    const char *listen_spec = NULL;
     int enable_debug = 0;
-    unsigned short port;
 
     while ((opt = getopt(argc, argv, "l:")) != -1) {
         switch (opt) {
             case 'l':
-                if (port_name)
+                if (listen_spec)
                     ERROR("only specify -l once");
 
-                port_name = optarg;
+                 listen_spec = optarg;
                 break;
 
             case 'd':
@@ -266,7 +265,7 @@
                 break;
 
             default:
-                err_exit("Usage: %s [-l port] [-d]", argv[0]);
+                err_exit("Usage: %s [-l addr_spec] [-d]", argv[0]);
         }
     }
     
@@ -274,31 +273,19 @@
     if (!(ev_base = event_init()))
         FATAL("event_init");
 
-    // post-process arguments
-    if (!port_name)
-        port_name = RENDER_PORT_NAME;
-
-    if (!(port = atoi(port_name)))
-        ERROR("invalid port: %s", port_name);
-    
     // per default it is enabled
     if (!enable_debug)
         event_set_log_callback(&log_null);
 
     // create the socket
-    if ((ssock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
-        PERROR("socket");
+    endpoint_init(&endpoint, RENDER_PORT);
 
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(port);
-    addr.sin_addr.s_addr = INADDR_ANY;
+    if (endpoint_parse(&endpoint, listen_spec))
+        goto error;
 
-    if (bind(ssock, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == -1)
-        PERROR("bind");
-    
-    if (listen(ssock, 1) == -1)
-        PERROR("listen");
-    
+    if ((ssock = socket_listen(&endpoint, SOCK_STREAM)) == -1)
+        goto error;
+
     // create the listen event
     struct event listen_ev;
 
@@ -311,7 +298,7 @@
     sigpipe_ignore();
     
     // run the libevent mainloop
-    INFO("run: %s:%hu", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
+    INFO("run");
  
     if (event_base_dispatch(ev_base))
         WARNING("event_dispatch");