forked from kristrev/tproxy-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtproxy_example_conn.c
277 lines (235 loc) · 8.5 KB
/
tproxy_example_conn.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
//Written by Kristian Evensen <[email protected]>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <errno.h>
#include <string.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/netfilter_ipv4.h>
#include "tproxy_example_conn.h"
//Createas a socket and initiates the connection to the host specified by
//remote_addr.
//Returns 0 if something fails, >0 on success (socket fd).
static int connect_remote(struct sockaddr_storage *remote_addr){
int remote_fd = 0, yes = 1;
//Use NONBLOCK to avoid slow connects affecting the performance of other
//connections
if((remote_fd = socket(remote_addr->ss_family, SOCK_STREAM |
SOCK_NONBLOCK, 0)) < 0){
perror("socket (connect_remote): ");
return 0;
}
if(setsockopt(remote_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0){
perror("setsockopt (SO_REUSEADDR, connect_remote): ");
close(remote_fd);
return 0;
}
if(connect(remote_fd, (struct sockaddr*) remote_addr,
remote_addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6)) < 0){
if(errno != EINPROGRESS){
perror("connect (connect_remote): ");
close(remote_fd);
return 0;
}
}
return remote_fd;
}
//Store the original destination address in remote_addr
//Return 0 on success, <0 on failure
static int get_org_dstaddr(int sockfd, struct sockaddr_storage *orig_dst){
u_short port = 0;
char orig_dst_str[INET6_ADDRSTRLEN];
socklen_t addrlen = sizeof(*orig_dst);
memset(orig_dst, 0, addrlen);
{
union {
struct sockaddr_in6 in6;
struct sockaddr_in in;
struct sockaddr at;
} dest;
socklen_t destlen = sizeof(dest);
getsockname(sockfd, &dest, &destlen);
if(dest.in.sin_family == AF_INET){
inet_ntop(AF_INET, &dest.in.sin_addr, orig_dst_str, INET_ADDRSTRLEN);
} else if(dest.in6.sin6_family == AF_INET6){
inet_ntop(AF_INET, &dest.in6.sin6_addr, orig_dst_str, INET_ADDRSTRLEN);
}
fprintf(stderr, "sockname: %s\n", orig_dst_str);
if (strcmp(orig_dst_str, "192.168.112.1")) {
return -1;
}
getpeername(sockfd, &dest, &destlen);
if(dest.in.sin_family == AF_INET){
inet_ntop(AF_INET, &dest.in.sin_addr, orig_dst_str, INET_ADDRSTRLEN);
} else if(dest.in6.sin6_family == AF_INET6){
inet_ntop(AF_INET, &dest.in6.sin6_addr, orig_dst_str, INET_ADDRSTRLEN);
}
fprintf(stderr, "peername%s\n", orig_dst_str);
if (strcmp(orig_dst_str, "192.168.111.1")) {
return -1;
}
}
//For UDP transparent proxying:
//Set IP_RECVORIGDSTADDR socket option for getting the original
//destination of a datagram
int rc = getsockopt(sockfd, SOL_IP, SO_ORIGINAL_DST, orig_dst, &addrlen);
if (rc) {
perror("getsockopt: ");
return -1;
}
goto check_and_return;
//Socket is bound to original destination
if(getsockname(sockfd, (struct sockaddr*) orig_dst, &addrlen)
< 0){
perror("getsockname: ");
return -1;
} else {
check_and_return:
#define GET_FIELD(type, ptr, field) ((type)(ptr))->field
if(orig_dst->ss_family == AF_INET){
inet_ntop(AF_INET,
&(((struct sockaddr_in*) orig_dst)->sin_addr),
orig_dst_str, INET_ADDRSTRLEN);
fprintf(stderr, "Original destination %s\n", orig_dst_str);
port = GET_FIELD(struct sockaddr_in*, orig_dst, sin_port);
} else if(orig_dst->ss_family == AF_INET6){
inet_ntop(AF_INET6,
&(((struct sockaddr_in6*) orig_dst)->sin6_addr),
orig_dst_str, INET6_ADDRSTRLEN);
fprintf(stderr, "Original destination %s\n", orig_dst_str);
port = GET_FIELD(struct sockaddr_in6*, orig_dst, sin6_port);
}
if (htons(port) == atoi(TPROXY_PORT)) {
return -1;
}
return 0;
}
}
//Acquires information, initiates a connect and initialises a new connection
//object. Return NULL if anything fails, pointer to object otherwise
tproxy_conn_t* add_tcp_connection(int efd, struct tailhead *conn_list,
int local_fd){
struct sockaddr_storage orig_dst;
tproxy_conn_t *conn;
int remote_fd;
struct epoll_event ev;
if(get_org_dstaddr(local_fd, &orig_dst)){
fprintf(stderr, "Could not get local address\n");
close(local_fd);
local_fd = 0;
return NULL;
}
if((remote_fd = connect_remote(&orig_dst)) == 0){
fprintf(stderr, "Failed to connect\n");
close(remote_fd);
close(local_fd);
return NULL;
}
//Create connection object and fill in information
if((conn = (tproxy_conn_t*) malloc(sizeof(tproxy_conn_t))) == NULL){
fprintf(stderr, "Could not allocate memory for connection\n");
close(remote_fd);
close(local_fd);
return NULL;
}
memset(conn, 0, sizeof(tproxy_conn_t));
conn->state = CONN_AVAILABLE;
conn->remote.fd = remote_fd;
conn->remote.conn = conn;
conn->local.fd = local_fd;
conn->local.conn = conn;
TAILQ_INSERT_HEAD(conn_list, conn, conn_ptrs);
if(pipe(conn->splice_pipe) != 0){
fprintf(stderr, "Could not create the required pipe\n");
free_conn(conn);
return NULL;
}
//remote_fd is connecting. Non-blocking connects are signaled as done by
//socket being marked as ready for writing
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN | EPOLLOUT;
ev.data.ptr = (void*) &conn->remote;
conn->remote.flags = ev.events;
if(epoll_ctl(efd, EPOLL_CTL_ADD, remote_fd, &ev) == -1){
perror("epoll_ctl (remote_fd)");
free_conn(conn);
return NULL;
}
//Local socket can be closed while waiting for connection attempt. I need
//to detect this when waiting for connect() to complete. However, I dont
//want to get EPOLLIN-events, as I dont want to receive any data before
//remote connection is established
ev.events = EPOLLRDHUP;
ev.data.ptr = (void*) &conn->local;
conn->local.flags = ev.events;
if(epoll_ctl(efd, EPOLL_CTL_ADD, local_fd, &ev) == -1){
perror("epoll_ctl (local_fd)");
free_conn(conn);
return NULL;
} else
return conn;
}
//Free resources occupied by this connection
void free_conn(tproxy_conn_t *conn){
close(conn->remote.fd);
close(conn->local.fd);
if(conn->splice_pipe[0] != 0){
close(conn->splice_pipe[0]);
close(conn->splice_pipe[1]);
}
free(conn);
}
//Checks if a connection attempt was successful or not
//Returns 0 if successfull, -1 if not
int8_t check_connection_attempt(tproxy_conn_t *conn, int efd){
struct epoll_event ev;
int conn_success = 0;
int fd_flags = 0;
socklen_t optlen = sizeof(conn_success);
//If the connection was sucessfull or not is contained in SO_ERROR
if(getsockopt(conn->remote.fd, SOL_SOCKET, SO_ERROR, &conn_success,
&optlen) == -1){
perror("getsockopt (SO_ERROR)");
return -1;
}
if(conn_success == 0){
fprintf(stderr, "Socket %d connected\n", conn->remote.fd);
//Set socket as blocking now, for ease of processing
//TODO: Non-blocking
if((fd_flags = fcntl(conn->remote.fd, F_GETFL)) == -1){
perror("fcntl (F_GETFL)");
return -1;
}
if(fcntl(conn->remote.fd, F_SETFL, fd_flags & ~O_NONBLOCK) == -1){
perror("fcntl (F_SETFL)");
return -1;
}
//Update both file descriptors. I am interested in EPOLLIN (if there is
//any data) and EPOLLRDHUP (remote peer closed socket). As this is just
//an example, EPOLLOUT is ignored and it is OK for send() to block
memset(&ev, 0, sizeof(ev));
ev.data.ptr = (void*) &conn->remote;
ev.events = EPOLLIN | EPOLLRDHUP;
conn->remote.flags = ev.events;
if (epoll_ctl(efd, EPOLL_CTL_MOD, conn->remote.fd, &ev) == -1) {
perror("epoll_ctl (check_connection_attempt)");
return -1;
}
ev.data.ptr = (void*) &conn->local;
ev.events = EPOLLIN | EPOLLRDHUP;
conn->local.flags = ev.events;
if (epoll_ctl(efd, EPOLL_CTL_MOD, conn->local.fd, &ev) == -1){
perror("epoll_ctl (check_connection_attempt)");
return -1;
}
conn->state = CONN_PIPLING;
return 0;
}
return -1;
}