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.

463 lines
13 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
​
​
​
​
​
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
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
  62. LOG("Destructor!");
  63. #endif
  64. disconnect();
  65. #ifdef DEBUG
  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
  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. LOG_ERROR("Cannot Connect!");
  90. };
  91. return false;
  92. };
  93. bool clientPort::_connect(void){
  94. int iResult;
  95. struct addrinfo * result, *ptr, hints;
  96. char cBuffer[256];
  97. struct sockaddr_in serv_addr;
  98. #ifdef __linux__
  99. struct hostent *server;
  100. m_nSocketFD = socket( AF_INET, SOCK_STREAM, 0);
  101. if( m_nSocketFD < 0 ){
  102. LOG_ERROR("ERROR creating socket");
  103. return false;
  104. };
  105. server = gethostbyname( m_cHostName );
  106. if( server == NULL ){
  107. sprintf( cBuffer, "ERROR, no such host [%s]", m_cHostName );
  108. LOG_ERROR(cBuffer);
  109. return false;
  110. };
  111. bzero((char *) &serv_addr, sizeof(serv_addr));
  112. serv_addr.sin_family = AF_INET;
  113. bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length );
  114. serv_addr.sin_port = htons( m_nPort );
  115. if ( connect(m_nSocketFD,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
  116. sprintf(cBuffer,"ERROR connect to [%s:%d] error: %d", m_cHostName, m_nPort, errno );
  117. LOG_ERROR( cBuffer );
  118. return false;
  119. };
  120. #ifdef CLIENT_SOCKET_NON_BLOCKING
  121. if ( fcntl(m_nSocketFD, F_SETFL, fcntl(m_nSocketFD, F_GETFL, 0) | O_NONBLOCK) < 0 ){
  122. sprintf(cBuffer,"ERROR cannot set NONBLOCK error: %d", errno );
  123. LOG_ERROR( cBuffer );
  124. return false;
  125. };
  126. #endif
  127. #else
  128. // Initialize Winsock
  129. WSADATA wsaData;
  130. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  131. if (iResult != 0) {
  132. sprintf(cBuffer, "WSAStartup failed with error: %d\n", iResult);
  133. LOG_ERROR(cBuffer);
  134. return false;
  135. };
  136. ZeroMemory( &hints, sizeof(hints) );
  137. hints.ai_family = AF_UNSPEC;
  138. hints.ai_socktype = SOCK_STREAM;
  139. hints.ai_protocol = IPPROTO_TCP;
  140. // Resolve the server address and port
  141. sprintf( cBuffer, "%d", m_nPort );
  142. iResult = getaddrinfo( m_cHostName, cBuffer, &hints, &result );
  143. if (iResult != 0) {
  144. sprintf(cBuffer, "getaddrinfo for [%s:%d] failed with error: %d", m_cHostName, m_nPort, iResult);
  145. LOG_ERROR(cBuffer);
  146. WSACleanup();
  147. return false;
  148. };
  149. // Attempt to connect to an address until one succeeds
  150. ptr = result;
  151. while( ptr != NULL ) {
  152. // Create a SOCKET for connecting to server
  153. m_nSocketFD = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
  154. if( m_nSocketFD == INVALID_SOCKET ){
  155. sprintf( cBuffer, "Socket Creation failed with error: %ld", WSAGetLastError());
  156. LOG_ERROR(cBuffer);
  157. WSACleanup();
  158. return false;
  159. };
  160. // Connect to server.
  161. iResult = connect( m_nSocketFD, ptr->ai_addr, (int)ptr->ai_addrlen );
  162. if (iResult == SOCKET_ERROR) {
  163. sprintf(cBuffer, "Socket Connection failed[%d] with error: %ld", iResult, WSAGetLastError());
  164. LOG_ERROR(cBuffer);
  165. closesocket(m_nSocketFD);
  166. m_nSocketFD = INVALID_SOCKET;
  167. ptr = ptr->ai_next;
  168. continue;
  169. };
  170. break;
  171. };
  172. freeaddrinfo(result);
  173. if (m_nSocketFD == INVALID_SOCKET) {
  174. sprintf( cBuffer, "Unable Connect to the Server[%s:%d]!", m_cHostName, m_nPort );
  175. LOG_ERROR(cBuffer);
  176. WSACleanup();
  177. return false;
  178. };
  179. #endif
  180. /*
  181. setConnectionStatus(CS_CONNECTED);
  182. #ifdef DEBUG
  183. LOG("In case THREADS are running -> SHUTTING DOWN THREADS");
  184. #endif
  185. if( uninit() ) {
  186. #ifdef DEBUG
  187. LOG("STARTING IO THREADS");
  188. #endif
  189. return init();
  190. } else {
  191. LOG_ERROR("CAN NOT SHUTDOWN IO THREADS!");
  192. };
  193. */
  194. return true;//false;
  195. };
  196. bool clientPort::connectTo(CSTR Host, int Port){
  197. if( m_eConnectionStatus == CS_CONNECTED ){
  198. setConnectionStatus( CS_DISCONNECTED );
  199. #ifdef __linux__
  200. shutdown( m_nSocketFD, SHUT_RDWR );
  201. close( m_nSocketFD );
  202. m_nSocketFD = -1;
  203. #else
  204. closesocket(m_nSocketFD);
  205. WSACleanup();
  206. m_nSocketFD = INVALID_SOCKET;
  207. #endif
  208. };
  209. //if( m_sHostName == NULL ) m_sHostName = (CSTR) malloc( strlen( Host ) +1 );
  210. if( m_cHostName != Host ) strncpy( m_cHostName, Host, MAX_HOST_STRING_SIZE );//sprintf( m_sHostName, "%s", Host );// Prevent Self Copy
  211. m_nPort = Port;
  212. return _connect();
  213. };
  214. int clientPort::sendBlock(unsigned char *Data, int DataSize){
  215. if( m_eConnectionStatus != CS_CONNECTED ){
  216. if( connectTo( m_cHostName, m_nPort ) ){
  217. char MsgBuffer[CLOG];
  218. sprintf(MsgBuffer, "Reconnect %s [%d] OK", m_cHostName, m_nPort);
  219. LOG(MsgBuffer);
  220. };
  221. };
  222. if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  223. #ifdef __linux__
  224. int RetVal;
  225. if( m_nSocketFD > -1 ){
  226. //sprintf(cBuffer,"Write into Socket %p %d", Data, DataSize );
  227. //LOG(cBuffer);
  228. RetVal = write(m_nSocketFD, Data, DataSize);
  229. //LOG("Data writen into Socket");
  230. if( ( RetVal == -1 ) && (errno != EINTR ) && ( errno != EWOULDBLOCK ) ) {
  231. LOG_ERROR("Error acures while writing data into the Socket");
  232. //shutdown( m_nSocketFD, 2 );
  233. //close( m_nSocketFD );
  234. //m_nSocketFD = -1;
  235. disconnect();
  236. return -1;
  237. };
  238. if( RetVal < 0 ) RetVal = 0;
  239. #ifdef DUBUG_EXTRA
  240. {
  241. char cBuffer[128];
  242. sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  243. LOG(cBuffer);
  244. #ifdef DUBUG_COMMUNICATION
  245. for (int i = 0; i < RetVal; i++) {
  246. sprintf(cBuffer, "0x%02X", Data[i]);
  247. LOG(cBuffer);
  248. };
  249. #endif
  250. }
  251. #endif
  252. } else {
  253. LOG_ERROR("Not Connected!");
  254. return -1;
  255. };
  256. #else
  257. DWORD RetVal;
  258. if (m_nSocketFD != INVALID_SOCKET){
  259. // Send an initial buffer
  260. RetVal = send(m_nSocketFD, (const char *)Data, DataSize, NULL);
  261. if (RetVal == SOCKET_ERROR) {
  262. char cBuffer[CLOG];
  263. //m_bConnected = false;
  264. setConnectionStatus(CS_DISCONNECTED);
  265. sprintf(cBuffer,"send failed with error: %d", WSAGetLastError());
  266. LOG_ERROR(cBuffer);
  267. closesocket(m_nSocketFD);
  268. WSACleanup();
  269. return -1;
  270. };
  271. //sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  272. //LOG(cBuffer);
  273. if (!m_bKeepAlive){
  274. setConnectionStatus(CS_DISCONNECTED);
  275. closesocket(m_nSocketFD);
  276. WSACleanup();
  277. //m_bConnected = false;
  278. m_nSocketFD = INVALID_SOCKET;
  279. };
  280. } else {
  281. setConnectionStatus(CS_DISCONNECTED);
  282. //m_bConnected = false;
  283. return -1;
  284. };
  285. #endif
  286. return RetVal;
  287. };
  288. int clientPort::recptBlock(unsigned char *Buffer, int *BufferLength){
  289. if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  290. #ifdef __linux__
  291. int RetVal;
  292. if( m_nSocketFD > -1 ) {
  293. RetVal = recv( m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0 );
  294. if( RetVal < 1 ){
  295. if( errno!=EAGAIN && errno!=EINTR ) {
  296. LOG_ERROR("Peer Closes Connection!");
  297. //shutdown( m_nSocketFD, 2 );
  298. //close( m_nSocketFD );
  299. disconnect();
  300. *BufferLength = 0;
  301. //m_nSocketFD = -1;
  302. return -1;
  303. };
  304. };
  305. if( !m_bKeepAlive ){
  306. //shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  307. //close( m_nSocketFD );
  308. //m_nSocketFD = -1;
  309. disconnect();
  310. };
  311. } else {
  312. LOG_ERROR("Not Connected!");
  313. *BufferLength = 0;
  314. return -1;
  315. };
  316. #else
  317. DWORD RetVal;
  318. if( m_nSocketFD != INVALID_SOCKET ){
  319. // Receive until the peer closes the connection
  320. RetVal = recv( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0);
  321. if( RetVal < 1 ){
  322. if( errno!=EAGAIN && errno!=EINTR ) {
  323. //setConnectionStatus(CS_DISCONNECTED);
  324. closesocket(m_nSocketFD);
  325. WSACleanup();
  326. *BufferLength = 0;
  327. //m_bConnected = false;
  328. m_nSocketFD = INVALID_SOCKET;
  329. return -1;
  330. };
  331. };
  332. if (!m_bKeepAlive){
  333. //setConnectionStatus(CS_DISCONNECTED);
  334. closesocket(m_nSocketFD);
  335. WSACleanup();
  336. //m_bConnected = false;
  337. m_nSocketFD = INVALID_SOCKET;
  338. };
  339. } else {
  340. //setConnectionStatus(CS_DISCONNECTED);
  341. LOG_ERROR("Not Connected!");
  342. *BufferLength = 0;
  343. //m_bConnected = false;
  344. return 0;
  345. };
  346. #endif
  347. if (RetVal > 0) {
  348. #ifdef DUBUG_EXTRA
  349. char cBuffer[165];
  350. sprintf(cBuffer, "Bytes received: %d", RetVal);
  351. LOG(cBuffer);
  352. #ifdef DUBUG_COMMUNICATION
  353. for (int i = 0; i < RetVal; i++) {
  354. sprintf(cBuffer, "0x%02X", Buffer[i]);
  355. LOG(cBuffer);
  356. };
  357. #endif
  358. #endif
  359. *BufferLength = RetVal;
  360. return RetVal;
  361. };
  362. *BufferLength = 0;
  363. if (RetVal == -1) return 0;
  364. if (RetVal == 0){
  365. errno = 0;
  366. return 0;
  367. };
  368. /* r < -1, should never happen. */
  369. errno = EIO;
  370. return 0;
  371. };
  372. bool clientPort::disconnect(void){
  373. if( m_eConnectionStatus == CS_CONNECTED ){
  374. setConnectionStatus(CS_DISCONNECTED);
  375. #ifdef __linux__
  376. shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  377. close( m_nSocketFD );
  378. m_nSocketFD = -1;
  379. #else
  380. closesocket(m_nSocketFD);
  381. WSACleanup();
  382. m_nSocketFD = INVALID_SOCKET;
  383. #endif
  384. };
  385. return true;
  386. };
  387. #include <stdio.h>
  388. #include <stdlib.h>
  389. #include <string.h>
  390. #include <sys/socket.h>
  391. #include <netinet/in.h>
  392. #include <arpa/inet.h>
  393. #include <unistd.h>
  394. CSTR clientPort::getLocalIp4(){
  395. #ifdef __linux__
  396. struct sockaddr_in server_addr, client_addr;
  397. socklen_t client_addr_len = sizeof(client_addr);
  398. char ip_str[INET_ADDRSTRLEN];
  399. // Get the local address of the connected socket
  400. if (getsockname( m_nSocketFD, (struct sockaddr *)&client_addr, &client_addr_len) < 0) {
  401. LOG_ERROR("getsockname failed");
  402. return NULL;
  403. };
  404. // Convert the IP address to a human-readable string
  405. if (inet_ntop(AF_INET, &client_addr.sin_addr, ip_str, INET_ADDRSTRLEN) == NULL) {
  406. LOG_ERROR("inet_ntop failed");
  407. return NULL;
  408. };
  409. return strdup( ip_str );
  410. #else
  411. return NULL;
  412. #endif
  413. };
  414. CSTR clientPort::getRemoteIp4(){
  415. #ifdef __linux__
  416. char ip_str[INET_ADDRSTRLEN];
  417. struct sockaddr_in peer_addr;
  418. socklen_t peer_addr_len = sizeof(peer_addr);
  419. if (getpeername( m_nSocketFD, (struct sockaddr *)&peer_addr, &peer_addr_len) == -1) {
  420. LOG_ERROR("Error getting peer name");
  421. return NULL;
  422. };
  423. if (inet_ntop(AF_INET, &peer_addr.sin_addr, ip_str, INET_ADDRSTRLEN) == NULL) {
  424. LOG_ERROR("Error converting IP to string");
  425. return NULL;
  426. };
  427. return strdup(ip_str);
  428. #else
  429. return NULL;
  430. #endif
  431. };
  432. //#undef DUBUG_EXTRA
  433. //#undef DUBUG_COMMUNICATION
  434. #undef LOG
  435. #undef LOG_ERROR