Feature #4735

SAT>IP server: add configuration to limit maximal count of sessions and user connections

Added by Mono Polimorph 22 days ago. Updated 15 days ago.

Status:FixedStart date:2017-11-22
Priority:NormalDue date:
Assignee:Jaroslav Kysela% Done:

100%

Category:SAT>IP
Target version:4.4

Description

Hi Jaroslav,

Please, can you help me to complete this code?

diff --git a/src/satip/server.c b/src/satip/server.c
index 57d8487..84e9b17 100644
--- a/src/satip/server.c
+++ b/src/satip/server.c
@@ -873,6 +873,25 @@ const idclass_t satip_server_class = {
       .group  = 4,
     },
     {
+      .type   = PT_INT,
+      .id     = "satip_max_sessions",
+      .name   = N_("Max Sessions"),
+      .desc   = N_("The maximum number of active RTSP sessions."),
+      .off    = offsetof(struct satip_server_conf, satip_max_sessions),
+      .opts   = PO_ADVANCED,
+      .group  = 4,
+    },
+    {
+      .type   = PT_INT,
+      .id     = "satip_max_user_connections",
+      .name   = N_("Max User connections"),
+      .desc   = N_("The maximum concurrent RTSP connections from the " 
+                   "same IP address."),
+      .off    = offsetof(struct satip_server_conf, satip_max_user_connections),
+      .opts   = PO_ADVANCED,
+      .group  = 4,
+    },
+    {
       .type   = PT_BOOL,
       .id     = "satip_rewrite_pmt",
       .name   = N_("Rewrite PMT"),
diff --git a/src/satip/server.h b/src/satip/server.h
index f72c01a..5b9ecbd 100644
--- a/src/satip/server.h
+++ b/src/satip/server.h
@@ -63,6 +63,8 @@ struct satip_server_conf {
   int satip_dvbc2;
   int satip_atsc_t;
   int satip_atsc_c;
+  int satip_max_sessions;
+  int satip_max_user_connections;
   char *satip_nat_ip;
   int satip_nat_rtsp;
   int satip_nat_name_force;
The idea is quite simple:
  1. Limit the total number of active sessions. The SAT>IP config has the exported tuner values. However, this is only informational data. No limit is applyed, except the real number of attached tuners. However, when using IPTV inputs, the number of "active" tuners is undefined. I like to use this value for not export more than X sessions at a time.
  2. The other value is for limit the number of concurrent RTSP connections from the same IP. Some wrong SAT>IP clients not disconnect the RTSP socket. Or they open more than one session. I like to limit this value.

So, I'm sure you know where to do the check. The code I feel is around two if's, two increments and some decrement.
Please, can you do that?
Thank you!

SAT-IP-Max-Sessions-Max-User-connections.diff Magnifier (3.17 KB) Mono Polimorph, 2017-11-28 11:42

Associated revisions

Revision 86d8ccc9
Added by Jaroslav Kysela 15 days ago

SAT>IP server: add configuration to limit maximal count of sessions and user connections, fixes #4735

From: Mono Polimorph

History

#1 Updated by Mono Polimorph 21 days ago

Hi Jaroslav,

Please can you help me to complete this patch?
I only need to know where to do the checks... ;)

Thank you for your effort!

#2 Updated by Mono Polimorph 19 days ago

Hi Jaroslav,

No help on this?

#3 Updated by Jaroslav Kysela 18 days ago

rtsp_new_session() in src/satip/rtsp.c

#4 Updated by Mono Polimorph 16 days ago

Hi Jaroslav,

Thank you for pointing to the correct function! ;)

Here the full patch:

