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.

450 lines
10 KiB

7 years ago
​
​
​
​
2 years ago
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
6 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
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
​
2 years ago
7 years ago
2 years ago
7 years ago
​
7 years ago
​
2 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
  1. #include "serverPort.h"
  2. #define LOG( X ) m_pLog->Log( "serverPort", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "serverPort", this, _LOG_ERROR, X )
  4. //#define DEBUG_EXTRA
  5. //#define DUBUG_COMMUNICATION
  6. #ifdef __linux__
  7. serverPort::serverPort( int AcceptedSocket ):transportSocket(){
  8. #else
  9. serverPort::serverPort( SOCKET AcceptedSocket ):transportSocket(){
  10. #endif
  11. m_pLog = basicLogCollection::getInstance();
  12. m_nSocketFD = AcceptedSocket;
  13. m_bKeepAlive = true;
  14. };
  15. serverPort::~serverPort(){
  16. };
  17. int serverPort::sendBlock(unsigned char *Data, int DataSize){
  18. if( !m_bInitComplete || m_eConnectionStatus!= CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  19. #ifdef __linux__
  20. int RetVal;
  21. if( m_nSocketFD > -1 ){
  22. #ifdef DUBUG_COMMUNICATION
  23. char cBuffer[256];
  24. sprintf(cBuffer,"Write into Socket %d bytes", DataSize );
  25. LOG(cBuffer);
  26. #endif
  27. RetVal = write(m_nSocketFD, Data, DataSize);
  28. //LOG("Data writen into Socket");
  29. if( ( RetVal == -1 ) && (errno != EINTR ) && ( errno != EWOULDBLOCK ) ) {
  30. LOG_ERROR("Error acures while writing data into the Socket");
  31. shutdown( m_nSocketFD, 2 );
  32. close( m_nSocketFD );
  33. m_nSocketFD = -1;
  34. //m_bConnected = false;
  35. setConnectionStatus( CS_DISCONNECTED );
  36. return -1;
  37. };
  38. if( RetVal < 0 ) RetVal = 0;
  39. #ifdef DUBUG_COMMUNICATION
  40. sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  41. LOG(cBuffer);
  42. #ifdef DUBUG_COMMUNICATION
  43. for (int i = 0; i < RetVal; i++) {
  44. sprintf(cBuffer, "0x%02X", Data[i]);
  45. LOG(cBuffer);
  46. };
  47. #endif
  48. #endif
  49. } else {
  50. LOG_ERROR("Not Connected!");
  51. //m_bConnected = false;
  52. setConnectionStatus( CS_DISCONNECTED );
  53. return -1;
  54. };
  55. #else
  56. DWORD RetVal;
  57. if (m_nSocketFD != INVALID_SOCKET){
  58. char cBuffer[256];
  59. // Send an initial buffer
  60. RetVal = send(m_nSocketFD, (const char *)Data, DataSize, NULL);
  61. if (RetVal == SOCKET_ERROR) {
  62. sprintf(cBuffer,"send failed with error: %d", WSAGetLastError());
  63. LOG_ERROR(cBuffer);
  64. closesocket(m_nSocketFD);
  65. WSACleanup();
  66. //m_bConnected = false;
  67. setConnectionStatus( CS_DISCONNECTED );
  68. return -1;
  69. };
  70. #ifdef DUBUG_COMMUNICATION
  71. sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  72. #endif
  73. LOG(cBuffer);
  74. if (!m_bKeepAlive){
  75. closesocket(m_nSocketFD);
  76. WSACleanup();
  77. //m_bConnected = false;
  78. m_nSocketFD = INVALID_SOCKET;
  79. setConnectionStatus( CS_DISCONNECTED );
  80. };
  81. } else {
  82. //m_bConnected = false;
  83. setConnectionStatus( CS_DISCONNECTED );
  84. return -1;
  85. };
  86. #endif
  87. return RetVal;
  88. };
  89. int serverPort::recptBlock( unsigned char *Buffer, int *BufferLength ){
  90. //char cBuffer[256];// Buffer For Messages Only
  91. if( !m_bInitComplete || m_eConnectionStatus!= CS_CONNECTED ){
  92. LOG_ERROR("Not Connected!");
  93. m_bInitComplete = false;
  94. //m_bConnected = false;
  95. setConnectionStatus( CS_DISCONNECTED );
  96. return -1; // Exit after unsuccess reconnect
  97. }
  98. #ifdef __linux__
  99. int RetVal;
  100. if( m_nSocketFD > -1 ) {
  101. RetVal = recv( m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0 );
  102. //RetVal = read(m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH);
  103. if( RetVal < 1 ){
  104. if( errno!=EAGAIN && errno!=EINTR ) {
  105. LOG_ERROR("Peer Closes Connection!");
  106. shutdown( m_nSocketFD, 2 );
  107. close( m_nSocketFD );
  108. *BufferLength = 0;
  109. //m_bConnected = false;
  110. m_nSocketFD = -1;
  111. setConnectionStatus( CS_DISCONNECTED );
  112. return -1;
  113. };
  114. };
  115. //if (!m_bKeepAlive ){
  116. // //LOG("KeepAlive Disabled - Clozing Socket");
  117. // shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  118. // close( m_nSocketFD );
  119. // m_bStatus = 0;
  120. // m_nSocketFD = -1;
  121. //};
  122. } else {
  123. LOG_ERROR("Not Connected!");
  124. *BufferLength = 0;
  125. //m_bConnected = false;
  126. setConnectionStatus( CS_DISCONNECTED );
  127. return -1;
  128. };
  129. #else
  130. DWORD RetVal;
  131. if( m_nSocketFD != INVALID_SOCKET ){
  132. // Receive until the peer closes the connection
  133. RetVal = recv(m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0);
  134. if( RetVal < 1 ){
  135. if( errno!=EAGAIN && errno!=EINTR ) {
  136. closesocket(m_nSocketFD);
  137. WSACleanup();
  138. *BufferLength = 0;
  139. //m_bConnected = false;
  140. m_nSocketFD = INVALID_SOCKET;
  141. setConnectionStatus( CS_DISCONNECTED );
  142. return -1;
  143. };
  144. };
  145. //if (!m_bKeepAlive){
  146. // closesocket(m_nSocketFD);
  147. // WSACleanup();
  148. // m_bConnected = false;
  149. // m_nSocketFD = INVALID_SOCKET;
  150. //};
  151. } else {
  152. LOG_ERROR("Not Connected!");
  153. *BufferLength = 0;
  154. //m_bConnected = false;
  155. setConnectionStatus( CS_DISCONNECTED );
  156. return -1;
  157. };
  158. #endif // linux
  159. if (RetVal > 0) {
  160. #ifdef DEBUG_EXTRA
  161. char cBuffer[165];
  162. sprintf(cBuffer,"Read max %d bytes", MAXIMAL_SOCKET_PACKET_LENGTH );
  163. LOG(cBuffer);
  164. sprintf(cBuffer, "Bytes received: %d", RetVal);
  165. LOG(cBuffer);
  166. #ifdef DUBUG_COMMUNICATION
  167. for (int i = 0; i < RetVal; i++) {
  168. sprintf(cBuffer, "0x%02X", Buffer[i]);
  169. LOG(cBuffer);
  170. };
  171. #endif
  172. #endif
  173. *BufferLength = RetVal;
  174. return RetVal;
  175. };
  176. *BufferLength = 0;
  177. if (RetVal == -1) return 0;
  178. if (RetVal == 0){
  179. errno = 0;
  180. return 0;
  181. };
  182. /* r < -1, should never happen. */
  183. errno = EIO;
  184. return 0;
  185. };
  186. //#undef DEBUG_EXTRA
  187. //#undef DUBUG_COMMUNICATION
  188. #undef LOG
  189. #undef LOG_ERROR
  190. /*
  191. #include<stdio.h>
  192. #include<sys/socket.h>
  193. #include<sys/types.h>
  194. #include<netinet/in.h>
  195. #include<netdb.h>
  196. #include<errno.h>
  197. #include<string.h>
  198. #include<netinet/if_ether.h>
  199. #include<net/if.h>
  200. #include<linux/sockios.h>
  201. int main()
  202. {
  203. int socket1, socket2;
  204. socket1 = 0;
  205. socket2 = 0;
  206. struct sockaddr_in server, client;
  207. int returnstatus = 0;
  208. socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  209. if(socket1 == -1)
  210. {
  211. perror("socket()");
  212. }
  213. server.sin_family = AF_INET;
  214. server.sin_addr.s_addr = INADDR_ANY;
  215. server.sin_port = htons(8888);
  216. returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
  217. if(returnstatus == -1)
  218. {
  219. perror("bind()");
  220. }
  221. returnstatus = listen(socket1, 5);
  222. if(returnstatus == -1)
  223. {
  224. perror("listen()");
  225. }
  226. for(;;)
  227. {
  228. int buffr[4];
  229. int addrlen, n;
  230. addrlen = sizeof(client);
  231. socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
  232. printf("%s\n",inet_ntoa(client.sin_addr));
  233. printf("%d\n",(int) ntohs(client.sin_port));
  234. struct arpreq arpreq_;
  235. bzero(&arpreq_, sizeof(struct arpreq));
  236. if( ( n = ioctl(socket2, SIOCGARP, &arpreq_) ) < 0 ){
  237. perror("ioctl");
  238. }
  239. unsigned char *ptr = &arpreq_.arp_ha.sa_data[0];
  240. printf("MAC: %x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3),
  241. *(ptr+4), *(ptr+5));
  242. if (socket2 == -1)
  243. {
  244. perror("accept()");
  245. }
  246. returnstatus = read(socket2, buffr, sizeof(buffr));
  247. if(returnstatus == -1)
  248. {
  249. perror("read()");
  250. }
  251. int c[2];
  252. int *w;
  253. c[0] = buffr[0] + buffr[1];
  254. w = c;
  255. returnstatus = write(socket2, w, sizeof(c));
  256. if(returnstatus == -1)
  257. {
  258. perror("write()");
  259. }
  260. close(socket2);
  261. }
  262. close(socket1);
  263. return 0;
  264. }
  265. int addition(int x, int y)
  266. {
  267. int z;
  268. z = x + y;
  269. return(z);
  270. }
  271. Sorry to have left you for a while. I actually had trouble re-finding the code on the internet, after finding numerous instances on my first search. The version I had tried and tested was on a computer that was unavailable to me for some time. Anyway, here it is, and it works for me. There is a network device name hardcoded into the source code; edit as necessary
  272. Code:
  273. #include <errno.h>
  274. #include <stdio.h>
  275. #include <stdlib.h>
  276. #include <string.h>
  277. #include <sys/socket.h>
  278. #include <sys/ioctl.h>
  279. #include <net/if_arp.h>
  280. #include <netinet/in.h>
  281. #include <linux/sockios.h>
  282. static char *ethernet_mactoa(struct sockaddr *addr) {
  283. static char buff[256];
  284. unsigned char *ptr = (unsigned char *) addr->sa_data;
  285. sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
  286. (ptr[0] & 0xff), (ptr[1] & 0xff), (ptr[2] & 0xff),
  287. (ptr[3] & 0xff), (ptr[4] & 0xff), (ptr[5] & 0xff));
  288. return (buff);
  289. }
  290. int main(int argc, char *argv[]) {
  291. int s;
  292. struct arpreq areq;
  293. struct sockaddr_in *sin;
  294. struct in_addr ipaddr;
  295. if (argc != 2) {
  296. fprintf(stderr, "-- Usage: %s ipaddress\n", argv[0]);
  297. exit(1);
  298. }
  299. // Get an internet domain socket.
  300. if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  301. perror("socket");
  302. exit(1);
  303. }
  304. // Make the ARP request.
  305. memset(&areq, 0, sizeof(areq));
  306. sin = (struct sockaddr_in *) &areq.arp_pa;
  307. sin->sin_family = AF_INET;
  308. if (inet_aton(argv[1], &ipaddr) == 0) {
  309. fprintf(stderr, "-- Error: bad dotted-decimal IP '%s'.\n",
  310. argv[1]);
  311. exit(1);
  312. }
  313. sin->sin_addr = ipaddr;
  314. sin = (struct sockaddr_in *) &areq.arp_ha;
  315. sin->sin_family = ARPHRD_ETHER;
  316. strncpy(areq.arp_dev, "eth1", 15);
  317. if (ioctl(s, SIOCGARP, (caddr_t) &areq) == -1) {
  318. perror("-- Error: unable to make ARP request, error");
  319. exit(1);
  320. }
  321. printf("%s (%s) -> %s\n", argv[1],
  322. inet_ntoa(&((struct sockaddr_in *) &areq.arp_pa)->sin_addr),
  323. ethernet_mactoa(&areq.arp_ha));
  324. return 0;
  325. }
  326. #include<stdio.h>
  327. #include<sys/socket.h>
  328. #include<sys/types.h>
  329. #include<netinet/in.h>
  330. #include<netdb.h>
  331. #include<errno.h>
  332. #include<string.h>
  333. int main()
  334. {
  335. int socket1, socket2;
  336. socket1 = 0;
  337. socket2 = 0;
  338. struct sockaddr_in server, client;
  339. int returnstatus = 0;
  340. socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  341. if(socket1 == -1)
  342. {
  343. perror("socket()");
  344. }
  345. server.sin_family = AF_INET;
  346. server.sin_addr.s_addr = INADDR_ANY;
  347. server.sin_port = htons(8888);
  348. returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
  349. if(returnstatus == -1)
  350. {
  351. perror("bind()");
  352. }
  353. returnstatus = listen(socket1, 5);
  354. if(returnstatus == -1)
  355. {
  356. perror("listen()");
  357. }
  358. for(;
  359. {
  360. int buffr[4];
  361. int addrlen;
  362. addrlen = sizeof(client);
  363. socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
  364. printf("%s\n",inet_ntoa(client.sin_addr));
  365. printf("%d\n",(int) ntohs(client.sin_port));
  366. if (socket2 == -1)
  367. {
  368. perror("accept()");
  369. }
  370. returnstatus = read(socket2, buffr, sizeof(buffr));
  371. if(returnstatus == -1)
  372. {
  373. perror("read()");
  374. }
  375. int c[2];
  376. int *w;
  377. c[0] = buffr[0] + buffr[1];
  378. w = c;
  379. returnstatus = write(socket2, w, sizeof(c));
  380. if(returnstatus == -1)
  381. {
  382. perror("write()");
  383. }
  384. close(socket2);
  385. }
  386. close(socket1);
  387. return 0;
  388. }
  389. int addition(int x, int y)
  390. {
  391. int z;
  392. z = x + y;
  393. return(z);
  394. }
  395. */