Retail products


Traffic interception SDK

Control every TCP/IP network connection

  • Route connections via proxy
  • Redirect connections and modify the data
  • Block connections and applications
SSL interception SDK

View SSL in plaintext and modify it

  • View the SSL stream decrypted in plaintext
  • Redirect SSL connection and modify decrypted data
  • Browser shows "SSL lock" without warnings

Documentation


CTCPSocket Class Reference

#include <TCPSocket.h>

Inheritance diagram for CTCPSocket:
Collaboration diagram for CTCPSocket:

List of all members.


Public Types

enum  _SocketShutdown { ssReceive, ssSend, ssBoth }
typedef enum
CSocket::_SocketShutdown 
SocketShutdown

Public Member Functions

BOOL SetNagle (BOOL bNagle)
virtual BOOL Close ()
BOOL IsConnected () const
virtual int Send (const char *pBuffer, unsigned long ulBufferLength)
virtual BOOL Accept (CTCPSocket *pNewSocket)
CTCPSocketAccept ()
virtual BOOL Listen (unsigned long ulBackLog=5)
virtual BOOL Connect (unsigned short usSourcePort, IP aDestinationAddress, unsigned short usDestinationPort)
virtual BOOL Connect (unsigned short usSourcePort, const std::string &rDestinationAddress, unsigned short usDestinationPort)
virtual BOOL Connect (IP aDestinationAddress, unsigned short usDestinationPort)
virtual BOOL Connect (const std::string &rDestinationAddress, unsigned short usDestinationPort)
virtual BOOL Create ()
 CTCPSocket (BOOL bRawSocket=false)
virtual ~CTCPSocket ()
unsigned char GetProtocol () const
void SetProtocol (unsigned char iProtocol)
BOOL IsRaw () const
BOOL SetReceiveTimeout (unsigned long ulMS)
SOCKET GetHandle () const
virtual SOCKET Detach ()
BOOL SetBroadcast (BOOL bBroadcast)
BOOL IsCreated () const
BOOL CanWrite () const
BOOL CanRead () const
unsigned short GetPeerPort () const
BOOL Shutdown (SocketShutdown eHow)
long GetPeerAddress () const
virtual int Receive (char *pBuffer, unsigned long ulBufferLength)
virtual int Peek (char *pBuffer, unsigned long ulBufferLength)
void SetTTL (unsigned char ucTTL)
virtual void SetSourceAddress (IP aSourceAddress)
virtual void SetSourceAddress (const std::string &rSourceAddress)
virtual IP GetBindAddress () const
virtual unsigned short GetBindPort () const
virtual BOOL Bind (const std::string &rSourceAddress, unsigned short usPort)
virtual BOOL Bind (IP aSourceAddress, unsigned short usPort)
virtual int Send (IP aDestinationAddress, const char *pBuffer, unsigned long ulBufferLength, unsigned short usDestinationPort=0)
virtual int Send (const std::string &rDestinationAddress, const char *pBuffer, unsigned long ulBufferLength, unsigned short usDestinationPort=0)
BOOL Create (int iProtocol)
bool operator== (const CSocket &rSocket) const
bool operator< (const CSocket &rSocket) const
int GetLastError () const
void SetLocalLog (CErrorLog *pLog)
void WriteError (const std::string &rClass, const std::string &rMethod, const std::string &rError) const
void WriteMessage (const std::string &rClass, const std::string &rMethod, const std::string &rMessage) const

Static Public Member Functions

static int GetErrorCode ()
static int GetSystemLastError ()
static BOOL IsInitialized ()
static unsigned long StringToLong (const std::string &rAddress)
static char FAR * LongToString (unsigned long ulAddr)
static std::string LongToStdString (unsigned long ulAddr)
static BOOL InitializeSockets (BOOL bMultiThreaded=TRUE, unsigned long ulNumberOfThreads=10)
static BOOL ShutdownSockets ()
static unsigned long GetNumberOfThreads ()
static BOOL IsMultiThreaded ()
static CGenericCriticalSectionGetGlobalCriticalSection ()
static unsigned long ResolveDNS (const std::string &rAddress)
static BOOL ValidAddress (const std::string &rAddress)
static std::string GetCurrentDateTime ()
static std::string ErrorCodeToString (DWORD dwErrorCode)
static void RegisterError (DWORD dwErrorCode, const std::string &rDescription)
static void SetLog (CErrorLog *pLog)
static void WriteStaticError (const std::string &rClass, const std::string &rMethod, const std::string &rError)
static std::string FormatData (const std::string &rClass, const std::string &rMethod, const std::string &rMsgOrError, bool bError=true)

Protected Member Functions

virtual void BeforeAccept ()
virtual BOOL IsAsyncClass () const
void SetConnectionStatus (BOOL bConnected)
virtual void Accepted ()
void SetAcceptance (CTCPSocket *pNewSocket, SOCKET aSocket, sockaddr_in aAddress) const
void SetConnectedTo (const sockaddr_in &rAddress)
IP GetSourceAddress () const
unsigned char GetTTL () const
BOOL CheckSocketValid () const
void AssignSocket (SOCKET aNewSocket, unsigned char ucProtocol=IPPROTO_TCP)
BOOL ValidSocket () const
virtual void InitializeIP ()
virtual int ReceiveFrom (char *pBuffer, unsigned long ulBufferLength, IP &rIP, unsigned short &rSourcePort)
virtual void NotifyShutdown ()
void RegisterShutdown (CSocketBase *pBase)
void SetLastError (const std::string &rMethod) const
void SetLastError (const std::string &, int iErrorCode) const
const std::string & GetClassName () const
virtual void ReportError (const std::string &rMethod) const
virtual void ReportError (const std::string &rMethod, const std::string &rMessage) const
virtual void ReportError (const std::string &rMethod, const std::string &rMessage, DWORD dwAdditionalData) const
virtual void ReportError (const std::string &rMethod, const std::string &rMessage, const std::string &rAdditionalData) const
virtual void ReportError (const std::string &rMethod, int iErrorCode) const
virtual void ReportError (const std::string &rMethod, const std::string &rMessage, int iErrorCode) const
virtual void ReportErrorOS (const std::string &rMethod, const std::string &rMessage) const
void SetName (const std::string &rName) const

Static Protected Member Functions

static void ReportStaticError (const std::string &rClass, const std::string &rMethod)
static void ReportStaticError (const std::string &rClass, const std::string &rMethod, const std::string &rMessage)
static void ReportStaticError (const std::string &rClass, const std::string &rMethod, const std::string &rMessage, DWORD dwAdditionalData)
static void ReportStaticErrorOS (const std::string &rClass, const std::string &rMethod, const std::string &rMessage)

Static Protected Attributes

static CGenericCriticalSectionm_pCSection = NULL

Friends

class CIPCrafter

Detailed Description

Definition at line 47 of file TCPSocket.h.


Member Typedef Documentation


Member Enumeration Documentation

enum CSocket::_SocketShutdown [inherited]

Enumerator:
ssReceive 
ssSend 
ssBoth 

Definition at line 51 of file Socket.h.

00052     {
00053         ssReceive,
00054         ssSend,
00055         ssBoth
00056     } SocketShutdown;


Constructor & Destructor Documentation

CTCPSocket::CTCPSocket ( BOOL  bRawSocket = false  ) 

Definition at line 54 of file TCPSocket.cpp.

00054                                       : CSocket(bRawSocket)
00055 {
00056     try
00057     {
00058         //Set our name
00059         SetName(CTCPSocket_LOGNAME);
00060 
00061         //Initialize the TCP
00062         InitializeTCP();
00063     }
00064     ERROR_HANDLER("CTCPSocket")
00065 }

CTCPSocket::~CTCPSocket (  )  [virtual]

Definition at line 80 of file TCPSocket.cpp.

00081 {
00082 }


Member Function Documentation

CTCPSocket * CTCPSocket::Accept (  ) 

Definition at line 242 of file TCPSocket.cpp.

