-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtproxy_example_conn.c
212 lines (179 loc) · 6.57 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
//Written by Kristian Evensen <[email protected]>
#include <stdio.h>
#include <stdint.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 "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){
char orig_dst_str[INET6_ADDRSTRLEN];
socklen_t addrlen = sizeof(*orig_dst);
memset(orig_dst, 0, addrlen);
//For UDP transparent proxying:
//Set IP_RECVORIGDSTADDR socket option for getting the original
//destination of a datagram
//Socket is bound to original destination
if(getsockname(sockfd, (struct sockaddr*) orig_dst, &addrlen)
< 0){
perror("getsockname: ");
return -1;
} else {
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);
} 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);
}
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->local_fd = local_fd;
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;
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;
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.events = EPOLLIN | EPOLLRDHUP;
ev.data.ptr = (void*) conn;
if(epoll_ctl(efd, EPOLL_CTL_MOD, conn->remote_fd, &ev) == -1 ||
epoll_ctl(efd, EPOLL_CTL_MOD, conn->local_fd, &ev) == -1){
perror("epoll_ctl (check_connection_attempt)");
return -1;
} else {
return 0;
}
}
return -1;
}