Browse code

implement listen only on a specific IP

Dario Rodriguez authored on 15/07/2014 11:17:20
Showing 1 changed files
... ...
@@ -50,17 +50,21 @@ wk_action http_newcomment(wk *web, int connid, wk_uri *uri, void *userptr,char *
50 50
 int
51 51
 main(int argc, char *argv[])
52 52
 {
53
-        int port;
54 53
         int timeout=500;
55 54
         int serverfd;
56 55
         kakumei *ka,kastore;
57 56
         memset(&kastore,0,sizeof(kastore));
58 57
         ka=&kastore;
58
+        char *hostnameport;
59
+        char hostname[128];
60
+        char *host;
61
+        long hostsize;
62
+        char *ptr,*sep;
59 63
         if(argc!=2) {
60
-                printf("Syntax: %s port\n",argv[0]);
64
+                printf("Syntax: %s [ip:]port\n",argv[0]);
61 65
                 return(1);
62 66
         }
63
-        port=atoi(argv[1]);
67
+        hostnameport=argv[1];
64 68
         if(kaconfig_exists(CONFIGFILE)!=0) {
65 69
                 log_setlogfile(CFLOGFILE);
66 70
                 log_write("INIT","Config file not found, writing default file %s",CONFIGFILE);
... ...
@@ -76,7 +80,20 @@ main(int argc, char *argv[])
76 80
                 log_write("INIT","ERROR: insufficient memory");
77 81
                 return(1);
78 82
         }
79
-        if((serverfd=ipv4_server(port))==-1) {
83
+        if((sep=strchr(hostnameport,':'))!=NULL) {
84
+                serverfd=-1;
85
+                strncpy(hostname,hostnameport,sizeof(hostname));
86
+                hostname[sizeof(hostname)-1]='\0';
87
+                if((ptr=strchr(hostname,':'))!=NULL)
88
+                        *ptr='\0';
89
+                if((host=ipv4_genip(hostname,&hostsize))!=NULL) {
90
+                        serverfd=ipv4_serverbinded(host,hostsize,atoi(sep+1));
91
+                        free(host),host=NULL;
92
+                }
93
+        } else {
94
+                serverfd=ipv4_server(atoi(hostnameport));
95
+        }
96
+        if(serverfd==-1) {
80 97
                 sselect_free(ka->ssel),ka->ssel=NULL;
81 98
                 log_write("INIT","ERROR: couldn't listen on port");
82 99
                 return(2);