00243 {
00244     try
00245     {
00246         //Quit if not ok
00247         if (!CheckSocketValid())
00248             return FALSE;
00249 
00250         //First accept the socket
00251         SOCKET aSocket;
00252         
00253         //Where we are connected to
00254         sockaddr_in aConnected;
00255         
00256         //Size of the structure
00257         int iSize;
00258         iSize=sizeof(aConnected);
00259 
00260         //Accept it
00261         aSocket=accept(GetHandle(),
00262                        (sockaddr*)&aConnected,
00263                        &iSize);
00264 
00265         //Is all OK
00266         if (aSocket!=INVALID_SOCKET)
00267         {
00268             //Create the new tcp socket
00269             CTCPSocket* pSocket;
00270             pSocket=new CTCPSocket(aSocket);
00271 
00272             //Set the address
00273             pSocket->SetConnectedTo(aConnected);
00274             pSocket->SetConnectionStatus(TRUE);
00275 
00276             //Done
00277             return pSocket;
00278         }
00279         else
00280         {
00281             //Error
00282             SetLastError("Accept");
00283 
00284             //Exit
00285             return NULL;
00286         }
00287     }
00288     ERROR_HANDLER_RETURN("Accept",NULL)
00289 }

BOOL CTCPSocket::Accept ( CTCPSocket pNewSocket  )  [virtual]

Reimplemented in CTCPSocketAsync.

Definition at line 301 of file TCPSocket.cpp.

00302 {
00303     try
00304     {
00305         //Quit if not ok
00306         if (!CheckSocketValid())
00307             return FALSE;
00308 
00309         //First accept the socket
00310         SOCKET aNewSocket;
00311         
00312         //Where we are connected to
00313         sockaddr_in aAddress;
00314         
00315         //Size of the structure
00316         int iSize;
00317         iSize=sizeof(aAddress);
00318 
00319         //Accept it
00320         aNewSocket=accept(GetHandle(),
00321                           (sockaddr*)&aAddress,
00322                           &iSize);
00323 
00324         //Is it OK
00325         if (aNewSocket!=INVALID_SOCKET)
00326         {
00327             //Delegate
00328             SetAcceptance(pNewSocket,
00329                           aNewSocket,
00330                           aAddress);
00331 
00332             //Exit
00333             return TRUE;
00334         }
00335         else
00336         {
00337             //Error
00338             SetLastError("Accept");
00339 
00340             //Exit
00341             return FALSE;
00342         }
00343     }
00344     ERROR_HANDLER_RETURN("Accept",FALSE)
00345 }

void CTCPSocket::Accepted (  )  [protected, virtual]

Definition at line 390 of file TCPSocket.cpp.

00391 {
00392 }

void CSocket::AssignSocket ( SOCKET  aNewSocket,
unsigned char  ucProtocol = IPPROTO_TCP 
) [protected, inherited]

Definition at line 500 of file Socket.cpp.

00502 {
00503     try
00504     {
00505         //Sets the protocol
00506         m_ucProtocol=ucProtocol;
00507 
00508         //Binds to a socket
00509         m_aSocket=aNewSocket;
00510     }
00511     ERROR_HANDLER("AssignSocket")
00512 }

void CTCPSocket::BeforeAccept (  )  [protected, virtual]

Definition at line 469 of file TCPSocket.cpp.

00470 {
00471 }

BOOL CSocket::Bind ( IP  aSourceAddress,
unsigned short  usPort 
) [virtual, inherited]

Reimplemented in CTCPSocketAsync.

Definition at line 269 of file Socket.cpp.

00271 {
00272     try
00273     {
00274         //Quit if not ok
00275         if (!CheckSocketValid())
00276             return FALSE;
00277 
00278         //Create the local address
00279         sockaddr_in soSrc;
00280 
00281         //Set to 0
00282         memset(&soSrc,0,sizeof(soSrc));
00283         soSrc.sin_family=AF_INET;
00284 
00285         //Populate the connection data
00286         if (aSourceAddress)
00287             soSrc.sin_addr.s_addr=aSourceAddress;
00288         else
00289             soSrc.sin_addr.s_addr=ADDR_ANY ;
00290 
00291         soSrc.sin_port=htons(usPort);
00292 
00293         //Now we need to bind it
00294         if (bind(GetHandle(),
00295                  (sockaddr*)&soSrc,
00296                  sizeof(soSrc)))
00297         {
00298             //Error
00299             SetLastError("Bind");
00300 
00301             //Exit
00302             return FALSE;
00303         }
00304         else
00305             //Save the address
00306             m_aConnectedTo=soSrc;
00307 
00308         //Save new addresses
00309         if (aSourceAddress &&
00310             !m_ulSourceAddress)
00311             m_ulSourceAddress=aSourceAddress;
00312 
00313         return TRUE;
00314     }
00315     ERROR_HANDLER_RETURN("Bind",FALSE)
00316 }

BOOL CSocket::Bind ( const std::string &  rSourceAddress,
unsigned short  usPort 
) [virtual, inherited]

Definition at line 318 of file Socket.cpp.

00320 {
00321     try
00322     {
00323         //Quit if not ok
00324         if (!CheckSocketValid())
00325             return FALSE;
00326 
00327         if (rSourceAddress.empty())
00328             return Bind((IP)0,
00329                         usPort);
00330         else
00331             return Bind(inet_addr(rSourceAddress.c_str()),
00332                         usPort);
00333     }
00334     ERROR_HANDLER_RETURN("Bind",FALSE)
00335 }

BOOL CSocket::CanRead (  )  const [inherited]

Definition at line 721 of file Socket.cpp.

00722 {
00723     try
00724     {
00725         //Quit if not ok
00726         if (!CheckSocketValid())
00727             return FALSE;
00728 
00729         //Create our structure
00730         fd_set aDescriptor;
00731         FD_ZERO(&aDescriptor);
00732 
00733         //Add our socket
00734         FD_SET(GetHandle(),
00735                &aDescriptor);
00736 
00737         //And create the timeval
00738         timeval aTime;
00739         aTime.tv_sec=0;
00740         aTime.tv_usec=0;
00741 
00742         //And run the select
00743         int iRetVal;
00744         iRetVal=select(NULL,
00745                        &aDescriptor,
00746                        NULL,
00747                        NULL,
00748                        &aTime);
00749 
00750         //Check if we had an error
00751         if (iRetVal==GetErrorCode())
00752         {
00753             //Report it
00754             SetLastError("CanRead");
00755 
00756             //Exit
00757             return FALSE;
00758         }
00759         else
00760             //Check is our socket set
00761             return FD_ISSET(GetHandle(),
00762                             &aDescriptor);
00763     }
00764     ERROR_HANDLER_RETURN("CanRead",FALSE)
00765 }

BOOL CSocket::CanWrite (  )  const [inherited]

Definition at line 767 of file Socket.cpp.

00768 {
00769     try
00770     {
00771         //Quit if not ok
00772         if (!CheckSocketValid())
00773             return FALSE;
00774 
00775         //Create our structure
00776         fd_set aDescriptor;
00777         FD_ZERO(&aDescriptor);
00778 
00779         //Add our socket
00780         FD_SET(GetHandle(),
00781                &aDescriptor);
00782 
00783         //And create the timeval
00784         timeval aTime;
00785         aTime.tv_sec=0;
00786         aTime.tv_usec=0;
00787 
00788         //And run the select
00789         int iRetVal;
00790         iRetVal=select(NULL,
00791                        NULL,
00792                        &aDescriptor,
00793                        NULL,
00794                        &aTime);
00795 
00796         //Check if we had an error
00797         if (iRetVal==GetErrorCode())
00798         {
00799             //Report it
00800             SetLastError("CanWrite");
00801 
00802             //Exit
00803             return FALSE;
00804         }
00805         else
00806             //Check is our socket set
00807             return FD_ISSET(GetHandle(),
00808                             &aDescriptor);
00809     }
00810     ERROR_HANDLER_RETURN("CanWrite",FALSE)
00811 }

BOOL CSocket::CheckSocketValid (  )  const [protected, inherited]

Definition at line 387 of file Socket.cpp.

00388 {
00389     try
00390     {
00391         //Check if socket is invalid
00392         if (!ValidSocket())
00393         {
00394             //Report it
00395             ReportError("CheckSocketValid","Operation made on non existant socket!");
00396 
00397             //Exit
00398             return FALSE;
00399         }   
00400 
00401         //OK
00402         return TRUE;
00403     }
00404     ERROR_HANDLER_RETURN("CheckSocketValid",FALSE)
00405 }

BOOL CTCPSocket::Close (  )  [virtual]

Reimplemented from CSocket.

Reimplemented in CTCPSocketAsync.

Definition at line 446 of file TCPSocket.cpp.

