/ghost/bnlsclient.cpp

http://ghostcb.googlecode.com/ · C++ · 199 lines · 149 code · 28 blank · 22 comment · 23 complexity · ece27487fa4eb726d27565f35b726f7f MD5 · raw file

  1. /*
  2. Copyright [2008] [Trevor Hogan]
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. CODE PORTED FROM THE ORIGINAL GHOST PROJECT: http://ghost.pwner.org/
  13. */
  14. #include "ghost.h"
  15. #include "util.h"
  16. #include "socket.h"
  17. #include "commandpacket.h"
  18. #include "bnlsprotocol.h"
  19. #include "bnlsclient.h"
  20. #include "ui/forward.h"
  21. //
  22. // CBNLSClient
  23. //
  24. CBNLSClient :: CBNLSClient( string nServer, uint16_t nPort, uint32_t nWardenCookie )
  25. {
  26. m_Socket = new CTCPClient( );
  27. m_Protocol = new CBNLSProtocol( );
  28. m_WasConnected = false;
  29. m_Server = nServer;
  30. m_Port = nPort;
  31. m_LastNullTime = 0;
  32. m_WardenCookie = nWardenCookie;
  33. m_TotalWardenIn = 0;
  34. m_TotalWardenOut = 0;
  35. }
  36. CBNLSClient :: ~CBNLSClient( )
  37. {
  38. delete m_Socket;
  39. delete m_Protocol;
  40. while( !m_Packets.empty( ) )
  41. {
  42. delete m_Packets.front( );
  43. m_Packets.pop( );
  44. }
  45. }
  46. BYTEARRAY CBNLSClient :: GetWardenResponse( )
  47. {
  48. BYTEARRAY WardenResponse;
  49. if( !m_WardenResponses.empty( ) )
  50. {
  51. WardenResponse = m_WardenResponses.front( );
  52. m_WardenResponses.pop( );
  53. m_TotalWardenOut++;
  54. }
  55. return WardenResponse;
  56. }
  57. unsigned int CBNLSClient :: SetFD( void *fd, void *send_fd, int *nfds )
  58. {
  59. if( !m_Socket->HasError( ) && m_Socket->GetConnected( ) )
  60. {
  61. m_Socket->SetFD( (fd_set *)fd, (fd_set *)send_fd, nfds );
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. bool CBNLSClient :: Update( void *fd, void *send_fd )
  67. {
  68. if( m_Socket->HasError( ) )
  69. {
  70. CONSOLE_Print( "[BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + "] disconnected from BNLS server due to socket error" );
  71. forward(new CFwdData(FWD_GENERAL, "BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + " - Disconnected from BNLS server due to socket error", 6, 0));
  72. return true;
  73. }
  74. if( !m_Socket->GetConnecting( ) && !m_Socket->GetConnected( ) && m_WasConnected )
  75. {
  76. CONSOLE_Print( "[BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + "] disconnected from BNLS server" );
  77. forward(new CFwdData(FWD_GENERAL, "BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + " - Disconnected from BNLS server", 6, 0));
  78. return true;
  79. }
  80. if( m_Socket->GetConnected( ) )
  81. {
  82. m_Socket->DoRecv( (fd_set *)fd );
  83. ExtractPackets( );
  84. ProcessPackets( );
  85. if( GetTime( ) - m_LastNullTime >= 50 )
  86. {
  87. m_Socket->PutBytes( m_Protocol->SEND_BNLS_NULL( ) );
  88. m_LastNullTime = GetTime( );
  89. }
  90. while( !m_OutPackets.empty( ) )
  91. {
  92. m_Socket->PutBytes( m_OutPackets.front( ) );
  93. m_OutPackets.pop( );
  94. }
  95. m_Socket->DoSend( (fd_set *)send_fd );
  96. return false;
  97. }
  98. if( m_Socket->GetConnecting( ) && m_Socket->CheckConnect( ) )
  99. {
  100. CONSOLE_Print( "[BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + "] connected" );
  101. forward(new CFwdData(FWD_GENERAL, "BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + " - Connected", 0, 0));
  102. m_WasConnected = true;
  103. m_LastNullTime = GetTime( );
  104. return false;
  105. }
  106. if( !m_Socket->GetConnecting( ) && !m_Socket->GetConnected( ) && !m_WasConnected )
  107. {
  108. CONSOLE_Print( "[BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + "] connecting to server [" + m_Server + "] on port " + UTIL_ToString( m_Port ) );
  109. forward(new CFwdData(FWD_GENERAL, "BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + " - Connecting to server [" + m_Server + "] on port " + UTIL_ToString( m_Port ), 0, 0));
  110. m_Socket->Connect( string( ), m_Server, m_Port );
  111. return false;
  112. }
  113. return false;
  114. }
  115. void CBNLSClient :: ExtractPackets( )
  116. {
  117. string *RecvBuffer = m_Socket->GetBytes( );
  118. BYTEARRAY Bytes = UTIL_CreateByteArray( (unsigned char *)RecvBuffer->c_str( ), RecvBuffer->size( ) );
  119. while( Bytes.size( ) >= 3 )
  120. {
  121. uint16_t Length = UTIL_ByteArrayToUInt16( Bytes, false );
  122. if( Length >= 3 )
  123. {
  124. if( Bytes.size( ) >= Length )
  125. {
  126. m_Packets.push( new CCommandPacket( 0, Bytes[2], BYTEARRAY( Bytes.begin( ), Bytes.begin( ) + Length ) ) );
  127. *RecvBuffer = RecvBuffer->substr( Length );
  128. Bytes = BYTEARRAY( Bytes.begin( ) + Length, Bytes.end( ) );
  129. }
  130. else
  131. return;
  132. }
  133. else
  134. {
  135. CONSOLE_Print( "[BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + "] error - received invalid packet from BNLS server (bad length), disconnecting" );
  136. forward(new CFwdData(FWD_GENERAL, "BNLSC: " + m_Server + ":" + UTIL_ToString( m_Port ) + ":C" + UTIL_ToString( m_WardenCookie ) + " - Error - received invalid packet from BNLS server (bad length), disconnecting", 0, 0));
  137. m_Socket->Disconnect( );
  138. return;
  139. }
  140. }
  141. }
  142. void CBNLSClient :: ProcessPackets( )
  143. {
  144. while( !m_Packets.empty( ) )
  145. {
  146. CCommandPacket *Packet = m_Packets.front( );
  147. m_Packets.pop( );
  148. if( Packet->GetID( ) == CBNLSProtocol :: BNLS_WARDEN )
  149. {
  150. BYTEARRAY WardenResponse = m_Protocol->RECEIVE_BNLS_WARDEN( Packet->GetData( ) );
  151. if( !WardenResponse.empty( ) )
  152. m_WardenResponses.push( WardenResponse );
  153. }
  154. delete Packet;
  155. }
  156. }
  157. void CBNLSClient :: QueueWardenSeed( uint32_t seed )
  158. {
  159. m_OutPackets.push( m_Protocol->SEND_BNLS_WARDEN_SEED( m_WardenCookie, seed ) );
  160. }
  161. void CBNLSClient :: QueueWardenRaw( BYTEARRAY wardenRaw )
  162. {
  163. m_OutPackets.push( m_Protocol->SEND_BNLS_WARDEN_RAW( m_WardenCookie, wardenRaw ) );
  164. m_TotalWardenIn++;
  165. }