-
Notifications
You must be signed in to change notification settings - Fork 8
/
tun2io.h
197 lines (164 loc) · 6.94 KB
/
tun2io.h
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
/*
* Copyright (C) Ambroz Bizjak <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TUN2IO_H
#define _TUN2IO_H
// name of the program
#define PROGRAM_NAME "tun2io"
// maximum number of udpgw connections
#define DEFAULT_UDPGW_MAX_CONNECTIONS 256
// udpgw per-connection send buffer size, in number of packets
#define DEFAULT_UDPGW_CONNECTION_BUFFER_SIZE 8
// udpgw reconnect time after connection fails
#define UDPGW_RECONNECT_TIME 5000
// udpgw keepalive sending interval
#define UDPGW_KEEPALIVE_TIME 10000
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <limits.h>
#include <misc/version.h>
#include <misc/loglevel.h>
//#include <misc/dead.h>
#include <misc/ipv4_proto.h>
#include <misc/ipv6_proto.h>
#include <misc/udp_proto.h>
#include <misc/open_standard_streams.h>
#include <misc/ipaddr6.h>
#include <system/BReactor.h>
#include <system/BSignal.h>
#include <system/BAddr.h>
#include <system/BNetwork.h>
#include <flow/SinglePacketBuffer.h>
#include <tuntap/BTap.h>
#include <lwip/init.h>
#include <lwip/tcp_impl.h>
#include <lwip/netif.h>
#include <lwip/tcp.h>
#include <protocol/udpgw_proto.h>
#include <misc/packed.h>
#include <flow/PacketProtoDecoder.h>
#ifndef BADVPN_USE_WINAPI
#include <base/BLog_syslog.h>
#endif
#define LOGGER_STDOUT 1
#define LOGGER_SYSLOG 2
#define SYNC_DECL \
BPending sync_mark; \
#define SYNC_FROMHERE \
BPending_Init(&sync_mark, BReactor_PendingGroup(&ss), NULL, NULL); \
BPending_Set(&sync_mark);
#define SYNC_BREAK \
BPending_Free(&sync_mark);
#define SYNC_COMMIT \
BReactor_Synchronize(&ss, &sync_mark.base); \
BPending_Free(&sync_mark);
// command-line options
typedef struct {
int logger;
#ifndef BADVPN_USE_WINAPI
char *logger_syslog_facility;
char *logger_syslog_ident;
#endif
int loglevel;
char *tundev;
char *netif_ipaddr;
char *netif_netmask;
char *netif_ip6addr;
char *udpgw_remote_server_addr;
int udpgw_max_connections;
int udpgw_connection_buffer_size;
int udpgw_transparent_dns;
int tun_fd;
int tun_mtu;
int set_signal;
} options_t;
options_t options;
// TCP client
struct tcp_client {
//dead_t dead;
//dead_t dead_client;
BAddr local_addr;
BAddr remote_addr;
struct tcp_pcb *pcb;
int client_closed;
uint8_t buf[TCP_WND];
int buf_used;
uint32_t tunnel_id;
};
static void terminate (void);
static BAddr baddr_from_lwip (int is_ipv6, const ipX_addr_t *ipx_addr, uint16_t port_hostorder);
static void lwip_init_job_hadler (void *unused);
static void tcp_timer_handler (void *unused);
static void device_error_handler (void *unused);
static void device_read_handler_send (void *unused, uint8_t *data, int data_len);
static err_t netif_init_func (struct netif *netif);
static err_t netif_output_func (struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr);
static err_t netif_output_ip6_func (struct netif *netif, struct pbuf *p, ip6_addr_t *ipaddr);
static err_t common_netif_output (struct netif *netif, struct pbuf *p);
static err_t netif_input_func (struct pbuf *p, struct netif *inp);
static void client_logfunc (struct tcp_client *client);
static void client_log (struct tcp_client *client, int level, const char *fmt, ...);
static err_t listener_accept_func (void *arg, struct tcp_pcb *newpcb, err_t err);
static void client_close (struct tcp_client *client);
static void client_free_client (struct tcp_client *client);
static void client_handle_freed_client(struct tcp_client *client);
static void client_err_func (void *arg, err_t err);
static void client_abort_client (struct tcp_client *client);
static void client_dealloc (struct tcp_client *client);
static err_t client_recv_func (void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
static err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len);
static void udpgw_client_handler_received (void *unused, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
static int setup_listener(options_t);
static int configure_defaults();
static int configure_commit();
static int configure_tun(char *tundev, char *ipaddr, char *netmask, char *udpgw_addr);
static int configure_fd(int tun_fd, int tun_mtu, char *ipaddr, char *netmask, char *udpgw_addr);
static char *baddr_to_str(BAddr *baddr);
uint32_t goNewTunnel(struct tcp_client *client);
int goTunnelWrite(uint32_t tunno, char *data, size_t size);
int goTunnelDestroy(uint32_t tunno);
int goTunnelSentACK(uint32_t tunno, u16_t len);
int goInitTunnel(uint32_t tunno);
void goLog(struct tcp_client *client, char *data);
uint16_t goUdpGwClient_FindConnectionIdByAddr(BAddr localAddr, BAddr remoteAddr);
uint16_t goUdpGwClient_NewConnection(BAddr localAddr, BAddr remoteAddr);
void goUdpGwClient_UnshiftConn(uint16_t connId);
int goUdpGwClient_Send(uint16_t connId, uint8_t *data, int data_len);
static void udpGWClient_ReceiveFromServer(char *data, int data_len);
BAddr goUdpGwClient_GetLocalAddrByConnId(uint16_t cConnID);
BAddr goUdpGwClient_GetRemoteAddrByConnId(uint16_t cConnID);
int goUdpGwClient_ConnIdExists(uint16_t cConnID);
int goUdpGwClient_Configure(int mtu, int maxConnections, int bufferSize, int keepAliveTime);
static void UdpGwClient_GotPacket(BAddr local_addr, BAddr remote_addr, int is_dns, const uint8_t *data, int data_len);
static char *dump_dest_addr(struct tcp_client *client);
static uint8_t dataAt(uint8_t *in, int i);
static char charAt(char *in, int i);
static unsigned int tcp_client_sndbuf(struct tcp_client *client);
static int tcp_client_outbuf(struct tcp_client *client);
static int process_device_udp_packet (uint8_t *data, int data_len);
static void signal_handler (void *unused);
#endif