00447 {
00448     try
00449     {
00450         //Quit if not ok
00451         if (!CheckSocketValid())
00452             return FALSE;
00453 
00454         //Check if the father can close
00455         if (CSocket::Close())
00456         {
00457             //Set the connection status
00458             SetConnectionStatus(FALSE);
00459 
00460             //And exit
00461             return TRUE;
00462         }
00463         else
00464             return FALSE;
00465     }
00466     ERROR_HANDLER_RETURN("Close",FALSE)
00467 }

BOOL CTCPSocket::Connect ( const std::string &  rDestinationAddress,
unsigned short  usDestinationPort 
) [virtual]

Definition at line 115 of file TCPSocket.cpp.

00117 {
00118     try
00119     {
00120         return Connect(0,
00121                        rDestinationAddress,
00122                        usDestinationPort);
00123     }
00124     ERROR_HANDLER_RETURN("Connect",FALSE)
00125 }

BOOL CTCPSocket::Connect ( IP  aDestinationAddress,
unsigned short  usDestinationPort 
) [virtual]

Definition at line 103 of file TCPSocket.cpp.

00105 {
00106     try
00107     {
00108         return Connect(0,
00109                        aDestinationAddress,
00110                        usDestinationPort);
00111     }
00112     ERROR_HANDLER_RETURN("Connect",FALSE)
00113 }

BOOL CTCPSocket::Connect ( unsigned short  usSourcePort,
const std::string &  rDestinationAddress,
unsigned short  usDestinationPort 
) [virtual]

Definition at line 196 of file TCPSocket.cpp.

00199 {
00200     try
00201     {
00202         //Quit if not ok
00203         if (!CheckSocketValid())
00204             return FALSE;
00205 
00206         //Delegate the call
00207         return Connect(usSourcePort,
00208                        StringToLong(rDestinationAddress),
00209                        usDestinationPort);
00210     }
00211     ERROR_HANDLER_RETURN("Connect",FALSE)
00212 }

BOOL CTCPSocket::Connect ( unsigned short  usSourcePort,
IP  aDestinationAddress,
unsigned short  usDestinationPort 
) [virtual]

Definition at line 127 of file TCPSocket.cpp.

00130 {
00131     try
00132     {
00133         //Quit if not ok
00134         if (!CheckSocketValid())
00135             return FALSE;
00136 
00137         //Set async notification
00138         int iResult;
00139         
00140         //Create the address
00141         sockaddr_in aSrc;
00142     
00143         //Set to 0
00144         memset(&aSrc,
00145                0,
00146                sizeof(aSrc));
00147         aSrc.sin_family=AF_INET;
00148         aSrc.sin_addr.s_addr=aDestinationAddress;
00149         aSrc.sin_port=htons(usDestinationPort);
00150 
00151         //Connect
00152         iResult=connect(GetHandle(),
00153                         (sockaddr*)&aSrc,
00154                         sizeof(aSrc));
00155 
00156         //Did we have a blocked error
00157         BOOL bBlocked;
00158         bBlocked=FALSE;
00159 
00160         //Check the result
00161         if (iResult==GetErrorCode())
00162         {
00163             //Check is it blocking error so we can ignore
00164             if (WSAGetLastError()!=WSAEWOULDBLOCK)
00165                 SetLastError("Connect");
00166             else
00167             {
00168                 //Indicate it's blocked error
00169                 bBlocked=TRUE;
00170                 iResult=!GetErrorCode();
00171             }
00172         }
00173         else
00174             //Report the error
00175             SetLastError("Connect");
00176         
00177         if (iResult!=GetErrorCode())
00178         {
00179             //Check if we are a sync socket
00180             if (!IsAsyncClass())
00181                 SetConnectionStatus(TRUE);
00182 
00183             //Save where we are connected
00184             SetConnectedTo(aSrc);
00185         }
00186 
00187         //Done
00188         if (!bBlocked)
00189             return iResult!=GetErrorCode();
00190         else
00191             return FALSE;
00192     }
00193     ERROR_HANDLER_RETURN("Connect",FALSE)
00194 }

BOOL CSocket::Create ( int  iProtocol  )  [inherited]

Definition at line 98 of file Socket.cpp.

00099 {
00100     //Close the socket if open
00101     if (ValidSocket())
00102         Close();
00103 
00104     try
00105     {
00106         //Are we overlapped
00107         if (!m_bOverlapped)
00108         {
00109             //Here we create the raw socket
00110             if (m_bRaw || iProtocol==IPPROTO_ICMP)
00111                 m_aSocket=socket(AF_INET,
00112                                       SOCK_RAW,
00113                                       iProtocol);
00114             else
00115                 if (iProtocol==IPPROTO_TCP)
00116                     m_aSocket=socket(AF_INET,
00117                                           SOCK_STREAM,
00118                                           iProtocol);
00119                 else if (iProtocol==IPPROTO_UDP)
00120                     m_aSocket=socket(AF_INET,
00121                                           SOCK_DGRAM,
00122                                           iProtocol);
00123         }
00124         else
00125         {
00126             //Here we create the raw socket
00127             if (m_bRaw || iProtocol==IPPROTO_ICMP)
00128                 m_aSocket=WSASocket(AF_INET,
00129                                          SOCK_RAW,
00130                                          iProtocol,
00131                                          NULL,
00132                                          NULL,
00133                                          WSA_FLAG_OVERLAPPED);
00134             else
00135                 if (iProtocol==IPPROTO_TCP)
00136                     m_aSocket=WSASocket(AF_INET,
00137                                              SOCK_STREAM,
00138                                              iProtocol,
00139                                              NULL,
00140                                              NULL,
00141                                              WSA_FLAG_OVERLAPPED);
00142                 else if (iProtocol==IPPROTO_UDP)
00143                     m_aSocket=WSASocket(AF_INET,
00144                                              SOCK_DGRAM,
00145                                              iProtocol,
00146                                              NULL,
00147                                              NULL,
00148                                              WSA_FLAG_OVERLAPPED);
00149         }
00150 
00151         //Check for socket validity
00152         if (m_aSocket==INVALID_SOCKET)
00153         {
00154             //Error
00155             SetLastError("Create");
00156 
00157             //Done
00158             return FALSE;
00159         }
00160 
00161         if (m_bRaw)
00162         {
00163             //Set that the application will send the IP header
00164             unsigned int iTrue=1;
00165 
00166             if(setsockopt(m_aSocket,
00167                           IPPROTO_IP,
00168                           IP_HDRINCL,
00169                           (char*)&iTrue,
00170                           sizeof(iTrue))==GetErrorCode())
00171             {
00172                 //Check for options error
00173                 SetLastError("Create");
00174 
00175                 //Exit
00176                 return FALSE;
00177             }
00178         }
00179 
00180         //Done
00181         return TRUE;
00182     }
00183     ERROR_HANDLER_RETURN("Create",FALSE)
00184 }

BOOL CTCPSocket::Create (  )  [virtual]

Reimplemented in CWhoisSocket, and CTCPSocketAsync.

Definition at line 84 of file TCPSocket.cpp.

00085 {
00086     try
00087     {
00088         //Initialize our data
00089         InitializeTCP();
00090 
00091         //Create it
00092         SetProtocol(IPPROTO_TCP);
00093 
00094         //Are we raw?
00095         if (IsRaw())
00096             return CSocket::Create(IPPROTO_IP); //Can't create working raw TCP socket
00097         else
00098             return CSocket::Create(IPPROTO_TCP);
00099     }
00100     ERROR_HANDLER_RETURN("Create",FALSE)
00101 }

SOCKET CSocket::Detach (  )  [virtual, inherited]

Reimplemented in CTCPSocketAsync.

Definition at line 374 of file Socket.cpp.

00375 {
00376     //Save the socket
00377     SOCKET aTmp;
00378     aTmp=m_aSocket;
00379 
00380     //Erase it
00381     m_aSocket=INVALID_SOCKET;
00382 
00383     //Return the original
00384     return aTmp;
00385 }

std::string CErrorHandler::ErrorCodeToString ( DWORD  dwErrorCode  )  [static, inherited]

Definition at line 218 of file ErrorHandler.cpp.

