VL1.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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: 2024-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 "Hashtable.hpp"
  21. #include "Mutex.hpp"
  22. #include "FCV.hpp"
  23. #include <vector>
  24. namespace ZeroTier {
  25. class RuntimeEnvironment;
  26. class Peer;
  27. class VL2;
  28. /**
  29. * VL1 (virtual layer 1) packet I/O and messaging.
  30. *
  31. * This class is thread safe.
  32. */
  33. class VL1
  34. {
  35. public:
  36. explicit VL1(const RuntimeEnvironment *renv);
  37. ~VL1();
  38. /**
  39. * Called when a packet is received from the real network
  40. *
  41. * The packet data supplied to this method may be modified. Internal
  42. * packet handler code may also take possession of it via atomic swap
  43. * and leave the 'data' pointer NULL. The 'data' pointer and its
  44. * contents should not be used after this call. Make a copy if the
  45. * data might still be needed.
  46. *
  47. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  48. * @param localSocket Local I/O socket as supplied by external code
  49. * @param fromAddr Internet IP address of origin
  50. * @param data Packet data
  51. * @param len Packet length
  52. */
  53. void onRemotePacket(void *tPtr,int64_t localSocket,const InetAddress &fromAddr,SharedPtr<Buf> &data,unsigned int len);
  54. private:
  55. const RuntimeEnvironment *RR;
  56. // Code to handle relaying of packets to other nodes.
  57. void _relay(void *tPtr,const SharedPtr<Path> &path,const Address &destination,SharedPtr<Buf> &data,unsigned int len);
  58. // Send any pending WHOIS requests.
  59. void _sendPendingWhois(void *tPtr,int64_t now);
  60. // Handlers for VL1 verbs -- for clarity's sake VL2 verbs are in the VL2 class.
  61. bool _HELLO(void *tPtr,const SharedPtr<Path> &path,SharedPtr<Peer> &peer,Buf &pkt,int packetSize,bool authenticated);
  62. bool _ERROR(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize,Protocol::Verb &inReVerb);
  63. bool _OK(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize,Protocol::Verb &inReVerb);
  64. bool _WHOIS(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize);
  65. bool _RENDEZVOUS(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize);
  66. bool _ECHO(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize);
  67. bool _PUSH_DIRECT_PATHS(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize);
  68. bool _USER_MESSAGE(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize);
  69. bool _ENCAP(void *tPtr,const SharedPtr<Path> &path,const SharedPtr<Peer> &peer,Buf &pkt,int packetSize);
  70. struct _WhoisQueueItem
  71. {
  72. ZT_ALWAYS_INLINE _WhoisQueueItem() : lastRetry(0),inboundPackets(),retries(0) {}
  73. int64_t lastRetry;
  74. FCV<Buf::Slice,32> inboundPackets; // capacity can be changed but this should be plenty
  75. unsigned int retries;
  76. };
  77. Defragmenter<ZT_MAX_PACKET_FRAGMENTS> _inputPacketAssembler;
  78. Hashtable<Address,_WhoisQueueItem> _whoisQueue;
  79. Mutex _whoisQueue_l;
  80. };
  81. } // namespace ZeroTier
  82. #endif