]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
SAT>IP server: more NAT addr cleanups, issue #4692
authorJaroslav Kysela <perex@perex.cz>
Tue, 21 Nov 2017 17:22:13 +0000 (18:22 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 21 Nov 2017 17:22:13 +0000 (18:22 +0100)
src/satip/rtsp.c
src/tcp.c
src/tcp.h

index e81ffd9471d235e4dc75bbfb8cfe26aa3d878dbd..f78e9cbbedcbded239a1e24c6c2c6fa1b1126f8b 100644 (file)
@@ -318,24 +318,26 @@ rtsp_parse_args(http_connection_t *hc, char *u)
 /*
  *
  */
-static inline char *
-rtsp_conn_ip(http_connection_t *hc, char *buf, size_t buflen, int *port)
+static inline const char *
+rtsp_conn_ip(http_connection_t *hc, int *port)
 {
-  char *used_ip = rtsp_ip;
+  const char *used_ip = rtsp_ip;
   int used_port = rtsp_port;
+  struct sockaddr_storage self;
 
   if (rtsp_nat_ip[0] == '\0')
     goto end;
-  if (satip_server_conf.satip_nat_name_force ||
-      !ip_check_is_local_address(hc->hc_peer, hc->hc_self)) {
+  self = *hc->hc_self;
+  /* note: call ip_check at first to initialize self (ip any) */
+  if (!ip_check_is_local_address(hc->hc_peer, hc->hc_self, &self) ||
+      satip_server_conf.satip_nat_name_force) {
     used_ip = rtsp_nat_ip;
     if (rtsp_nat_port > 0)
       used_port = rtsp_nat_port;
   }
 
   if (used_ip[0] == '*' || used_ip[0] == '\0' || used_ip == NULL) {
-    tcp_get_str_from_ip(hc->hc_self, buf, buflen);
-    used_ip = buf;
+    used_ip = "127.0.0.1";
   }
 
 end:
@@ -1349,7 +1351,8 @@ rtsp_process_describe(http_connection_t *hc)
   char *u = tvh_strdupa(hc->hc_url);
   session_t *rs;
   htsbuf_queue_t q;
-  char buf[96], buf1[46], *used_ip = NULL;
+  char buf[96];
+  const char *used_ip = NULL;
   int r = HTTP_STATUS_BAD_REQUEST;
   int stream, first = 1, valid, used_port;
 
@@ -1408,7 +1411,7 @@ rtsp_process_describe(http_connection_t *hc)
   http_arg_init(&args);
   if (hc->hc_session)
     http_arg_set(&args, "Session", hc->hc_session);
-  used_ip = rtsp_conn_ip(hc, buf1, sizeof(buf1), &used_port);
+  used_ip = rtsp_conn_ip(hc, &used_port);
   if ((stream > 0) && (used_port != 554))
     snprintf(buf, sizeof(buf), "rtsp://%s:%d/stream=%i", used_ip, used_port, stream);
   else if ((stream > 0) && (used_port == 554))
@@ -1441,7 +1444,8 @@ rtsp_process_play(http_connection_t *hc, int cmd)
 {
   session_t *rs;
   int errcode = HTTP_STATUS_BAD_REQUEST, valid = 0, i, stream, used_port;
-  char buf[256], buf1[46], *u = tvh_strdupa(hc->hc_url), *used_ip = NULL;
+  char buf[256], *u = tvh_strdupa(hc->hc_url);
+  const char *used_ip = NULL;
   http_arg_list_t args;
 
   http_arg_init(&args);
@@ -1499,7 +1503,7 @@ rtsp_process_play(http_connection_t *hc, int cmd)
     snprintf(buf, sizeof(buf), "%d", rs->stream);
     http_arg_set(&args, "com.ses.streamID", buf);
   } else {
-    used_ip = rtsp_conn_ip(hc, buf1, sizeof(buf1), &used_port);
+    used_ip = rtsp_conn_ip(hc, &used_port);
     if (used_port != 554)
       snprintf(buf, sizeof(buf), "url=rtsp://%s:%d/stream=%d", used_ip, used_port, rs->stream);
     else
index 4ac1f52f2bbf1dbed39cd6f4a27d44414c3e4def..ffe30256cbe55bf89569313283d75670f1e86302 100644 (file)
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -77,7 +77,8 @@ socket_set_dscp(int sockfd, uint32_t dscp, char *errbuf, size_t errbufsize)
  */
 int
 ip_check_is_local_address
-  (const struct sockaddr_storage *peer, const struct sockaddr_storage *local)
+  (const struct sockaddr_storage *peer, const struct sockaddr_storage *local,
+   struct sockaddr_storage *used_local)
 {
   struct ifaddrs *iflist, *ifdev = NULL;
   struct sockaddr_storage *ifaddr, *ifnetmask;
@@ -100,6 +101,11 @@ ip_check_is_local_address
     if (ifaddr->ss_family != local->ss_family) continue;
     if (!any_address && !ip_check_equal(ifaddr, local)) continue;
     ret = !!ip_check_in_network_v4(ifaddr, ifnetmask, peer);
+    if (ret) {
+      if (used_local)
+        memcpy(used_local, ifaddr, sizeof(struct sockaddr));
+      break;
+    }
   }
   freeifaddrs(iflist);
   return ret;
index 97d34e6cd485e5d6f7c8d8b134007ede1f3e437a..1c786d0cb86ff3262b367f31428d9baa9aa4a4db 100644 (file)
--- a/src/tcp.h
+++ b/src/tcp.h
@@ -104,7 +104,9 @@ static inline int ip_check_is_any(const struct sockaddr_storage *address)
   { return address->ss_family == AF_INET ? ip_check_is_any_v4(address) :
            (address->ss_family == AF_INET6 ? ip_check_is_any_v6(address) : 0); }
 
-int ip_check_is_local_address(const struct sockaddr_storage *peer, const struct sockaddr_storage *local);
+int ip_check_is_local_address(const struct sockaddr_storage *peer,
+                              const struct sockaddr_storage *local,
+                              struct sockaddr_storage *used_local);
 
 int socket_set_dscp(int sockfd, uint32_t dscp, char *errbuf, size_t errbufsize);