00219 {
00220     try
00221     {
00222         //Try to look it in the errors
00223         if (!CErrorsRepository::GetInstance().GetErrorsMap().empty())
00224         {
00225             //Search
00226             CErrorsRepository::ErrorMap::const_iterator aIterator;
00227             aIterator=CErrorsRepository::GetInstance().GetErrorsMap().find(dwErrorCode);
00228 
00229             //Do we have it
00230             if (aIterator!=CErrorsRepository::GetInstance().GetErrorsMap().end())
00231                 return aIterator->second;
00232         }
00233 
00234         //Get the error string
00235         LPVOID lpMsgBuf;
00236         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00237                       FORMAT_MESSAGE_FROM_SYSTEM | 
00238                       FORMAT_MESSAGE_IGNORE_INSERTS,
00239                       NULL,
00240                       dwErrorCode,
00241                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00242                       (LPTSTR) &lpMsgBuf,
00243                       0,
00244                       NULL);
00245 
00246         //Save it
00247         std::string sMessage;
00248 
00249         //Do we have the message?
00250         if (lpMsgBuf)
00251         {
00252             //Save it
00253             sMessage=(char*)lpMsgBuf;
00254             
00255             //Release the buffer
00256             LocalFree(lpMsgBuf);
00257         }
00258         else
00259             //Failed to find
00260             sMessage="No error description found!";
00261 
00262         //Done
00263         return sMessage;
00264     }
00265     catch (...)
00266     {
00267         return "Unknown";
00268     }
00269 }

std::string CErrorHandler::FormatData ( const std::string &  rClass,
const std::string &  rMethod,
const std::string &  rMsgOrError,
bool  bError = true 
) [static, inherited]

Definition at line 140 of file ErrorHandler.cpp.

00144 {
00145     //Our message
00146     std::string sMsg;
00147 
00148     //Is it an error?
00149     if (bError)
00150         sMsg="*** Error *** ";
00151     else
00152         sMsg="###  Msg  ### ";
00153 
00154     //Add the data now
00155     sMsg+="in "+rClass;
00156     sMsg+=":"+rMethod;
00157     sMsg+=" - " + rMsgOrError;
00158 
00159     //Done
00160     return sMsg;
00161 }

IP CSocket::GetBindAddress (  )  const [virtual, inherited]

Reimplemented in CTCPSocketAsync.

Definition at line 902 of file Socket.cpp.

00903 {
00904     return m_ulSourceAddress;
00905 }

unsigned short CSocket::GetBindPort (  )  const [virtual, inherited]

Reimplemented in CTCPSocketAsync.

Definition at line 337 of file Socket.cpp.

00338 {
00339     try
00340     {
00341         //Quit if not ok
00342         if (!CheckSocketValid())
00343             return FALSE;
00344 
00345         //Create the local address
00346         sockaddr_in soSrc;
00347 
00348         //Buffer size
00349         int iSize;
00350         iSize=sizeof(soSrc);
00351 
00352         //Try to get it
00353         if (getsockname(GetHandle(),
00354                         (sockaddr*)&soSrc,
00355                         &iSize))
00356         {
00357             //Error
00358             SetLastError("Bind");
00359 
00360             //Exit
00361             return 0;
00362         }
00363         else
00364             return htons(soSrc.sin_port);
00365     }
00366     ERROR_HANDLER_RETURN("GetBindPort",0)
00367 }

const std::string & CErrorHandler::GetClassName (  )  const [protected, inherited]

Reimplemented in CSocketThreadManager.

Definition at line 567 of file ErrorHandler.cpp.

00568 {
00569     return m_sClassName;
00570 }

std::string CErrorHandler::GetCurrentDateTime (  )  [static, inherited]

Definition at line 572 of file ErrorHandler.cpp.

00573 {
00574     try
00575     {
00576         //Our string
00577         std::string sDate;
00578 
00579         //Our tmp buf
00580         char cTmp[128];
00581 
00582         //Get date
00583         _strdate(cTmp);
00584         sDate=cTmp;
00585         sDate+=' ';
00586 
00587         //Get time
00588         _strtime(cTmp);
00589         sDate+=cTmp;
00590 
00591         //Done
00592         return sDate;
00593     }
00594     ERROR_HANDLER_STATIC_RETURN(CErrorHandler_Class,"GetCurrentDateTime","")
00595 }

int CSocketBase::GetErrorCode (  )  [static, inherited]

Definition at line 368 of file SocketBase.cpp.

00369 {
00370     return SOCKET_ERROR;
00371 }

CGenericCriticalSection * CSocketBase::GetGlobalCriticalSection (  )  [static, inherited]

Definition at line 363 of file SocketBase.cpp.

00364 {
00365     return m_pCSection;
00366 }

SOCKET CSocket::GetHandle (  )  const [inherited]

Definition at line 369 of file Socket.cpp.

00370 {
00371     return m_aSocket;
00372 }

int CSocketBase::GetLastError (  )  const [inherited]

Definition at line 141 of file SocketBase.cpp.

00142 {
00143     return m_iLastError;
00144 }

unsigned long CSocketBase::GetNumberOfThreads (  )  [static, inherited]

Definition at line 334 of file SocketBase.cpp.

00335 {
00336     return m_ulNumberOfThreads;
00337 }

long CSocket::GetPeerAddress (  )  const [inherited]

Definition at line 677 of file Socket.cpp.

00678 {
00679     //Get the address we are connected to
00680     return m_aConnectedTo.sin_addr.S_un.S_addr;
00681 }

unsigned short CSocket::GetPeerPort (  )  const [inherited]

Definition at line 716 of file Socket.cpp.

00717 {
00718     return htons(m_aConnectedTo.sin_port);
00719 }

unsigned char CSocket::GetProtocol (  )  const [inherited]

Definition at line 855 of file Socket.cpp.

00856 {
00857     return m_ucProtocol;
00858 }

IP CSocket::GetSourceAddress (  )  const [protected, inherited]

Definition at line 850 of file Socket.cpp.

00851 {
00852     return m_ulSourceAddress;
00853 }

int CSocketBase::GetSystemLastError (  )  [static, inherited]

Definition at line 358 of file SocketBase.cpp.

00359 {
00360     return WSAGetLastError();
00361 }

unsigned char CSocket::GetTTL (  )  const [protected, inherited]

Definition at line 845 of file Socket.cpp.

00846 {
00847     return m_ucTTL;
00848 }

void CSocket::InitializeIP (  )  [protected, virtual, inherited]

Definition at line 476 of file Socket.cpp.

00477 {
00478     try
00479     {
00480         //Invalid the socket
00481         m_aSocket=INVALID_SOCKET;
00482 
00483         //More invalids
00484         m_ulSourceAddress=0;
00485 
00486         //Some defaults
00487         m_ucTTL=64;
00488 
00489         //We are not overlapped
00490         m_bOverlapped=FALSE;
00491 
00492         //Not connected
00493         memset(&m_aConnectedTo,
00494                0,
00495                sizeof(m_aConnectedTo));
00496     }
00497     ERROR_HANDLER("InitializeIP")
00498 }

BOOL CSocketBase::InitializeSockets ( BOOL  bMultiThreaded = TRUE,
unsigned long  ulNumberOfThreads = 10 
) [static, inherited]

Definition at line 146 of file SocketBase.cpp.

00148 {
00149     //To avoid double initialize
00150     if (m_bInitialized)
00151     {
00152         //Report it
00153         ReportStaticError(CSocketBase_Class,"InitializeSockets","Already initialized!");
00154 
00155         //Exit
00156         return TRUE;
00157     }
00158 
00159     //Check that the number of threads are OK?
00160     if (ulNumberOfThreads>CLibConfig::GetInstance().GetMaxThreads())
00161     {
00162         //Report it
00163         ReportStaticError(CSocketBase_Class,"InitializeSockets","Too many threads!");
00164 
00165         //Exit
00166         return FALSE;
00167     }
00168 
00169     //Do we have threads at all
00170     if (bMultiThreaded &&
00171         !ulNumberOfThreads)
00172     {
00173         //Report it
00174         ReportStaticError(CSocketBase_Class,"InitializeSockets","Didn't receive any threads!");
00175 
00176         //Exit
00177         return FALSE;
00178     }
00179 
00180     try
00181     {
00182         //Initialize the sockets
00183         WORD wVersionRequested;
00184         wVersionRequested=MAKEWORD(2,2);
00185  
00186         //Try to initialize
00187         WSADATA wsaData;
00188         int iErr;
00189         iErr=WSAStartup(wVersionRequested, 
00190                         &wsaData);
00191 
00192         //Did we succeed?
00193         if (iErr!=0)
00194             /* Tell the user that we could not find a usable */
00195             /* WinSock DLL.                                  */
00196             return FALSE;
00197  
00198         /* Confirm that the WinSock DLL supports 2.2.*/
00199         /* Note that if the DLL supports versions greater    */
00200         /* than 2.2 in addition to 2.2, it will still return */
00201         /* 2.2 in wVersion since that is the version we      */
00202         /* requested.                                        */
00203  
00204         if (LOBYTE(wsaData.wVersion)!=2 || 
00205             HIBYTE(wsaData.wVersion)!=2)
00206         {
00207             /* Tell the user that we could not find a usable */
00208             /* WinSock DLL.                                  */
00209             WSACleanup();
00210 
00211             //Exit
00212             return FALSE;
00213         }
00214 
00215         //Save the threading information
00216         m_bMultiThreaded=bMultiThreaded;
00217         m_ulNumberOfThreads=ulNumberOfThreads;
00218 
00219         //Create the critical section
00220         m_pCSection=COSManager::CreateCriticalSection();
00221         m_pCSectionDNS=COSManager::CreateCriticalSection();
00222 
00223         //And we are initialized
00224         m_bInitialized=TRUE;
00225 
00226         return TRUE;
00227     }
00228     ERROR_HANDLER_STATIC_RETURN(CSocketBase_Class,"InitializeSockets",FALSE)
00229 }

