SessionManager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/Ca.h"
  19. #include "memory/Allocator.h"
  20. #include "net/EventEmitter.h"
  21. #include "util/Linker.h"
  22. Linker_require("net/SessionManager.c")
  23. /**
  24. * Purpose of this module is to take packets from "the inside" which contain ipv6 address and
  25. * skeleton switch header and find an appropriate CryptoAuth session for them or begin one.
  26. * If a key for this node cannot be found then the packet will be blocked and a search will be
  27. * triggered. If the skeleton switch header contains "zero" as the switch label, the packet will
  28. * also be buffered and a search triggered. If a search is in progress (and another packet is
  29. * already buffered, the packet will be dropped instead).
  30. * Incoming messages from the outside will be decrypted and their key and path will be stored.
  31. */
  32. struct SessionManager
  33. {
  34. /** Sends and handles packets prepped to/from switch. */
  35. struct Iface switchIf;
  36. /**
  37. * Sends and handles packets with RouteHeader on top.
  38. * When sending a packet to SessionManager:
  39. * header.sh.label_be may be zero
  40. * version may be zero
  41. * publicKey may be zero
  42. * If these values are not known, the packet will be taken from the cache or a search will
  43. * be triggered.
  44. */
  45. struct Iface insideIf;
  46. /**
  47. * Maximum number of packets to hold in buffer before summarily dropping...
  48. */
  49. #define SessionManager_MAX_BUFFERED_MESSAGES_DEFAULT 30
  50. int maxBufferedMessages;
  51. /** Number of milliseconds with no reply before a session should be timed out. */
  52. #define SessionManager_SESSION_TIMEOUT_MILLISECONDS_DEFAULT 120000
  53. int64_t sessionTimeoutMilliseconds;
  54. /**
  55. * Number of milliseconds after which a new DHT search will be run to verify the path.
  56. * This is guaged off of lastSearchTime so it need not be less than sessionTimeoutMilliseconds
  57. * which is guaged off of time of last incoming message (timeOfLastIn).
  58. */
  59. #define SessionManager_SESSION_SEARCH_AFTER_MILLISECONDS_DEFAULT 30000
  60. int64_t sessionSearchAfterMilliseconds;
  61. };
  62. typedef struct SessionManager_Path_s
  63. {
  64. int64_t timeLastValidated;
  65. uint64_t label;
  66. uint32_t metric;
  67. } SessionManager_Path_t;
  68. #define SessionManager_PATH_COUNT 3
  69. struct SessionManager_Session
  70. {
  71. Ca_Session_t* caSession;
  72. SessionManager_Path_t paths[SessionManager_PATH_COUNT];
  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. };
  87. struct SessionManager_HandleList
  88. {
  89. int length;
  90. uint32_t* handles;
  91. };
  92. uint32_t SessionManager_effectiveMetric(struct SessionManager_Session* sess);
  93. /**
  94. * Get a session by its handle.
  95. *
  96. * @param handle an opaque handle associated with the session.
  97. * @param sm the session manager.
  98. * @return the sesssion if there is one by that handle or null.
  99. */
  100. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  101. struct SessionManager* sm);
  102. struct SessionManager_Session* SessionManager_sessionForIp6(uint8_t* lookupKey,
  103. struct SessionManager* sm);
  104. int SessionManager_handleCount(struct SessionManager* sm);
  105. /**
  106. * Get the list of all handles.
  107. */
  108. struct SessionManager_HandleList* SessionManager_getHandleList(struct SessionManager* sm,
  109. struct Allocator* alloc);
  110. struct SessionManager* SessionManager_new(struct Allocator* alloc,
  111. EventBase_t* eventBase,
  112. Ca_t* cryptoAuth,
  113. struct Random* rand,
  114. struct Log* log,
  115. struct EventEmitter* ee);
  116. #endif