Add bind address and port to cmdline arguments. Add log of address and port.
authorfatbob313 <martin@fatbob.nu>
Sun, 7 Nov 2010 21:56:13 +0000 (21:56 +0000)
committerfatbob313 <martin@fatbob.nu>
Sun, 7 Nov 2010 21:56:13 +0000 (21:56 +0000)
src/main.c
src/server.c

index a4c1c12b3182045174f8af9e8ee0c106d6e8bcc9..d1351ddc080f9c76a761023f1f43fb2e288975b6 100644 (file)
@@ -51,6 +51,8 @@
 #include "version.h"
 
 char system_string[64], version_string[64];
+int bindport;
+char *bindaddr;
 
 void lockfile(const char *pidfile)
 {
@@ -129,6 +131,8 @@ void printhelp()
        printf("       -p <pidfile>   - Write PID to this file\n");
        printf("       -c <conf file> - Specify configuration file\n");
        printf("       -r             - Run with realtime priority\n");
+       printf("       -a <address>   - Bind to IP address\n");
+       printf("       -b <port>      - Bind to port\n");
        printf("       -h             - Print this help\n");
        exit(0);
 }
@@ -142,7 +146,7 @@ int main(int argc, char **argv)
        struct utsname utsbuf;
        
        /* Arguments */
-       while ((c = getopt(argc, argv, "drp:c:h")) != EOF) {
+       while ((c = getopt(argc, argv, "drp:c:a:b:h")) != EOF) {
                switch(c) {
                case 'c':
                        conffile = optarg;
@@ -150,6 +154,12 @@ int main(int argc, char **argv)
                case 'p':
                        pidfile = optarg;
                        break;
+               case 'a':
+                       bindaddr = optarg;
+                       break;
+               case 'b':
+                       bindport = atoi(optarg);
+                       break;
                case 'd':
                        nodaemon = true;
                        break;
index 752a2006129052f89dec41afede71ddf64ae2a97..d41aab10e231bb64133ec15b6399c7f34ebac924 100644 (file)
@@ -54,6 +54,8 @@
 /* globals */
 int udpsock; 
 bool_t shutdown_server;
+extern char *bindaddr;
+extern int bindport;
 
 void Server_run()
 {
@@ -63,11 +65,27 @@ void Server_run()
        struct sockaddr_in sin;
        int val, clientcount;
        etimer_t janitorTimer;
-
+       unsigned short port;
+       in_addr_t inet_address;
+       
        /* max clients + listen sock + udp sock + client connecting that will be disconnected */
        pollfds = malloc((getIntConf(MAX_CLIENTS) + 3) * sizeof(struct pollfd));
        if (pollfds == NULL)
                Log_fatal("out of memory");
+
+       /* Figure out bind address and port */
+       if (bindport != 0)
+               port = htons(bindport);
+       else
+               port = htons(getIntConf(BINDPORT));
+       
+       if (bindaddr != NULL && inet_addr(bindaddr) != -1)
+               inet_address = inet_addr(bindaddr);
+       else if (inet_addr(getStrConf(BINDADDR)) !=  -1)
+               inet_address = inet_addr(getStrConf(BINDADDR));
+       else
+               inet_address = inet_addr("0.0.0.0");
+       Log_info("Bind to %s:%hu", inet_address == 0 ? "*" : inet_ntoa(*((struct in_addr *)&inet_address)), ntohs(port));
        
        /* Prepare TCP socket */
        memset(&sin, 0, sizeof(sin));
@@ -77,8 +95,9 @@ void Server_run()
        if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) != 0)
                Log_fatal("setsockopt: %s", strerror(errno));
        sin.sin_family = AF_INET;
-       sin.sin_port = htons(getIntConf(BINDPORT));
-       sin.sin_addr.s_addr = inet_addr(getStrConf(BINDADDR)) ==  -1 ? inet_addr("0.0.0.0") : inet_addr(getStrConf(BINDADDR));
+       sin.sin_port = port;    
+       sin.sin_addr.s_addr = inet_address;
+       
        rc = bind(tcpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
        if (rc < 0) Log_fatal("bind: %s", strerror(errno));
        rc = listen(tcpsock, 3);
@@ -92,8 +111,9 @@ void Server_run()
        memset(&sin, 0, sizeof(sin));
        udpsock = socket(PF_INET, SOCK_DGRAM, 0);
        sin.sin_family = AF_INET;
-       sin.sin_port = htons(getIntConf(BINDPORT));
-       sin.sin_addr.s_addr = inet_addr(getStrConf(BINDADDR)) ==  -1 ? inet_addr("0.0.0.0") : inet_addr(getStrConf(BINDADDR));
+       sin.sin_port = port;
+       sin.sin_addr.s_addr = inet_address;
+       
        rc = bind(udpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
        if (rc < 0)
                Log_fatal("bind %d %s: %s", getIntConf(BINDPORT), getStrConf(BINDADDR), strerror(errno));