Shadowrun: Awakened 29 September 2011 - Build 871
SphynxClient.hpp
Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2010 Christopher A. Taylor.  All rights reserved.
00003 
00004     Redistribution and use in source and binary forms, with or without
00005     modification, are permitted provided that the following conditions are met:
00006 
00007     * Redistributions of source code must retain the above copyright notice,
00008       this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010       this list of conditions and the following disclaimer in the documentation
00011       and/or other materials provided with the distribution.
00012     * Neither the name of LibCat nor the names of its contributors may be used
00013       to endorse or promote products derived from this software without
00014       specific prior written permission.
00015 
00016     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026     POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00029 #ifndef CAT_SPHYNX_CLIENT_HPP
00030 #define CAT_SPHYNX_CLIENT_HPP
00031 
00032 #include <cat/net/SphynxTransport.hpp>
00033 #include <cat/crypt/tunnel/KeyAgreementInitiator.hpp>
00034 #include <cat/threads/Thread.hpp>
00035 #include <cat/threads/WaitableFlag.hpp>
00036 
00037 namespace cat {
00038 
00039 
00040 namespace sphynx {
00041 
00042 
00044 
00045 class Client : Thread, public UDPEndpoint, public Transport
00046 {
00047     static const int HANDSHAKE_TICK_RATE = 100; // milliseconds
00048     static const int INITIAL_HELLO_POST_INTERVAL = 200; // milliseconds
00049     static const int CONNECT_TIMEOUT = 6000; // milliseconds
00050     static const u32 MTU_PROBE_INTERVAL = 8000; // 8 seconds
00051     static const int CLIENT_THREAD_KILL_TIMEOUT = 10000; // 10 seconds
00052 
00053     static const int SESSION_KEY_BYTES = 32;
00054     char _session_key[SESSION_KEY_BYTES];
00055 
00056     KeyAgreementInitiator _key_agreement_initiator;
00057     u8 _server_public_key[PUBLIC_KEY_BYTES];
00058     u8 _cached_challenge[CHALLENGE_BYTES];
00059 
00060     WaitableFlag _kill_flag;
00061 
00062 protected:
00063     u32 _last_send_mstsc;
00064     NetAddr _server_addr;
00065     bool _connected;
00066     AuthenticatedEncryption _auth_enc;
00067 
00068     // Last time a packet was received from the server -- for disconnect timeouts
00069     u32 _last_recv_tsc;
00070 
00071 public:
00072     Client();
00073     virtual ~Client();
00074 
00075     bool SetServerKey(ThreadPoolLocalStorage *tls, const void *server_key, int key_bytes, const char *session_key);
00076 
00077     bool Connect(const char *hostname, Port port);
00078     bool Connect(const NetAddr &addr);
00079 
00080     void Disconnect();
00081 
00082 protected:
00083     bool IsConnected() { return _connected; }
00084 
00085     virtual void OnClose() = 0;
00086     virtual void OnConnectFail() = 0;
00087     virtual void OnConnect(ThreadPoolLocalStorage *tls) = 0;
00088     virtual void OnTimestampDeltaUpdate(u32 rtt, s32 delta) {}
00089     virtual void OnMessage(ThreadPoolLocalStorage *tls, BufferStream msg, u32 bytes) = 0;
00090     virtual void OnDisconnect() = 0;
00091     virtual void OnTick(ThreadPoolLocalStorage *tls, u32 now) = 0;
00092 
00093 private:
00094     virtual void OnRead(ThreadPoolLocalStorage *tls, const NetAddr &src, u8 *data, u32 bytes);
00095     virtual void OnWrite(u32 bytes);
00096 
00097 private:
00098     bool PostHello();
00099     void OnUnreachable(const NetAddr &src);
00100 
00101     // Return false to remove resolve from cache
00102     bool OnResolve(const char *hostname, const NetAddr *array, int array_length);
00103 
00104     virtual bool PostPacket(u8 *buffer, u32 buf_bytes, u32 msg_bytes, u32 skip_bytes);
00105 
00106 private:
00107     bool ThreadFunction(void *param);
00108 };
00109 
00110 
00111 } // namespace sphynx
00112 
00113 
00114 } // namespace cat
00115 
00116 #endif // CAT_SPHYNX_CLIENT_HPP

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net