SessionManager.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. };
  45. struct SessionManager_HandleList
  46. {
  47. uint32_t count;
  48. uint32_t* handles;
  49. };
  50. /**
  51. * Create a new session manager for keeping track of and expiring crypto sessions.
  52. * The typical use case is discriminating between packets by ip address, keyOffset is the number
  53. * of bytes between the beginning of each message and the beginning of the ip address and keySize
  54. * is the number of bytes in the address.
  55. *
  56. * @param decryptedIncoming the callback to call with incoming data after it has been decrypted.
  57. * @param encryptedOutgoing the callback to call with outgoing data after it has been encrypted.
  58. * @param interfaceContext the context which will become the senderContext and receiverContext for
  59. * encryptedOutgoing and decryptedIncoming respectively.
  60. * @param eventBase the libevent event base.
  61. * @param cryptoAuth the cryptoauthenticator for the sessions.
  62. * @param allocator means of getting memory.
  63. * @return a session manager.
  64. */
  65. struct SessionManager* SessionManager_new(Interface_CALLBACK(decryptedIncoming),
  66. Interface_CALLBACK(encryptedOutgoing),
  67. void* interfaceContext,
  68. struct EventBase* eventBase,
  69. struct CryptoAuth* cryptoAuth,
  70. struct Random* rand,
  71. struct Allocator* allocator);
  72. /**
  73. * Get a session from the session manager.
  74. * If there is no session for the lookup key, it will be created.
  75. *
  76. * @param lookupKey this must be the size given by keySize in SessionManager_new().
  77. * @param cryptoKey optional encryption key if it is known, otherwise NULL.
  78. * @param sm the session manager.
  79. */
  80. struct SessionManager_Session* SessionManager_getSession(uint8_t* lookupKey,
  81. uint8_t cryptoKey[32],
  82. struct SessionManager* sm);
  83. /**
  84. * Get a session by its handle.
  85. *
  86. * @param handle an opaque handle associated with the session.
  87. * @param sm the session manager.
  88. * @return the sesssion if there is one by that handle or null.
  89. */
  90. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  91. struct SessionManager* sm);
  92. /**
  93. * Get the IPv6 address for a session.
  94. *
  95. * @param handle the handle for the session
  96. * @param sm the session manager
  97. * @return a binary ipv6 address or NULL.
  98. */
  99. uint8_t* SessionManager_getIp6(uint32_t handle, struct SessionManager* sm);
  100. /**
  101. * Get the list of all handles.
  102. */
  103. struct SessionManager_HandleList* SessionManager_getHandleList(struct SessionManager* sm,
  104. struct Allocator* alloc);
  105. #endif