Skip to content

Commit

Permalink
add support for binding to an address and/or an interface
Browse files Browse the repository at this point in the history
-V --bind address: address for listening (all services)
-J --bind-dev device: device name for binding (all services)
    * beware that only works with 1 device. loopback may not work!
  • Loading branch information
André Valentin authored and catalinii committed Jun 22, 2021
1 parent 3f2f042 commit 2a758a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/minisatip.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <getopt.h>
#include <netdb.h>
#include <netinet/in.h>
#include <net/if.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
Expand Down Expand Up @@ -70,6 +71,8 @@ int rtsp, http, si, si1, ssdp1;
#define RRTP_OPT 'r'
#define DISABLEDVB_OPT 'N'
#define DISABLESSDP_OPT 'G'
#define BIND_OPT 'V'
#define BIND_DEV_OPT 'J'
#define HTTPSERVER_OPT 'w'
#define HTTPPORT_OPT 'x'
#define LOG_OPT 'l'
Expand Down Expand Up @@ -127,6 +130,8 @@ static const struct option long_options[] = {
{"adapter-timeout", required_argument, NULL, ADAPTERTIMEOUT_OPT},
{"app-buffer", required_argument, NULL, APPBUFFER_OPT},
{"buffer", required_argument, NULL, DVRBUFFER_OPT},
{"bind", required_argument, NULL, BIND_OPT},
{"bind-dev", required_argument, NULL, BIND_DEV_OPT},
{"clean-psi", no_argument, NULL, CLEANPSI_OPT},
{"delsys", required_argument, NULL, DELSYS_OPT},
{"debug", required_argument, NULL, DEBUG_OPT},
Expand Down Expand Up @@ -496,6 +501,9 @@ Help\n\
* 1 - use demuxX device \n\
* 2 - use dvrX device and additionally capture PSI data from demuxX device \n\
* 3 - use demuxX device and additionally capture PSI data from demuxX device \n\
* -V --bind address: address for listening (all services)\n\
* -J --bind-dev device: device name for binding (all services)\n\
* beware that only works with 1 device. loopback may not work!\n\
\n "
#ifdef AXE
"\
Expand Down Expand Up @@ -678,6 +686,16 @@ void set_options(int argc, char *argv[]) {
break;
}

case BIND_OPT: {
opts.bind = optarg;
break;
}

case BIND_DEV_OPT: {
opts.bind_dev = optarg;
break;
}

case DEBUG_OPT:
case LOG_OPT: {
is_log = 1;
Expand Down Expand Up @@ -1796,15 +1814,25 @@ int main(int argc, char *argv[]) {
}

readBootID();
if ((rtsp = tcp_listen(NULL, opts.rtsp_port, opts.use_ipv4_only)) < 1)
if ((rtsp = tcp_listen(opts.bind, opts.rtsp_port, opts.use_ipv4_only)) < 1)
FAIL("RTSP: Could not listen on port %d", opts.rtsp_port);
if ((http = tcp_listen(NULL, opts.http_port, opts.use_ipv4_only)) < 1)
if ((http = tcp_listen(opts.bind, opts.http_port, opts.use_ipv4_only)) < 1)
FAIL("Could not listen on http port %d", opts.http_port);
if (!opts.disable_ssdp) {
if ((ssdp = udp_bind(NULL, 1900, opts.use_ipv4_only)) < 1)
if ((ssdp = udp_bind(opts.bind, 1900, opts.use_ipv4_only)) < 1)
FAIL("SSDP: Could not bind on udp port 1900");
if ((ssdp1 = udp_bind(opts.disc_host, 1900, 1)) < 1)
FAIL("SSDP: Could not bind on %s udp port 1900", opts.disc_host);
if (opts.bind_dev) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), opts.bind_dev);

This comment has been minimized.

Copy link
@seahawk1986

seahawk1986 Jun 24, 2021

This produces a compiler Error on Ubuntu 20.04:

make -C src
make[2]: Entering directory '/build/minisatip-1.1.11/src'
gcc -g -O2 -fdebug-prefix-map=/build/minisatip-1.1.11=. -fstack-protector-strong -Wformat -Werror=format-security   -DDISABLE_NETCVCLIENT -DDISABLE_T2MI  -c minisatip.c -o ../build/minisatip.o
minisatip.c: In function 'main':
minisatip.c:1829:3: error: format not a string literal and no format arguments [-Werror=format-security]
 1829 |   snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), opts.bind_dev);
      |   ^~~~~~~~
cc1: some warnings being treated as errors
make[2]: *** [Makefile:160: ../build/minisatip.o] Error 1
if (setsockopt(ssdp, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0)
LOG("SSDP: Failed to set SO_BINDTODEVICE to %s", opts.bind_dev);
if (setsockopt(ssdp1, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0)
LOG("SSDP: Failed to set SO_BINDTODEVICE to %s", opts.bind_dev);
LOG("SSDP: Bound to device %s", opts.bind_dev);
}

si = sockets_add(ssdp, NULL, -1, TYPE_UDP, (socket_action)ssdp_reply,
(socket_action)ssdp_byebye,
Expand Down
2 changes: 2 additions & 0 deletions src/minisatip.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct struct_opts {
char *name_app;
char *http_host; // http-server host
char *rtsp_host; // rtsp-server host
char *bind; // bind address
char *bind_dev; // bind device
char *datetime_compile;
time_t start_time;
char *datetime_start;
Expand Down

1 comment on commit 2a758a5

@9000h
Copy link
Collaborator

@9000h 9000h commented on 2a758a5 Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 minisatip.c: In function ‘main’:
minisatip.c:1829:3: warning: format not a string literal and no format arguments [-Wformat-security]
   snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), opts.bind_dev);
   ^~~~~~~~

Please sign in to comment.