From: Jaroslav Kysela Date: Wed, 22 Nov 2017 08:09:08 +0000 (+0100) Subject: http: add http_check_local_ip() for SAT>IP server, fixes #4692 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bafd78560c6fc2a0691c07708c32931bb1cac97b;p=thirdparty%2Ftvheadend.git http: add http_check_local_ip() for SAT>IP server, fixes #4692 --- diff --git a/src/http.c b/src/http.c index d54a05525..3c8d95a0e 100644 --- a/src/http.c +++ b/src/http.c @@ -589,6 +589,20 @@ next: return NULL; } +/** + * + */ +int +http_check_local_ip( http_connection_t *hc ) +{ + if (hc->hc_local_ip == NULL) { + hc->hc_local_ip = malloc(sizeof(*hc->hc_local_ip)); + *hc->hc_local_ip = *hc->hc_self; + hc->hc_is_local_ip = ip_check_is_local_address(hc->hc_peer, hc->hc_self, hc->hc_local_ip) > 0; + } + return hc->hc_is_local_ip; +} + /** * Transmit a HTTP reply */ @@ -1922,6 +1936,7 @@ error: free(hc->hc_nonce); hc->hc_nonce = NULL; + free(hc->hc_local_ip); } diff --git a/src/http.h b/src/http.h index 7f2614863..a35c6094a 100644 --- a/src/http.h +++ b/src/http.h @@ -136,6 +136,7 @@ typedef struct http_connection { char *hc_peer_ipstr; struct sockaddr_storage *hc_self; char *hc_representative; + struct sockaddr_storage *hc_local_ip; pthread_mutex_t *hc_paths_mutex; http_path_list_t *hc_paths; @@ -143,7 +144,6 @@ typedef struct http_connection { char *hc_url; char *hc_url_orig; - int hc_keep_alive; htsbuf_queue_t hc_reply; @@ -166,13 +166,16 @@ typedef struct http_connection { char *hc_nonce; access_t *hc_access; - struct config_head *hc_user_config; - - int hc_no_output; - int hc_shutdown; + /* RTSP */ uint64_t hc_cseq; char *hc_session; + /* Flags */ + uint8_t hc_keep_alive; + uint8_t hc_no_output; + uint8_t hc_shutdown; + uint8_t hc_is_local_ip; /*< a connection from the local network */ + /* Support for HTTP POST */ char *hc_post_data; @@ -264,6 +267,8 @@ void http_serve_requests(http_connection_t *hc); void http_cancel(void *opaque); +int http_check_local_ip(http_connection_t *hc); + typedef int (http_callback_t)(http_connection_t *hc, const char *remain, void *opaque); diff --git a/src/satip/rtsp.c b/src/satip/rtsp.c index bd8c97842..7cda53e1f 100644 --- a/src/satip/rtsp.c +++ b/src/satip/rtsp.c @@ -92,6 +92,14 @@ static pthread_mutex_t rtsp_lock; static void rtsp_close_session(session_t *rs); static void rtsp_free_session(session_t *rs); +/* + * + */ +static inline int rtsp_is_nat_active(void) +{ + return rtsp_nat_ip[0] != '\0'; +} + /* * */ @@ -279,9 +287,10 @@ rtsp_check_urlbase(char *u) if (strcmp(u, rtsp_ip)) { if (rtsp_nat_ip == NULL) return NULL; - if (rtsp_nat_ip[0] != '*') - if (rtsp_nat_ip[0] == '\0' || strcmp(u, rtsp_nat_ip)) + if (rtsp_is_nat_active()) { + if (rtsp_nat_ip[0] != '*' && strcmp(u, rtsp_nat_ip)) return NULL; + } } return p ? p + 1 : u + strlen(u); } @@ -323,13 +332,11 @@ rtsp_conn_ip(http_connection_t *hc, char *buf, size_t buflen, int *port) { const char *used_ip = rtsp_ip; int used_port = rtsp_port, local; - struct sockaddr_storage self; - if (rtsp_nat_ip[0] == '\0') + if (!rtsp_is_nat_active()) goto end; - self = *hc->hc_self; - /* note: call ip_check at first to initialize self (ip any) */ - local = ip_check_is_local_address(hc->hc_peer, hc->hc_self, &self); + /* note: it initializes hc->hc_local_ip, too */ + local = http_check_local_ip(hc) > 0; if (local || satip_server_conf.satip_nat_name_force) { used_ip = rtsp_nat_ip; if (rtsp_nat_port > 0) @@ -338,7 +345,7 @@ rtsp_conn_ip(http_connection_t *hc, char *buf, size_t buflen, int *port) if (used_ip[0] == '*' || used_ip[0] == '\0' || used_ip == NULL) { if (local) { - tcp_get_str_from_ip(&self, buf, buflen); + tcp_get_str_from_ip(hc->hc_local_ip, buf, buflen); used_ip = buf; } else { used_ip = "127.0.0.1";