Router.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "dht/dhtcore/Router.h"
  16. #include "dht/dhtcore/NodeStore.h"
  17. #include "dht/dhtcore/RouterModule.h"
  18. #include "dht/dhtcore/SearchRunner.h"
  19. #include "util/Identity.h"
  20. struct Router_pvt
  21. {
  22. struct Router pub;
  23. struct NodeStore* nodeStore;
  24. struct SearchRunner* searchRunner;
  25. struct RouterModule* routerModule;
  26. Identity
  27. };
  28. struct Node_Link* Router_linkForPath(struct Router* r, uint64_t path)
  29. {
  30. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  31. return NodeStore_linkForPath(rr->nodeStore, path);
  32. }
  33. void Router_sendGetPeers(struct Router* r,
  34. struct Address* addr,
  35. uint64_t nearbyLabel,
  36. uint32_t timeoutMilliseconds,
  37. struct Allocator* alloc)
  38. {
  39. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  40. RouterModule_getPeers(addr, nearbyLabel, timeoutMilliseconds, rr->routerModule, alloc);
  41. }
  42. struct Node_Two* Router_lookup(struct Router* r, uint8_t targetAddr[16])
  43. {
  44. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  45. return NodeStore_getBest(rr->nodeStore, targetAddr);
  46. }
  47. void Router_brokenLink(struct Router* r, uint64_t path, uint64_t labelAtErrorHop)
  48. {
  49. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  50. NodeStore_brokenLink(rr->nodeStore, path, labelAtErrorHop);
  51. }
  52. void Router_searchForNode(struct Router* r, uint8_t ip6[16], struct Allocator* alloc)
  53. {
  54. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  55. SearchRunner_search(ip6, -1, rr->searchRunner, alloc);
  56. }
  57. void Router_disconnectedPeer(struct Router* r, uint64_t path)
  58. {
  59. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  60. NodeStore_disconnectedPeer(rr->nodeStore, path);
  61. }
  62. struct Router* Router_new(struct RouterModule* rm,
  63. struct NodeStore* ns,
  64. struct SearchRunner* sr,
  65. struct Allocator* alloc)
  66. {
  67. struct Router_pvt* rp = Allocator_clone(alloc, (&(struct Router_pvt) {
  68. .routerModule = rm,
  69. .nodeStore = ns,
  70. .searchRunner = sr
  71. }));
  72. Identity_set(rp);
  73. return &rp->pub;
  74. }