Netzwerk.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "Netzwerk.h"
  2. #include "Keys.h"
  3. // Inhalt der ErhaltungKlient Klasse aus Netzwerk.h
  4. // Konstruktor
  5. ErhaltungKlient::ErhaltungKlient( int klientId )
  6. : Thread()
  7. {
  8. klient = 0;
  9. this->klientId = klientId;
  10. abmel = 0;
  11. trenn = 0;
  12. ref = 1;
  13. }
  14. // Destruktor
  15. ErhaltungKlient::~ErhaltungKlient()
  16. {
  17. ende();
  18. if( klient )
  19. klient->release();
  20. }
  21. // nicht constant
  22. void ErhaltungKlient::verbinden( int mSP, Text *zMSIp, char *netwSchlüssel, char netwSchlüsselLän )
  23. {
  24. if( !klient )
  25. {
  26. klient = new Klient();
  27. int l = 0;
  28. char *key;
  29. Keys::getServerKey( &key, l, Keys::MAIN, Keys::SENDEN );
  30. klient->setSendeKey( key, l );
  31. delete[] key;
  32. Keys::getServerKey( &key, l, Keys::MAIN, Keys::EMPFANGEN );
  33. klient->setEmpfangKey( key, l );
  34. delete[] key;
  35. if( !klient->verbinde( mSP, zMSIp->getText() ) )
  36. {
  37. klient = klient->release();
  38. return;
  39. }
  40. klient->sende( "\0", 1 ); // Verschlüsselung aktivieren
  41. klient->sendeEncrypted( "\1", 1 );
  42. klient->sendeEncrypted( (char*)&klientId, 4 );
  43. char serverReturn = 0;
  44. klient->getNachrichtEncrypted( &serverReturn, 1 );
  45. if( serverReturn == 3 )
  46. {
  47. char län = 0;
  48. klient->getNachrichtEncrypted( &län, 1 );
  49. char *nachricht = new char[ län + 1 ];
  50. nachricht[ län ] = 0;
  51. klient->getNachrichtEncrypted( nachricht, län );
  52. delete[]nachricht;
  53. klient->sendeEncrypted( "\3", 1 );
  54. klient->getNachrichtEncrypted( &serverReturn, 1 );
  55. klient->trenne();
  56. klient = klient->release();
  57. return;
  58. }
  59. klient->setSendeKey( (char*)netwSchlüssel, netwSchlüsselLän );
  60. klient->setEmpfangKey( (char*)netwSchlüssel, netwSchlüsselLän );
  61. klient->sendeEncrypted( "\6\x8", 2 );
  62. char byte = 0;
  63. klient->getNachrichtEncrypted( &byte, 1 );
  64. if( byte == 2 )
  65. {
  66. unsigned char lsIp[ 4 ];
  67. klient->getNachrichtEncrypted( (char *)lsIp, 4 );
  68. unsigned short lsPort = 0;
  69. klient->getNachrichtEncrypted( (char*)&lsPort, 2 );
  70. klient->sendeEncrypted( "\3", 1 );
  71. klient->getNachrichtEncrypted( &serverReturn, 1 );
  72. klient->trenne();
  73. Text *lsIpT = new Text( "" );
  74. lsIpT->append( (int)lsIp[ 0 ] );
  75. lsIpT->append( "." );
  76. lsIpT->append( (int)lsIp[ 1 ] );
  77. lsIpT->append( "." );
  78. lsIpT->append( (int)lsIp[ 2 ] );
  79. lsIpT->append( "." );
  80. lsIpT->append( (int)lsIp[ 3 ] );
  81. int l = 0;
  82. char *key;
  83. Keys::getServerKey( &key, l, Keys::ERHALTUNG, Keys::SENDEN );
  84. klient->setSendeKey( key, l );
  85. delete[] key;
  86. Keys::getServerKey( &key, l, Keys::ERHALTUNG, Keys::EMPFANGEN );
  87. klient->setEmpfangKey( key, l );
  88. delete[] key;
  89. klient->verbinde( lsPort, lsIpT->getText() );
  90. lsIpT = lsIpT->release();
  91. klient->sende( "\0", 1 ); // Verschlüsselung aktivieren
  92. klient->sendeEncrypted( "\3", 1 );
  93. klient->getNachrichtEncrypted( &serverReturn, 1 );
  94. klient->trenne();
  95. }
  96. if( byte == 3 )
  97. {
  98. klient->getNachrichtEncrypted( &byte, 1 );
  99. char *f = new char[ byte + 1 ];
  100. f[ byte ] = 0;
  101. klient->getNachrichtEncrypted( f, byte );
  102. delete[]f;
  103. klient->sendeEncrypted( "\3", 1 );
  104. klient->getNachrichtEncrypted( &serverReturn, 1 );
  105. klient->trenne();
  106. klient = klient->release();
  107. return;
  108. }
  109. }
  110. if( klient->verbinde( klient->getServerPort(), klient->getServerIp() ) )
  111. {
  112. klient->sende( "\0", 1 ); // Verschlüsselung Aktivieren
  113. if( klient->sendeEncrypted( "\1", 1 ) )
  114. {
  115. klient->sendeEncrypted( (char*)&klientId, 4 );
  116. char serverReturn = 0;
  117. klient->getNachrichtEncrypted( &serverReturn, 1 );
  118. if( serverReturn == 3 )
  119. {
  120. char byte = 0;
  121. klient->getNachrichtEncrypted( &byte, 1 );
  122. char *f = new char[ byte + 1 ];
  123. f[ byte ] = 0;
  124. klient->getNachrichtEncrypted( f, byte );
  125. delete[]f;
  126. klient->sendeEncrypted( "\3", 1 );
  127. klient->getNachrichtEncrypted( &serverReturn, 1 );
  128. klient->trenne();
  129. return;
  130. }
  131. klient->setSendeKey( (char*)netwSchlüssel, netwSchlüsselLän );
  132. klient->setEmpfangKey( (char*)netwSchlüssel, netwSchlüsselLän );
  133. start();
  134. }
  135. else
  136. klient = klient->release();
  137. }
  138. else
  139. klient = klient->release();
  140. }
  141. void ErhaltungKlient::abmelden()
  142. {
  143. abmel = 1;
  144. }
  145. void ErhaltungKlient::trennen()
  146. {
  147. trenn = 1;
  148. }
  149. void ErhaltungKlient::thread()
  150. {
  151. while( 1 )
  152. {
  153. char n = 0;
  154. klient->getNachrichtEncrypted( &n, 1 );
  155. if( n != 1 )
  156. {
  157. Sleep( 250 );
  158. continue;
  159. }
  160. if( trenn )
  161. {
  162. if( abmel )
  163. {
  164. klient->sendeEncrypted( "\1", 1 );
  165. klient->getNachrichtEncrypted( &n, 1 );
  166. }
  167. klient->trenne();
  168. run = 0;
  169. return;
  170. }
  171. else
  172. klient->sendeEncrypted( "\0", 1 );
  173. }
  174. }
  175. // constant
  176. // Reference Counting
  177. ErhaltungKlient *ErhaltungKlient::getThis()
  178. {
  179. ref++;
  180. return this;
  181. }
  182. ErhaltungKlient *ErhaltungKlient::release()
  183. {
  184. ref--;
  185. if( !ref )
  186. delete this;
  187. return 0;
  188. }