From: Jaroslav Kysela Date: Sat, 9 Apr 2016 17:49:25 +0000 (+0200) Subject: SAT>IP server: fix possible NULL dereference (shuffle the init code) X-Git-Tag: v4.2.1~694 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=71e8b003590eb6efd8ba46d0c5ca506687f6031b;p=thirdparty%2Ftvheadend.git SAT>IP server: fix possible NULL dereference (shuffle the init code) --- diff --git a/src/satip/server.c b/src/satip/server.c index 1585f3ce3..c887179c8 100644 --- a/src/satip/server.c +++ b/src/satip/server.c @@ -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); @@ -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"); @@ -776,10 +812,7 @@ 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); @@ -787,29 +820,11 @@ void satip_server_init(int rtsp_port) 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)