-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
xkcp_client.c
179 lines (148 loc) · 5.31 KB
/
xkcp_client.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
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA [email protected] *
* *
\********************************************************************/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/bufferevent_ssl.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <syslog.h>
#include "ikcp.h"
#include "xkcp_util.h"
#include "tcp_proxy.h"
#include "xkcp_config.h"
#include "commandline.h"
#include "xkcp_client.h"
#include "xkcp_mon.h"
#include "debug.h"
IQUEUE_HEAD(xkcp_task_list);
static short mport = 9086;
void
timer_event_cb(evutil_socket_t fd, short event, void *arg)
{
xkcp_timer_event_cb(arg, &xkcp_task_list);
}
void
xkcp_rcv_cb(const int sock, short int which, void *arg)
{
struct xkcp_proxy_param *ptr = arg;
char buf[XKCP_RECV_BUF_LEN] = {0};
int nrecv = 0;
int index = 0;
if ((nrecv = recvfrom(sock, buf, sizeof(buf)-1, 0, (struct sockaddr *) &ptr->sockaddr, (socklen_t*)&ptr->addr_len)) > 0) {
int conv = ikcp_getconv(buf);
ikcpcb *kcp = get_kcp_from_conv(conv, &xkcp_task_list);
debug(LOG_DEBUG, "[%d] xkcp_rcv_cb [%d] len [%d] conv [%d] kcp is [%d]",
index++, sock, nrecv, conv, kcp?1:0);
if (kcp) {
int nret = ikcp_input(kcp, buf, nrecv);
if (nret < 0) {
debug(LOG_INFO, "conv [%d] ikcp_input failed [%d]", conv, nret);
}
} else {
debug(LOG_ERR, "xkcp_rcv_cb -- cant get kcp from peer data!!!!!!");
}
xkcp_forward_all_data( &xkcp_task_list);
}
}
static struct evconnlistener *set_tcp_proxy_listener(struct event_base *base, void *ptr)
{
short lport = xkcp_get_param()->local_port;
struct sockaddr_in sin;
char *addr = get_iface_ip(xkcp_get_param()->local_interface);
if (!addr) {
debug(LOG_ERR, "get_iface_ip [%s] failed", xkcp_get_param()->local_interface);
exit(0);
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(addr);
sin.sin_port = htons(lport);
struct evconnlistener * listener = evconnlistener_new_bind(base, tcp_proxy_accept_cb, ptr,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE,
-1, (struct sockaddr*)&sin, sizeof(sin));
if (!listener) {
debug(LOG_ERR, "Couldn't create listener: [%s]", strerror(errno));
exit(0);
}
return listener;
}
int client_main_loop(void)
{
struct event_base *base = NULL;
struct evconnlistener *listener = NULL, *mlistener = NULL;
int xkcp_fd = socket(AF_INET, SOCK_DGRAM, 0);
struct event timer_event, *xkcp_event;
if (xkcp_fd < 0) {
debug(LOG_ERR, "ERROR, open udp socket");
exit(0);
}
if (fcntl(xkcp_fd, F_SETFL, O_NONBLOCK) == -1) {
debug(LOG_ERR, "ERROR, fcntl error: %s", strerror(errno));
exit(0);
}
struct hostent *server = gethostbyname(xkcp_get_param()->remote_addr);
if (!server) {
debug(LOG_ERR, "ERROR, no such host as %s", xkcp_get_param()->remote_addr);
exit(0);
}
base = event_base_new();
if (!base) {
debug(LOG_ERR, "event_base_new()");
exit(0);
}
struct xkcp_proxy_param proxy_param;
memset(&proxy_param, 0, sizeof(proxy_param));
proxy_param.base = base;
proxy_param.xkcpfd = xkcp_fd;
proxy_param.sockaddr.sin_family = AF_INET;
proxy_param.sockaddr.sin_port = htons(xkcp_get_param()->remote_port);
memcpy((char *)&proxy_param.sockaddr.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
listener = set_tcp_proxy_listener(base, &proxy_param);
mlistener = set_xkcp_mon_listener(base, mport, &xkcp_task_list);
event_assign(&timer_event, base, -1, EV_PERSIST, timer_event_cb, &timer_event);
set_timer_interval(&timer_event);
xkcp_event = event_new(base, xkcp_fd, EV_READ|EV_PERSIST, xkcp_rcv_cb, &proxy_param);
event_add(xkcp_event, NULL);
event_base_dispatch(base);
evconnlistener_free(mlistener);
evconnlistener_free(listener);
close(xkcp_fd);
event_base_free(base);
return 0;
}
int main(int argc, char **argv)
{
struct xkcp_config *config = xkcp_get_config();
config->main_loop = client_main_loop;
return xkcp_main(argc, argv);
}