[sane-devel] [PATCH v3 2/2] add option to bind address

Matteo Croce matteo at openwrt.org
Tue Sep 15 10:06:27 UTC 2015


add optional argument '-b' to let saned bind a specific address
---
 doc/saned.man    | 13 ++++++++++---
 frontend/saned.c | 17 ++++++++++++-----
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/doc/saned.man b/doc/saned.man
index 0264c01..6a09d42 100644
--- a/doc/saned.man
+++ b/doc/saned.man
@@ -4,8 +4,11 @@
 saned \- SANE network daemon
 .SH SYNOPSIS
 .B saned
-.B \-ausername
-|
+.B \-ausername
+[
+.B -b
+.I address
+] |
 .B \-dn
 |
 .B \-sn
@@ -24,7 +27,11 @@ flag requests that
 run in standalone daemon mode. In this mode, 
 .B saned
 will detach from the console and run in the background, listening for incoming
-client connections; 
+client connections, if
+.B \-b
+is specified then saned will bind the address
+.I address
+;
 .B inetd
 is not required for
 .B saned
diff --git a/frontend/saned.c b/frontend/saned.c
index 64cd076..73b36de 100644
--- a/frontend/saned.c
+++ b/frontend/saned.c
@@ -247,6 +247,7 @@ static int num_handles;
 static int debug;
 static int run_mode;
 static Handle *handle;
+static char *bind_addr;
 static union
 {
   int w;
@@ -2807,13 +2808,13 @@ do_bindings (int *nfds, struct pollfd **fds)
   hints.ai_flags = AI_PASSIVE;
   hints.ai_socktype = SOCK_STREAM;
 
-  err = getaddrinfo (NULL, SANED_SERVICE_NAME, &hints, &res);
+  err = getaddrinfo (bind_addr, SANED_SERVICE_NAME, &hints, &res);
   if (err)
     {
       DBG (DBG_WARN, "do_bindings: \" %s \" service unknown on your host; you should add\n", SANED_SERVICE_NAME);
       DBG (DBG_WARN, "do_bindings:      %s %d/tcp saned # SANE network scanner daemon\n", SANED_SERVICE_NAME, SANED_SERVICE_PORT);
       DBG (DBG_WARN, "do_bindings: to your /etc/services file (or equivalent). Proceeding anyway.\n");
-      err = getaddrinfo (NULL, SANED_SERVICE_PORT_S, &hints, &res);
+      err = getaddrinfo (bind_addr, SANED_SERVICE_PORT_S, &hints, &res);
       if (err)
 	{
 	  DBG (DBG_ERR, "do_bindings: getaddrinfo() failed even with numeric port: %s\n", gai_strerror (err));
@@ -2891,7 +2892,10 @@ do_bindings (int *nfds, struct pollfd **fds)
   memset (&sin, 0, sizeof (sin));
 
   sin.sin_family = AF_INET;
-  sin.sin_addr.s_addr = INADDR_ANY;
+  if(bind_addr)
+    sin.sin_addr.s_addr = inet_addr(bind_addr);
+  else
+    sin.sin_addr.s_addr = INADDR_ANY;
   sin.sin_port = port;
 
   DBG (DBG_DBG, "do_bindings: socket ()\n");
@@ -3255,7 +3259,7 @@ run_inetd (char *sock)
 
 void usage(char *me, int err)
 {
-  fprintf (stderr, "Usage: %s -ausername | -dn | -sn | -h\n", me);
+  fprintf (stderr, "Usage: %s -ausername [-b address] | -dn | -sn | -h\n", me);
   exit(err);
 }
 
@@ -3278,7 +3282,7 @@ main (int argc, char *argv[])
   numchildren = 0;
   run_mode = SANED_RUN_INETD;
 
-  while((c = getopt(argc, argv, "a::d::s::h")) != -1)
+  while((c = getopt(argc, argv, "a::d::s::b:h")) != -1)
   {
     switch(c) {
     case 'a':
@@ -3298,6 +3302,9 @@ main (int argc, char *argv[])
       break;
     case 'h':
       usage(argv[0], EXIT_SUCCESS);
+    case 'b':
+      bind_addr = optarg;
+      break;
     default:
       usage(argv[0], EXIT_FAILURE);
     }
-- 
2.1.4




More information about the sane-devel mailing list