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.

451 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
​
​
​
​
​
​
​
​
2 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
​
​
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. //char cBuffer[256];
  102. //sprintf(cBuffer,"Read from Socket %p", Buffer );
  103. //LOG(cBuffer);
  104. RetVal = recv( m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0 );//
  105. //RetVal = read(m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH);
  106. if( RetVal < 1 ){
  107. if( errno!=EAGAIN && errno!=EINTR ) {
  108. LOG_ERROR("Peer Closes Connection!");
  109. shutdown( m_nSocketFD, 2 );
  110. close( m_nSocketFD );
  111. *BufferLength = 0;
  112. //m_bConnected = false;
  113. m_nSocketFD = -1;
  114. setConnectionStatus( CS_DISCONNECTED );
  115. return -1;
  116. };
  117. };
  118. //if (!m_bKeepAlive ){
  119. // //LOG("KeepAlive Disabled - Clozing Socket");
  120. // shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  121. // close( m_nSocketFD );
  122. // m_bStatus = 0;
  123. // m_nSocketFD = -1;
  124. //};
  125. } else {
  126. LOG_ERROR("Not Connected!");
  127. *BufferLength = 0;
  128. //m_bConnected = false;
  129. setConnectionStatus( CS_DISCONNECTED );
  130. return -1;
  131. };
  132. #else
  133. DWORD RetVal;
  134. if( m_nSocketFD != INVALID_SOCKET ){
  135. // Receive until the peer closes the connection
  136. RetVal = recv(m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0);
  137. if( RetVal < 1 ){
  138. if( errno!=EAGAIN && errno!=EINTR ) {
  139. closesocket(m_nSocketFD);
  140. WSACleanup();
  141. *BufferLength = 0;
  142. //m_bConnected = false;
  143. m_nSocketFD = INVALID_SOCKET;
  144. setConnectionStatus( CS_DISCONNECTED );
  145. return -1;
  146. };
  147. };
  148. //if (!m_bKeepAlive){
  149. // closesocket(m_nSocketFD);
  150. // WSACleanup();
  151. // m_bConnected = false;
  152. // m_nSocketFD = INVALID_SOCKET;
  153. //};
  154. } else {
  155. LOG_ERROR("Not Connected!");
  156. *BufferLength = 0;
  157. //m_bConnected = false;
  158. setConnectionStatus( CS_DISCONNECTED );
  159. return -1;
  160. };
  161. #endif // linux
  162. if (RetVal > 0) {
  163. #ifdef DEBUG_EXTRA
  164. char cBuffer[165];
  165. sprintf(cBuffer, "Bytes received: %d", RetVal);
  166. LOG(cBuffer);
  167. #ifdef DUBUG_COMMUNICATION
  168. for (int i = 0; i < RetVal; i++) {
  169. sprintf(cBuffer, "0x%02X", Buffer[i]);
  170. LOG(cBuffer);
  171. };
  172. #endif
  173. #endif
  174. *BufferLength = RetVal;
  175. return RetVal;
  176. };
  177. *BufferLength = 0;
  178. if (RetVal == -1) return 0;
  179. if (RetVal == 0){
  180. errno = 0;
  181. return 0;
  182. };
  183. /* r < -1, should never happen. */
  184. errno = EIO;
  185. return 0;
  186. };
  187. //#undef DEBUG_EXTRA
  188. //#undef DUBUG_COMMUNICATION
  189. #undef LOG
  190. #undef LOG_ERROR
  191. /*
  192. #include<stdio.h>
  193. #include<sys/socket.h>
  194. #include<sys/types.h>
  195. #include<netinet/in.h>
  196. #include<netdb.h>
  197. #include<errno.h>
  198. #include<string.h>
  199. #include<netinet/if_ether.h>
  200. #include<net/if.h>
  201. #include<linux/sockios.h>
  202. int main()
  203. {
  204. int socket1, socket2;
  205. socket1 = 0;
  206. socket2 = 0;
  207. struct sockaddr_in server, client;
  208. int returnstatus = 0;
  209. socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  210. if(socket1 == -1)
  211. {
  212. perror("socket()");
  213. }
  214. server.sin_family = AF_INET;
  215. server.sin_addr.s_addr = INADDR_ANY;
  216. server.sin_port = htons(8888);
  217. returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
  218. if(returnstatus == -1)
  219. {
  220. perror("bind()");
  221. }
  222. returnstatus = listen(socket1, 5);
  223. if(returnstatus == -1)
  224. {
  225. perror("listen()");
  226. }
  227. for(;;)
  228. {
  229. int buffr[4];
  230. int addrlen, n;
  231. addrlen = sizeof(client);
  232. socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
  233. printf("%s\n",inet_ntoa(client.sin_addr));
  234. printf("%d\n",(int) ntohs(client.sin_port));
  235. struct arpreq arpreq_;
  236. bzero(&arpreq_, sizeof(struct arpreq));
  237. if( ( n = ioctl(socket2, SIOCGARP, &arpreq_) ) < 0 ){
  238. perror("ioctl");
  239. }
  240. unsigned char *ptr = &arpreq_.arp_ha.sa_data[0];
  241. printf("MAC: %x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3),
  242. *(ptr+4), *(ptr+5));
  243. if (socket2 == -1)
  244. {
  245. perror("accept()");
  246. }
  247. returnstatus = read(socket2, buffr, sizeof(buffr));
  248. if(returnstatus == -1)
  249. {
  250. perror("read()");
  251. }
  252. int c[2];
  253. int *w;
  254. c[0] = buffr[0] + buffr[1];
  255. w = c;
  256. returnstatus = write(socket2, w, sizeof(c));
  257. if(returnstatus == -1)
  258. {
  259. perror("write()");
  260. }
  261. close(socket2);
  262. }
  263. close(socket1);
  264. return 0;
  265. }
  266. int addition(int x, int y)
  267. {
  268. int z;
  269. z = x + y;
  270. return(z);
  271. }
  272. 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
  273. Code:
  274. #include <errno.h>
  275. #include <stdio.h>
  276. #include <stdlib.h>
  277. #include <string.h>
  278. #include <sys/socket.h>
  279. #include <sys/ioctl.h>
  280. #include <net/if_arp.h>
  281. #include <netinet/in.h>
  282. #include <linux/sockios.h>
  283. static char *ethernet_mactoa(struct sockaddr *addr) {
  284. static char buff[256];
  285. unsigned char *ptr = (unsigned char *) addr->sa_data;
  286. sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
  287. (ptr[0] & 0xff), (ptr[1] & 0xff), (ptr[2] & 0xff),
  288. (ptr[3] & 0xff), (ptr[4] & 0xff), (ptr[5] & 0xff));
  289. return (buff);
  290. }
  291. int main(int argc, char *argv[]) {
  292. int s;
  293. struct arpreq areq;
  294. struct sockaddr_in *sin;
  295. struct in_addr ipaddr;
  296. if (argc != 2) {
  297. fprintf(stderr, "-- Usage: %s ipaddress\n", argv[0]);
  298. exit(1);
  299. }
  300. // Get an internet domain socket.
  301. if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  302. perror("socket");
  303. exit(1);
  304. }
  305. // Make the ARP request.
  306. memset(&areq, 0, sizeof(areq));
  307. sin = (struct sockaddr_in *) &areq.arp_pa;
  308. sin->sin_family = AF_INET;
  309. if (inet_aton(argv[1], &ipaddr) == 0) {
  310. fprintf(stderr, "-- Error: bad dotted-decimal IP '%s'.\n",
  311. argv[1]);
  312. exit(1);
  313. }
  314. sin->sin_addr = ipaddr;
  315. sin = (struct sockaddr_in *) &areq.arp_ha;
  316. sin->sin_family = ARPHRD_ETHER;
  317. strncpy(areq.arp_dev, "eth1", 15);
  318. if (ioctl(s, SIOCGARP, (caddr_t) &areq) == -1) {
  319. perror("-- Error: unable to make ARP request, error");
  320. exit(1);
  321. }
  322. printf("%s (%s) -> %s\n", argv[1],
  323. inet_ntoa(&((struct sockaddr_in *) &areq.arp_pa)->sin_addr),
  324. ethernet_mactoa(&areq.arp_ha));
  325. return 0;
  326. }
  327. #include<stdio.h>
  328. #include<sys/socket.h>
  329. #include<sys/types.h>
  330. #include<netinet/in.h>
  331. #include<netdb.h>
  332. #include<errno.h>
  333. #include<string.h>
  334. int main()
  335. {
  336. int socket1, socket2;
  337. socket1 = 0;
  338. socket2 = 0;
  339. struct sockaddr_in server, client;
  340. int returnstatus = 0;
  341. socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  342. if(socket1 == -1)
  343. {
  344. perror("socket()");
  345. }
  346. server.sin_family = AF_INET;
  347. server.sin_addr.s_addr = INADDR_ANY;
  348. server.sin_port = htons(8888);
  349. returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
  350. if(returnstatus == -1)
  351. {
  352. perror("bind()");
  353. }
  354. returnstatus = listen(socket1, 5);
  355. if(returnstatus == -1)
  356. {
  357. perror("listen()");
  358. }
  359. for(;
  360. {
  361. int buffr[4];
  362. int addrlen;
  363. addrlen = sizeof(client);
  364. socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
  365. printf("%s\n",inet_ntoa(client.sin_addr));
  366. printf("%d\n",(int) ntohs(client.sin_port));
  367. if (socket2 == -1)
  368. {
  369. perror("accept()");
  370. }
  371. returnstatus = read(socket2, buffr, sizeof(buffr));
  372. if(returnstatus == -1)
  373. {
  374. perror("read()");
  375. }
  376. int c[2];
  377. int *w;
  378. c[0] = buffr[0] + buffr[1];
  379. w = c;
  380. returnstatus = write(socket2, w, sizeof(c));
  381. if(returnstatus == -1)
  382. {
  383. perror("write()");
  384. }
  385. close(socket2);
  386. }
  387. close(socket1);
  388. return 0;
  389. }
  390. int addition(int x, int y)
  391. {
  392. int z;
  393. z = x + y;
  394. return(z);
  395. }
  396. */