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.

525 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
10 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
10 hours ago
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
10 hours 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) :
  8. transportSocket()
  9. {
  10. #else
  11. serverPort::serverPort(SOCKET AcceptedSocket) :
  12. transportSocket()
  13. {
  14. #endif
  15. m_pLog = basicLogCollection::getInstance();
  16. m_nSocketFD = AcceptedSocket;
  17. m_bKeepAlive = true;
  18. }
  19. serverPort::~serverPort()
  20. {
  21. // The accepted socket belongs to this port. It was only closed on a read/write error, so a client
  22. // removed for any other reason (module disabled / not registered, dead connection) leaked its descriptor
  23. // until accept() failed with EMFILE.
  24. #ifdef __linux__
  25. if (m_nSocketFD > -1)
  26. {
  27. shutdown(m_nSocketFD, SHUT_RDWR); // wakes up the receiver thread blocked in select()
  28. }
  29. _close(); // stop both I/O threads before the descriptor goes away
  30. if (m_nSocketFD > -1)
  31. {
  32. close(m_nSocketFD);
  33. m_nSocketFD = -1;
  34. }
  35. #else
  36. _close();
  37. if (m_nSocketFD != INVALID_SOCKET)
  38. {
  39. closesocket(m_nSocketFD);
  40. m_nSocketFD = INVALID_SOCKET;
  41. }
  42. #endif
  43. }
  44. bool serverPort::create(void)
  45. {
  46. FD_SET(m_nSocketFD, &m_sReaderSet);
  47. if (m_nReaderSetMax < m_nSocketFD)
  48. {
  49. m_nReaderSetMax = m_nSocketFD;
  50. };
  51. // No need to update reader set -> the thread is not run yet!
  52. // write( m_iControlPipeFD[1], "UPD", 4);
  53. // close( m_iControlPipeFD[1] );
  54. return transportSocket::create();
  55. }
  56. int serverPort::sendBlock(unsigned char * Data, int DataSize)
  57. {
  58. if (! m_bInitComplete || m_eConnectionStatus != CS_CONNECTED)
  59. {
  60. return -1; // Exit after unsuccess reconnect
  61. }
  62. #ifdef __linux__
  63. int RetVal;
  64. if (m_nSocketFD > -1)
  65. {
  66. #ifdef DUBUG_COMMUNICATION
  67. char cBuffer[256];
  68. sprintf(cBuffer, "Write into Socket %d bytes", DataSize);
  69. LOG(cBuffer);
  70. #endif
  71. RetVal = write(m_nSocketFD, Data, DataSize);
  72. // LOG("Data writen into Socket");
  73. if ((RetVal == -1) && (errno != EINTR) && (errno != EWOULDBLOCK))
  74. {
  75. LOG_ERROR("Error acures while writing data into the Socket");
  76. shutdown(m_nSocketFD, 2);
  77. close(m_nSocketFD);
  78. m_nSocketFD = -1;
  79. // m_bConnected = false;
  80. setConnectionStatus(CS_DISCONNECTED);
  81. return -1;
  82. };
  83. if (RetVal < 0)
  84. {
  85. RetVal = 0;
  86. }
  87. #ifdef DUBUG_COMMUNICATION
  88. sprintf(cBuffer, "Bytes Sent: %ld", RetVal);
  89. LOG(cBuffer);
  90. #ifdef DUBUG_COMMUNICATION
  91. for (int i = 0; i < RetVal; i++)
  92. {
  93. sprintf(cBuffer, "0x%02X", Data[i]);
  94. LOG(cBuffer);
  95. };
  96. #endif
  97. #endif
  98. }
  99. else
  100. {
  101. LOG_ERROR("Not Connected!");
  102. // m_bConnected = false;
  103. setConnectionStatus(CS_DISCONNECTED);
  104. return -1;
  105. };
  106. #else
  107. DWORD RetVal;
  108. if (m_nSocketFD != INVALID_SOCKET)
  109. {
  110. char cBuffer[256];
  111. // Send an initial buffer
  112. RetVal = send(m_nSocketFD, (const char *) Data, DataSize, NULL);
  113. if (RetVal == SOCKET_ERROR)
  114. {
  115. sprintf(cBuffer, "send failed with error: %d", WSAGetLastError());
  116. LOG_ERROR(cBuffer);
  117. closesocket(m_nSocketFD);
  118. WSACleanup();
  119. // m_bConnected = false;
  120. setConnectionStatus(CS_DISCONNECTED);
  121. return -1;
  122. };
  123. #ifdef DUBUG_COMMUNICATION
  124. sprintf(cBuffer, "Bytes Sent: %ld", RetVal);
  125. #endif
  126. LOG(cBuffer);
  127. if (! m_bKeepAlive)
  128. {
  129. closesocket(m_nSocketFD);
  130. WSACleanup();
  131. // m_bConnected = false;
  132. m_nSocketFD = INVALID_SOCKET;
  133. setConnectionStatus(CS_DISCONNECTED);
  134. };
  135. }
  136. else
  137. {
  138. // m_bConnected = false;
  139. setConnectionStatus(CS_DISCONNECTED);
  140. return -1;
  141. };
  142. #endif
  143. return RetVal;
  144. }
  145. int serverPort::recptBlock(unsigned char * Buffer, int * BufferLength)
  146. {
  147. // char cBuffer[256];// Buffer For Messages Only
  148. if (! m_bInitComplete || m_eConnectionStatus != CS_CONNECTED)
  149. {
  150. LOG_ERROR("Not Connected!");
  151. m_bInitComplete = false;
  152. // m_bConnected = false;
  153. setConnectionStatus(CS_DISCONNECTED);
  154. return -1; // Exit after unsuccess reconnect
  155. }
  156. #ifdef __linux__
  157. int RetVal;
  158. if (m_nSocketFD > -1)
  159. {
  160. RetVal = recv(m_nSocketFD, Buffer, MAXIMAL_SOCKET_IO_BUFFER_LENGTH, 0);
  161. // RetVal = read(m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH);
  162. // 0 = orderly close by the peer (errno is not set then and may hold an old EAGAIN);
  163. // < 0 is an error unless the call would block / was interrupted
  164. if (RetVal < 1)
  165. {
  166. if (RetVal == 0 || (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR))
  167. {
  168. LOG_ERROR("Peer Closes Connection!");
  169. shutdown(m_nSocketFD, 2);
  170. close(m_nSocketFD);
  171. *BufferLength = 0;
  172. // m_bConnected = false;
  173. m_nSocketFD = -1;
  174. setConnectionStatus(CS_DISCONNECTED);
  175. return -1;
  176. };
  177. };
  178. // if (!m_bKeepAlive ){
  179. // //LOG("KeepAlive Disabled - Clozing Socket");
  180. // shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  181. // close( m_nSocketFD );
  182. // m_bStatus = 0;
  183. // m_nSocketFD = -1;
  184. // };
  185. }
  186. else
  187. {
  188. LOG_ERROR("Not Connected!");
  189. *BufferLength = 0;
  190. // m_bConnected = false;
  191. setConnectionStatus(CS_DISCONNECTED);
  192. return -1;
  193. };
  194. #else
  195. DWORD RetVal;
  196. if (m_nSocketFD != INVALID_SOCKET)
  197. {
  198. // Receive until the peer closes the connection
  199. RetVal = recv(m_nSocketFD, (char *) Buffer, MAXIMAL_SOCKET_IO_BUFFER_LENGTH, 0);
  200. if (RetVal < 1)
  201. {
  202. if (errno != EAGAIN && errno != EINTR)
  203. {
  204. closesocket(m_nSocketFD);
  205. WSACleanup();
  206. *BufferLength = 0;
  207. // m_bConnected = false;
  208. m_nSocketFD = INVALID_SOCKET;
  209. setConnectionStatus(CS_DISCONNECTED);
  210. return -1;
  211. };
  212. };
  213. // if (!m_bKeepAlive){
  214. // closesocket(m_nSocketFD);
  215. // WSACleanup();
  216. // m_bConnected = false;
  217. // m_nSocketFD = INVALID_SOCKET;
  218. // };
  219. }
  220. else
  221. {
  222. LOG_ERROR("Not Connected!");
  223. *BufferLength = 0;
  224. // m_bConnected = false;
  225. setConnectionStatus(CS_DISCONNECTED);
  226. return -1;
  227. };
  228. #endif // linux
  229. if (RetVal > 0)
  230. {
  231. #ifdef DEBUG_EXTRA
  232. char cBuffer[165];
  233. sprintf(cBuffer, "Read max %d bytes", MAXIMAL_SOCKET_IO_BUFFER_LENGTH);
  234. LOG(cBuffer);
  235. sprintf(cBuffer, "Bytes received: %d", RetVal);
  236. LOG(cBuffer);
  237. #ifdef DUBUG_COMMUNICATION
  238. for (int i = 0; i < RetVal; i++)
  239. {
  240. sprintf(cBuffer, "0x%02X", Buffer[i]);
  241. LOG(cBuffer);
  242. };
  243. #endif
  244. #endif
  245. *BufferLength = RetVal;
  246. return RetVal;
  247. };
  248. *BufferLength = 0;
  249. if (RetVal == -1)
  250. {
  251. return 0;
  252. }
  253. if (RetVal == 0)
  254. {
  255. errno = 0;
  256. return 0;
  257. };
  258. /* r < -1, should never happen. */
  259. errno = EIO;
  260. return 0;
  261. }
  262. // #undef DEBUG_EXTRA
  263. // #undef DUBUG_COMMUNICATION
  264. #undef LOG
  265. #undef LOG_ERROR
  266. /*
  267. #include<stdio.h>
  268. #include<sys/socket.h>
  269. #include<sys/types.h>
  270. #include<netinet/in.h>
  271. #include<netdb.h>
  272. #include<errno.h>
  273. #include<string.h>
  274. #include<netinet/if_ether.h>
  275. #include<net/if.h>
  276. #include<linux/sockios.h>
  277. int main()
  278. {
  279. int socket1, socket2;
  280. socket1 = 0;
  281. socket2 = 0;
  282. struct sockaddr_in server, client;
  283. int returnstatus = 0;
  284. socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  285. if(socket1 == -1)
  286. {
  287. perror("socket()");
  288. }
  289. server.sin_family = AF_INET;
  290. server.sin_addr.s_addr = INADDR_ANY;
  291. server.sin_port = htons(8888);
  292. returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
  293. if(returnstatus == -1)
  294. {
  295. perror("bind()");
  296. }
  297. returnstatus = listen(socket1, 5);
  298. if(returnstatus == -1)
  299. {
  300. perror("listen()");
  301. }
  302. for(;;)
  303. {
  304. int buffr[4];
  305. int addrlen, n;
  306. addrlen = sizeof(client);
  307. socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
  308. printf("%s\n",inet_ntoa(client.sin_addr));
  309. printf("%d\n",(int) ntohs(client.sin_port));
  310. struct arpreq arpreq_;
  311. bzero(&arpreq_, sizeof(struct arpreq));
  312. if( ( n = ioctl(socket2, SIOCGARP, &arpreq_) ) < 0 ){
  313. perror("ioctl");
  314. }
  315. unsigned char *ptr = &arpreq_.arp_ha.sa_data[0];
  316. printf("MAC: %x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3),
  317. *(ptr+4), *(ptr+5));
  318. if (socket2 == -1)
  319. {
  320. perror("accept()");
  321. }
  322. returnstatus = read(socket2, buffr, sizeof(buffr));
  323. if(returnstatus == -1)
  324. {
  325. perror("read()");
  326. }
  327. int c[2];
  328. int *w;
  329. c[0] = buffr[0] + buffr[1];
  330. w = c;
  331. returnstatus = write(socket2, w, sizeof(c));
  332. if(returnstatus == -1)
  333. {
  334. perror("write()");
  335. }
  336. close(socket2);
  337. }
  338. close(socket1);
  339. return 0;
  340. }
  341. int addition(int x, int y)
  342. {
  343. int z;
  344. z = x + y;
  345. return(z);
  346. }
  347. 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.
  348. Anyway, here it is, and it works for me. There is a network device name hardcoded into the source code; edit as necessary
  349. Code:
  350. #include <errno.h>
  351. #include <stdio.h>
  352. #include <stdlib.h>
  353. #include <string.h>
  354. #include <sys/socket.h>
  355. #include <sys/ioctl.h>
  356. #include <net/if_arp.h>
  357. #include <netinet/in.h>
  358. #include <linux/sockios.h>
  359. static char *ethernet_mactoa(struct sockaddr *addr) {
  360. static char buff[256];
  361. unsigned char *ptr = (unsigned char *) addr->sa_data;
  362. sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
  363. (ptr[0] & 0xff), (ptr[1] & 0xff), (ptr[2] & 0xff),
  364. (ptr[3] & 0xff), (ptr[4] & 0xff), (ptr[5] & 0xff));
  365. return (buff);
  366. }
  367. int main(int argc, char *argv[]) {
  368. int s;
  369. struct arpreq areq;
  370. struct sockaddr_in *sin;
  371. struct in_addr ipaddr;
  372. if (argc != 2) {
  373. fprintf(stderr, "-- Usage: %s ipaddress\n", argv[0]);
  374. exit(1);
  375. }
  376. // Get an internet domain socket.
  377. if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  378. perror("socket");
  379. exit(1);
  380. }
  381. // Make the ARP request.
  382. memset(&areq, 0, sizeof(areq));
  383. sin = (struct sockaddr_in *) &areq.arp_pa;
  384. sin->sin_family = AF_INET;
  385. if (inet_aton(argv[1], &ipaddr) == 0) {
  386. fprintf(stderr, "-- Error: bad dotted-decimal IP '%s'.\n",
  387. argv[1]);
  388. exit(1);
  389. }
  390. sin->sin_addr = ipaddr;
  391. sin = (struct sockaddr_in *) &areq.arp_ha;
  392. sin->sin_family = ARPHRD_ETHER;
  393. strncpy(areq.arp_dev, "eth1", 15);
  394. if (ioctl(s, SIOCGARP, (caddr_t) &areq) == -1) {
  395. perror("-- Error: unable to make ARP request, error");
  396. exit(1);
  397. }
  398. printf("%s (%s) -> %s\n", argv[1],
  399. inet_ntoa(&((struct sockaddr_in *) &areq.arp_pa)->sin_addr),
  400. ethernet_mactoa(&areq.arp_ha));
  401. return 0;
  402. }
  403. #include<stdio.h>
  404. #include<sys/socket.h>
  405. #include<sys/types.h>
  406. #include<netinet/in.h>
  407. #include<netdb.h>
  408. #include<errno.h>
  409. #include<string.h>
  410. int main()
  411. {
  412. int socket1, socket2;
  413. socket1 = 0;
  414. socket2 = 0;
  415. struct sockaddr_in server, client;
  416. int returnstatus = 0;
  417. socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  418. if(socket1 == -1)
  419. {
  420. perror("socket()");
  421. }
  422. server.sin_family = AF_INET;
  423. server.sin_addr.s_addr = INADDR_ANY;
  424. server.sin_port = htons(8888);
  425. returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
  426. if(returnstatus == -1)
  427. {
  428. perror("bind()");
  429. }
  430. returnstatus = listen(socket1, 5);
  431. if(returnstatus == -1)
  432. {
  433. perror("listen()");
  434. }
  435. for(;
  436. {
  437. int buffr[4];
  438. int addrlen;
  439. addrlen = sizeof(client);
  440. socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
  441. printf("%s\n",inet_ntoa(client.sin_addr));
  442. printf("%d\n",(int) ntohs(client.sin_port));
  443. if (socket2 == -1)
  444. {
  445. perror("accept()");
  446. }
  447. returnstatus = read(socket2, buffr, sizeof(buffr));
  448. if(returnstatus == -1)
  449. {
  450. perror("read()");
  451. }
  452. int c[2];
  453. int *w;
  454. c[0] = buffr[0] + buffr[1];
  455. w = c;
  456. returnstatus = write(socket2, w, sizeof(c));
  457. if(returnstatus == -1)
  458. {
  459. perror("write()");
  460. }
  461. close(socket2);
  462. }
  463. close(socket1);
  464. return 0;
  465. }
  466. int addition(int x, int y)
  467. {
  468. int z;
  469. z = x + y;
  470. return(z);
  471. }
  472. */