CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/UDP_Proxy/udpproxy.c
Views: 1798
1
/*
2
UDP proxy code for connecting two UDP endpoints
3
Released under GNU GPLv3
4
Author: Andrew Tridgell
5
*/
6
#define _GNU_SOURCE
7
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <time.h>
12
#include <errno.h>
13
#include <stdbool.h>
14
#include <sys/socket.h>
15
#include <netdb.h>
16
#include <unistd.h>
17
#include <stdlib.h>
18
#include <fcntl.h>
19
#include <sys/types.h>
20
#include <sys/time.h>
21
#include <arpa/inet.h>
22
#include <netinet/in.h>
23
24
static bool verbose;
25
static int listen_port1, listen_port2;
26
27
static double timestamp()
28
{
29
struct timeval tval;
30
gettimeofday(&tval,NULL);
31
return tval.tv_sec + (tval.tv_usec*1.0e-6);
32
}
33
34
/*
35
open a socket of the specified type, port and address for incoming data
36
*/
37
int open_socket_in(int port)
38
{
39
struct sockaddr_in sock;
40
int res;
41
int one=1;
42
43
memset(&sock,0,sizeof(sock));
44
45
#ifdef HAVE_SOCK_SIN_LEN
46
sock.sin_len = sizeof(sock);
47
#endif
48
sock.sin_port = htons(port);
49
sock.sin_family = AF_INET;
50
51
res = socket(AF_INET, SOCK_DGRAM, 0);
52
if (res == -1) {
53
fprintf(stderr, "socket failed\n"); return -1;
54
return -1;
55
}
56
57
setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
58
59
if (bind(res, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
60
return(-1);
61
}
62
63
return res;
64
}
65
66
static void main_loop(int sock1, int sock2)
67
{
68
unsigned char buf[10240];
69
bool have_conn1=false;
70
bool have_conn2=false;
71
double last_pkt1=0;
72
double last_pkt2=0;
73
int fdmax = (sock1>sock2?sock1:sock2)+1;
74
double last_stats = timestamp();
75
uint32_t bytes_in1=0;
76
uint32_t bytes_in2=0;
77
78
while (1) {
79
fd_set fds;
80
int ret;
81
struct timeval tval;
82
double now = timestamp();
83
84
if (verbose && now - last_stats > 1) {
85
double dt = now - last_stats;
86
printf("%u: %u bytes/sec %u: %u bytes/sec\n",
87
(unsigned)listen_port1, (unsigned)(bytes_in1/dt),
88
(unsigned)listen_port2, (unsigned)(bytes_in2/dt));
89
bytes_in1 = bytes_in2 = 0;
90
last_stats = now;
91
}
92
93
if (have_conn1 && now - last_pkt1 > 10) {
94
break;
95
}
96
if (have_conn2 && now - last_pkt2 > 10) {
97
break;
98
}
99
100
FD_ZERO(&fds);
101
FD_SET(sock1, &fds);
102
FD_SET(sock2, &fds);
103
104
tval.tv_sec = 10;
105
tval.tv_usec = 0;
106
107
ret = select(fdmax, &fds, NULL, NULL, &tval);
108
if (ret == -1 && errno == EINTR) continue;
109
if (ret <= 0) break;
110
111
now = timestamp();
112
113
if (FD_ISSET(sock1, &fds)) {
114
struct sockaddr_in from;
115
socklen_t fromlen = sizeof(from);
116
int n = recvfrom(sock1, buf, sizeof(buf), 0,
117
(struct sockaddr *)&from, &fromlen);
118
if (n <= 0) break;
119
120
bytes_in1 += n;
121
122
last_pkt1 = now;
123
if (!have_conn1) {
124
if (connect(sock1, (struct sockaddr *)&from, fromlen) != 0) {
125
break;
126
}
127
have_conn1 = true;
128
printf("have conn1\n");
129
}
130
if (have_conn2) {
131
if (send(sock2, buf, n, 0) != n) {
132
break;
133
}
134
}
135
}
136
137
if (FD_ISSET(sock2, &fds)) {
138
struct sockaddr_in from;
139
socklen_t fromlen = sizeof(from);
140
int n = recvfrom(sock2, buf, sizeof(buf), 0,
141
(struct sockaddr *)&from, &fromlen);
142
if (n <= 0) break;
143
144
bytes_in2 += n;
145
146
last_pkt2 = now;
147
if (!have_conn2) {
148
if (connect(sock2, (struct sockaddr *)&from, fromlen) != 0) {
149
break;
150
}
151
have_conn2 = true;
152
printf("have conn2\n");
153
}
154
if (have_conn1) {
155
if (send(sock1, buf, n, 0) != n) {
156
break;
157
}
158
}
159
}
160
}
161
}
162
163
int main(int argc, char *argv[])
164
{
165
int sock_in1, sock_in2;
166
167
if (argc < 3) {
168
printf("Usage: udpproxy <port1> <port2>\n");
169
exit(1);
170
}
171
if (argc > 3) {
172
verbose = strcmp(argv[3],"-v") == 0;
173
printf("verbose=%u\n", (unsigned)verbose);
174
}
175
176
while (true) {
177
listen_port1 = atoi(argv[1]);
178
listen_port2 = atoi(argv[2]);
179
180
printf("Opening sockets %u %u\n", listen_port1, listen_port2);
181
sock_in1 = open_socket_in(listen_port1);
182
sock_in2 = open_socket_in(listen_port2);
183
if (sock_in1 == -1 || sock_in2 == -1) {
184
fprintf(stderr,"sock on ports %d or %d failed - %s\n",
185
listen_port1, listen_port2, strerror(errno));
186
exit(1);
187
}
188
189
main_loop(sock_in1, sock_in2);
190
close(sock_in1);
191
close(sock_in2);
192
}
193
194
return 0;
195
}
196
197