forked from yrutschle/sslh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echosrv.c
344 lines (281 loc) · 8.99 KB
/
echosrv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* echosrv: a simple line echo server with optional prefix adding.
*
* echosrv --listen localhost6:1234 --prefix "ssl: "
*
* This will bind to 1234, and echo every line pre-pending "ssl: ". This is
* used for testing: we create several such servers with different prefixes,
* then we connect test clients that can then check they get the proper data
* back (thus testing that shoveling works both ways) with the correct prefix
* (thus testing it connected to the expected service).
* **/
#define _GNU_SOURCE
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pwd.h>
#include <syslog.h>
#include <libgen.h>
#include <getopt.h>
#include <errno.h>
#define cfg sslhcfg
#include "common.h"
#undef cfg
#include "echosrv-conf.h"
/* Added to make the code compilable under CYGWIN
* */
#ifndef SA_NOCLDWAIT
#define SA_NOCLDWAIT 0
#endif
struct echocfg_item cfg;
void check_res_dump(int res, struct addrinfo *addr, char* syscall)
{
char buf[NI_MAXHOST];
if (res == -1) {
if (addr)
fprintf(stderr, "error %s:%s: %s\n",
sprintaddr(buf, sizeof(buf), addr),
syscall,
strerror(errno));
else
fprintf(stderr, "Dying just because\n");
exit(1);
}
}
void start_echo(int fd)
{
ssize_t res;
char buffer[1 << 20];
ssize_t ret;
size_t prefix_len;
int first = 1;
prefix_len = strlen(cfg.prefix);
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, cfg.prefix);
while (1) {
ret = read(fd, buffer + prefix_len, sizeof(buffer) - prefix_len);
if (ret <= 0) {
fprintf(stderr, "%s", strerror(errno));
return;
}
if (first) {
res = write(fd, buffer, ret + prefix_len);
first = 0;
if (write(1, buffer, ret + prefix_len) < 0) {
fprintf(stderr, "%s", strerror(errno));
}
} else {
res = write(fd, buffer + prefix_len, ret);
}
if (res < 0) {
fprintf(stderr, "%s", strerror(errno));
return;
}
}
}
/* TCP echo server: accepts connections to an endpoint, forks an echo for each
* connection, forever. Prefix is added at start of response stream */
void tcp_echo(struct listen_endpoint* listen_socket)
{
while (1) {
int in_socket = accept(listen_socket->socketfd, 0, 0);
if (in_socket == -1) {
perror("tcp_echo:accept");
exit(1);
}
if (!fork())
{
close(listen_socket->socketfd);
start_echo(in_socket);
exit(0);
}
close(in_socket);
waitpid(-1, NULL, WNOHANG);
}
}
void print_udp_xchange(int sockfd, struct sockaddr* addr, socklen_t addrlen)
{
struct addrinfo src_addrinfo, to_addrinfo;
char str_addr[NI_MAXHOST+1+NI_MAXSERV+1];
char str_addr2[NI_MAXHOST+1+NI_MAXSERV+1];
struct sockaddr_storage ss;
src_addrinfo.ai_addr = (struct sockaddr*)&ss;
src_addrinfo.ai_addrlen = sizeof(ss);
getsockname(sockfd, src_addrinfo.ai_addr, &src_addrinfo.ai_addrlen);
to_addrinfo.ai_addr = addr;
to_addrinfo.ai_addrlen = sizeof(*addr);
fprintf(stderr, "UDP local %s remote %s\n",
sprintaddr(str_addr, sizeof(str_addr), &src_addrinfo),
sprintaddr(str_addr2, sizeof(str_addr2), &to_addrinfo)
);
}
/* UDP echo server: receive packets, return them, forever.
* Prefix is added at each packet */
void udp_echo(struct listen_endpoint* listen_socket)
{
char data[65536];
struct sockaddr src_addr;
socklen_t addrlen;
memset(data, 0, sizeof(data));
size_t prefix_len = strlen(cfg.prefix);
memcpy(data, cfg.prefix, prefix_len);
while (1) {
addrlen = sizeof(src_addr);
ssize_t len = recvfrom(listen_socket->socketfd,
data + prefix_len,
sizeof(data) - prefix_len,
0,
&src_addr,
&addrlen);
if (len < 0) {
perror("recvfrom");
}
*(data + prefix_len + len) = 0;
fprintf(stderr, "%zd %s\n", len, data + prefix_len);
print_udp_xchange(listen_socket->socketfd, &src_addr, addrlen);
ssize_t res = sendto(listen_socket->socketfd,
data,
len + prefix_len,
0,
&src_addr,
addrlen);
if (res < 0) {
perror("sendto");
}
}
}
void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
{
int i;
for (i = 0; i < num_addr_listen; i++) {
if (!fork()) {
if (cfg.udp) {
udp_echo(&listen_sockets[i]);
} else {
tcp_echo(&listen_sockets[i]);
}
}
}
wait(NULL);
}
/* Following is a number of utility functions copied from common.c: linking
* against common.o directly means echosrv has to work with sslh config struct,
* which makes it all too awkward */
/* simplified from common.c */
char* sprintaddr(char* buf, size_t size, struct addrinfo *a)
{
char host[NI_MAXHOST], serv[NI_MAXSERV];
int res;
res = getnameinfo(a->ai_addr, a->ai_addrlen,
host, sizeof(host),
serv, sizeof(serv),
0 );
if (res) {
/* Name resolution failed: do it numerically instead */
res = getnameinfo(a->ai_addr, a->ai_addrlen,
host, sizeof(host),
serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
/* should not fail but... */
if (res) {
strcpy(host, "?");
strcpy(serv, "?");
}
}
snprintf(buf, size, "%s:%s", host, serv);
return buf;
}
/* simplified from common.c */
int listen_single_addr(struct addrinfo* addr, int keepalive, int udp)
{
struct sockaddr_storage *saddr;
int sockfd, one, res;
saddr = (struct sockaddr_storage*)addr->ai_addr;
sockfd = socket(saddr->ss_family, udp ? SOCK_DGRAM : SOCK_STREAM, 0);
check_res_dump(sockfd, addr, "socket");
one = 1;
res = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one));
check_res_dump(res, addr, "setsockopt(SO_REUSEADDR)");
if (addr->ai_addr->sa_family == AF_INET6) {
res = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one));
check_res_dump(res, addr, "setsockopt(IPV6_V6ONLY)");
}
res = bind(sockfd, addr->ai_addr, addr->ai_addrlen);
check_res_dump(res, addr, "bind");
if (!udp) {
res = listen (sockfd, 50);
check_res_dump(res, addr, "listen");
}
return sockfd;
}
/* simplified from common.c */
int resolve_split_name(struct addrinfo **out, char* host, char* serv)
{
struct addrinfo hint;
char *end;
int res;
memset(&hint, 0, sizeof(hint));
hint.ai_family = PF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
/* If it is a RFC-Compliant IPv6 address ("[1234::12]:443"), remove brackets
* around IP address */
if (host[0] == '[') {
end = strrchr(host, ']');
if (!end) {
fprintf(stderr, "%s: no closing bracket in IPv6 address?\n", host);
return -1;
}
host++; /* skip first bracket */
*end = 0; /* remove last bracket */
}
res = getaddrinfo(host, serv, &hint, out);
if (res)
fprintf(stderr, "%s `%s:%s'\n", gai_strerror(res), host, serv);
return res;
}
int start_listen_sockets(struct listen_endpoint *sockfd[])
{
struct addrinfo *addr, *start_addr;
char buf[NI_MAXHOST];
int i, res;
int num_addr = 0, keepalive = 0, udp = 0;
*sockfd = NULL;
fprintf(stderr, "Listening to:\n");
for (i = 0; i < cfg.listen_len; i++) {
udp = cfg.udp;
res = resolve_split_name(&start_addr, cfg.listen[i].host, cfg.listen[i].port);
if (res) exit(4);
for (addr = start_addr; addr; addr = addr->ai_next) {
num_addr++;
*sockfd = realloc(*sockfd, num_addr * sizeof(*sockfd));
(*sockfd)[num_addr-1].socketfd = listen_single_addr(addr, keepalive, udp);
(*sockfd)[num_addr-1].type = udp ? SOCK_DGRAM : SOCK_STREAM;
fprintf(stderr, "%d:\t%s\n", (*sockfd)[num_addr-1].socketfd, sprintaddr(buf, sizeof(buf), addr));
}
freeaddrinfo(start_addr);
}
return num_addr;
}
int main(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
int num_addr_listen;
struct listen_endpoint *listen_sockets;
memset(&cfg, 0, sizeof(cfg));
if (echocfg_cl_parse(argc, argv, &cfg))
exit(1);
echocfg_fprint(stdout, &cfg, 0);
num_addr_listen = start_listen_sockets(&listen_sockets);
main_loop(listen_sockets, num_addr_listen);
return 0;
}