SupernodeHunter.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef SupernodeHunter_H
  16. #define SupernodeHunter_H
  17. #include "memory/Allocator.h"
  18. #include "util/log/Log.h"
  19. #include "util/events/EventBase.h"
  20. #include "subnode/AddrSet.h"
  21. #include "subnode/MsgCore.h"
  22. #include "dht/Address.h"
  23. #include "net/SwitchPinger.h"
  24. #include "util/Linker.h"
  25. Linker_require("subnode/SupernodeHunter.c");
  26. struct SupernodeHunter;
  27. typedef void (* SupernodeHunter_Callback)(struct SupernodeHunter* sh,
  28. int64_t sendTime,
  29. int64_t snodeRecvTime);
  30. struct SupernodeHunter
  31. {
  32. // This will be set to:
  33. // 1 when a supernode is found and
  34. // 2 if the supernode is found AND it is one of the ones in the authorized list.
  35. // If it is in state 1 and there exist authorized snodes, it will keep on looking for anyone
  36. // who knows the way to one of the snodes in the authorized list.
  37. //
  38. // If you lose connection to your snode, you can use
  39. int snodeIsReachable;
  40. struct Address snodeAddr;
  41. SupernodeHunter_Callback onSnodeChange;
  42. SupernodeHunter_Callback onSnodeUnreachable;
  43. void* userData;
  44. };
  45. #define SupernodeHunter_addSnode_EXISTS -1
  46. int SupernodeHunter_addSnode(struct SupernodeHunter* snh, struct Address* snodeAddr);
  47. int SupernodeHunter_listSnodes(struct SupernodeHunter* snh,
  48. struct Address*** out,
  49. struct Allocator* alloc);
  50. #define SupernodeHunter_removeSnode_NONEXISTANT -1
  51. int SupernodeHunter_removeSnode(struct SupernodeHunter* snh, struct Address* toRemove);
  52. /**
  53. * The algorithm of this module is to ask each peer for a supernode using a findNode request.
  54. * If each peer comes up empty then each peer is sent a getPeers request and those nodes are
  55. * requested in turn.
  56. */
  57. struct SupernodeHunter* SupernodeHunter_new(struct Allocator* allocator,
  58. struct Log* log,
  59. struct EventBase* base,
  60. struct SwitchPinger* sp,
  61. struct AddrSet* peers,
  62. struct MsgCore* msgCore,
  63. struct Address* myAddress);
  64. #endif