SessionManager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #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. * Should be set to a number less than sessionSearchAfterMilliseconds, if no incoming nor
  59. * outgoing messages were sent on this session for this amount of time, re-running of the
  60. * search will be skipped when sessionSearchAfterMilliseconds is reached allowing the session
  61. * to be removed from the table after sessionTimeoutMilliseconds.
  62. */
  63. #define SessionManager_SESSION_IDLE_AFTER_MILLISECONDS_DEFAULT 50000
  64. int64_t sessionIdleAfterMilliseconds;
  65. /**
  66. * Number of milliseconds after which a new DHT search will be run to verify the path.
  67. * If the session is in "idle" state (sessionIdleAfterMilliseconds has elapsed without any
  68. * incoming or outgoing traffic) then the search will be skipped, although it will be triggered
  69. * again if more traffic is sent.
  70. * This is guaged off of lastSearchTime so it need not be less than sessionTimeoutMilliseconds
  71. * which is guaged off of time of last incoming message (timeOfLastIn).
  72. */
  73. #define SessionManager_SESSION_SEARCH_AFTER_MILLISECONDS_DEFAULT 100000
  74. int64_t sessionSearchAfterMilliseconds;
  75. };
  76. struct SessionManager_Session
  77. {
  78. struct CryptoAuth_Session* caSession;
  79. /** When the last message was received on this session (milliseconds since epoch). */
  80. int64_t timeOfLastIn;
  81. /** When the last message was sent on this session (milliseconds since epoch). */
  82. int64_t timeOfLastOut;
  83. uint64_t bytesOut;
  84. uint64_t bytesIn;
  85. /** This is the time the last search was triggered for this session. */
  86. int64_t lastSearchTime;
  87. /** The handle which will be used to lookup this session on our side. */
  88. uint32_t receiveHandle;
  89. /** The handle which we are expected to send to identify ourselves */
  90. uint32_t sendHandle;
  91. /** The version of the other node. */
  92. uint32_t version;
  93. /** The best known switch label for reaching this node. */
  94. uint64_t sendSwitchLabel;
  95. /** The switch label which this node uses for reaching us. */
  96. uint64_t recvSwitchLabel;
  97. };
  98. struct SessionManager_HandleList
  99. {
  100. int length;
  101. uint32_t* handles;
  102. };
  103. /**
  104. * Get a session by its handle.
  105. *
  106. * @param handle an opaque handle associated with the session.
  107. * @param sm the session manager.
  108. * @return the sesssion if there is one by that handle or null.
  109. */
  110. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  111. struct SessionManager* sm);
  112. struct SessionManager_Session* SessionManager_sessionForIp6(uint8_t* lookupKey,
  113. struct SessionManager* sm);
  114. /**
  115. * Get the list of all handles.
  116. */
  117. struct SessionManager_HandleList* SessionManager_getHandleList(struct SessionManager* sm,
  118. struct Allocator* alloc);
  119. struct SessionManager* SessionManager_new(struct Allocator* alloc,
  120. struct EventBase* eventBase,
  121. struct CryptoAuth* cryptoAuth,
  122. struct Random* rand,
  123. struct Log* log,
  124. struct EventEmitter* ee);
  125. #endif