PeeringSeeder_admin.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. char* err = "none";
  104. RTypes_Error_t* er =
  105. PeeringSeeder_publicPeer(ctx->sp, peerCode, requestAlloc);
  106. if (er) {
  107. err = Rffi_printError(er, requestAlloc);
  108. }
  109. reply(ctx, txid, requestAlloc, err);
  110. }
  111. static void publicStatus(Gcc_UNUSED Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  112. {
  113. struct Context* ctx = Identity_check((struct Context*) vcontext);
  114. PeeringSeeder_PublicStatus_t* out = NULL;
  115. RTypes_Error_t* er = PeeringSeeder_publicStatus(&out, ctx->sp, requestAlloc);
  116. if (er) {
  117. char* err = Rffi_printError(er, requestAlloc);
  118. reply(ctx, txid, requestAlloc, err);
  119. return;
  120. }
  121. Dict_t* reply = Dict_new(requestAlloc);
  122. if (out->ipv4) {
  123. Dict_putStringC(reply, "ipv4", out->ipv4, requestAlloc);
  124. }
  125. if (out->ipv6) {
  126. Dict_putStringC(reply, "ipv6", out->ipv6, requestAlloc);
  127. }
  128. if (out->peerId) {
  129. Dict_putStringC(reply, "peerId", out->peerId, requestAlloc);
  130. }
  131. if (out->snode) {
  132. Dict_putStringC(reply, "snode", out->snode, requestAlloc);
  133. }
  134. Dict_putStringCC(reply, "error", "none", requestAlloc);
  135. Admin_sendMessage(reply, txid, ctx->admin);
  136. }
  137. void PeeringSeeder_admin_register(PeeringSeeder_t* sp,
  138. struct Admin* admin,
  139. struct Allocator* alloc)
  140. {
  141. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  142. .admin = admin,
  143. .alloc = alloc,
  144. .sp = sp
  145. }));
  146. Identity_set(ctx);
  147. Admin_registerFunction("PeeringSeeder_addDnsSeed", addDnsSeed, ctx, true,
  148. ((struct Admin_FunctionArg[]) {
  149. { .name = "seed", .required = true, .type = "String" },
  150. { .name = "trustSnode", .required = false, .type = "Int" }
  151. }), admin);
  152. Admin_registerFunction("PeeringSeeder_rmDnsSeed", rmDnsSeed, ctx, true,
  153. ((struct Admin_FunctionArg[]) {
  154. { .name = "seed", .required = true, .type = "String" },
  155. }), admin);
  156. Admin_registerFunctionNoArgs("PeeringSeeder_listDnsSeeds", listDnsSeeds, ctx, true, admin);
  157. Admin_registerFunction("PeeringSeeder_publicPeer", publicPeer, ctx, true,
  158. ((struct Admin_FunctionArg[]) {
  159. { .name = "peerID", .required = false, .type = "String" }
  160. }), admin);
  161. Admin_registerFunctionNoArgs("PeeringSeeder_publicStatus", publicStatus, ctx, true, admin);
  162. }