-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtproxy_example.c
264 lines (222 loc) · 7.91 KB
/
tproxy_example.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
//Written by Kristian Evensen <[email protected]>
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/queue.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <errno.h>
#include "tproxy_example.h"
#include "tproxy_example_conn.h"
void close_tcp_conn(tproxy_conn_t *conn, struct tailhead *conn_list,
struct tailhead *close_list){
conn->state = CONN_CLOSED;
TAILQ_REMOVE(conn_list, conn, conn_ptrs);
TAILQ_INSERT_TAIL(close_list, conn, conn_ptrs);
}
int handle_epollin(tproxy_conn_t *conn){
int numbytes;
int fd_in, fd_out;
//Easy way to determin which socket is ready for reading
//TODO: Optimize. This one allows me quick lookup for conn, but
//I need to make a system call to determin which socket
if(ioctl(conn->local_fd, FIONREAD, &numbytes) != -1
&& numbytes > 0){
fd_in = conn->local_fd;
fd_out = conn->remote_fd;
} else {
fd_in = conn->remote_fd;
fd_out = conn->local_fd;
}
//Optimize with SPLICE_F_MORE later
numbytes = splice(fd_in, NULL, conn->splice_pipe[1], NULL,
SPLICE_LEN, SPLICE_F_MOVE);
if(numbytes > 0)
numbytes = splice(conn->splice_pipe[0], NULL, fd_out, NULL,
numbytes, SPLICE_F_MOVE);
return numbytes;
}
void remove_closed_connections(struct tailhead *close_list){
tproxy_conn_t *conn = NULL;
while(close_list->tqh_first != NULL){
conn = (tproxy_conn_t*) close_list->tqh_first;
TAILQ_REMOVE(close_list, close_list->tqh_first, conn_ptrs);
fprintf(stderr, "Socket %d and %d closed, connection removed\n",
conn->local_fd, conn->remote_fd);
free_conn(conn);
}
}
int event_loop(int listen_fd){
int numbytes = 0, retval = 0, num_events = 0;
int tmp_fd = 0; //Used to temporarily hold the accepted file descriptor
tproxy_conn_t *conn = NULL;
int efd, i;
struct epoll_event ev, events[MAX_EPOLL_EVENTS];
struct tailhead conn_list, close_list;
uint8_t check_close = 0;
//Initialize queue (remember that TAILQ_HEAD just defines the struct)
TAILQ_INIT(&conn_list);
TAILQ_INIT(&close_list);
if((efd = epoll_create(1)) == -1){
perror("epoll_create");
return -1;
}
//Start monitoring listen socket
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
//There is only one listen socket, and I want to use ptr in order to have
//easy access to the connections. So if ptr is NULL that means an event on
//listen socket.
ev.data.ptr = NULL;
if(epoll_ctl(efd, EPOLL_CTL_ADD, listen_fd, &ev) == -1){
perror("epoll_ctl (listen socket)");
return -1;
}
while(1){
if((num_events = epoll_wait(efd, events, MAX_EPOLL_EVENTS, -1)) == -1){
perror("epoll_wait");
retval = -1;
break;
}
for(i=0; i<num_events; i++){
if(events[i].data.ptr == NULL){
//Accept new connection
tmp_fd = accept(listen_fd, NULL, NULL);
if((conn = add_tcp_connection(efd, &conn_list, tmp_fd))
== NULL){
fprintf(stderr, "Failed to add connection\n");
close(tmp_fd);
}
} else {
conn = (tproxy_conn_t*) events[i].data.ptr;
//Only applies to remote_fd, connection attempt has
//succeeded/failed
if(events[i].events & EPOLLOUT){
if(check_connection_attempt(conn, efd) == -1){
fprintf(stderr, "Connection attempt failed for %d\n",
conn->remote_fd);
check_close = 1;
close_tcp_conn(conn, &conn_list, &close_list);
}
continue;
} else if(conn->state != CONN_CLOSED &&
(events[i].events & EPOLLRDHUP ||
events[i].events & EPOLLHUP ||
events[i].events & EPOLLERR)){
check_close = 1;
close_tcp_conn(conn, &conn_list, &close_list);
continue;
}
//Since I use an event cache, earlier events might cause for
//example this connection to be closed. No need to process fd if
//that is the case
if(conn->state == CONN_CLOSED){
continue;
}
numbytes = handle_epollin(conn);
//Splice fails if for example remote socket is
//closed during splicing. Returns 0 if a peer closes the socket.
//Another way of detecting the latter is to use EPOLLRDHUP
if(numbytes <= 0){
close_tcp_conn(conn, &conn_list, &close_list);
check_close = 1;
}
}
}
//Remove connections
if(check_close)
remove_closed_connections(&close_list);
check_close = 0;
}
//Add cleanup
return retval;
}
int8_t block_sigpipe(){
sigset_t sigset;
memset(&sigset, 0, sizeof(sigset));
//Get the old sigset, add SIGPIPE and update sigset
if(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1){
perror("sigprocmask (get)");
return -1;
}
if(sigaddset(&sigset, SIGPIPE) == -1){
perror("sigaddset");
return -1;
}
if(sigprocmask(SIG_BLOCK, &sigset, NULL) == -1){
perror("sigprocmask (set)");
return -1;
}
return 0;
}
int main(int argc, char *argv[]){
int listen_fd = 0;
int yes = 1, retval = 0;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
//IPv4/IPv6 transition works out of the box, so no need to specify family
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
//Create the listen sock and bind it to the desired port
if(getaddrinfo(NULL, TPROXY_PORT, &hints, &res) != 0){
perror("getaddrinfo: ");
exit(EXIT_FAILURE);
}
if((listen_fd = socket(res->ai_family, res->ai_socktype,
res->ai_protocol)) == -1){
perror("socket: ");
exit(EXIT_FAILURE);
}
if(setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))
== -1){
perror("setsockopt (SO_REUSEADDR): ");
close(listen_fd);
exit(EXIT_FAILURE);
}
//Mark that this socket can be used for transparent proxying
//This allows the socket to accept connections for non-local IPs
if(setsockopt(listen_fd, SOL_IP, IP_TRANSPARENT, &yes, sizeof(yes))
== -1){
perror("setsockopt (IP_TRANSPARENT): ");
close(listen_fd);
exit(EXIT_FAILURE);
}
if(bind(listen_fd, res->ai_addr, res->ai_addrlen) == -1){
perror("bind: ");
close(listen_fd);
exit(EXIT_FAILURE);
}
if(listen(listen_fd, BACKLOG) == -1){
perror("listen: ");
close(listen_fd);
exit(EXIT_FAILURE);
}
//splice() causes the process to receive the SIGPIPE-signal if one part (for
//example a socket) is closed during splice(). I would rather have splice()
//fail and return -1, so blocking SIGPIPE.
if(block_sigpipe() == -1){
fprintf(stderr, "Could not block SIGPIPE signal\n");
close(listen_fd);
exit(EXIT_FAILURE);
}
fprintf(stderr, "Will listen to port %s\n", TPROXY_PORT);
retval = event_loop(listen_fd);
close(listen_fd);
fprintf(stderr, "Will exit\n");
if(retval < 0)
exit(EXIT_FAILURE);
else
exit(EXIT_SUCCESS);
}