Review at  https://gerrit.osmocom.org/7257

abisip-find: Add option to bind to a specific source address

When the BTS answers, it uses the src addr used by abisip-find to send
the boardcast packets. This way a different IP than the one
automatically specified by default routing can be used.
An extra benefit: more than one abisip-find process can now be run in
parallel on the same interface.

Change-Id: I6b805f22d261003239d7002d9e568ea4797a2b0b
---
M src/ipaccess/abisip-find.c
1 file changed, 20 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-bsc refs/changes/57/7257/1

diff --git a/src/ipaccess/abisip-find.c b/src/ipaccess/abisip-find.c
index 0f398b4..a4ed93e 100644
--- a/src/ipaccess/abisip-find.c
+++ b/src/ipaccess/abisip-find.c
@@ -39,11 +39,13 @@
 
 static struct {
        const char *ifname;
+       const char *bind_ip;
        int send_interval;
        bool list_view;
        time_t list_view_timeout;
 } cmdline_opts = {
        .ifname = NULL,
+       .bind_ip = NULL,
        .send_interval = 5,
        .list_view = false,
        .list_view_timeout = 10,
@@ -55,6 +57,8 @@
        printf("Usage: abisip-find [-l] [<interface-name>]\n");
        printf("  <interface-name>  Specify the outgoing network interface,\n"
               "                    e.g. 'eth0'\n");
+       printf("  -b --bind-ip <ip> Specify the local IP to bind to,\n"
+              "                    e.g. '192.168.1.10'\n");
        printf("  -i --interval <s> Send broadcast frames every <s> 
seconds.\n");
        printf("  -l --list-view    Instead of printing received responses,\n"
               "                    output a sorted list of currently present\n"
@@ -70,13 +74,14 @@
                int option_index = 0, c;
                static struct option long_options[] = {
                        {"help", 0, 0, 'h'},
+                       {"bind-ip", 1, 0, 'b'},
                        {"send-interval", 1, 0, 'i'},
                        {"list-view", 0, 0, 'l'},
                        {"timeout", 1, 0, 't'},
                        {0, 0, 0, 0}
                };
 
-               c = getopt_long(argc, argv, "hi:lt:",
+               c = getopt_long(argc, argv, "hb:i:lt:",
                                long_options, &option_index);
                if (c == -1)
                        break;
@@ -85,6 +90,9 @@
                case 'h':
                        print_help();
                        exit(EXIT_SUCCESS);
+               case 'b':
+                       cmdline_opts.bind_ip = optarg;
+                       break;
                case 'i':
                        errno = 0;
                        cmdline_opts.send_interval = strtoul(optarg, NULL, 10);
@@ -122,7 +130,7 @@
        }
 }
 
-static int udp_sock(const char *ifname)
+static int udp_sock(const char *ifname, const char *bind_ip)
 {
        int fd, rc, bc = 1;
        struct sockaddr_in sa;
@@ -146,7 +154,15 @@
        memset(&sa, 0, sizeof(sa));
        sa.sin_family = AF_INET;
        sa.sin_port = htons(3006);
-       sa.sin_addr.s_addr = INADDR_ANY;
+       if (bind_ip) {
+               rc = inet_pton(AF_INET, bind_ip, &sa.sin_addr);
+               if (rc != 1) {
+                       fprintf(stderr, "bind ip addr: inet_pton failed, 
returned %d\n", rc);
+                       goto err;
+               }
+       } else {
+               sa.sin_addr.s_addr = INADDR_ANY;
+       }
 
        rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
        if (rc < 0)
@@ -395,7 +411,7 @@
 
        bfd.cb = bfd_cb;
        bfd.when = BSC_FD_READ | BSC_FD_WRITE;
-       bfd.fd = udp_sock(cmdline_opts.ifname);
+       bfd.fd = udp_sock(cmdline_opts.ifname, cmdline_opts.bind_ip);
        if (bfd.fd < 0) {
                perror("Cannot create local socket for broadcast udp");
                exit(1);

-- 
To view, visit https://gerrit.osmocom.org/7257
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b805f22d261003239d7002d9e568ea4797a2b0b
Gerrit-PatchSet: 1
Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pes...@sysmocom.de>

Reply via email to