1
0

SessionManager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 SessionManager_H
  16. #define SessionManager_H
  17. #include "crypto/random/Random.h"
  18. #include "crypto/CryptoAuth.h"
  19. #include "memory/Allocator.h"
  20. #include "wire/PFChan.h"
  21. #include "net/EventEmitter.h"
  22. #include "wire/SwitchHeader.h"
  23. #include "wire/CryptoHeader.h"
  24. #include "util/Linker.h"
  25. Linker_require("net/SessionManager.c");
  26. /**
  27. * Purpose of this module is to take packets from "the inside" which contain ipv6 address and
  28. * skeleton switch header and find an appropriate CryptoAuth session for them or begin one.
  29. * If a key for this node cannot be found then the packet will be blocked and a search will be
  30. * triggered. If the skeleton switch header contains "zero" as the switch label, the packet will
  31. * also be buffered and a search triggered. If a search is in progress (and another packet is
  32. * already buffered, the packet will be dropped instead).
  33. * Incoming messages from the outside will be decrypted and their key and path will be stored.
  34. */
  35. struct SessionManager
  36. {
  37. /** Sends and handles packets prepped to/from switch. */
  38. struct Iface switchIf;
  39. /**
  40. * Sends and handles packets with RouteHeader on top.
  41. * When sending a packet to SessionManager:
  42. * header.sh.label_be may be zero
  43. * version may be zero
  44. * publicKey may be zero
  45. * If these values are not known, the packet will be taken from the cache or a search will
  46. * be triggered.
  47. */
  48. struct Iface insideIf;
  49. /**
  50. * Maximum number of packets to hold in buffer before summarily dropping...
  51. */
  52. #define SessionManager_MAX_BUFFERED_MESSAGES_DEFAULT 30
  53. int maxBufferedMessages;
  54. /** Number of milliseconds with no reply before a session should be timed out. */
  55. #define SessionManager_SESSION_TIMEOUT_MILLISECONDS_DEFAULT 120000
  56. int64_t sessionTimeoutMilliseconds;
  57. /**
  58. * Number of milliseconds after which a new DHT search will be run to verify the path.
  59. * This is guaged off of lastSearchTime so it need not be less than sessionTimeoutMilliseconds
  60. * which is guaged off of time of last incoming message (timeOfLastIn).
  61. */
  62. #define SessionManager_SESSION_SEARCH_AFTER_MILLISECONDS_DEFAULT 30000
  63. int64_t sessionSearchAfterMilliseconds;
  64. };
  65. struct SessionManager_Session
  66. {
  67. struct CryptoAuth_Session* caSession;
  68. /**
  69. * When the last message was received on this session (milliseconds since epoch).
  70. * Used for session keep alive.
  71. */
  72. int64_t timeOfKeepAliveIn;
  73. /** When the last non-CJDHT message was received on this session (milliseconds since epoch). */
  74. int64_t timeOfLastIn;
  75. /** When the last non-CJDHT message was sent on this session (milliseconds since epoch). */
  76. int64_t timeOfLastOut;
  77. uint64_t bytesOut;
  78. uint64_t bytesIn;
  79. /** This is the time the last search was triggered for this session. */
  80. int64_t lastSearchTime;
  81. /** The handle which will be used to lookup this session on our side. */
  82. uint32_t receiveHandle;
  83. /** The handle which we are expected to send to identify ourselves */
  84. uint32_t sendHandle;
  85. /** The version of the other node. */
  86. uint32_t version;
  87. uint32_t metric;
  88. /** The best known switch label for reaching this node. */
  89. uint64_t sendSwitchLabel;
  90. /** The switch label which this node uses for reaching us. */
  91. uint64_t recvSwitchLabel;
  92. };
  93. struct SessionManager_HandleList
  94. {
  95. int length;
  96. uint32_t* handles;
  97. };
  98. /**
  99. * Get a session by its handle.
  100. *
  101. * @param handle an opaque handle associated with the session.
  102. * @param sm the session manager.
  103. * @return the sesssion if there is one by that handle or null.
  104. */
  105. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  106. struct SessionManager* sm);
  107. struct SessionManager_Session* SessionManager_sessionForIp6(uint8_t* lookupKey,
  108. struct SessionManager* sm);
  109. /**
  110. * Get the list of all handles.
  111. */
  112. struct SessionManager_HandleList* SessionManager_getHandleList(struct SessionManager* sm,
  113. struct Allocator* alloc);
  114. struct SessionManager* SessionManager_new(struct Allocator* alloc,
  115. struct EventBase* eventBase,
  116. struct CryptoAuth* cryptoAuth,
  117. struct Random* rand,
  118. struct Log* log,
  119. struct EventEmitter* ee);
  120. #endif