瀏覽代碼

sctp: sctp autoclose can now be changed at runtime

sctp_autoclose can now be changed at runtime, but it will affect
only new associations (autoclose for the  pre-existing ones will
not be changed).
E.g.:
$ sercmd cfg.set_now_int sctp autoclose 120
Andrei Pelinescu-Onciul 16 年之前
父節點
當前提交
5b4d228889
共有 4 個文件被更改,包括 62 次插入5 次删除
  1. 7 4
      NEWS
  2. 36 1
      sctp_options.c
  3. 17 0
      sctp_server.c
  4. 2 0
      sctp_server.h

+ 7 - 4
NEWS

@@ -271,11 +271,14 @@ new config variables:
   sctp_socket_rcvbuf = number - size for the sctp socket receive buffer
   sctp_socket_sndbuf = number - size for the sctp socket send buffer
   sctp_autoclose = seconds - number of seconds before autoclosing an idle
-     assocation (default: 180 s).
+                   assocation (default: 180 s).
+                   Can be changed at runtime, but it will affect only new
+                   associations. E.g.:
+                   $ sercmd cfg.set_now_int sctp autoclose 120
   sctp_send_ttl = milliseconds - number of milliseconds before an unsent
-     message/chunk is dropped (default: 32000 ms or 32 s).
-     Can be changed at runtime, e.g.:
-        $ sercmd cfg.set_now_int sctp send_ttl 180000
+                  message/chunk is dropped (default: 32000 ms or 32 s).
+                  Can be changed at runtime, e.g.:
+                  $ sercmd cfg.set_now_int sctp send_ttl 180000
   sctp_send_retries - how many times to attempt re-sending a message on a
                       re-opened association, if the sctp stack did give up
                       sending it (it's not related to sctp protocol level

+ 36 - 1
sctp_options.c

@@ -26,16 +26,29 @@
  */
 
 #include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+#include <netinet/sctp.h>
+#include <errno.h>
 
 #include "sctp_options.h"
 #include "dprint.h"
 #include "cfg/cfg.h"
+#include "socket_info.h"
+#include "sctp_server.h"
 
 struct cfg_group_sctp sctp_default_cfg;
 
 
 
 #ifdef USE_SCTP
+
+
+static int set_autoclose(void* cfg_h, str* gname, str* name, void** val);
+
 /** cfg_group_sctp description (for the config framework). */
 static cfg_def_t sctp_cfg_def[] = {
 	/*   name        , type |input type| chg type, min, max, fixup, proc. cbk.
@@ -44,7 +57,7 @@ static cfg_def_t sctp_cfg_def[] = {
 		"socket receive buffer size (read-only)" },
 	{ "socket_sndbuf", CFG_VAR_INT| CFG_READONLY, 512, 102400, 0, 0,
 		"socket send buffer size (read-only)" },
-	{ "autoclose", CFG_VAR_INT| CFG_READONLY, 1, 1<<30, 0, 0,
+	{ "autoclose", CFG_VAR_INT| CFG_ATOMIC, 1, 1<<30, set_autoclose, 0,
 		"seconds before closing and idle connection (must be non-zero)" },
 	{ "send_ttl", CFG_VAR_INT| CFG_ATOMIC, 0, 1<<30, 0, 0,
 		"milliseconds before aborting a send" },
@@ -123,4 +136,26 @@ int sctp_register_cfg()
 	}
 	return 0;
 }
+
+
+
+static int set_autoclose(void* cfg_h, str* gname, str* name, void** val)
+{
+#ifdef SCTP_AUTOCLOSE
+	int optval;
+	int err;
+	struct socket_info* si;
+	
+	optval=(int)(long)(*val);
+	err=0;
+	for (si=sctp_listen; si; si=si->next){
+		err+=(sctp_sockopt(si, IPPROTO_SCTP, SCTP_AUTOCLOSE, (void*)&optval,
+							sizeof(optval), "cfg: setting SCTP_AUTOCLOSE")<0);
+	}
+	return -(err!=0);
+#else
+	ERR("no SCTP_AUTOCLOSE support, please upgrade your sctp library\n");
+	return -1;
+#endif /* SCTP_AUTOCLOSE */
+}
 #endif /* USE_SCTP */

+ 17 - 0
sctp_server.c

@@ -185,6 +185,23 @@ error:
 
 
 
+/** set a socket option (wrapper over setsockopt).
+  * @param err_prefix - if 0 no error message is printed on failure, if !=0
+  *                     it will be prepended to the error message.
+  * @return 0 on success, -1 on error */
+int sctp_sockopt(struct socket_info* si, int level, int optname, void* optval,
+					socklen_t optlen, char* err_prefix)
+{
+	if (setsockopt(si->socket, level, optname, optval, optlen) ==-1){
+		if (err_prefix)
+			ERR("%s: %s [%d]\n", err_prefix, strerror(errno), errno);
+		return -1;
+	}
+	return 0;
+}
+
+
+
 /* set common (for one to many and one to one) sctp socket options
    tries to ignore non-critical errors (it will only log them), for
    improved portability (for example older linux kernel version support

+ 2 - 0
sctp_server.h

@@ -48,4 +48,6 @@ void sctp_get_info(struct sctp_gen_info* sinf);
 
 void destroy_sctp();
 
+int sctp_sockopt(struct socket_info* si, int level, int optname, void* optval,
+					socklen_t optlen, char* err_prefix);
 #endif /* _sctp_server_h */