PeeringSeeder_admin.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "subnode/PeeringSeeder_admin.h"
  16. #include "admin/Admin.h"
  17. #include "benc/Dict.h"
  18. #include "rust/cjdns_sys/RTypes.h"
  19. #include "rust/cjdns_sys/Rffi.h"
  20. #include "subnode/PeeringSeeder.h"
  21. #include "util/Gcc.h"
  22. #include "util/Identity.h"
  23. struct Context {
  24. struct Admin* admin;
  25. struct Allocator* alloc;
  26. PeeringSeeder_t* sp;
  27. Identity
  28. };
  29. static void reply(struct Context* ctx, String* txid, struct Allocator* requestAlloc, char* err)
  30. {
  31. Dict* out = Dict_new(requestAlloc);
  32. Dict_putStringCC(out, "error", err, requestAlloc);
  33. Admin_sendMessage(out, txid, ctx->admin);
  34. }
  35. static void addDnsSeed(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  36. {
  37. struct Context* ctx = Identity_check((struct Context*) vcontext);
  38. Rffi_Seeder* rs = PeeringSeeder_getRsSeeder(ctx->sp);
  39. String* seed = Dict_getStringC(args, "seed");
  40. int64_t* trustSnodeP = Dict_getIntC(args, "trustSnode");
  41. bool trustSnode = trustSnodeP && *trustSnodeP != 0;
  42. RTypes_Error_t* err =
  43. Rffi_Seeder_addDnsSeed(
  44. rs,
  45. seed,
  46. trustSnode,
  47. requestAlloc);
  48. if (err) {
  49. reply(ctx, txid, requestAlloc, Rffi_printError(err, requestAlloc));
  50. } else {
  51. reply(ctx, txid, requestAlloc, "none");
  52. }
  53. }
  54. static void rmDnsSeed(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  55. {
  56. struct Context* ctx = Identity_check((struct Context*) vcontext);
  57. Rffi_Seeder* rs = PeeringSeeder_getRsSeeder(ctx->sp);
  58. String* seed = Dict_getStringC(args, "seed");
  59. bool found = false;
  60. RTypes_Error_t* err = Rffi_Seeder_rmDnsSeed(
  61. &found,
  62. rs,
  63. seed,
  64. requestAlloc);
  65. if (err) {
  66. reply(ctx, txid, requestAlloc, Rffi_printError(err, requestAlloc));
  67. } else if (!found) {
  68. reply(ctx, txid, requestAlloc, "seed not in list");
  69. } else {
  70. reply(ctx, txid, requestAlloc, "none");
  71. }
  72. }
  73. static void listDnsSeeds(Gcc_UNUSED Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  74. {
  75. struct Context* ctx = Identity_check((struct Context*) vcontext);
  76. Rffi_Seeder* rs = PeeringSeeder_getRsSeeder(ctx->sp);
  77. RTypes_Seeder_DnsSeeds_t* seeds = NULL;
  78. RTypes_Error_t* err = Rffi_Seeder_listDnsSeeds(
  79. &seeds,
  80. rs,
  81. requestAlloc);
  82. if (err) {
  83. reply(ctx, txid, requestAlloc, Rffi_printError(err, requestAlloc));
  84. return;
  85. }
  86. Dict* seedsDict = Dict_new(requestAlloc);
  87. for (uintptr_t i = 0; i < seeds->len; i++) {
  88. Dict_putIntC(
  89. seedsDict,
  90. seeds->items[i].seed,
  91. seeds->items[i].snode_trusted ? 1 : 0,
  92. requestAlloc);
  93. }
  94. Dict* out = Dict_new(requestAlloc);
  95. Dict_putDictC(out, "seeds", seedsDict, requestAlloc);
  96. Dict_putStringCC(out, "error", "none", requestAlloc);
  97. Admin_sendMessage(out, txid, ctx->admin);
  98. }
  99. static void publicPeer(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  100. {
  101. struct Context* ctx = Identity_check((struct Context*) vcontext);
  102. String* peerCode = Dict_getStringC(args, "peerID");
  103. String* ipv4 = Dict_getStringC(args, "ipv4");
  104. String* ipv6 = Dict_getStringC(args, "ipv6");
  105. char* err = "none";
  106. RTypes_Error_t* er =
  107. PeeringSeeder_publicPeer(ctx->sp, peerCode, ipv4, ipv6, requestAlloc);
  108. if (er) {
  109. err = Rffi_printError(er, requestAlloc);
  110. }
  111. reply(ctx, txid, requestAlloc, err);
  112. }
  113. static void publicStatus(Gcc_UNUSED Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  114. {
  115. struct Context* ctx = Identity_check((struct Context*) vcontext);
  116. PeeringSeeder_PublicStatus_t* out = NULL;
  117. RTypes_Error_t* er = PeeringSeeder_publicStatus(&out, ctx->sp, requestAlloc);
  118. if (er) {
  119. char* err = Rffi_printError(er, requestAlloc);
  120. reply(ctx, txid, requestAlloc, err);
  121. return;
  122. }
  123. Dict_t* reply = Dict_new(requestAlloc);
  124. if (out->ipv4) {
  125. Dict_putStringC(reply, "ipv4", out->ipv4, requestAlloc);
  126. }
  127. if (out->ipv6) {
  128. Dict_putStringC(reply, "ipv6", out->ipv6, requestAlloc);
  129. }
  130. if (out->peerId) {
  131. Dict_putStringC(reply, "peerId", out->peerId, requestAlloc);
  132. }
  133. if (out->snode) {
  134. Dict_putStringC(reply, "snode", out->snode, requestAlloc);
  135. }
  136. Dict_putStringCC(reply, "error", "none", requestAlloc);
  137. Admin_sendMessage(reply, txid, ctx->admin);
  138. }
  139. void PeeringSeeder_admin_register(PeeringSeeder_t* sp,
  140. struct Admin* admin,
  141. struct Allocator* alloc)
  142. {
  143. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  144. .admin = admin,
  145. .alloc = alloc,
  146. .sp = sp
  147. }));
  148. Identity_set(ctx);
  149. Admin_registerFunction("PeeringSeeder_addDnsSeed", addDnsSeed, ctx, true,
  150. ((struct Admin_FunctionArg[]) {
  151. { .name = "seed", .required = true, .type = "String" },
  152. { .name = "trustSnode", .required = false, .type = "Int" }
  153. }), admin);
  154. Admin_registerFunction("PeeringSeeder_rmDnsSeed", rmDnsSeed, ctx, true,
  155. ((struct Admin_FunctionArg[]) {
  156. { .name = "seed", .required = true, .type = "String" },
  157. }), admin);
  158. Admin_registerFunctionNoArgs("PeeringSeeder_listDnsSeeds", listDnsSeeds, ctx, true, admin);
  159. Admin_registerFunction("PeeringSeeder_publicPeer", publicPeer, ctx, true,
  160. ((struct Admin_FunctionArg[]) {
  161. { .name = "peerID", .required = false, .type = "String" },
  162. { .name = "ipv4", .required = false, .type = "String" },
  163. { .name = "ipv6", .required = false, .type = "String" }
  164. }), admin);
  165. Admin_registerFunctionNoArgs("PeeringSeeder_publicStatus", publicStatus, ctx, true, admin);
  166. }