BOOL CTCPSocket::IsAsyncClass (  )  const [protected, virtual]

Reimplemented in CTCPSocketAsync.

Definition at line 404 of file TCPSocket.cpp.

00405 {
00406     return FALSE;
00407 }

BOOL CTCPSocket::IsConnected (  )  const

Definition at line 399 of file TCPSocket.cpp.

00400 {
00401     return m_bConnected;
00402 }

BOOL CSocket::IsCreated (  )  const [inherited]

Definition at line 813 of file Socket.cpp.

00814 {
00815     return ValidSocket();
00816 }

BOOL CSocketBase::IsInitialized (  )  [static, inherited]

Definition at line 339 of file SocketBase.cpp.

00340 {
00341     return m_bInitialized;
00342 }

BOOL CSocketBase::IsMultiThreaded (  )  [static, inherited]

Definition at line 329 of file SocketBase.cpp.

00330 {
00331     return m_bMultiThreaded;
00332 }

BOOL CSocket::IsRaw (  )  const [inherited]

Definition at line 471 of file Socket.cpp.

00472 {
00473     return m_bRaw;
00474 }

BOOL CTCPSocket::Listen ( unsigned long  ulBackLog = 5  )  [virtual]

Reimplemented in CTCPSocketAsync.

Definition at line 214 of file TCPSocket.cpp.

00215 {
00216     try
00217     {
00218         //Quit if not ok
00219         if (!CheckSocketValid())
00220             return FALSE;
00221 
00222         //Try to listen
00223         int iResult;
00224         iResult=listen(GetHandle(),
00225                        ulBackLog);
00226 
00227         //Is all OK
00228         if (iResult)
00229         {
00230             //Report it
00231             SetLastError("Listen");
00232 
00233             //Exit
00234             return FALSE;
00235         }
00236         else
00237             return TRUE;
00238     }
00239     ERROR_HANDLER_RETURN("Listen",FALSE)
00240 }

std::string CSocketBase::LongToStdString ( unsigned long  ulAddr  )  [static, inherited]

Definition at line 291 of file SocketBase.cpp.

00292 {
00293     try
00294     {
00295         //First create the address
00296         in_addr addr;
00297 
00298         //Assign it
00299         addr.S_un.S_addr=ulAddr;
00300 
00301         //Enter the critical section
00302         CCriticalAutoRelease aRelease(m_pCSection);
00303 
00304         //Return the value
00305         return inet_ntoa(addr);
00306     }
00307     ERROR_HANDLER_STATIC_RETURN(CSocketBase_Class,"LongToStdString","0.0.0.0")
00308 }

char FAR * CSocketBase::LongToString ( unsigned long  ulAddr  )  [static, inherited]

Definition at line 310 of file SocketBase.cpp.

00311 {
00312     try
00313     {
00314         //First create the address
00315         in_addr addr;
00316 
00317         //Assign it
00318         addr.S_un.S_addr=ulAddr;
00319 
00320         //Enter the critical section
00321         CCriticalAutoRelease aRelease(m_pCSection);
00322 
00323         //Return the value
00324         return inet_ntoa(addr);
00325     }
00326     ERROR_HANDLER_STATIC_RETURN(CSocketBase_Class,"LongToString",NULL)
00327 }

void CSocketBase::NotifyShutdown (  )  [protected, virtual, inherited]

Definition at line 273 of file SocketBase.cpp.

00274 {
00275 }

bool CSocket::operator< ( const CSocket rSocket  )  const [inherited]

Definition at line 892 of file Socket.cpp.

00893 {
00894     return m_aSocket<rSocket.m_aSocket;
00895 }

bool CSocket::operator== ( const CSocket rSocket  )  const [inherited]

Definition at line 887 of file Socket.cpp.

00888 {
00889     return m_aSocket==rSocket.m_aSocket;
00890 }

int CSocket::Peek ( char *  pBuffer,
unsigned long  ulBufferLength 
) [virtual, inherited]

Reimplemented in CTCPSocketAsync.

Definition at line 665 of file Socket.cpp.

00667 {
00668     try
00669     {
00670         return LocalReceive(pBuffer,
00671                             ulBufferLength,
00672                             TRUE);
00673     }
00674     ERROR_HANDLER_RETURN("Peek",GetErrorCode())
00675 }

int CSocket::Receive ( char *  pBuffer,
unsigned long  ulBufferLength 
) [virtual, inherited]

Reimplemented in CTCPSocketAsync, and CUDPSocket.

Definition at line 653 of file Socket.cpp.

00655 {
00656     try
00657     {
00658         return LocalReceive(pBuffer,
00659                             ulBufferLength,
00660                             FALSE);
00661     }
00662     ERROR_HANDLER_RETURN("Receive",GetErrorCode())
00663 }

int CSocket::ReceiveFrom ( char *  pBuffer,
unsigned long  ulBufferLength,
IP rIP,
unsigned short &  rSourcePort 
) [protected, virtual, inherited]

Definition at line 514 of file Socket.cpp.

00518 {
00519     try
00520     {
00521         if (!ValidSocket() ||
00522             !pBuffer ||
00523             !ulBufferLength)
00524             return GetErrorCode();
00525 
00526         //Receive data
00527         int iResult;
00528 
00529         //Receive
00530         if (m_ucProtocol!=IPPROTO_TCP)
00531         {
00532             //Get the remote address
00533             sockaddr saConnected;
00534 
00535             int iTmp;
00536             iTmp=sizeof(saConnected);
00537 
00538             //Accept it
00539             iResult=recvfrom(GetHandle(),
00540                              pBuffer,
00541                              ulBufferLength,
00542                              NULL,
00543                              &saConnected,
00544                              &iTmp);
00545 
00546             //If OK set it
00547             if (iResult!=GetErrorCode())
00548             {
00549                 //Address
00550                 rIP=((sockaddr_in*)&saConnected)->sin_addr.S_un.S_addr;
00551 
00552                 //Port
00553                 rSourcePort=htons(((sockaddr_in*)&saConnected)->sin_port);
00554 
00555                 //Done
00556                 return iResult;
00557             }
00558             else
00559             {
00560                 //Error
00561                 SetLastError("Receive");
00562 
00563                 //Reset the data
00564                 rIP=0;
00565                 rSourcePort=0;
00566 
00567                 //Done
00568                 return iResult;
00569             }
00570         }
00571         else
00572         {
00573             //Report it
00574             ReportError("Receive","Can't run on TCP socket!");
00575 
00576             //Exit
00577             return GetErrorCode();
00578         }
00579     }
00580     ERROR_HANDLER_RETURN("Receive",GetErrorCode())
00581 }

void CErrorHandler::RegisterError ( DWORD  dwErrorCode,
const std::string &  rDescription 
) [static, inherited]

Definition at line 597 of file ErrorHandler.cpp.

00599 {
00600     try
00601     {
00602         //Add the error
00603         CErrorsRepository::GetInstance().GetErrorsMap().insert(CErrorsRepository::ErrorMap::value_type(dwErrorCode,rDescription));
00604     }
00605     ERROR_HANDLER_STATIC(CErrorHandler_Class,"RegisterError")
00606 }

