]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
http: add http_check_local_ip() for SAT>IP server, fixes #4692
authorJaroslav Kysela <perex@perex.cz>
Wed, 22 Nov 2017 08:09:08 +0000 (09:09 +0100)
committerJaroslav Kysela <perex@perex.cz>
Wed, 22 Nov 2017 08:15:26 +0000 (09:15 +0100)
src/http.c
src/http.h
src/satip/rtsp.c

index d54a055255888f873b6ce6bcdf5b628a70d1b48c..3c8d95a0e1217a13b7257dcad8ed31d2a0f73018 100644 (file)
@@ -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);
 }
 
 
index 7f2614863e5202390ce315efda099f47586960b0..a35c6094a8dfbb54f0a75b063af436dea06aefe4 100644 (file)
@@ -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);
 
index bd8c9784264c99323389275ee0d6ebf817ae6abf..7cda53e1ffce49b9de374bcc0b8e8b761adc142f 100644 (file)
@@ -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";