Add a way to bind to a specific IP address.
[codemux.git] / codemux.c
index 7d55e0d..5957850 100644 (file)
--- a/codemux.c
+++ b/codemux.c
@@ -457,7 +457,7 @@ static int
 FindService(FlowBuf *fb, int *whichService, struct in_addr addr)
 {
   char *end;
-  char *lowerBuf;
+  char lowerBuf[FB_ALLOCSIZE];
   char *hostVal;
   char *buf = fb->fb_buf;
   char orig[256];
@@ -466,7 +466,7 @@ FindService(FlowBuf *fb, int *whichService, struct in_addr addr)
   int i;
   int len;
 #endif
-    
+
   if (strstr(buf, "\n\r\n") == NULL && strstr(buf, "\n\n") == NULL)
     return(FAILURE);
 
@@ -475,7 +475,7 @@ FindService(FlowBuf *fb, int *whichService, struct in_addr addr)
   fb->fb_used += InsertHeader(buf, fb->fb_used + 1, orig);
     
   /* get just the header, so we can work on it */
-  LOCAL_STR_DUP_LOWER(lowerBuf, buf);
+  StrcpyLower(lowerBuf, buf);
   if ((end = strstr(lowerBuf, "\n\r\n")) == NULL)
     end = strstr(lowerBuf, "\n\n");
   *end = '\0';
@@ -503,7 +503,6 @@ FindService(FlowBuf *fb, int *whichService, struct in_addr addr)
            DoesDotlessSuffixMatch(hostVal, 0, serviceSig[i].ss_host)) {
          *whichService = i;
          free(hostVal);
-         /* printf("%s", buf); */
          return(SUCCESS);
        }
       }
@@ -1064,9 +1063,29 @@ main(int argc, char *argv[])
 {
   int lisSock;
   int logFd;
+  int doDaemon = 1;
+  int opt;
+  struct in_addr lisAddress = { .s_addr = htonl(INADDR_ANY) };
+
+  while ((opt = getopt(argc, argv, "dl:")) != -1) {
+    switch (opt) {
+      case 'd':
+       doDaemon = 0;
+       break;
+      case 'l':
+       if (inet_pton(AF_INET, optarg, &lisAddress) <= 0) {
+         fprintf(stderr, "`%s' is not a valid address\n", optarg);
+         exit(-1);
+       }
+       break;
+      default:
+       fprintf(stderr, "Usage: %s [-d] [-l <listening address>]\n", argv[0]);
+       exit(-1);
+    }
+  }
 
   /* do the daemon stuff */
-  if (argc <= 1 || strcmp(argv[1], "-d") != 0) {
+  if (doDaemon) {
     if (InitDaemon() < 0) {
       fprintf(stderr, "codemux daemon_init() failed\n");
       exit(-1);
@@ -1074,7 +1093,8 @@ main(int argc, char *argv[])
   }
 
   /* create the accept socket */
-  if ((lisSock = CreatePrivateAcceptSocket(DEMUX_PORT, TRUE)) < 0) {
+  if ((lisSock = CreatePrivateAcceptSocket(DEMUX_PORT, TRUE,
+                                          &lisAddress)) < 0) {
     fprintf(stderr, "failed creating accept socket\n");
     exit(-1);
   }