void CSocketBase::RegisterShutdown ( CSocketBase pBase  )  [protected, inherited]

Definition at line 277 of file SocketBase.cpp.

00278 {
00279     try
00280     {
00281         //Check if we already have a class
00282         if (m_pShutdownClass)
00283             delete m_pShutdownClass;
00284 
00285         //Take it
00286         m_pShutdownClass=pBase;
00287     }
00288     ERROR_HANDLER("RegisterShutdown")
00289 }

void CErrorHandler::ReportError ( const std::string &  rMethod,
const std::string &  rMessage,
int  iErrorCode 
) const [protected, virtual, inherited]

Definition at line 271 of file ErrorHandler.cpp.

00274 {
00275     if (!GetLog())
00276         return;
00277 
00278     try
00279     {
00280         //Convert the error code
00281         char aTmp[11];
00282         sprintf(aTmp,"%d",iErrorCode);
00283 
00284         //Get the string for it
00285         std::string sError;
00286         sError=rMessage;
00287         sError+=", and Socket error: ";
00288         sError+=aTmp;
00289         sError+=", ";
00290         sError+=ErrorCodeToString(iErrorCode);
00291 
00292         //Report to the log
00293         WriteError(m_sClassName,
00294                    rMethod,
00295                    sError);
00296     }
00297     ERROR_UNKNOWN("ReportError")
00298 }

void CErrorHandler::ReportError ( const std::string &  rMethod,
int  iErrorCode 
) const [protected, virtual, inherited]

Definition at line 300 of file ErrorHandler.cpp.

00302 {
00303     if (!GetLog())
00304         return;
00305 
00306     try
00307     {
00308         //Convert the error code
00309         char aTmp[11];
00310         sprintf(aTmp,"%d",iErrorCode);
00311 
00312         //Get the string for it
00313         std::string sError;
00314         sError="Socket error: ";
00315         sError+=aTmp;
00316         sError+=", ";
00317         sError+=ErrorCodeToString(iErrorCode);
00318 
00319         //Report to the log
00320         WriteError(m_sClassName,
00321                    rMethod,
00322                    sError);
00323     }
00324     ERROR_UNKNOWN("ReportError")
00325 }

void CErrorHandler::ReportError ( const std::string &  rMethod,
const std::string &  rMessage,
const std::string &  rAdditionalData 
) const [protected, virtual, inherited]

Definition at line 416 of file ErrorHandler.cpp.

00419 {
00420     if (!GetLog())
00421         return;
00422 
00423     try
00424     {
00425         //Create the new message
00426         std::string sNewMessage(rMessage);
00427         sNewMessage+=", Additional data: ";
00428         sNewMessage+=rAdditionalData;
00429 
00430         //Report to the log
00431         WriteError(m_sClassName,
00432                    rMethod,
00433                    sNewMessage);
00434     }
00435     ERROR_UNKNOWN("ReportError")
00436 }

void CErrorHandler::ReportError ( const std::string &  rMethod,
const std::string &  rMessage,
DWORD  dwAdditionalData 
) const [protected, virtual, inherited]

Definition at line 438 of file ErrorHandler.cpp.

00441 {
00442     if (!GetLog())
00443         return;
00444 
00445     try
00446     {
00447         //Convert the number
00448         char aTmp[11];
00449         ltoa(dwAdditionalData,aTmp,10);
00450 
00451         //Create the new message
00452         std::string sNewMessage(rMessage);
00453         sNewMessage+=", Additional data: ";
00454         sNewMessage+=aTmp;
00455 
00456         //Report to the log
00457         WriteError(m_sClassName,
00458                    rMethod,
00459                    sNewMessage);
00460     }
00461     ERROR_UNKNOWN("ReportError")
00462 }

void CErrorHandler::ReportError ( const std::string &  rMethod,
const std::string &  rMessage 
) const [protected, virtual, inherited]

Definition at line 327 of file ErrorHandler.cpp.

00329 {
00330     if (!GetLog())
00331         return;
00332 
00333     try
00334     {
00335         //Report to the log
00336         WriteError(m_sClassName,
00337                    rMethod,
00338                    rMessage);
00339     }
00340     ERROR_UNKNOWN("ReportError")
00341 }

void CErrorHandler::ReportError ( const std::string &  rMethod  )  const [protected, virtual, inherited]

Definition at line 470 of file ErrorHandler.cpp.

00471 {
00472     if (!GetLog())
00473         return;
00474 
00475     try
00476     {
00477         //Get the last error
00478         DWORD dwLastError;
00479         dwLastError=GetLastError();
00480 
00481         //Report the error
00482         WriteError(m_sClassName,
00483                    rMethod,
00484                    ErrorCodeToString(dwLastError));
00485     }
00486     ERROR_UNKNOWN("ReportError")
00487 }

void CErrorHandler::ReportErrorOS ( const std::string &  rMethod,
const std::string &  rMessage 
) const [protected, virtual, inherited]

Definition at line 378 of file ErrorHandler.cpp.

00380 {
00381     if (!GetLog())
00382         return;
00383 
00384     try
00385     {
00386         //Get the last error
00387         DWORD dwLastError;
00388         dwLastError=GetLastError();
00389 
00390         //Format the message
00391         std::string sMessage;
00392         sMessage=rMessage;
00393         sMessage+=", with error code: ";
00394 
00395         //Convert the error code
00396         char aTmp[11];
00397         itoa(dwLastError,aTmp,10);
00398 
00399         //Add it again
00400         sMessage+=aTmp;
00401         sMessage+=" ";
00402         sMessage+=ErrorCodeToString(dwLastError);
00403         
00404         //Get the log
00405         CErrorLog* pLog;
00406         pLog=GetLog();
00407 
00408         //Report to the log
00409         WriteError(m_sClassName,
00410                    rMethod,
00411                    sMessage);
00412     }
00413     ERROR_UNKNOWN("ReportErrorOS")
00414 }

void CErrorHandler::ReportStaticError ( const std::string &  rClass,
const std::string &  rMethod,
const std::string &  rMessage,
DWORD  dwAdditionalData 
) [static, protected, inherited]

Definition at line 540 of file ErrorHandler.cpp.

00544 {
00545     if (!m_pLog)
00546         return;
00547 
00548     try
00549     {
00550         //Convert the number
00551         char aTmp[11];
00552         ltoa(dwAdditionalData,aTmp,10);
00553 
00554         //Create the new message
00555         std::string sNewMessage(rMessage);
00556         sNewMessage+=", Additional data: ";
00557         sNewMessage+=aTmp;
00558 
00559         //Report to the log
00560         WriteStaticError(rClass,
00561                          rMethod,
00562                          sNewMessage);
00563     }
00564     ERROR_UNKNOWN("ReportStaticError")
00565 }

void CErrorHandler::ReportStaticError ( const std::string &  rClass,
const std::string &  rMethod,
const std::string &  rMessage 
) [static, protected, inherited]

Definition at line 523 of file ErrorHandler.cpp.

00526 {
00527     if (!m_pLog)
00528         return;
00529 
00530     try
00531     {
00532         //Report to the log
00533         WriteStaticError(rClass,
00534                          rMethod,
00535                          rMessage);
00536     }
00537     ERROR_UNKNOWN("ReportStaticError")
00538 }

void CErrorHandler::ReportStaticError ( const std::string &  rClass,
const std::string &  rMethod 
) [static, protected, inherited]

Definition at line 503 of file ErrorHandler.cpp.

00505 {
00506     if (!m_pLog)
00507         return;
00508 
00509     try
00510     {
00511         //Get the last error
00512         DWORD dwLastError;
00513         dwLastError=GetLastError();
00514 
00515         //Report the error
00516         WriteStaticError(rClass,
00517                           rMethod,
00518                           ErrorCodeToString(dwLastError));
00519     }
00520     ERROR_UNKNOWN("ReportStaticError")
00521 }

void CErrorHandler::ReportStaticErrorOS ( const std::string &  rClass,
const std::string &  rMethod,
const std::string &  rMessage 
) [static, protected, inherited]

Definition at line 343 of file ErrorHandler.cpp.

