SessionManager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 timeOfLastIncoming;
  73. // This is used to know when we should stop maintaining the session.
  74. // It is only flagged for real traffic, not router traffic.
  75. int64_t timeOfLastUsage;
  76. uint64_t bytesOut;
  77. uint64_t bytesIn;
  78. /** This is the time the last search was triggered for this session. */
  79. int64_t lastSearchTime;
  80. /** The handle which will be used to lookup this session on our side. */
  81. uint32_t receiveHandle;
  82. /** The handle which we are expected to send to identify ourselves */
  83. uint32_t sendHandle;
  84. /** The version of the other node. */
  85. uint32_t version;
  86. uint32_t metric;
  87. /** The best known switch label for reaching this node. */
  88. uint64_t sendSwitchLabel;
  89. /** The switch label which this node uses for reaching us. */
  90. uint64_t recvSwitchLabel;
  91. };
  92. struct SessionManager_HandleList
  93. {
  94. int length;
  95. uint32_t* handles;
  96. };
  97. /**
  98. * Get a session by its handle.
  99. *
  100. * @param handle an opaque handle associated with the session.
  101. * @param sm the session manager.
  102. * @return the sesssion if there is one by that handle or null.
  103. */
  104. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  105. struct SessionManager* sm);
  106. struct SessionManager_Session* SessionManager_sessionForIp6(uint8_t* lookupKey,
  107. struct SessionManager* sm);
  108. /**
  109. * Get the list of all handles.
  110. */
  111. struct SessionManager_HandleList* SessionManager_getHandleList(struct SessionManager* sm,
  112. struct Allocator* alloc);
  113. struct SessionManager* SessionManager_new(struct Allocator* alloc,
  114. struct EventBase* eventBase,
  115. struct CryptoAuth* cryptoAuth,
  116. struct Random* rand,
  117. struct Log* log,
  118. struct EventEmitter* ee);
  119. #endif