UpperDistributor_admin.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/Dict.h"
  16. #include "benc/List.h"
  17. #include "admin/Admin.h"
  18. #include "memory/Allocator.h"
  19. #include "net/UpperDistributor.h"
  20. #include "net/UpperDistributor_admin.h"
  21. struct Context
  22. {
  23. struct Admin* admin;
  24. struct Allocator* alloc;
  25. struct UpperDistributor* ud;
  26. Identity
  27. };
  28. static void sendError(struct Context* ctx, String* txid, struct Allocator* requestAlloc, char* err)
  29. {
  30. Dict* out = Dict_new(requestAlloc);
  31. Dict_putStringCC(out, "error", err, requestAlloc);
  32. Admin_sendMessage(out, txid, ctx->admin);
  33. }
  34. static void unregisterHandler(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  35. {
  36. struct Context* ctx = Identity_check((struct Context*) vctx);
  37. int64_t* udpPort = Dict_getIntC(args, "udpPort");
  38. char* err;
  39. if (*udpPort < 1 || *udpPort > 65535) {
  40. err = "invalid_udpPort";
  41. } else {
  42. int ret = UpperDistributor_unregisterHandler(ctx->ud, (int) *udpPort);
  43. if (!ret) {
  44. err = "none";
  45. } else if (ret == UpperDistributor_unregisterHandler_NONEXISTANT) {
  46. err = "UpperDistributor_unregisterHandler_NONEXISTANT";
  47. } else {
  48. err = "UNKNOWN";
  49. }
  50. }
  51. sendError(ctx, txid, requestAlloc, err);
  52. }
  53. #define HANDLERS_PER_PAGE 8
  54. static void listHandlers(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  55. {
  56. struct Context* ctx = Identity_check((struct Context*) vctx);
  57. int page = 0;
  58. int64_t* pageP = Dict_getIntC(args, "page");
  59. if (pageP && *pageP > 0) { page = *pageP; }
  60. struct UpperDistributor_Handler* handlers;
  61. int count = UpperDistributor_listHandlers(ctx->ud, &handlers, requestAlloc);
  62. List* handlerList = List_new(requestAlloc);
  63. for (int i = page * HANDLERS_PER_PAGE, j = 0; i < count && j < HANDLERS_PER_PAGE; i++, j++) {
  64. Dict* d = Dict_new(requestAlloc);
  65. Dict_putIntC(d, "udpPort", handlers[i].udpPort, requestAlloc);
  66. Dict_putIntC(d, "type", handlers[i].type, requestAlloc);
  67. List_addDict(handlerList, d, requestAlloc);
  68. }
  69. Dict* out = Dict_new(requestAlloc);
  70. Dict_putListC(out, "handlers", handlerList, requestAlloc);
  71. Dict_putStringCC(out, "error", "none", requestAlloc);
  72. Admin_sendMessage(out, txid, ctx->admin);
  73. }
  74. static void registerHandler(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  75. {
  76. struct Context* ctx = Identity_check((struct Context*) vctx);
  77. int64_t* udpPort = Dict_getIntC(args, "udpPort");
  78. if (*udpPort < 1 || *udpPort > 65535) {
  79. sendError(ctx, txid, requestAlloc, "invalid_udpPort");
  80. return;
  81. }
  82. int64_t* contentTypeP = Dict_getIntC(args, "contentType");
  83. if (*contentTypeP < 0 || *contentTypeP > ContentType_MAX) {
  84. sendError(ctx, txid, requestAlloc, "invalid_contentType");
  85. return;
  86. }
  87. enum ContentType ct = (enum ContentType) *contentTypeP;
  88. int ret = UpperDistributor_registerHandler(ctx->ud, ct, (int) *udpPort);
  89. if (ret < 0) {
  90. char* err = "UNKNOWN";
  91. if (ret == UpperDistributor_registerHandler_PORT_REGISTERED) {
  92. err = "UpperDistributor_registerHandler_PORT_REGISTERED";
  93. }
  94. sendError(ctx, txid, requestAlloc, err);
  95. return;
  96. }
  97. sendError(ctx, txid, requestAlloc, "none");
  98. }
  99. void UpperDistributor_admin_register(struct UpperDistributor* ud,
  100. struct Admin* admin,
  101. struct Allocator* alloc)
  102. {
  103. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  104. .admin = admin,
  105. .alloc = alloc,
  106. .ud = ud
  107. }));
  108. Identity_set(ctx);
  109. Admin_registerFunction("UpperDistributor_unregisterHandler", unregisterHandler, ctx, true,
  110. ((struct Admin_FunctionArg[]) {
  111. { .name = "udpPort", .required = true, .type = "Int" }
  112. }), admin);
  113. Admin_registerFunction("UpperDistributor_listHandlers", listHandlers, ctx, true,
  114. ((struct Admin_FunctionArg[]) {
  115. { .name = "page", .required = false, .type = "Int" }
  116. }), admin);
  117. Admin_registerFunction("UpperDistributor_registerHandler", registerHandler, ctx, true,
  118. ((struct Admin_FunctionArg[]) {
  119. { .name = "contentType", .required = true, .type = "Int" },
  120. { .name = "udpPort", .required = true, .type = "Int" },
  121. }), admin);
  122. }