00346 {
00347     if (!m_pLog)
00348         return;
00349 
00350     try
00351     {
00352         //Get the last error
00353         DWORD dwLastError;
00354         dwLastError=GetLastError();
00355 
00356         //Format the message
00357         std::string sMessage;
00358         sMessage=rMessage;
00359         sMessage+=", with error code: ";
00360 
00361         //Convert the error code
00362         char aTmp[11];
00363         itoa(dwLastError,aTmp,10);
00364 
00365         //Add it again
00366         sMessage+=aTmp;
00367         sMessage+=" ";
00368         sMessage+=ErrorCodeToString(dwLastError);
00369         
00370         //Report to the log
00371         WriteStaticError(rClass,
00372                          rMethod,
00373                          sMessage);
00374     }
00375     ERROR_UNKNOWN("ReportStaticError")
00376 }

unsigned long CSocketBase::ResolveDNS ( const std::string &  rAddress  )  [static, inherited]

Definition at line 431 of file SocketBase.cpp.

00432 {
00433     try
00434     {
00435         //Resolve the DNS
00436         sockaddr_in aAddr;
00437         aAddr=InternalResolveDNS(rAddress.c_str());
00438 
00439         //Check if valid
00440         if (aAddr.sin_addr.S_un.S_addr==0)
00441             //Error
00442             return 0;
00443         else
00444             return aAddr.sin_addr.S_un.S_addr;
00445     }
00446     ERROR_HANDLER_STATIC_RETURN(CSocketBase_Class,"ResolveDNS",0)
00447 }

int CSocket::Send ( const std::string &  rDestinationAddress,
const char *  pBuffer,
unsigned long  ulBufferLength,
unsigned short  usDestinationPort = 0 
) [virtual, inherited]

Definition at line 245 of file Socket.cpp.

00249 {
00250     try
00251     {
00252         //Quit if not ok
00253         if (!CheckSocketValid())
00254             return GetErrorCode();
00255 
00256         return Send(inet_addr(rDestinationAddress.c_str()),
00257                     pBuffer,
00258                     ulBufferLength,
00259                     usDestinationPort);
00260     }
00261     ERROR_HANDLER_RETURN("Send",GetErrorCode())
00262 }

int CSocket::Send ( IP  aDestinationAddress,
const char *  pBuffer,
unsigned long  ulBufferLength,
unsigned short  usDestinationPort = 0 
) [virtual, inherited]

Definition at line 191 of file Socket.cpp.

00195 {
00196     try
00197     {
00198         //Quit if not ok
00199         if (!CheckSocketValid())
00200             return GetErrorCode();
00201 
00202         //Is it a valid size
00203         if (ulBufferLength &&
00204             !pBuffer)
00205             return GetErrorCode();
00206 
00207         //Define the target address
00208         sockaddr_in aTargetAddress;
00209         memset(&aTargetAddress,
00210                0,
00211                sizeof(aTargetAddress));
00212 
00213         aTargetAddress.sin_family=AF_INET;
00214         aTargetAddress.sin_addr.s_addr=aDestinationAddress;
00215         aTargetAddress.sin_port=htons(usDestinationPort);
00216 
00217         //packet send status ?
00218         int iResult;
00219 
00220         //Set to no error
00221         iResult=!GetErrorCode();
00222 
00223         //Check if we had an error
00224         if (iResult!=GetErrorCode())
00225             //Use regular send !!!
00226             iResult=sendto(GetHandle(),
00227                            (const char*)pBuffer,
00228                            ulBufferLength,
00229                            0,
00230                            (sockaddr*)&aTargetAddress,
00231                            sizeof(aTargetAddress));
00232         
00233         //Is all OK?
00234         if (iResult==GetErrorCode() &&
00235             GetSystemLastError()!=WSAEWOULDBLOCK)
00236             //Set the error
00237             SetLastError("Send");
00238         
00239         //Done
00240         return iResult;
00241     }
00242     ERROR_HANDLER_RETURN("Send",GetErrorCode())
00243 }

int CTCPSocket::Send ( const char *  pBuffer,
unsigned long  ulBufferLength 
) [virtual]

Reimplemented in CTCPSocketAsync.

Definition at line 361 of file TCPSocket.cpp.

00363 {
00364     try
00365     {
00366         //Quit if not ok
00367         if (!CheckSocketValid())
00368             return FALSE;
00369 
00370         //Send the data
00371         int iResult;
00372 
00373         //And send it
00374         iResult=send(GetHandle(),
00375                      pBuffer,
00376                      ulBufferLength,
00377                      NULL);
00378 
00379         //Did we succeed ?
00380         if (iResult==GetErrorCode())
00381             //Set the error code
00382             SetLastError("Send");
00383 
00384         //Done
00385         return iResult;
00386     }
00387     ERROR_HANDLER_RETURN("Send",FALSE)
00388 }

void CTCPSocket::SetAcceptance ( CTCPSocket pNewSocket,
SOCKET  aSocket,
sockaddr_in  aAddress 
) const [protected]

Definition at line 347 of file TCPSocket.cpp.

00350 {
00351     //Call before accept routing
00352     pNewSocket->BeforeAccept();
00353 
00354     //Set the socket data
00355     pNewSocket->SetConnectedTo(aAddress);
00356     pNewSocket->AssignSocket(aSocket);
00357     pNewSocket->SetConnectionStatus(TRUE);
00358     pNewSocket->Accepted();
00359 }

BOOL CSocket::SetBroadcast ( BOOL  bBroadcast  )  [inherited]

Definition at line 818 of file Socket.cpp.

00819 {
00820     try
00821     {
00822         //Quit if not ok
00823         if (!CheckSocketValid())
00824             return FALSE;
00825 
00826         //Set broadcast option
00827         if(setsockopt(GetHandle(),
00828                       SOL_SOCKET,
00829                       SO_BROADCAST,
00830                       (char*)&bBroadcast,
00831                       sizeof(bBroadcast))==GetErrorCode())
00832         {
00833             //Check for options error
00834             SetLastError("SetBroadcast");
00835 
00836             //Exit
00837             return FALSE;
00838         }   
00839 
00840         return TRUE;
00841     }
00842     ERROR_HANDLER_RETURN("SetBroadcast",FALSE)
00843 }

void CSocket::SetConnectedTo ( const sockaddr_in &  rAddress  )  [protected, inherited]

Definition at line 897 of file Socket.cpp.

00898 {
00899     m_aConnectedTo=rAddress;
00900 }

void CTCPSocket::SetConnectionStatus ( BOOL  bConnected  )  [protected]

Definition at line 394 of file TCPSocket.cpp.

00395 {
00396     m_bConnected=bConnected;
00397 }

void CSocketBase::SetLastError ( const std::string &  rMethod,
int  iErrorCode 
) const [protected, inherited]

Definition at line 124 of file SocketBase.cpp.

00126 {
00127     try
00128     {
00129         //First set the error
00130         m_iLastError=iErrorCode;
00131 
00132         //Check if there is an error
00133         if (m_iLastError)
00134             ReportError(rMethod,
00135                         "Through SetLastError",
00136                         m_iLastError);
00137     }
00138     ERROR_HANDLER("SetLastError")
00139 }

void CSocketBase::SetLastError ( const std::string &  rMethod  )  const [protected, inherited]

Definition at line 104 of file SocketBase.cpp.

00105 {
00106     try
00107     {
00108 #ifdef WIN32
00109         //First set the error
00110         m_iLastError=WSAGetLastError();
00111 #else
00112         m_iLastError=errno();
00113 #endif
00114 
00115         //Check if there is an error
00116         if (m_iLastError)
00117             ReportError(rMethod,
00118                         "Through SetLastError",
00119                         m_iLastError);
00120     }
00121     ERROR_HANDLER("SetLastError")
00122 }

void CErrorHandler::SetLocalLog ( CErrorLog pLog  )  [inherited]

Definition at line 498 of file ErrorHandler.cpp.

00499 {
00500     m_pLocalLog=pLog;
00501 }

void CErrorHandler::SetLog ( CErrorLog pLog  )  [static, inherited]

Definition at line 464 of file ErrorHandler.cpp.

00465 {
00466     //Save the new log
00467     m_pLog=pLog;
00468 }

BOOL CTCPSocket::SetNagle ( BOOL  bNagle  ) 

Definition at line 409 of file TCPSocket.cpp.

