You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 lines
15 KiB

7 years ago
​
​
​
​
2 years ago
​
​
7 years ago
​
5 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
7 years ago
5 years ago
7 years ago
​
​
5 years ago
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
7 years ago
​
​
2 years ago
2 years ago
7 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
5 years ago
5 years ago
​
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
5 years ago
7 years ago
5 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
5 years ago
7 years ago
5 years ago
7 years ago
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
5 years ago
7 years ago
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
7 years ago
​
​
​
5 years ago
7 years ago
​
​
​
​
​
​
​
​
​
5 years ago
​
7 years ago
​
​
​
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
2 years ago
5 years ago
​
​
​
​
​
​
​
​
5 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
6 years ago
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
7 years ago
​
​
​
​
  1. #include "clientPort.h"
  2. #define LOG( X ) m_pLog->Log( "clientPort", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "clientPort", this, _LOG_ERROR, X )
  4. //#define DUBUG_EXTRA
  5. //#define DUBUG_COMMUNICATION
  6. clientPort::clientPort():transportSocket(){
  7. m_pLog = basicLogCollection::getInstance();
  8. m_nPort = 0;
  9. memset( m_cHostName, 0, MAX_HOST_STRING_SIZE );
  10. //m_sHostName = NULL;
  11. m_bKeepAlive = true;
  12. };
  13. clientPort::clientPort(CSTR URI):transportSocket(){
  14. m_pLog = basicLogCollection::getInstance();
  15. m_bKeepAlive = true;
  16. if( URI!= NULL ) {
  17. char *ptChop = NULL;
  18. char *ptSt = NULL;
  19. int tSize = 0;
  20. ptChop = strstr(URI, "/");
  21. if (ptChop != NULL){
  22. ptChop = strstr(ptChop + 1, "/");
  23. ptSt = ptChop + 1;
  24. ptChop = strstr(ptChop + 1, "/");
  25. tSize = ptChop - ptSt;
  26. ptSt[tSize] = (char)0;
  27. strncpy( m_cHostName, ptSt, MAX_HOST_STRING_SIZE );
  28. m_nPort = 80;//??
  29. //if( connectTo(ptSt, m_nPort) ){
  30. // char MsgBuffer[64];
  31. // sprintf(MsgBuffer, "Connect %s [%d] OK", m_sHostName, m_nPort);
  32. // LOG(MsgBuffer);
  33. //} else {
  34. // LOG_ERROR("Cannot Connect!");
  35. //};
  36. };
  37. } else {
  38. memset( m_cHostName, 0, 128 );
  39. m_nPort = 0;
  40. };
  41. };
  42. clientPort::clientPort(CSTR Host, int Port, bool KeepALive ) :transportSocket(){
  43. m_pLog = basicLogCollection::getInstance();
  44. m_bKeepAlive = KeepALive;
  45. if( strlen(Host) ){
  46. strncpy( m_cHostName, Host, MAX_HOST_STRING_SIZE );
  47. m_nPort = Port;
  48. } else {
  49. memset( m_cHostName, 0, MAX_HOST_STRING_SIZE );
  50. m_nPort = 0;
  51. };
  52. //if( connectTo( Host, Port ) ) {
  53. // char MsgBuffer[64];
  54. // sprintf(MsgBuffer, "Connect %s [%d] OK", m_sHostName, m_nPort );
  55. // LOG(MsgBuffer);
  56. //} else {
  57. // LOG_ERROR("Cannot Connect!");
  58. //};
  59. };
  60. clientPort::~clientPort(){
  61. #ifdef DEBUG_EXTRA
  62. LOG("Destructor!");
  63. #endif
  64. disconnect();
  65. #ifdef DEBUG_EXTRA
  66. LOG("Disconnected!");
  67. LOG("Destructor Done!");
  68. #endif
  69. };
  70. bool clientPort::create(){
  71. if ( strlen( m_cHostName) == 0 ) {
  72. LOG_ERROR("Host to connect is not Set!");
  73. return false;
  74. };
  75. if( m_nPort == 0 ) {
  76. LOG_ERROR("Port to connect is not Set!");
  77. return false;
  78. };
  79. if( connectTo( m_cHostName, m_nPort ) ){
  80. #ifdef DEBUG_EXTRA
  81. char tB[256];
  82. sprintf(tB, "Connect %s [%d] OK", m_cHostName, m_nPort);
  83. LOG(tB);
  84. #endif
  85. FD_SET( m_nSocketFD, &m_sReaderSet );
  86. if ( m_nReaderSetMax < m_nSocketFD ) m_nReaderSetMax = m_nSocketFD;
  87. return transportSocket::create();
  88. } else {
  89. #ifdef DEBUG_EXTRA
  90. LOG_ERROR("Cannot Connect!");
  91. #endif
  92. };
  93. return false;
  94. };
  95. bool clientPort::_connect(void){
  96. int iResult;
  97. long arg;
  98. struct addrinfo * result, *ptr, hints;
  99. char cBuffer[256];
  100. struct sockaddr_in serv_addr;
  101. #ifdef __linux__
  102. struct hostent *server;
  103. m_nSocketFD = socket( AF_INET, SOCK_STREAM, 0);
  104. if( m_nSocketFD < 0 ){
  105. LOG_ERROR("ERROR creating socket");
  106. return false;
  107. };
  108. server = gethostbyname( m_cHostName );
  109. if( server == NULL ){
  110. sprintf( cBuffer, "ERROR, no such host [%s]", m_cHostName );
  111. LOG_ERROR(cBuffer);
  112. return false;
  113. };
  114. bzero((char *) &serv_addr, sizeof(serv_addr));
  115. serv_addr.sin_family = AF_INET;
  116. bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length );
  117. serv_addr.sin_port = htons( m_nPort );
  118. fd_set fdset;
  119. struct timeval tv;
  120. if( ( arg = fcntl( m_nSocketFD, F_GETFL, NULL)) < 0) {
  121. sprintf( cBuffer, "Error fcntl(..., F_GETFL) (%s)", strerror(errno) );
  122. LOG_ERROR(cBuffer);
  123. return false;
  124. };
  125. arg |= O_NONBLOCK;
  126. if( fcntl( m_nSocketFD, F_SETFL, arg) < 0) {
  127. sprintf( cBuffer, "Error fcntl(..., F_SETFL) (%s)", strerror(errno) );
  128. LOG_ERROR(cBuffer);
  129. return false;
  130. };
  131. // Trying to connect with timeout
  132. iResult = connect( m_nSocketFD, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  133. if( iResult < 0 ){
  134. if( errno == EINPROGRESS ){
  135. //LOG("EINPROGRESS in connect() - selecting");
  136. do {
  137. tv.tv_sec = 15;
  138. tv.tv_usec = 0;
  139. FD_ZERO( &fdset );
  140. FD_SET( m_nSocketFD, &fdset );
  141. iResult = select( m_nSocketFD+1, NULL, &fdset, NULL, &tv );
  142. if( iResult < 0 && errno != EINTR ) {
  143. #ifdef DEBUG
  144. sprintf( cBuffer, "Error connecting %d - %s", errno, strerror(errno) );
  145. LOG_ERROR(cBuffer);
  146. #endif
  147. return false;
  148. } else if ( iResult > 0) {
  149. // Socket selected for write
  150. socklen_t par = sizeof(int);
  151. if( getsockopt( m_nSocketFD, SOL_SOCKET, SO_ERROR, (void*)(&iResult), &par) < 0 ){
  152. #ifdef DEBUG
  153. sprintf( cBuffer, "Error in getsockopt() %d - %s", errno, strerror(errno) );
  154. LOG_ERROR(cBuffer);
  155. #endif
  156. return false;
  157. };
  158. // Check the value returned...
  159. if( iResult ){
  160. #ifdef DEBUG
  161. sprintf( cBuffer, "Error in delayed connection() %d - %s", iResult, strerror(iResult) );
  162. LOG_ERROR(cBuffer);
  163. #endif
  164. return false;
  165. };
  166. break;
  167. } else {
  168. #ifdef DEBUG
  169. LOG_ERROR( "Timeout in select() - Cancelling!" );
  170. #endif
  171. return false;
  172. };
  173. } while (1);
  174. } else {
  175. #ifdef DEBUG
  176. sprintf( cBuffer, "Error connecting %d - %s", errno, strerror(errno) );
  177. LOG_ERROR(cBuffer);
  178. #endif
  179. return false;
  180. };
  181. };
  182. #if 0
  183. // Set to blocking mode again...
  184. if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) {
  185. fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
  186. exit(0);
  187. }
  188. arg &= (~O_NONBLOCK);
  189. if( fcntl(soc, F_SETFL, arg) < 0) {
  190. fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
  191. exit(0);
  192. }
  193. #endif
  194. #else
  195. // Initialize Winsock
  196. WSADATA wsaData;
  197. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  198. if (iResult != 0) {
  199. sprintf(cBuffer, "WSAStartup failed with error: %d\n", iResult);
  200. LOG_ERROR(cBuffer);
  201. return false;
  202. };
  203. ZeroMemory( &hints, sizeof(hints) );
  204. hints.ai_family = AF_UNSPEC;
  205. hints.ai_socktype = SOCK_STREAM;
  206. hints.ai_protocol = IPPROTO_TCP;
  207. // Resolve the server address and port
  208. sprintf( cBuffer, "%d", m_nPort );
  209. iResult = getaddrinfo( m_cHostName, cBuffer, &hints, &result );
  210. if (iResult != 0) {
  211. sprintf(cBuffer, "getaddrinfo for [%s:%d] failed with error: %d", m_cHostName, m_nPort, iResult);
  212. LOG_ERROR(cBuffer);
  213. WSACleanup();
  214. return false;
  215. };
  216. // Attempt to connect to an address until one succeeds
  217. ptr = result;
  218. while( ptr != NULL ) {
  219. // Create a SOCKET for connecting to server
  220. m_nSocketFD = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
  221. if( m_nSocketFD == INVALID_SOCKET ){
  222. sprintf( cBuffer, "Socket Creation failed with error: %ld", WSAGetLastError());
  223. LOG_ERROR(cBuffer);
  224. WSACleanup();
  225. return false;
  226. };
  227. // Connect to server.
  228. iResult = connect( m_nSocketFD, ptr->ai_addr, (int)ptr->ai_addrlen );
  229. if (iResult == SOCKET_ERROR) {
  230. sprintf(cBuffer, "Socket Connection failed[%d] with error: %ld", iResult, WSAGetLastError());
  231. LOG_ERROR(cBuffer);
  232. closesocket(m_nSocketFD);
  233. m_nSocketFD = INVALID_SOCKET;
  234. ptr = ptr->ai_next;
  235. continue;
  236. };
  237. break;
  238. };
  239. freeaddrinfo(result);
  240. if (m_nSocketFD == INVALID_SOCKET) {
  241. sprintf( cBuffer, "Unable Connect to the Server[%s:%d]!", m_cHostName, m_nPort );
  242. LOG_ERROR(cBuffer);
  243. WSACleanup();
  244. return false;
  245. };
  246. #endif
  247. /*
  248. setConnectionStatus(CS_CONNECTED);
  249. #ifdef DEBUG
  250. LOG("In case THREADS are running -> SHUTTING DOWN THREADS");
  251. #endif
  252. if( uninit() ) {
  253. #ifdef DEBUG
  254. LOG("STARTING IO THREADS");
  255. #endif
  256. return init();
  257. } else {
  258. LOG_ERROR("CAN NOT SHUTDOWN IO THREADS!");
  259. };
  260. */
  261. return true;//false;
  262. };
  263. bool clientPort::connectTo(CSTR Host, int Port){
  264. if( m_eConnectionStatus == CS_CONNECTED ){
  265. setConnectionStatus( CS_DISCONNECTED );
  266. #ifdef __linux__
  267. shutdown( m_nSocketFD, SHUT_RDWR );
  268. close( m_nSocketFD );
  269. m_nSocketFD = -1;
  270. #else
  271. closesocket(m_nSocketFD);
  272. WSACleanup();
  273. m_nSocketFD = INVALID_SOCKET;
  274. #endif
  275. };
  276. //if( m_sHostName == NULL ) m_sHostName = (CSTR) malloc( strlen( Host ) +1 );
  277. if( m_cHostName != Host ) strncpy( m_cHostName, Host, MAX_HOST_STRING_SIZE );//sprintf( m_sHostName, "%s", Host );// Prevent Self Copy
  278. m_nPort = Port;
  279. return _connect();
  280. };
  281. int clientPort::sendBlock(unsigned char *Data, int DataSize){
  282. if( m_eConnectionStatus != CS_CONNECTED ){
  283. if( connectTo( m_cHostName, m_nPort ) ){
  284. char MsgBuffer[CLOG];
  285. sprintf(MsgBuffer, "Reconnect %s [%d] OK", m_cHostName, m_nPort);
  286. LOG(MsgBuffer);
  287. };
  288. };
  289. if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  290. #ifdef __linux__
  291. int RetVal;
  292. if( m_nSocketFD > -1 ){
  293. //sprintf(cBuffer,"Write into Socket %p %d", Data, DataSize );
  294. //LOG(cBuffer);
  295. RetVal = write(m_nSocketFD, Data, DataSize);
  296. //LOG("Data writen into Socket");
  297. if( ( RetVal == -1 ) && (errno != EINTR ) && ( errno != EWOULDBLOCK ) ) {
  298. LOG_ERROR("Error acures while writing data into the Socket");
  299. //shutdown( m_nSocketFD, 2 );
  300. //close( m_nSocketFD );
  301. //m_nSocketFD = -1;
  302. disconnect();
  303. return -1;
  304. };
  305. if( RetVal < 0 ) RetVal = 0;
  306. #ifdef DUBUG_EXTRA
  307. {
  308. char cBuffer[128];
  309. sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  310. LOG(cBuffer);
  311. #ifdef DUBUG_COMMUNICATION
  312. for (int i = 0; i < RetVal; i++) {
  313. sprintf(cBuffer, "0x%02X", Data[i]);
  314. LOG(cBuffer);
  315. };
  316. #endif
  317. }
  318. #endif
  319. } else {
  320. LOG_ERROR("Not Connected!");
  321. return -1;
  322. };
  323. #else
  324. DWORD RetVal;
  325. if (m_nSocketFD != INVALID_SOCKET){
  326. // Send an initial buffer
  327. RetVal = send(m_nSocketFD, (const char *)Data, DataSize, NULL);
  328. if (RetVal == SOCKET_ERROR) {
  329. char cBuffer[CLOG];
  330. //m_bConnected = false;
  331. setConnectionStatus(CS_DISCONNECTED);
  332. sprintf(cBuffer,"send failed with error: %d", WSAGetLastError());
  333. LOG_ERROR(cBuffer);
  334. closesocket(m_nSocketFD);
  335. WSACleanup();
  336. return -1;
  337. };
  338. //sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  339. //LOG(cBuffer);
  340. if (!m_bKeepAlive){
  341. setConnectionStatus(CS_DISCONNECTED);
  342. closesocket(m_nSocketFD);
  343. WSACleanup();
  344. //m_bConnected = false;
  345. m_nSocketFD = INVALID_SOCKET;
  346. };
  347. } else {
  348. setConnectionStatus(CS_DISCONNECTED);
  349. //m_bConnected = false;
  350. return -1;
  351. };
  352. #endif
  353. return RetVal;
  354. };
  355. int clientPort::recptBlock(unsigned char *Buffer, int *BufferLength){
  356. if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  357. #ifdef __linux__
  358. int RetVal;
  359. if( m_nSocketFD > -1 ) {
  360. RetVal = recv( m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0 );
  361. if( RetVal < 1 ){
  362. if( errno!=EAGAIN && errno!=EINTR ) {
  363. LOG_ERROR("Peer Closes Connection!");
  364. //shutdown( m_nSocketFD, 2 );
  365. //close( m_nSocketFD );
  366. disconnect();
  367. *BufferLength = 0;
  368. //m_nSocketFD = -1;
  369. return -1;
  370. };
  371. };
  372. if( !m_bKeepAlive ){
  373. //shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  374. //close( m_nSocketFD );
  375. //m_nSocketFD = -1;
  376. disconnect();
  377. };
  378. } else {
  379. LOG_ERROR("Not Connected!");
  380. *BufferLength = 0;
  381. return -1;
  382. };
  383. #else
  384. DWORD RetVal;
  385. if( m_nSocketFD != INVALID_SOCKET ){
  386. // Receive until the peer closes the connection
  387. RetVal = recv( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0);
  388. if( RetVal < 1 ){
  389. if( errno!=EAGAIN && errno!=EINTR ) {
  390. //setConnectionStatus(CS_DISCONNECTED);
  391. closesocket(m_nSocketFD);
  392. WSACleanup();
  393. *BufferLength = 0;
  394. //m_bConnected = false;
  395. m_nSocketFD = INVALID_SOCKET;
  396. return -1;
  397. };
  398. };
  399. if (!m_bKeepAlive){
  400. //setConnectionStatus(CS_DISCONNECTED);
  401. closesocket(m_nSocketFD);
  402. WSACleanup();
  403. //m_bConnected = false;
  404. m_nSocketFD = INVALID_SOCKET;
  405. };
  406. } else {
  407. //setConnectionStatus(CS_DISCONNECTED);
  408. LOG_ERROR("Not Connected!");
  409. *BufferLength = 0;
  410. //m_bConnected = false;
  411. return 0;
  412. };
  413. #endif
  414. if (RetVal > 0) {
  415. #ifdef DUBUG_EXTRA
  416. char cBuffer[165];
  417. sprintf(cBuffer, "Bytes received: %d", RetVal);
  418. LOG(cBuffer);
  419. #ifdef DUBUG_COMMUNICATION
  420. for (int i = 0; i < RetVal; i++) {
  421. sprintf(cBuffer, "0x%02X", Buffer[i]);
  422. LOG(cBuffer);
  423. };
  424. #endif
  425. #endif
  426. *BufferLength = RetVal;
  427. return RetVal;
  428. };
  429. *BufferLength = 0;
  430. if (RetVal == -1) return 0;
  431. if (RetVal == 0){
  432. errno = 0;
  433. return 0;
  434. };
  435. /* r < -1, should never happen. */
  436. errno = EIO;
  437. return 0;
  438. };
  439. bool clientPort::disconnect(void){
  440. if( m_eConnectionStatus == CS_CONNECTED ){
  441. setConnectionStatus(CS_DISCONNECTED);
  442. #ifdef __linux__
  443. shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  444. close( m_nSocketFD );
  445. m_nSocketFD = -1;
  446. #else
  447. closesocket(m_nSocketFD);
  448. WSACleanup();
  449. m_nSocketFD = INVALID_SOCKET;
  450. #endif
  451. };
  452. return true;
  453. };
  454. #include <stdio.h>
  455. #include <stdlib.h>
  456. #include <string.h>
  457. #include <sys/socket.h>
  458. #include <netinet/in.h>
  459. #include <arpa/inet.h>
  460. #include <unistd.h>
  461. CSTR clientPort::getLocalIp4(){
  462. #ifdef __linux__
  463. struct sockaddr_in server_addr, client_addr;
  464. socklen_t client_addr_len = sizeof(client_addr);
  465. char ip_str[INET_ADDRSTRLEN];
  466. // Get the local address of the connected socket
  467. if (getsockname( m_nSocketFD, (struct sockaddr *)&client_addr, &client_addr_len) < 0) {
  468. LOG_ERROR("getsockname failed");
  469. return NULL;
  470. };
  471. // Convert the IP address to a human-readable string
  472. if (inet_ntop(AF_INET, &client_addr.sin_addr, ip_str, INET_ADDRSTRLEN) == NULL) {
  473. LOG_ERROR("inet_ntop failed");
  474. return NULL;
  475. };
  476. return strdup( ip_str );
  477. #else
  478. return NULL;
  479. #endif
  480. };
  481. CSTR clientPort::getRemoteIp4(){
  482. #ifdef __linux__
  483. char ip_str[INET_ADDRSTRLEN];
  484. struct sockaddr_in peer_addr;
  485. socklen_t peer_addr_len = sizeof(peer_addr);
  486. if (getpeername( m_nSocketFD, (struct sockaddr *)&peer_addr, &peer_addr_len) == -1) {
  487. LOG_ERROR("Error getting peer name");
  488. return NULL;
  489. };
  490. if (inet_ntop(AF_INET, &peer_addr.sin_addr, ip_str, INET_ADDRSTRLEN) == NULL) {
  491. LOG_ERROR("Error converting IP to string");
  492. return NULL;
  493. };
  494. return strdup(ip_str);
  495. #else
  496. return NULL;
  497. #endif
  498. };
  499. //#undef DUBUG_EXTRA
  500. //#undef DUBUG_COMMUNICATION
  501. #undef LOG
  502. #undef LOG_ERROR