throttlectl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <string.h>
  2. #include <netinet/ip6.h>
  3. #include "dhcp6.h"
  4. #include "l2tpns.h"
  5. #include "plugin.h"
  6. #include "control.h"
  7. /* throttle control */
  8. int plugin_api_version = PLUGIN_API_VERSION;
  9. static struct pluginfuncs *f = 0;
  10. char *plugin_control_help[] = {
  11. " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
  12. " unthrottle USER|SID Stop throttling user",
  13. 0
  14. };
  15. int plugin_control(struct param_control *data)
  16. {
  17. sessionidt session;
  18. sessiont *s = 0;
  19. int flag;
  20. char *end;
  21. int rate_in = 0;
  22. int rate_out = 0;
  23. if (data->argc < 1)
  24. return PLUGIN_RET_OK;
  25. if (strcmp(data->argv[0], "throttle") &&
  26. strcmp(data->argv[0], "unthrottle"))
  27. return PLUGIN_RET_OK; // not for us
  28. if (!data->iam_master)
  29. return PLUGIN_RET_NOTMASTER;
  30. flag = data->argv[0][0] == 't';
  31. if (flag)
  32. {
  33. if (data->argc < 2 || data->argc > 6)
  34. {
  35. data->response = NSCTL_RES_ERR;
  36. data->additional = "requires username or session id and optional rate(s)";
  37. return PLUGIN_RET_STOP;
  38. }
  39. }
  40. else
  41. {
  42. if (data->argc != 2)
  43. {
  44. data->response = NSCTL_RES_ERR;
  45. data->additional = "requires username or session id";
  46. return PLUGIN_RET_STOP;
  47. }
  48. }
  49. if (!(session = strtol(data->argv[1], &end, 10)) || *end)
  50. session = f->get_session_by_username(data->argv[1]);
  51. if (session)
  52. s = f->get_session_by_id(session);
  53. if (!s || !s->ip)
  54. {
  55. data->response = NSCTL_RES_ERR;
  56. data->additional = "session not found";
  57. return PLUGIN_RET_STOP;
  58. }
  59. if (flag)
  60. {
  61. rate_in = rate_out = -1;
  62. if (data->argc == 2)
  63. {
  64. unsigned long *rate = f->getconfig("throttle_speed", UNSIGNED_LONG);
  65. rate_in = rate_out = *rate;
  66. }
  67. else if (data->argc == 3)
  68. {
  69. rate_in = rate_out = atoi(data->argv[2]);
  70. }
  71. else
  72. {
  73. int i;
  74. for (i = 2; i < data->argc - 1; i += 2)
  75. {
  76. int len = strlen(data->argv[i]);
  77. if (!strncmp(data->argv[i], "in", len))
  78. {
  79. rate_in = atoi(data->argv[i+1]);
  80. }
  81. else if (!strncmp(data->argv[i], "out", len))
  82. {
  83. rate_out = atoi(data->argv[i+1]);
  84. }
  85. else
  86. {
  87. data->response = NSCTL_RES_ERR;
  88. data->additional = "invalid rate";
  89. return PLUGIN_RET_STOP;
  90. }
  91. }
  92. }
  93. if (!rate_in || !rate_out)
  94. {
  95. data->response = NSCTL_RES_ERR;
  96. data->additional = "invalid rate";
  97. return PLUGIN_RET_STOP;
  98. }
  99. }
  100. if (rate_in != -1 && rate_in == s->throttle_in &&
  101. rate_out != -1 && rate_out == s->throttle_out)
  102. {
  103. data->response = NSCTL_RES_ERR;
  104. data->additional = flag ? "already throttled" : "not throttled";
  105. return PLUGIN_RET_STOP;
  106. }
  107. f->throttle(session, rate_in, rate_out);
  108. f->session_changed(session);
  109. data->response = NSCTL_RES_OK;
  110. data->additional = 0;
  111. return PLUGIN_RET_STOP;
  112. }
  113. int plugin_init(struct pluginfuncs *funcs)
  114. {
  115. return ((f = funcs)) ? 1 : 0;
  116. }