SphynxClient.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_SPHYNX_CLIENT_HPP
  26. #define CAT_SPHYNX_CLIENT_HPP
  27. #include <cat/net/SphynxTransport.hpp>
  28. #include <cat/crypt/tunnel/KeyAgreementInitiator.hpp>
  29. #include <cat/threads/Thread.hpp>
  30. #include <cat/threads/WaitableFlag.hpp>
  31. namespace cat {
  32. namespace sphynx {
  33. //// sphynx::Client
  34. class Client : Thread, public UDPEndpoint, public Transport
  35. {
  36. static const int HANDSHAKE_TICK_RATE = 100; // milliseconds
  37. static const int INITIAL_HELLO_POST_INTERVAL = 200; // milliseconds
  38. static const int CONNECT_TIMEOUT = 6000; // milliseconds
  39. static const u32 MTU_PROBE_INTERVAL = 8000; // 8 seconds
  40. static const int CLIENT_THREAD_KILL_TIMEOUT = 10000; // 10 seconds
  41. static const int SESSION_KEY_BYTES = 32;
  42. char _session_key[SESSION_KEY_BYTES];
  43. KeyAgreementInitiator _key_agreement_initiator;
  44. u8 _server_public_key[PUBLIC_KEY_BYTES];
  45. u8 _cached_challenge[CHALLENGE_BYTES];
  46. WaitableFlag _kill_flag;
  47. protected:
  48. u32 _last_send_mstsc;
  49. NetAddr _server_addr;
  50. bool _connected;
  51. AuthenticatedEncryption _auth_enc;
  52. // Last time a packet was received from the server -- for disconnect timeouts
  53. u32 _last_recv_tsc;
  54. public:
  55. Client();
  56. virtual ~Client();
  57. bool SetServerKey(ThreadPoolLocalStorage *tls, const void *server_key, int key_bytes, const char *session_key);
  58. bool Connect(const char *hostname, Port port);
  59. bool Connect(const NetAddr &addr);
  60. void Disconnect();
  61. protected:
  62. bool IsConnected() { return _connected; }
  63. virtual void OnClose() = 0;
  64. virtual void OnConnectFail() = 0;
  65. virtual void OnConnect(ThreadPoolLocalStorage *tls) = 0;
  66. virtual void OnTimestampDeltaUpdate(u32 rtt, s32 delta) {}
  67. virtual void OnMessage(ThreadPoolLocalStorage *tls, BufferStream msg, u32 bytes) = 0;
  68. virtual void OnDisconnect() = 0;
  69. virtual void OnTick(ThreadPoolLocalStorage *tls, u32 now) = 0;
  70. private:
  71. virtual void OnRead(ThreadPoolLocalStorage *tls, const NetAddr &src, u8 *data, u32 bytes);
  72. virtual void OnWrite(u32 bytes);
  73. private:
  74. bool PostHello();
  75. void OnUnreachable(const NetAddr &src);
  76. // Return false to remove resolve from cache
  77. bool OnResolve(const char *hostname, const NetAddr *array, int array_length);
  78. virtual bool PostPacket(u8 *buffer, u32 buf_bytes, u32 msg_bytes, u32 skip_bytes);
  79. private:
  80. bool ThreadFunction(void *param);
  81. };
  82. } // namespace sphynx
  83. } // namespace cat
  84. #endif // CAT_SPHYNX_CLIENT_HPP
粤ICP备19079148号