Add the ability to set the socket listen address for the daemon, thus
enabling support to run it within a separate container than postfix.

Patch-Origin: upstream
Patch-Source: https://github.com/roehling/postsrsd/commit/fc2ba94684c99f771ea35f37b29009ba5ec0edb3
---
 postsrsd.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/postsrsd.c b/postsrsd.c
index 5a8bd1c..adf65d3 100644
--- a/postsrsd.c
+++ b/postsrsd.c
@@ -48,7 +48,7 @@
 
 static char *self = NULL;
 
-static size_t bind_service (const char *service, int family, int* socks, size_t max_socks)
+static size_t bind_service (const char *listen_addr, const char *service, int family, int* socks, size_t max_socks)
 {
   struct addrinfo *addr, *it;
   struct addrinfo hints;
@@ -60,7 +60,7 @@ static size_t bind_service (const char *service, int family, int* socks, size_t
   hints.ai_family = family;
   hints.ai_socktype = SOCK_STREAM;
 
-  err = getaddrinfo(NULL, service, &hints, &addr);
+  err = getaddrinfo(listen_addr, service, &hints, &addr);
   if (err != 0) {
     fprintf(stderr, "%s: bind_service(%s): %s\n", self, service, gai_strerror(err));
     return count;
@@ -219,6 +219,7 @@ static void show_help ()
     "   -s<file>       read secrets from file (required)\n"
     "   -d<domain>     set domain name for rewrite (required)\n"
     "   -a<char>       set first separator character which can be one of: -=+ (default: =)\n"
+    "   -l<addr>       set socket listen address (default: 127.0.0.1)\n"
     "   -f<port>       set port for the forward SRS lookup (default: 10001)\n"
     "   -r<port>       set port for the reverse SRS lookup (default: 10002)\n"
     "   -p<pidfile>    write process ID to pidfile (default: none)\n"
@@ -243,7 +244,7 @@ int main (int argc, char **argv)
 {
   int opt, timeout = 1800, family = AF_UNSPEC;
   int daemonize = FALSE;
-  char *forward_service = NULL, *reverse_service = NULL,
+  char *listen_addr = NULL, *forward_service = NULL, *reverse_service = NULL,
        *user = NULL, *domain = NULL, *chroot_dir = NULL;
   char separator = '=';
   char *secret_file = NULL, *pid_file = NULL;
@@ -264,7 +265,7 @@ int main (int argc, char **argv)
   tmp = strrchr(argv[0], '/');
   if (tmp) self = strdup(tmp + 1); else self = strdup(argv[0]);
 
-  while ((opt = getopt(argc, argv, "46d:a:f:r:s:u:t:p:c:X::Dhev")) != -1) {
+  while ((opt = getopt(argc, argv, "46d:a:l:f:r:s:u:t:p:c:X::Dhev")) != -1) {
     switch (opt) {
       case '?':
         return EXIT_FAILURE;
@@ -280,6 +281,9 @@ int main (int argc, char **argv)
       case 'a':
         separator = *optarg;
         break;
+      case 'l':
+        listen_addr = strdup(optarg);
+        break;
       case 'f':
         forward_service = strdup(optarg);
         break;
@@ -404,11 +408,11 @@ int main (int argc, char **argv)
     return EXIT_FAILURE;
   }
   /* Bind ports. May require privileges if the config specifies ports below 1024 */
-  sc = bind_service(forward_service, family, &sockets[socket_count], 4 - socket_count);
+  sc = bind_service(listen_addr, forward_service, family, &sockets[socket_count], 4 - socket_count);
   if (sc == 0) return EXIT_FAILURE;
   while (sc-- > 0) handler[socket_count++] = handle_forward;
   free (forward_service);
-  sc = bind_service(reverse_service, family, &sockets[socket_count], 4 - socket_count);
+  sc = bind_service(listen_addr, reverse_service, family, &sockets[socket_count], 4 - socket_count);
   if (sc == 0) return EXIT_FAILURE;
   while (sc-- > 0) handler[socket_count++] = handle_reverse;
   free (reverse_service);
