1
0

SessionManager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "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. typedef struct SessionManager_Path_s
  66. {
  67. int64_t timeLastValidated;
  68. uint64_t label;
  69. uint32_t metric;
  70. } SessionManager_Path_t;
  71. #define SessionManager_PATH_COUNT 3
  72. struct SessionManager_Session
  73. {
  74. Ca_Session_t* caSession;
  75. SessionManager_Path_t paths[SessionManager_PATH_COUNT];
  76. // This is used to know when we should stop maintaining the session.
  77. // It is only flagged for real traffic, not router traffic.
  78. int64_t timeOfLastUsage;
  79. uint64_t bytesOut;
  80. uint64_t bytesIn;
  81. /** This is the time the last search was triggered for this session. */
  82. int64_t lastSearchTime;
  83. /** The handle which will be used to lookup this session on our side. */
  84. uint32_t receiveHandle;
  85. /** The handle which we are expected to send to identify ourselves */
  86. uint32_t sendHandle;
  87. /** The version of the other node. */
  88. uint32_t version;
  89. };
  90. struct SessionManager_HandleList
  91. {
  92. int length;
  93. uint32_t* handles;
  94. };
  95. uint32_t SessionManager_effectiveMetric(struct SessionManager_Session* sess);
  96. /**
  97. * Get a session by its handle.
  98. *
  99. * @param handle an opaque handle associated with the session.
  100. * @param sm the session manager.
  101. * @return the sesssion if there is one by that handle or null.
  102. */
  103. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  104. struct SessionManager* sm);
  105. struct SessionManager_Session* SessionManager_sessionForIp6(uint8_t* lookupKey,
  106. struct SessionManager* sm);
  107. int SessionManager_handleCount(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. Ca_t* cryptoAuth,
  116. struct Random* rand,
  117. struct Log* log,
  118. struct EventEmitter* ee);
  119. #endif