Server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #include <openssl/ssl.h>
  2. #include "Server.h"
  3. #ifndef WIN32
  4. #include <string.h>
  5. #endif
  6. #include <Key.h>
  7. #include <Text.h>
  8. using namespace Network;
  9. // Inhalt der Server Klasse aus Server.h
  10. // Konstruktor
  11. Server::Server()
  12. : ReferenceCounter()
  13. {
  14. sock = 0;
  15. memset( &addresse, 0, sizeof( addresse ) ); // Adresse setzen
  16. addresse.sin_family = AF_INET;
  17. addresse.sin_addr.s_addr = ADDR_ANY;
  18. klients = 0;
  19. }
  20. // Destruktor
  21. Server::~Server()
  22. {
  23. trenne();
  24. }
  25. // nicht constant
  26. bool Server::verbinde( unsigned short port, int warteschlangenLen ) // Öffnet das Socket
  27. {
  28. sock = socket( AF_INET, SOCK_STREAM, 0 ); // Socket erstellen
  29. addresse.sin_port = htons( port ); // port setzen
  30. if( bind( sock, (struct sockaddr *)&addresse, sizeof( addresse ) ) == -1 ) // socket öffnen
  31. {
  32. trenne();
  33. return 0; // Fehler
  34. }
  35. if( listen( sock, warteschlangenLen ) == -1 ) // Klients annehmen
  36. {
  37. trenne();
  38. return 0; // Fehler
  39. }
  40. return 1;
  41. }
  42. SKlient *Server::getKlient() // nimmt Klient an
  43. {
  44. if( !sock )
  45. return 0;
  46. sockaddr_in client;
  47. int len = sizeof( addresse );
  48. #ifdef WIN32
  49. SOCKET cls = accept( sock, (sockaddr *)&client, &len ); // Klient empfangen
  50. if( cls == INVALID_SOCKET )
  51. {
  52. trenne();
  53. return 0;
  54. }
  55. #else
  56. SOCKET cls = accept( sock, (sockaddr *)&client, (socklen_t *)&len ); // Klient empfangen
  57. if( !cls )
  58. {
  59. if( errno == ECONNABORTED || errno == EBADF )
  60. trenne();
  61. return 0;
  62. }
  63. #endif
  64. client.sin_port = addresse.sin_port;
  65. klients++;
  66. return new SKlient( client, cls ); // Klient Handle Klasse zurückgeben
  67. }
  68. int Server::getKlients( bool reset ) // gibt die Anzahl der Klients zurück
  69. {
  70. int ret = klients;
  71. if( reset )
  72. klients = 0;
  73. return ret;
  74. }
  75. bool Server::trenne() // beendet den Server
  76. {
  77. if( !sock )
  78. return 1;
  79. if( closesocket( sock ) < 0 ) // socket schließen
  80. return 0;
  81. sock = 0;
  82. return 1;
  83. }
  84. // constant
  85. unsigned short Server::getPort() const // gibt den Port zurück
  86. {
  87. return htons( addresse.sin_port );
  88. }
  89. bool Server::isConnected() const // giebt 1 zurück, falls der Server verbunden ist
  90. {
  91. return sock != 0;
  92. }
  93. // Inhalt der SKlient Klasse aus Server.h
  94. // Konstruktor
  95. SKlient::SKlient( sockaddr_in addresse, SOCKET sock )
  96. : ReferenceCounter()
  97. {
  98. clientAddr = addresse;
  99. this->sock = sock;
  100. downStreamBytes = 0;
  101. upStreamBytes = 0;
  102. sendeKey = 0;
  103. empfangKey = 0;
  104. }
  105. // Destruktor
  106. SKlient::~SKlient()
  107. {
  108. trenne();
  109. if( sendeKey )
  110. sendeKey->release();
  111. if( empfangKey )
  112. empfangKey->release();
  113. }
  114. // nicht constant
  115. void SKlient::setSendeKeyZ( Encryption::Key *key ) // Setzt den Key fürs Senden
  116. {
  117. if( sendeKey )
  118. sendeKey->release();
  119. sendeKey = key;
  120. }
  121. void SKlient::setEmpfangKeyZ( Encryption::Key *key ) // Setzt den Key fürs Empfangen
  122. {
  123. if( empfangKey )
  124. empfangKey->release();
  125. empfangKey = key;
  126. }
  127. void SKlient::setSendeKey( char *key, int len ) // Setzt den Key fürs Senden
  128. {
  129. if( !sendeKey )
  130. sendeKey = new Encryption::Key();
  131. sendeKey->setKey( key, len );
  132. }
  133. void SKlient::setEmpfangKey( char *key, int len ) // Setzt den Key fürs Empfangen
  134. {
  135. if( !empfangKey )
  136. empfangKey = new Encryption::Key();
  137. empfangKey->setKey( key, len );
  138. }
  139. bool SKlient::sende( const char *nachricht, int len ) // sendet zum Klient
  140. {
  141. if( !sock )
  142. return 0;
  143. int ll = 0;
  144. while( len > 0 )
  145. {
  146. #ifdef WIN32
  147. int l = send( sock, nachricht + ll, len, 0 );
  148. #else
  149. int l = (int)send( sock, nachricht + ll, len, MSG_NOSIGNAL );
  150. #endif
  151. if( l <= 0 )
  152. return 0; // Fehler
  153. len -= l;
  154. ll += l;
  155. }
  156. upStreamBytes += ll;
  157. return 1;
  158. }
  159. bool SKlient::getNachricht( char *nachricht, int len ) // empfängt Nachricht von Klient
  160. {
  161. if( !sock )
  162. return 0;
  163. int ll = 0;
  164. while( len > 0 )
  165. {
  166. int l = (int)recv( sock, nachricht + ll, len, MSG_WAITALL );
  167. if( l <= 0 )
  168. return 0; // Fehler
  169. len -= l;
  170. ll += l;
  171. }
  172. downStreamBytes += ll;
  173. return 1;
  174. }
  175. bool SKlient::sendeEncrypted( const char *nachricht, int len ) // sendet zum Server
  176. {
  177. if( !sendeKey )
  178. return sende( nachricht, len );
  179. Encryption::Bytes *n = new Encryption::Bytes( nachricht, len );
  180. sendeKey->codieren( (Encryption::Bytes *)n->getThis() ); int ll = 0;
  181. while( len > 0 )
  182. {
  183. #ifdef WIN32
  184. int l = send( sock, n->getBytes() + ll, len, 0 );
  185. #else
  186. int l = (int)send( sock, n->getBytes() + ll, len, MSG_NOSIGNAL );
  187. #endif
  188. if( l <= 0 )
  189. {
  190. n->release();
  191. return 0; // Fehler
  192. }
  193. len -= l;
  194. ll += l;
  195. }
  196. upStreamBytes += ll;
  197. n->release();
  198. return 1;
  199. }
  200. bool SKlient::getNachrichtEncrypted( char *nachricht, int len ) // empfängt Nachricht
  201. {
  202. if( !empfangKey )
  203. return getNachricht( nachricht, len );
  204. int ll = 0;
  205. while( len > 0 )
  206. {
  207. int l = (int)recv( sock, nachricht + ll, len, MSG_WAITALL );
  208. if( l <= 0 )
  209. return 0; // Fehler
  210. len -= l;
  211. ll += l;
  212. }
  213. Encryption::Bytes *n = new Encryption::Bytes();
  214. n->setBytesZ( nachricht, ll );
  215. empfangKey->decodieren( n );
  216. downStreamBytes += ll;
  217. return 1;
  218. }
  219. int SKlient::getDownloadBytes( bool reset ) // gibt die anzahl von empfangen bytes zurück
  220. {
  221. int ret = downStreamBytes;
  222. if( reset )
  223. downStreamBytes = 0;
  224. return ret;
  225. }
  226. int SKlient::getUploadBytes( bool reset ) // gibt die anzahl von versendeter bytes zurück
  227. {
  228. int ret = upStreamBytes;
  229. if( reset )
  230. upStreamBytes = 0;
  231. return ret;
  232. }
  233. bool SKlient::trenne() // trennt die Verbindung zum Klient
  234. {
  235. if( !sock )
  236. return 0;
  237. if( closesocket( sock ) < 0 ) // trennen
  238. return 0;
  239. sock = 0;
  240. return 1;
  241. }
  242. // constant
  243. unsigned short SKlient::getPort() const // gibt den Port zurück
  244. {
  245. return htons( clientAddr.sin_port );
  246. }
  247. const char *SKlient::getIp() const // gibt die Ip des Klients zurück
  248. {
  249. return inet_ntoa( clientAddr.sin_addr );
  250. }
  251. int pem_passwd_cb( char *buf, int size, int rwflag, void *userdata )
  252. {
  253. const char *passw = ( (Text *)userdata )->getText();
  254. memcpy( buf, passw, MIN( (unsigned int)size, strlen( passw ) + 1 ) );
  255. return (int)strlen( buf );
  256. }
  257. // Inhalt der SSLServer Klasse
  258. // Konstruktor
  259. SSLServer::SSLServer()
  260. : ReferenceCounter()
  261. {
  262. s = 0;
  263. ctx = SSL_CTX_new( SSLv23_server_method() );
  264. SSL_CTX_set_default_passwd_cb( ctx, pem_passwd_cb );
  265. passw = new Text();
  266. SSL_CTX_set_default_passwd_cb_userdata( ctx, passw );
  267. addr.sin_family = AF_INET;
  268. addr.sin_addr.s_addr = htonl( INADDR_ANY );
  269. klients = 0;
  270. }
  271. // Destruktor
  272. SSLServer::~SSLServer()
  273. {
  274. trenne();
  275. SSL_CTX_free( ctx );
  276. passw->release();
  277. #ifdef WIN32
  278. OPENSSL_thread_stop();
  279. #endif
  280. }
  281. // nicht constant
  282. // Setzt den Pfad zur Datei, in dem das Certifikat gespeichert ist
  283. bool SSLServer::setCertificateFile( const char *file )
  284. {
  285. return SSL_CTX_use_certificate_file( ctx, file, SSL_FILETYPE_PEM ) > 0;
  286. }
  287. // Setzt den Pfad zur Datei, in dem der private Schlüssel gespeichert ist
  288. bool SSLServer::setPrivateKeyFile( const char *file )
  289. {
  290. return SSL_CTX_use_PrivateKey_file( ctx, file, SSL_FILETYPE_PEM ) > 0;
  291. }
  292. // setzt das passwort des private keys (muss vor setPrivateKeyFile aufgerufen werden)
  293. void SSLServer::setPrivateKeyPassword( const char *password )
  294. {
  295. passw->setText( password );
  296. }
  297. // Öffnet das Socket
  298. bool SSLServer::verbinde( unsigned short port, int warteschlangenLen )
  299. {
  300. addr.sin_port = htons( port );
  301. s = socket( AF_INET, SOCK_STREAM, 0 );
  302. if( s < 0 )
  303. {
  304. s = 0;
  305. return 0;
  306. }
  307. if( bind( s, (struct sockaddr *)&addr, sizeof( addr ) ) < 0 )
  308. {
  309. trenne();
  310. return 0;
  311. }
  312. if( listen( s, warteschlangenLen ) < 0 )
  313. {
  314. trenne();
  315. return 0;
  316. }
  317. return 1;
  318. }
  319. // nimmt Klient an
  320. SSLSKlient *SSLServer::getKlient()
  321. {
  322. if( !s )
  323. return 0;
  324. int len = sizeof( addr );
  325. struct sockaddr_in addr;
  326. #ifdef WIN32
  327. SOCKET client = accept( s, (struct sockaddr *)&addr, &len );
  328. if( client == INVALID_SOCKET )
  329. {
  330. trenne();
  331. return 0;
  332. }
  333. #else
  334. SOCKET client = accept( s, (sockaddr *)&addr, (socklen_t *)&len ); // Klient empfangen
  335. if( !client )
  336. {
  337. if( errno == ECONNABORTED || errno == EBADF )
  338. trenne();
  339. return 0;
  340. }
  341. #endif
  342. addr.sin_port = this->addr.sin_port;
  343. SSL *ssl = SSL_new( ctx );
  344. SSL_set_fd( ssl, (int)client );
  345. if( SSL_accept( ssl ) <= 0 )
  346. {
  347. SSL_free( ssl );
  348. closesocket( client );
  349. return 0;
  350. }
  351. klients++;
  352. return new SSLSKlient( addr, ssl, client );
  353. }
  354. // gibt die Anzahl der Klients zurück
  355. int SSLServer::getKlients( bool reset )
  356. {
  357. int ret = klients;
  358. if( reset )
  359. klients = 0;
  360. return ret;
  361. }
  362. // beendet den Server
  363. bool SSLServer::trenne()
  364. {
  365. if( !s )
  366. return 1;
  367. if( closesocket( s ) < 0 ) // socket schließen
  368. return 0;
  369. s = 0;
  370. return 1;
  371. }
  372. // constant
  373. // gibt den Port zurück
  374. unsigned short SSLServer::getPort() const
  375. {
  376. return htons( addr.sin_port );
  377. }
  378. // giebt 1 zurück, falls der Server verbunden ist
  379. bool SSLServer::isConnected() const
  380. {
  381. return s != 0;
  382. }
  383. // Inhalt der SSLSKlient Klasse
  384. // Konstruktor
  385. SSLSKlient::SSLSKlient( sockaddr_in client, SSL *ssl, SOCKET s )
  386. : ReferenceCounter()
  387. {
  388. this->s = s;
  389. clientAddr = client;
  390. this->ssl = ssl;
  391. downStreamBytes = 0;
  392. upStreamBytes = 0;
  393. }
  394. // Destruktor
  395. SSLSKlient::~SSLSKlient()
  396. {
  397. trenne();
  398. #ifdef WIN32
  399. OPENSSL_thread_stop();
  400. #endif
  401. }
  402. // nicht constant
  403. bool SSLSKlient::sende( const char *nachricht, int len ) // sendet zum Klient
  404. {
  405. if( !ssl )
  406. return 0;
  407. int ll = 0;
  408. while( len > 0 )
  409. {
  410. int l = SSL_write( ssl, nachricht + ll, len );
  411. if( l <= 0 )
  412. return 0; // Fehler
  413. len -= l;
  414. ll += l;
  415. }
  416. upStreamBytes += ll;
  417. return 1;
  418. }
  419. bool SSLSKlient::getNachricht( char *nachricht, int len ) // empfängt Nachricht von Klient
  420. {
  421. if( !ssl )
  422. return 0;
  423. int ll = 0;
  424. while( len > 0 )
  425. {
  426. int l = (int)SSL_read( ssl, nachricht + ll, len );
  427. if( l <= 0 )
  428. return 0; // Fehler
  429. len -= l;
  430. ll += l;
  431. }
  432. downStreamBytes += ll;
  433. return 1;
  434. }
  435. int SSLSKlient::getDownloadBytes( bool reset ) // gibt die anzahl von empfangen bytes zurück
  436. {
  437. int ret = downStreamBytes;
  438. if( reset )
  439. downStreamBytes = 0;
  440. return ret;
  441. }
  442. int SSLSKlient::getUploadBytes( bool reset ) // gibt die anzahl von versendeter bytes zurück
  443. {
  444. int ret = upStreamBytes;
  445. if( reset )
  446. upStreamBytes = 0;
  447. return ret;
  448. }
  449. bool SSLSKlient::trenne() // trennt die Verbindung zum Klient
  450. {
  451. if( !ssl )
  452. return 0;
  453. SSL_free( ssl );
  454. if( closesocket( s ) < 0 ) // trennen
  455. return 0;
  456. ssl = 0;
  457. s = 0;
  458. return 1;
  459. }
  460. // constant
  461. unsigned short SSLSKlient::getPort() const // gibt den Port zurück
  462. {
  463. return htons( clientAddr.sin_port );
  464. }
  465. const char *SSLSKlient::getIp() const // gibt die Ip des Klients zurück
  466. {
  467. return inet_ntoa( clientAddr.sin_addr );
  468. }