forked from getlantern/tunio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tun2io.h
146 lines (128 loc) · 5.13 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
/*
* 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"
#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/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>
#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;
} 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 void signal_handler (void *unused);
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 int setup_listener(options_t);
static int configure(char *tundev, char *ipaddr, char *netmask);
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);
static char *dump_dest_addr(struct tcp_client *client);
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);
#endif