00410 {
00411     try
00412     {
00413         //Quit if not ok
00414         if (!CheckSocketValid())
00415             return FALSE;
00416 
00417         //Invert the flag (convert to true bool)
00418         if (bNagle)
00419             bNagle=FALSE;
00420         else
00421             bNagle=TRUE;
00422 
00423         //Try to set the option
00424         if (setsockopt(GetHandle(),
00425                        IPPROTO_TCP,
00426                        TCP_NODELAY,
00427                        (const char*)&bNagle,
00428                        sizeof(bNagle))==GetErrorCode())
00429         {
00430             //Report it
00431             ReportError("SetNagle","Failed to set nagle");
00432 
00433             //Set it
00434             SetLastError("SetNagle");
00435         
00436             //Exit
00437             return FALSE;
00438         }
00439 
00440         //Done
00441         return TRUE;
00442     }
00443     ERROR_HANDLER_RETURN("SetNagle",FALSE)
00444 }

void CErrorHandler::SetName ( const std::string &  rName  )  const [protected, inherited]

Definition at line 134 of file ErrorHandler.cpp.

00135 {
00136     //Save the class name
00137     m_sClassName=rName;
00138 }

void CSocket::SetProtocol ( unsigned char  iProtocol  )  [inherited]

Definition at line 186 of file Socket.cpp.

00187 {
00188     m_ucProtocol=ucProtocol;
00189 }

BOOL CSocket::SetReceiveTimeout ( unsigned long  ulMS  )  [inherited]

Definition at line 860 of file Socket.cpp.

00861 {
00862     try
00863     {
00864         //Quit if not ok
00865         if (!CheckSocketValid())
00866             return FALSE;
00867 
00868         //Set it
00869         if(setsockopt(GetHandle(),
00870                       SOL_SOCKET,
00871                       SO_RCVTIMEO,
00872                       (char*)&ulMS,
00873                       sizeof(ulMS))==GetErrorCode())
00874         {
00875             //Check for options error
00876             SetLastError("SetReceiveTimeout");
00877 
00878             //Exit
00879             return FALSE;
00880         }
00881         else
00882             return TRUE;
00883     }
00884     ERROR_HANDLER_RETURN("SetReceiveTimeout",FALSE)
00885 }

void CSocket::SetSourceAddress ( const std::string &  rSourceAddress  )  [virtual, inherited]

Definition at line 440 of file Socket.cpp.

00441 {
00442     try
00443     {
00444         //Set the source address, in case we want to spoof it
00445         if (rSourceAddress.empty())
00446             m_ulSourceAddress=0;
00447         else
00448             m_ulSourceAddress=inet_addr(rSourceAddress.c_str());
00449     }
00450     ERROR_HANDLER("SetSourceAddress")
00451 }

void CSocket::SetSourceAddress ( IP  aSourceAddress  )  [virtual, inherited]

Definition at line 435 of file Socket.cpp.

00436 {
00437     m_ulSourceAddress=aSourceAddress;
00438 }

void CSocket::SetTTL ( unsigned char  ucTTL  )  [inherited]

Definition at line 453 of file Socket.cpp.

00454 {
00455     try
00456     {
00457         //Quit if not ok
00458         if (!CheckSocketValid())
00459             return;
00460 
00461         if(setsockopt(GetHandle(),
00462                       IPPROTO_IP,
00463                       IP_TTL,
00464                       (const char*)&ucTTL,
00465                       sizeof(ucTTL)))
00466             SetLastError("SetTTL");
00467     }
00468     ERROR_HANDLER("SetTTL")
00469 }

BOOL CSocket::Shutdown ( SocketShutdown  eHow  )  [inherited]

Definition at line 683 of file Socket.cpp.

00684 {
00685     if (!CheckSocketValid())
00686         return FALSE;
00687 
00688     try
00689     {
00690         int iHow;
00691 
00692         //Convert the how to a real flag
00693         if (eHow==ssReceive)
00694             iHow=SD_RECEIVE;
00695         else if (eHow==ssSend)
00696             iHow=SD_SEND;
00697         else
00698             iHow=SD_BOTH;
00699 
00700         //Do it
00701         if (shutdown(GetHandle(),iHow))
00702         {
00703             //Report it
00704             SetLastError("Shutdown");
00705 
00706             //Exit
00707             return FALSE;
00708         }
00709 
00710         //Done
00711         return TRUE;
00712     }
00713     ERROR_HANDLER_RETURN("Shutdown",FALSE)
00714 }

BOOL CSocketBase::ShutdownSockets (  )  [static, inherited]

Definition at line 231 of file SocketBase.cpp.

00232 {
00233     //Only if initialized
00234     if (!m_bInitialized)
00235         return TRUE;
00236 
00237     try
00238     {
00239         //Delete the CS
00240         delete m_pCSection;
00241         m_pCSection=NULL;
00242 
00243         //Delete the DNS CS
00244         delete m_pCSectionDNS;
00245         m_pCSectionDNS=NULL;
00246 
00247         //Clear the DNS map
00248         m_aDNSMap.clear();
00249 
00250         //Notify shutdown class
00251         if (m_pShutdownClass)
00252         {
00253             //Notify we are shuting down
00254             m_pShutdownClass->NotifyShutdown();
00255 
00256             //Delete it
00257             delete m_pShutdownClass;
00258             m_pShutdownClass=NULL;
00259         }
00260 
00261         //Not initialized anymore
00262         m_bInitialized=FALSE;
00263 
00264         if (WSACleanup()==GetErrorCode())
00265             return FALSE;
00266 
00267         //Done
00268         return TRUE;
00269     }
00270     ERROR_HANDLER_STATIC_RETURN(CSocketBase_Class,"ShutdownSockets",FALSE)
00271 }

unsigned long CSocketBase::StringToLong ( const std::string &  rAddress  )  [static, inherited]

Definition at line 344 of file SocketBase.cpp.

00345 {
00346     //Try to convert it
00347     unsigned long ulAddress;
00348     ulAddress=inet_addr(rAddress.c_str());
00349 
00350     //Is it valid
00351     if (ulAddress!=INADDR_NONE)
00352         return ulAddress;
00353     else
00354         //Try to resolve it
00355         return ResolveDNS(rAddress);
00356 }

BOOL CSocketBase::ValidAddress ( const std::string &  rAddress  )  [static, inherited]

Definition at line 373 of file SocketBase.cpp.

00374 {
00375     try
00376     {
00377         return inet_addr(rAddress.c_str())!=INADDR_NONE;
00378     }
00379     ERROR_HANDLER_STATIC_RETURN(CSocketBase_Class,"ValidAddress",FALSE)
00380 }

BOOL CSocket::ValidSocket (  )  const [protected, inherited]

Definition at line 264 of file Socket.cpp.

00265 {
00266     return m_aSocket!=INVALID_SOCKET;
00267 }

void CErrorHandler::WriteError ( const std::string &  rClass,
const std::string &  rMethod,
const std::string &  rError 
) const [inherited]

Definition at line 163 of file ErrorHandler.cpp.

00166 {
00167     //Do we have a log?
00168     if (!GetLog())
00169         return;
00170 
00171     //Format the msg
00172     std::string sError;
00173     sError=FormatData(rClass,
00174                       rMethod,
00175                       rError);
00176 
00177     //Write it
00178     GetLog()->WriteError(sError);
00179 }

void CErrorHandler::WriteMessage ( const std::string &  rClass,
const std::string &  rMethod,
const std::string &  rMessage 
) const [inherited]

Definition at line 199 of file ErrorHandler.cpp.

00202 {
00203     //Do we have a log?
00204     if (!GetLog())
00205         return;
00206 
00207     //Format the msg
00208     std::string sMsg;
00209     sMsg=FormatData(rClass,
00210                     rMethod,
00211                     rMessage,
00212                     true);
00213 
00214     //Write it
00215     GetLog()->WriteMessage(sMsg);
00216 }

void CErrorHandler::WriteStaticError ( const std::string &  rClass,
const std::string &  rMethod,
const std::string &  rError 
) [static, inherited]

Definition at line 181 of file ErrorHandler.cpp.

00184 {
00185     //Do we have a log?
00186     if (!m_pLog)
00187         return;
00188 
00189     //Format the msg
00190     std::string sError;
00191     sError=FormatData(rClass,
00192                       rMethod,
00193                       rError);
00194 
00195     //Write it
00196     m_pLog->WriteError(sError);
00197 }


Friends And Related Function Documentation

friend class CIPCrafter [friend, inherited]

Definition at line 59 of file Socket.h.


Member Data Documentation

CGenericCriticalSection * CSocketBase::m_pCSection = NULL [static, protected, inherited]

Definition at line 122 of file SocketBase.h.


The documentation for this class was generated from the following files: