TCPSocketAsyncBlowFish.hGo to the documentation of this file.00001 /* 00002 * Copyright (c) 2000-2003 Barak Weichselbaum <barak@komodia.com> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00015 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00018 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00019 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00020 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00022 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00023 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00024 * SUCH DAMAGE. 00025 * 00026 * Contact info: 00027 * ------------- 00028 * 00029 * Site: http://www.komodia.com 00030 * Main contact: barak@komodia.com 00031 * For custom projects, 00032 * consulting, or other 00033 * paid services: sales@komodia.com 00034 */ 00035 00036 #if !defined(AFX_TCPSOCKETASYNCBLOWFISH_H__71B6C66D_832F_4EE5_9257_C55AEBBF048B__INCLUDED_) 00037 #define AFX_TCPSOCKETASYNCBLOWFISH_H__71B6C66D_832F_4EE5_9257_C55AEBBF048B__INCLUDED_ 00038 00039 #if _MSC_VER > 1000 00040 #pragma once 00041 #endif // _MSC_VER > 1000 00042 00043 #ifdef _SSLInheritance 00044 #include "TCPSocketAsyncSSL.h" 00045 #else 00046 #include "TCPSocketAsyncMsg.h" 00047 #endif 00048 00049 #include "BlowFish.h" 00050 00051 KOMODIA_NAMESPACE_START 00052 00053 #ifdef _SSLInheritance 00054 typedef CTCPSocketAsyncSSL BlowFishBaseClass; 00055 #else 00056 typedef CTCPSocketAsyncMsg BlowFishBaseClass; 00057 #endif 00058 00059 class CTCPSocketAsyncBlowFish : public BlowFishBaseClass 00060 { 00061 public: 00062 //Accept a connection, supply an already made socket 00063 virtual BOOL Accept(CTCPSocket* pNewSocket); 00064 00065 //Set/Get the key phrase 00066 void SetKey(const std::string& rKey); 00067 const std::string& GetKey()const; 00068 00069 //Receive a message 00070 //Returns number of bytes received or error code (-4 decryption problems!) 00071 //iMoreMessages - If more messages exist it will be 1 00072 // 0 for no more messages 00073 // -size for buffer too small to accept message so abs() will give the size 00074 virtual int ReceiveMsg(char* pData, 00075 int iSize, 00076 int& iMoreMessages); 00077 00078 //Send a message 00079 //Returns number of bytes sent, or -1 for error 00080 //-2 size mismatch 00081 virtual int SendMsg(const char* pData, 00082 int iSize); 00083 00084 //Send the message but abort if event is set 00085 //Same error codes as the normal SendMsg 00086 //-3 for user abort 00087 virtual int SendMsg(const char* pData, 00088 int iSize, 00089 CGenericEvent* pStopEvent); 00090 00091 //Ctor and Dtor 00092 CTCPSocketAsyncBlowFish(); 00093 virtual ~CTCPSocketAsyncBlowFish(); 00094 private: 00095 //Encrypt a memory buffer (will overwrite data, make sure buffer is 20 bytes longer the size) 00096 unsigned long EncryptMemory(char* pBuffer, 00097 unsigned long ulSize)const; 00098 00099 //Decrypt a memory buffer (will overwrite data) 00100 unsigned long DecryptMemory(char* pBuffer, 00101 unsigned long ulSize)const; 00102 00103 //Our key 00104 std::string m_sKey; 00105 00106 //Our blowfish encrypt chain 00107 mutable blowfish::Block m_aEncryptChain; 00108 00109 //Our blowfish decrypt chain 00110 mutable blowfish::Block m_aDecryptChain; 00111 }; 00112 00113 KOMODIA_NAMESPACE_END 00114 00115 #endif // !defined(AFX_TCPSOCKETASYNCBLOWFISH_H__71B6C66D_832F_4EE5_9257_C55AEBBF048B__INCLUDED_) |