diff --git a/src/satip/rtsp.c b/src/satip/rtsp.c
index 7cda53e..de1ebbe 100644
--- a/src/satip/rtsp.c
+++ b/src/satip/rtsp.c
@@ -183,8 +183,28 @@ result:
 static struct session *
 rtsp_new_session(const char *ipstr, int delsys, uint32_t nsession, int session)
 {
-  struct session *rs = calloc(1, sizeof(*rs));
+  struct session *rs = NULL;
+  int count_s = satip_server_conf.satip_max_sessions;
+  int count_u = satip_server_conf.satip_max_user_connections;
+
+  if (count_s > 0 || count_u > 0)
+  TAILQ_FOREACH(rs, &rtsp_sessions, link) {
+    count_s--;
+    if (count_s == 0) {
+      tvhnotice(LS_SATIPS, "Max number (%i) of active RTSP sessions reached.",
+                satip_server_conf.satip_max_sessions);
+      return NULL;
+    }
+    if (strcmp(rs->peer_ipstr, strdup(ipstr)) == 0)
+      count_u--;
+    if (count_u == 0) {
+      tvhnotice(LS_SATIPS, "Max number (%i) of active RTSP sessions per user (IP: %s).",
+                satip_server_conf.satip_max_user_connections, strdup(ipstr));
+      return NULL;
+    }
+  }

+  rs = calloc(1, sizeof(*rs));
   if (rs == NULL)
     return NULL;

@@ -1024,12 +1044,14 @@ rtsp_parse_cmd
   if (cmd == RTSP_CMD_SETUP) {
     if (!rs) {
       rs = rtsp_new_session(hc->hc_peer_ipstr, msys, 0, -1);
+      if (rs == NULL) goto end;
       if (delsys == DVB_SYS_NONE) goto end;
       if (msys == DVB_SYS_NONE) goto end;
       if (!(*valid)) goto end;
       alloc_stream_id = 1;
     } else if (stream != rs->stream) {
       rs = rtsp_new_session(hc->hc_peer_ipstr, msys, rs->nsession, stream);
+      if (rs == NULL) goto end;
       if (delsys == DVB_SYS_NONE) goto end;
       if (msys == DVB_SYS_NONE) goto end;
       if (!(*valid)) goto end;
diff --git a/src/satip/server.c b/src/satip/server.c
index 57d8487..ba05491 100644
--- a/src/satip/server.c
+++ b/src/satip/server.c
@@ -873,6 +873,26 @@ const idclass_t satip_server_class = {
       .group  = 4,
     },
     {
+      .type   = PT_INT,
+      .id     = "satip_max_sessions",
+      .name   = N_("Max Sessions"),
+      .desc   = N_("The maximum number of active RTSP sessions " 
+                   "(if 0 no limit)."),
+      .off    = offsetof(struct satip_server_conf, satip_max_sessions),
+      .opts   = PO_ADVANCED,
+      .group  = 4,
+    },
+    {
+      .type   = PT_INT,
+      .id     = "satip_max_user_connections",
+      .name   = N_("Max User connections"),
+      .desc   = N_("The maximum concurrent RTSP connections from the " 
+                   "same IP address (if 0 no limit)."),
+      .off    = offsetof(struct satip_server_conf, satip_max_user_connections),
+      .opts   = PO_ADVANCED,
+      .group  = 4,
+    },
+    {
       .type   = PT_BOOL,
       .id     = "satip_rewrite_pmt",
       .name   = N_("Rewrite PMT"),
diff --git a/src/satip/server.h b/src/satip/server.h
index f72c01a..5b9ecbd 100644
--- a/src/satip/server.h
+++ b/src/satip/server.h
@@ -63,6 +63,8 @@ struct satip_server_conf {
   int satip_dvbc2;
   int satip_atsc_t;
   int satip_atsc_c;
+  int satip_max_sessions;
+  int satip_max_user_connections;
   char *satip_nat_ip;
   int satip_nat_rtsp;
   int satip_nat_name_force;

This patch implements in the correct way both functions: Max Sessions, and Max per user. Note that "0" indicates "no limit".

Some comments about the patch:

  1. The "if (count_s > 0 || count_u > 0)" is for optimize: not loop over the list if no limits.
  2. The strict "if == 0" is for considering cases with no limit (if "0" in the configuration then the value will be negative after one loop).
  3. And I fixed a NULL pointer error in function "rtsp_parse_cmd()" as if the rtsp_new_session() returns NULL (no new session created) then the code needs to exit.

I hope you agree with it and commit it!
Regards.

#5 Updated by Jaroslav Kysela 15 days ago

  • Target version set to 4.4

#6 Updated by Jaroslav Kysela 15 days ago

  • Subject changed from SAT>IP: Max Sessions & Max User connections to SAT>IP server: add configuration to limit maximal count of sessions and user connections

#7 Updated by Jaroslav Kysela 15 days ago

  • Status changed from New to Fixed
  • % Done changed from 0 to 100

#8 Updated by Mono Polimorph 15 days ago

Jaroslav Kysela wrote:

Applied in changeset tvheadend|86d8ccc9701101d55b46efff9f75e2d6685e9e29.

Thank you! And good optimization (--precondition)! ;)

Also available in: Atom PDF