Debian Codegraph @

tests/proxy-test-server.c

raw symbols SCIP
1 /*
2 * Copyright © Stéphane Raimbault <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 /* Proxy test server: listens on TCP port 1503 and forwards all requests
8 to a backend server on TCP port 1502 using modbus_proxy(). */
9
10 #include <errno.h>
11 #include <modbus.h>cxx . . $ `<file>/src/modbus.h`/
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 #ifndef PROXY_PORT
18 #define PROXY_PORTcxx . . $ `tests/proxy-test-server.c:18:9`! 1503
19 #endif
20
21 #ifndef BACKEND_PORT
22 #define BACKEND_PORTcxx . . $ `tests/proxy-test-server.c:22:9`! 1502
23 #endif
24
25 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
26 {
27 modbus_tcxx . . $ modbus_t# *frontend_ctx;
28 modbus_tcxx . . $ modbus_t# *backend_ctx;
29 int s = -1;
30 int rc;
31 uint8_tcxx . . $ uint8_t# query[MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!];
32 char *backend_ip = "127.0.0.1";
33
34 if (argc > 1) {
35 backend_ip = argv[1];
36 }
37
38 /* Frontend: accept connections from clients */
39 frontend_ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).("0.0.0.0", PROXY_PORTcxx . . $ `tests/proxy-test-server.c:18:9`!);
40 if (frontend_ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
41 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Failed to create frontend context: %s\n",
42 modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
43 return -1;
44 }
45
46 /* Backend: connect to the real Modbus server */
47 backend_ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).(backend_ip, BACKEND_PORTcxx . . $ `tests/proxy-test-server.c:22:9`!);
48 if (backend_ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
49 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Failed to create backend context: %s\n",
50 modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
51 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(frontend_ctx);
52 return -1;
53 }
54
55 modbus_set_debugcxx . . $ modbus_set_debug(2a7e0e283938d456).(frontend_ctx, TRUEcxx . . $ `src/modbus.h:51:9`!);
56 modbus_set_debugcxx . . $ modbus_set_debug(2a7e0e283938d456).(backend_ctx, TRUEcxx . . $ `src/modbus.h:51:9`!);
57
58 /* Connect to the backend server */
59 if (modbus_connectcxx . . $ modbus_connect(c57adc5f48380e39).(backend_ctx) == -1) {
60 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Backend connection failed: %s\n",
61 modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
62 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(frontend_ctx);
63 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(backend_ctx);
64 return -1;
65 }
66
67 /* Listen for frontend client connections */
68 s = modbus_tcp_listencxx . . $ modbus_tcp_listen(2a7e0e283938d456).(frontend_ctx, 1);
69 if (s == -1) {
70 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Frontend listen failed: %s\n",
71 modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
72 modbus_closecxx . . $ modbus_close(24b4870c26038e9).(backend_ctx);
73 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(frontend_ctx);
74 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(backend_ctx);
75 return -1;
76 }
77
78 modbus_tcp_acceptcxx . . $ modbus_tcp_accept(e415c7f53a3f0b7).(frontend_ctx, &s);
79
80 for (;;) {
81 do {
82 rc = modbus_receivecxx . . $ modbus_receive(744a031d86f39170).(frontend_ctx, query);
83 /* Filtered queries return 0 */
84 } while (rc == 0);
85
86 if (rc == -1) {
87 /* Connection closed or error */
88 break;
89 }
90
91 /* Forward request to backend and relay response */
92 rc = modbus_proxycxx . . $ modbus_proxy(6441bf31ea5943db).(frontend_ctx, backend_ctx, query, rc);
93 if (rc == -1) {
94 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Proxy error: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
95 /* Continue serving, the exception was already sent */
96 }
97 }
98
99 printfcxx . . $ printf(581fddd72a8db8fc).("Proxy server exiting: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
100
101 if (s != -1) {
102 closecxx . . $ close(7864480464b09eea).(s);
103 }
104 modbus_closecxx . . $ modbus_close(24b4870c26038e9).(backend_ctx);
105 modbus_closecxx . . $ modbus_close(24b4870c26038e9).(frontend_ctx);
106 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(backend_ctx);
107 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(frontend_ctx);
108
109 return 0;
110 }