Skip to content

Commit

Permalink
add support for setting upstream interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stackia committed Jan 18, 2025
1 parent 485b914 commit 08bf0b2
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ return view.extend({
o.value("3", _("rtp2httpd_Debug"));
o.default = "1";

o = s.option(
form.Value,
"upstream-interface",
_("rtp2httpd_Upstream Interface"),
_(
"rtp2httpd_Interface to use for requesting upstream media stream (default none, which follows the routing table)"
)
);
o.datatype = "interface";

o = s.option(form.Value, "maxclients", _("rtp2httpd_Max clients"));
o.datatype = "range(1, 5000)";
o.default = "5";
Expand Down
6 changes: 6 additions & 0 deletions openwrt-support/luci-app-rtp2httpd/po/en/rtp2httpd.po
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ msgstr "Info"
msgid "rtp2httpd_Debug"
msgstr "Debug"

msgid "rtp2httpd_Upstream Interface"
msgstr "Upstream Interface"

msgid "rtp2httpd_Interface to use for requesting upstream media stream (default none, which follows the routing table)"
msgstr "Interface to use for requesting upstream media stream (default none, which follows the routing table)"

msgid "rtp2httpd_Max clients"
msgstr "Max clients allowed"

Expand Down
6 changes: 6 additions & 0 deletions openwrt-support/luci-app-rtp2httpd/po/zh_Hans/rtp2httpd.po
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ msgstr "Info"
msgid "rtp2httpd_Debug"
msgstr "Debug"

msgid "rtp2httpd_Upstream Interface"
msgstr "上游接口"

msgid "rtp2httpd_Interface to use for requesting upstream media stream (default none, which follows the routing table)"
msgstr "用于请求上游媒体流(组播流或 FCC 单播流)的接口(默认不指定,跟随路由表)"

msgid "rtp2httpd_Max clients"
msgstr "最大客户端数"

Expand Down
1 change: 1 addition & 0 deletions openwrt-support/rtp2httpd/files/rtp2httpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ config rtp2httpd
option respawn '1'
option port '5140'
# option verbose '2'
# option upstream-interface 'iptv'
# option maxclients '5'
# option hostname 'somehost.example.com'
# option fcc-nat-traversal '0'
1 change: 1 addition & 0 deletions openwrt-support/rtp2httpd/files/rtp2httpd.init
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ start_instance() {
append_arg "$cfg" maxclients "--maxclients"
append_arg "$cfg" hostname "--hostname"
append_arg "$cfg" fcc-nat-traversal "--fcc-nat-traversal"
append_arg "$cfg" upstream-interface "--upstream-interface"

config_get_bool aux "$cfg" 'respawn' '0'
[ "$aux" = 1 ] && procd_set_param respawn
Expand Down
3 changes: 3 additions & 0 deletions rtp2httpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ verbosity = 2
# 0: disabled, 1: NAT punchhole (maybe unreliable), 2: NAT-PMP
;fcc-nat-traversal = 0

# Interface to use for requesting upstream media stream (default none, which follows the routing table)
;upstream-interface = iptv

[bind]
#List of address and ports to bind to, eg.
;mybox.example.net 8080
Expand Down
32 changes: 31 additions & 1 deletion src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <arpa/inet.h>
#include <ctype.h>
#include <getopt.h>
#include <net/if.h>

#include "rtp2httpd.h"

Expand All @@ -48,6 +49,7 @@ int conf_daemonise;
int conf_udpxy;
int conf_maxclients;
char *conf_hostname = NULL;
struct ifreq conf_upstream_interface;
enum fcc_nat_traversal conf_fcc_nat_traversal;

/* *** */
Expand All @@ -59,6 +61,7 @@ int cmd_maxclients_set;
int cmd_bind_set;
int cmd_fcc_nat_traversal_set;
int cmd_hostname_set;
int cmd_upstream_interface_set;

enum section_e
{
Expand Down Expand Up @@ -357,6 +360,19 @@ void parseGlobalSec(char *line)
}
return;
}
if (strcasecmp("upstream-interface", param) == 0)
{
if (!cmd_upstream_interface_set)
{
strncpy(conf_upstream_interface.ifr_name, value, IFNAMSIZ - 1);
conf_upstream_interface.ifr_ifindex = if_nametoindex(conf_upstream_interface.ifr_name);
}
else
{
logger(LOG_INFO, "Warning: Config file value \"upstream-interface\" ignored. It's already set on CmdLine.\n");
}
return;
}
if (strcasecmp("fcc-nat-traversal", param) == 0)
{
if (!cmd_fcc_nat_traversal_set)
Expand Down Expand Up @@ -500,9 +516,16 @@ void restoreConfDefaults()
if (conf_hostname != NULL)
{
free(conf_hostname);
conf_hostname = NULL;
}
cmd_hostname_set = 0;

if (conf_upstream_interface.ifr_name != NULL)
{
memset(&conf_upstream_interface, 0, sizeof(struct ifreq));
}
cmd_upstream_interface_set = 0;

while (services != NULL)
{
servtmp = services;
Expand Down Expand Up @@ -557,6 +580,7 @@ void usage(FILE *f, char *progname)
"\t-C --noconfig Do not read the default config\n"
"\t-n --fcc-nat-traversal <0/1/2> NAT traversal for FCC media stream, 0=disabled, 1=punchhole, 2=NAT-PMP (default 0)\n"
"\t-H --hostname <hostname> Hostname to check in the Host: HTTP header (default none)\n"
"\t-i --upstream-interface <interface> Interface to use for requesting upstream media stream (default none, which follows the routing table)\n"
"\t default " CONFIGFILE "\n",
prog);
}
Expand Down Expand Up @@ -614,9 +638,10 @@ void parseCmdLine(int argc, char *argv[])
{"noconfig", no_argument, 0, 'C'},
{"fcc-nat-traversal", required_argument, 0, 'n'},
{"hostname", required_argument, 0, 'H'},
{"upstream-interface", required_argument, 0, 'i'},
{0, 0, 0, 0}};

const char shortopts[] = "v:qhdDUm:c:l:n:H:C";
const char shortopts[] = "v:qhdDUm:c:l:n:H:i:C";
int option_index, opt;
int configfile_failed = 1;

Expand Down Expand Up @@ -682,6 +707,11 @@ void parseCmdLine(int argc, char *argv[])
conf_hostname = strdup(optarg);
cmd_hostname_set = 1;
break;
case 'i':
strncpy(conf_upstream_interface.ifr_name, optarg, IFNAMSIZ - 1);
conf_upstream_interface.ifr_ifindex = if_nametoindex(conf_upstream_interface.ifr_name);
cmd_upstream_interface_set = 1;
break;
default:
logger(LOG_FATAL, "Unknown option! %d \n", opt);
usage(stderr, argv[0]);
Expand Down
19 changes: 19 additions & 0 deletions src/httpclients.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>

#include "rtp2httpd.h"

Expand Down Expand Up @@ -391,6 +392,17 @@ static struct services_s *udpxy_parse(char *url)
return &serv;
}

