-
Notifications
You must be signed in to change notification settings - Fork 1
/
gw.c
185 lines (155 loc) · 3.9 KB
/
gw.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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <poll.h>
#include "chaos.h"
static const char *argv0;
static const char *port;
static const char *contact;
static const char *host;
static char *peer;
static int server;
static char buffer[MAX_PACKET];
static void fatal(const char *message, int error)
{
fprintf(stderr, "Fatal error %s\n", message);
if (error)
fprintf(stderr, ": %s", strerror(error));
fputc('\n', stderr);
exit(1);
}
static void disconnect(const char *message, int error, int code)
{
fprintf(stderr, "Host %s %s\n", peer, message);
if (error)
fprintf(stderr, ": %s", strerror(error));
fputc('\n', stderr);
exit(code);
}
static void copy(int src, int dst)
{
ssize_t n = read(src, buffer, sizeof buffer);
if (n < 0)
disconnect("read error", errno, 1);
if (n == 0)
disconnect("connection closed", 0, 0);
if (write(dst, buffer, n) < 0)
disconnect("write error", errno, 1);
}
static void forward(int s, int c)
{
struct pollfd pfd[2];
pfd[0].fd = s;
pfd[0].events = POLLIN|POLLERR|POLLHUP;
pfd[1].fd = c;
pfd[1].events = POLLIN|POLLERR|POLLHUP;
for(;;) {
int x = poll(pfd, 2, 10*1000);
if (x < 0) {
fprintf(stderr, "Poll error: %s\n", strerror(errno));
continue;
}
if (x == 0)
continue;
if (pfd[0].revents & POLLIN)
copy(pfd[0].fd, pfd[1].fd);
if (pfd[1].revents & POLLIN)
copy(pfd[1].fd, pfd[0].fd);
if ((pfd[0].revents | pfd[1].revents) & POLLERR)
disconnect("connection error", 0, 1);
if ((pfd[0].revents | pfd[1].revents) & POLLHUP)
disconnect("connection hung up", 0, 1);
}
}
static void incoming(void)
{
struct sockaddr addr;
socklen_t len = sizeof addr;
int s, c;
s = accept(server, &addr, &len);
if (s < 0) {
fprintf(stderr, "Error calling accept: %s\n", strerror(errno));
return;
}
if (fork()) {
close(s);
return;
}
if (addr.sa_family == AF_INET) {
struct sockaddr_in *x = (struct sockaddr_in *)&addr;
peer = inet_ntoa(x->sin_addr);
fprintf(stderr, "Incoming connection from %s to port %s\n", peer, port);
}
c = chaos_stream();
if (c < 0)
fatal("creating Chaosnet stream", errno);
if (chaos_stream_rfc(c, host, contact) < 0)
fatal("error during request for connection", errno);
fprintf(stderr, "Opened connection to %s contact %s\n", host, contact);
forward(s, c);
}
static void serve(void)
{
struct addrinfo hints;
struct addrinfo *addr;
struct addrinfo *rp;
int reuse = 1;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(NULL, port, &hints, &addr) != 0)
fatal("calling getaddrinfo", errno);
for (rp = addr; rp != NULL; rp = rp->ai_next) {
server = socket(rp->ai_family, rp->ai_socktype, 0);
if (server < 0)
fatal("creating socket", errno);
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse) < 0)
fatal("calling setsockopt", errno);
if (bind(server, rp->ai_addr, rp->ai_addrlen) == 0)
goto ok;
fprintf(stderr, "Bind: %s\n", strerror(errno));
}
fatal("binding socket", 0);
ok:
freeaddrinfo(addr);
if (listen(server, 1) < 0)
fatal("listening", errno);
for (;;)
incoming();
}
static int usage(int code)
{
FILE *f = code ? stderr : stdout;
fprintf(f, "Usage: %s [-h] <port> <contact> <host>\n", argv0);
exit(code);
}
int main(int argc, char **argv)
{
int c;
argv0 = argv[0];
while((c = getopt(argc, argv, "h")) != -1) {
switch(c) {
case 'h':
usage(0);
break;
default:
usage(1);
break;
}
}
if (argc - optind != 3)
usage(1);
port = argv[optind];
contact = argv[optind + 1];
host = argv[optind + 2];
serve();
return 0;
}