sctp_options.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2008 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * sctp options
  20. */
  21. /*
  22. * History:
  23. * --------
  24. * 2008-08-07 initial version (andrei)
  25. * 2009-05-26 runtime cfg support (andrei)
  26. */
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <netinet/in_systm.h>
  32. #include <netinet/ip.h>
  33. #include <netinet/sctp.h>
  34. #include <errno.h>
  35. #include "sctp_options.h"
  36. #include "dprint.h"
  37. #include "cfg/cfg.h"
  38. #include "socket_info.h"
  39. #include "sctp_server.h"
  40. struct cfg_group_sctp sctp_default_cfg;
  41. #ifdef USE_SCTP
  42. static int set_autoclose(void* cfg_h, str* gname, str* name, void** val);
  43. /** cfg_group_sctp description (for the config framework). */
  44. static cfg_def_t sctp_cfg_def[] = {
  45. /* name , type |input type| chg type, min, max, fixup, proc. cbk.
  46. description */
  47. { "socket_rcvbuf", CFG_VAR_INT| CFG_READONLY, 512, 102400, 0, 0,
  48. "socket receive buffer size (read-only)" },
  49. { "socket_sndbuf", CFG_VAR_INT| CFG_READONLY, 512, 102400, 0, 0,
  50. "socket send buffer size (read-only)" },
  51. { "autoclose", CFG_VAR_INT| CFG_ATOMIC, 1, 1<<30, set_autoclose, 0,
  52. "seconds before closing and idle connection (must be non-zero)" },
  53. { "send_ttl", CFG_VAR_INT| CFG_ATOMIC, 0, 1<<30, 0, 0,
  54. "milliseconds before aborting a send" },
  55. { "send_retries", CFG_VAR_INT| CFG_ATOMIC, 0, MAX_SCTP_SEND_RETRIES, 0, 0,
  56. "re-send attempts on failure" },
  57. {0, 0, 0, 0, 0, 0, 0}
  58. };
  59. void* sctp_cfg; /* sctp config handle */
  60. #endif /* USE_SCTP */
  61. void init_sctp_options()
  62. {
  63. #ifdef USE_SCTP
  64. sctp_default_cfg.so_rcvbuf=0; /* do nothing, use the kernel default */
  65. sctp_default_cfg.so_sndbuf=0; /* do nothing, use the kernel default */
  66. sctp_default_cfg.autoclose=DEFAULT_SCTP_AUTOCLOSE; /* in seconds */
  67. sctp_default_cfg.send_ttl=DEFAULT_SCTP_SEND_TTL; /* in milliseconds */
  68. sctp_default_cfg.send_retries=DEFAULT_SCTP_SEND_RETRIES;
  69. #endif
  70. }
  71. #define W_OPT_NSCTP(option) \
  72. if (sctp_default_cfg.option){\
  73. WARN("sctp_options: " #option \
  74. " cannot be enabled (sctp support not compiled-in)\n"); \
  75. sctp_default_cfg.option=0; \
  76. }
  77. void sctp_options_check()
  78. {
  79. #ifndef USE_SCTP
  80. W_OPT_NSCTP(autoclose);
  81. W_OPT_NSCTP(send_ttl);
  82. W_OPT_NSCTP(send_retries);
  83. #else
  84. if (sctp_default_cfg.send_retries>MAX_SCTP_SEND_RETRIES) {
  85. WARN("sctp: sctp_send_retries too high (%d), setting it to %d\n",
  86. sctp_default_cfg.send_retries, MAX_SCTP_SEND_RETRIES);
  87. sctp_default_cfg.send_retries=MAX_SCTP_SEND_RETRIES;
  88. }
  89. #endif
  90. }
  91. void sctp_options_get(struct cfg_group_sctp *s)
  92. {
  93. #ifdef USE_SCTP
  94. *s=*(struct cfg_group_sctp*)sctp_cfg;
  95. #else
  96. memset(s, 0, sizeof(*s));
  97. #endif /* USE_SCTP */
  98. }
  99. #ifdef USE_SCTP
  100. /** register sctp config into the configuration framework.
  101. * @return 0 on success, -1 on error */
  102. int sctp_register_cfg()
  103. {
  104. if (cfg_declare("sctp", sctp_cfg_def, &sctp_default_cfg, cfg_sizeof(sctp),
  105. &sctp_cfg))
  106. return -1;
  107. if (sctp_cfg==0){
  108. BUG("null sctp cfg");
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. static int set_autoclose(void* cfg_h, str* gname, str* name, void** val)
  114. {
  115. #ifdef SCTP_AUTOCLOSE
  116. int optval;
  117. int err;
  118. struct socket_info* si;
  119. optval=(int)(long)(*val);
  120. err=0;
  121. for (si=sctp_listen; si; si=si->next){
  122. err+=(sctp_sockopt(si, IPPROTO_SCTP, SCTP_AUTOCLOSE, (void*)&optval,
  123. sizeof(optval), "cfg: setting SCTP_AUTOCLOSE")<0);
  124. }
  125. return -(err!=0);
  126. #else
  127. ERR("no SCTP_AUTOCLOSE support, please upgrade your sctp library\n");
  128. return -1;
  129. #endif /* SCTP_AUTOCLOSE */
  130. }
  131. #endif /* USE_SCTP */