static void bind_to_upstream_interface(int sock)
{
if (conf_upstream_interface.ifr_name != NULL)
{
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)&conf_upstream_interface, sizeof(struct ifreq)) < 0)
{
logger(LOG_ERROR, "Failed to bind to upstream interface %s: %s\n", conf_upstream_interface.ifr_name, strerror(errno));
}
}
}

static int join_mcast_group(struct services_s *service)
{
struct group_req gr;
Expand Down Expand Up @@ -435,6 +447,11 @@ static int join_mcast_group(struct services_s *service)
exit(RETVAL_SOCK_READ_FAILED);
}

if (conf_upstream_interface.ifr_name != NULL)
{
gr.gr_interface = conf_upstream_interface.ifr_ifindex;
}

if (strcmp(service->msrc, "") != 0 && service->msrc != NULL)
{
gsr.gsr_group = gr.gr_group;
Expand Down Expand Up @@ -530,6 +547,7 @@ static uint16_t nat_pmp(uint16_t nport, uint32_t lifetime)
if (get_gw_ip(&gw_addr.sin_addr.s_addr) < 0)
return 0;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bind_to_upstream_interface(sock);
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv);
pk[0] = 0; // Version
pk[1] = 1; // UDP
Expand Down Expand Up @@ -703,6 +721,7 @@ static void startRTPstream(int client, struct services_s *service)
if (!fcc_sock)
{
fcc_sock = socket(AF_INET, service->fcc_addr->ai_socktype, service->fcc_addr->ai_protocol);
bind_to_upstream_interface(fcc_sock);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = 0;
Expand Down
1 change: 1 addition & 0 deletions src/rtp2httpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ extern int conf_udpxy;
extern int conf_maxclients;
extern char *conf_hostname;
extern enum fcc_nat_traversal conf_fcc_nat_traversal;
extern struct ifreq conf_upstream_interface;

/* GLOBALS */
extern struct services_s *services;
Expand Down

0 comments on commit 08bf0b2

Please sign in to comment.