SessionManager.h 4.3 KB

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