1
0

SessionManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/CryptoAuth.h"
  18. #include "crypto/random/Random.h"
  19. #include "interface/Interface.h"
  20. #include "memory/Allocator.h"
  21. #include "util/events/EventBase.h"
  22. #include "util/Linker.h"
  23. Linker_require("interface/SessionManager.c")
  24. #include <stdint.h>
  25. struct SessionManager;
  26. struct SessionManager_Session
  27. {
  28. struct Interface external;
  29. struct Interface* internal;
  30. /** When the last message was received on this session (milliseconds since epoch). */
  31. uint64_t timeOfLastIn;
  32. /** When the last message was sent on this session (milliseconds since epoch). */
  33. uint64_t timeOfLastOut;
  34. /** The CryptoAuth state as of the last message. See: CryptoAuth_getState() */
  35. int cryptoAuthState;
  36. /** The handle which will be used to lookup this session on our side, big endian. */
  37. uint32_t receiveHandle_be;
  38. /** The handle which we are expected to send to identify ourselves, big endian. */
  39. uint32_t sendHandle_be;
  40. /** The version of the other node. */
  41. uint32_t version;
  42. /** The IPv6 address of the other node. */
  43. uint8_t ip6[16];
  44. /** Some label which is known to go to this node... */
  45. uint64_t knownSwitchLabel;
  46. };
  47. struct SessionManager_HandleList
  48. {
  49. uint32_t count;
  50. uint32_t* handles;
  51. };
  52. /**
  53. * Create a new session manager for keeping track of and expiring crypto sessions.
  54. * The typical use case is discriminating between packets by ip address, keyOffset is the number
  55. * of bytes between the beginning of each message and the beginning of the ip address and keySize
  56. * is the number of bytes in the address.
  57. *
  58. * @param decryptedIncoming the callback to call with incoming data after it has been decrypted.
  59. * @param encryptedOutgoing the callback to call with outgoing data after it has been encrypted.
  60. * @param interfaceContext the context which will become the senderContext and receiverContext for
  61. * encryptedOutgoing and decryptedIncoming respectively.
  62. * @param eventBase the libevent event base.
  63. * @param cryptoAuth the cryptoauthenticator for the sessions.
  64. * @param allocator means of getting memory.
  65. * @return a session manager.
  66. */
  67. struct SessionManager* SessionManager_new(Interface_Callback decryptedIncoming,
  68. Interface_Callback encryptedOutgoing,
  69. void* interfaceContext,
  70. struct EventBase* eventBase,
  71. struct CryptoAuth* cryptoAuth,
  72. struct Random* rand,
  73. struct Allocator* allocator);
  74. /**
  75. * Get a session from the session manager.
  76. * If there is no session for the lookup key, it will be created.
  77. *
  78. * @param lookupKey this must be the size given by keySize in SessionManager_new().
  79. * @param cryptoKey optional encryption key if it is known, otherwise NULL.
  80. * @param sm the session manager.
  81. */
  82. struct SessionManager_Session* SessionManager_getSession(uint8_t* lookupKey,
  83. uint8_t cryptoKey[32],
  84. struct SessionManager* sm);
  85. /**
  86. * Get a session by its handle.
  87. *
  88. * @param handle an opaque handle associated with the session.
  89. * @param sm the session manager.
  90. * @return the sesssion if there is one by that handle or null.
  91. */
  92. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  93. struct SessionManager* sm);
  94. /**
  95. * Get the IPv6 address for a session.
  96. *
  97. * @param handle the handle for the session
  98. * @param sm the session manager
  99. * @return a binary ipv6 address or NULL.
  100. */
  101. uint8_t* SessionManager_getIp6(uint32_t handle, struct SessionManager* sm);
  102. /**
  103. * Get the list of all handles.
  104. */
  105. struct SessionManager_HandleList* SessionManager_getHandleList(struct SessionManager* sm,
  106. struct Allocator* alloc);
  107. #endif