Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
SAT>IP server: fix possible NULL dereference (shuffle the init code)
  • Loading branch information
perexg committed Apr 9, 2016
1 parent 1560eff commit 71e8b00
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions src/satip/server.c
Expand Up @@ -33,6 +33,7 @@ static time_t satip_server_bootid;
static int satip_server_rtsp_port;
static int satip_server_rtsp_port_locked;
static upnp_service_t *satips_upnp_discovery;
static pthread_mutex_t satip_server_reinit;

static void satip_server_save(void);

Expand Down Expand Up @@ -742,24 +743,59 @@ const idclass_t satip_server_class = {
/*
*
*/
static void satip_server_save(void)
static void satip_server_init_common(const char *prefix, int announce)
{
struct sockaddr_storage http;
char http_ip[128];
int descramble, rewrite_pmt, muxcnf;
char *nat_ip;

if (http_server_ip == NULL) {
if (tcp_server_bound(http_server, &http, PF_INET) < 0) {
tvherror("satips", "Unable to determine the HTTP/RTSP address");
return;
}
tcp_get_str_from_ip((const struct sockaddr *)&http, http_ip, sizeof(http_ip));
http_server_ip = strdup(http_ip);
http_server_port = ntohs(IP_PORT(http));
}

if (satip_server_rtsp_port <= 0)
return;

descramble = satip_server_conf.satip_descramble;
rewrite_pmt = satip_server_conf.satip_rewrite_pmt;
muxcnf = satip_server_conf.satip_muxcnf;
nat_ip = strdup(satip_server_conf.satip_nat_ip ?: "");

if (announce)
pthread_mutex_unlock(&global_lock);

pthread_mutex_lock(&satip_server_reinit);

satip_server_rtsp_init(http_server_ip, satip_server_rtsp_port, descramble, rewrite_pmt, muxcnf, nat_ip);
satip_server_info(prefix, descramble, muxcnf);

if (announce)
satips_upnp_send_announce();

pthread_mutex_unlock(&satip_server_reinit);

if (announce)
pthread_mutex_lock(&global_lock);

free(nat_ip);
}

/*
*
*/
static void satip_server_save(void)
{
if (!satip_server_rtsp_port_locked) {
satips_rtsp_port(0);
if (satip_server_rtsp_port > 0) {
descramble = satip_server_conf.satip_descramble;
rewrite_pmt = satip_server_conf.satip_rewrite_pmt;
muxcnf = satip_server_conf.satip_muxcnf;
nat_ip = satip_server_conf.satip_nat_ip ? strdup(satip_server_conf.satip_nat_ip) : NULL;
pthread_mutex_unlock(&global_lock);
satip_server_rtsp_init(http_server_ip, satip_server_rtsp_port, descramble, rewrite_pmt, muxcnf, nat_ip);
satip_server_info("re", descramble, muxcnf);
satips_upnp_send_announce();
pthread_mutex_lock(&global_lock);
free(nat_ip);
satip_server_init_common("re", 1);
} else {
pthread_mutex_unlock(&global_lock);
tvhinfo("satips", "SAT>IP Server shutdown");
Expand All @@ -776,40 +812,19 @@ static void satip_server_save(void)

void satip_server_init(int rtsp_port)
{
struct sockaddr_storage http;
char http_ip[128];
int descramble, rewrite_pmt, muxcnf;
char *nat_ip;
pthread_mutex_init(&satip_server_reinit, NULL);

idclass_register(&satip_server_class);

http_server_ip = NULL;
satip_server_bootid = time(NULL);
satip_server_conf.satip_deviceid = 1;

if (tcp_server_bound(http_server, &http, PF_INET) < 0) {
tvherror("satips", "Unable to determine the HTTP/RTSP address");
return;
}
tcp_get_str_from_ip((const struct sockaddr *)&http, http_ip, sizeof(http_ip));
http_server_ip = strdup(http_ip);
http_server_port = ntohs(IP_PORT(http));

satip_server_rtsp_port_locked = rtsp_port > 0;
satip_server_rtsp_port = rtsp_port;
satips_rtsp_port(rtsp_port);

if (satip_server_rtsp_port <= 0)
return;

descramble = satip_server_conf.satip_descramble;
rewrite_pmt = satip_server_conf.satip_rewrite_pmt;
muxcnf = satip_server_conf.satip_muxcnf;
nat_ip = satip_server_conf.satip_nat_ip;

satip_server_rtsp_init(http_server_ip, satip_server_rtsp_port, descramble, rewrite_pmt, muxcnf, nat_ip);

satip_server_info("", descramble, muxcnf);
satip_server_init_common("", 0);
}

void satip_server_register(void)
Expand Down

0 comments on commit 71e8b00

Please sign in to comment.