VL1.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_VL1_HPP
  14. #define ZT_VL1_HPP
  15. #include "Constants.hpp"
  16. #include "Defragmenter.hpp"
  17. #include "Buf.hpp"
  18. #include "Address.hpp"
  19. #include "Protocol.hpp"
  20. #include "Mutex.hpp"
  21. #include "FCV.hpp"
  22. #include "Containers.hpp"
  23. #define ZT_VL1_MAX_WHOIS_WAITING_PACKETS 32
  24. #define ZT_VL1_AUTH_RESULT_FLAG_AUTHENTICATED 0x01U
  25. #define ZT_VL1_AUTH_RESULT_FLAG_ENCRYPTED 0x02U
  26. #define ZT_VL1_AUTH_RESULT_FLAG_FORWARD_SECRET 0x04U
  27. namespace ZeroTier {
  28. class RuntimeEnvironment;
  29. class Peer;
  30. class VL2;
  31. /**
  32. * VL1 (virtual layer 1) packet I/O and messaging.
  33. *
  34. * This class is thread safe.
  35. */
  36. class VL1
  37. {
  38. public:
  39. explicit VL1(const RuntimeEnvironment *renv);
  40. /**
  41. * Called when a packet is received from the real network
  42. *
  43. * The packet data supplied to this method may be modified. Internal
  44. * packet handler code may also take possession of it via atomic swap
  45. * and leave the 'data' pointer NULL. The 'data' pointer and its
  46. * contents should not be used after this call. Make a copy if the
  47. * data might still be needed.
  48. *
  49. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  50. * @param localSocket Local I/O socket as supplied by external code
  51. * @param fromAddr Internet IP address of origin
  52. * @param data Packet data
  53. * @param len Packet length
  54. */
  55. void onRemotePacket(void *tPtr, int64_t localSocket, const InetAddress &fromAddr, SharedPtr< Buf > &data, unsigned int len) noexcept;
  56. private:
  57. const RuntimeEnvironment *RR;
  58. void m_relay(void *tPtr, const SharedPtr< Path > &path, Address destination, SharedPtr< Buf > &pkt, int pktSize);
  59. void m_sendPendingWhois(void *tPtr, int64_t now);
  60. SharedPtr< Peer > m_HELLO(void *tPtr, const SharedPtr< Path > &path, Buf &pkt, int packetSize);
  61. bool m_ERROR(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize, Protocol::Verb &inReVerb);
  62. bool m_OK(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize, Protocol::Verb &inReVerb);
  63. bool m_WHOIS(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize);
  64. bool m_RENDEZVOUS(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize);
  65. bool m_ECHO(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize);
  66. bool m_PUSH_DIRECT_PATHS(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize);
  67. bool m_USER_MESSAGE(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize);
  68. bool m_ENCAP(void *tPtr, uint64_t packetId, unsigned int auth, const SharedPtr< Path > &path, const SharedPtr< Peer > &peer, Buf &pkt, int packetSize);
  69. // Defragmentation engine for handling inbound packets with more than one fragment.
  70. Defragmenter< ZT_MAX_PACKET_FRAGMENTS > m_inputPacketAssembler;
  71. // Queue of outbound WHOIS reqeusts and packets waiting on them.
  72. struct p_WhoisQueueItem
  73. {
  74. ZT_INLINE p_WhoisQueueItem() : lastRetry(0), retries(0), waitingPacketCount(0)
  75. {}
  76. int64_t lastRetry;
  77. unsigned int retries;
  78. unsigned int waitingPacketCount;
  79. unsigned int waitingPacketSize[ZT_VL1_MAX_WHOIS_WAITING_PACKETS];
  80. SharedPtr< Buf > waitingPacket[ZT_VL1_MAX_WHOIS_WAITING_PACKETS];
  81. };
  82. Map< Address, p_WhoisQueueItem > m_whoisQueue;
  83. Mutex m_whoisQueue_l;
  84. };
  85. } // namespace ZeroTier
  86. #endif