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.

632 lines
14 KiB

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