-
Notifications
You must be signed in to change notification settings - Fork 80
/
tun.cpp
212 lines (149 loc) · 4.76 KB
/
tun.cpp
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
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "tun.h"
#include "utils.h"
extern "C" {
#include "base64encode.h"
}
#include <iostream>
#include <thread>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
/* network, tcp/ip */
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
typedef tcphdr TcpHeader;
typedef ip IpHeader;
using namespace std;
int Tun::allocate( char* device ) {
struct ifreq ifr;
int fd, err;
char cldev[32];
strcpy( cldev, "/dev/net/tun" );
if( ( fd = open( cldev, O_RDWR ) ) < 0 ) {
return fd;
};
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN;
if (*device) {
strncpy(ifr.ifr_name, device, IFNAMSIZ);
};
if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
close(fd);
printf("err: %d\n", err );
};
sprintf( this->device, "%s", device);
return fd;
}
void Tun::setup( char* device ) {
char cmdl[512];
snprintf(cmdl, sizeof(cmdl), "/sbin/ifconfig %s mtu %u", this->device, this->mtu);
if (system(cmdl) != 0)
syslog(LOG_ERR, "could not set tun device mtu");
};
Tun::Tun( char* device, int mtu, int mode, FacebookClient* facebook ) {
this->fd = allocate( device );
this->mtu = mtu;
this->mode = mode;
this->facebook = facebook;
setup( device );
setIp();
syslog( LOG_DEBUG, "Tun interface up!" );
};
Tun::~Tun() {
tun_close(fd, device);
}
void Tun::setIp() {
// take mode in account
char cmdline[512];
snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s 10.1.1.1 netmask 255.255.255.0", this->device);
if (system(cmdline) != 0)
syslog(LOG_ERR, "could not set tun device ip address");
system("/sbin/route add 176.9.168.100 gw 10.1.1.1");
};
void Tun::write(const char *buffer, int length) {
if (tun_write(fd, (char *)buffer, length) == -1)
syslog(LOG_ERR, "error writing %d bytes to tun: %s", length, strerror(errno));
}
int Tun::read(char *buffer) {
int length = tun_read(fd, buffer, mtu);
if (length == -1)
syslog(LOG_ERR, "error reading from tun: %s", strerror(errno));
return length;
}
int Tun::read(char *buffer, uint32_t &sourceIp, uint32_t &destIp) {
int length = read(buffer);
IpHeader *header = (IpHeader *)buffer;
sourceIp = ntohl(header->ip_src.s_addr);
destIp = ntohl(header->ip_dst.s_addr);
return length;
}
void Tun::keepWriting() {
int alive = 1; // fixme.
int length = 0;
char buf[ this->mtu ];
cout << "Ready and waiting for packets." << endl;
while( alive ) {
length = this->read( buf );
if( length > 0 ) {
char serialized_packet[ 30 + 10 + 4 + sizeof( buf ) ];
IpHeader *header = (IpHeader *)(buf + 4 );
TcpHeader *tcpheader = (TcpHeader *)( buf + 4 + sizeof(struct ip) );
string source_ip, dest_ip;
source_ip = Utils::formatIp( ntohl( header->ip_src.s_addr ) );
dest_ip = Utils::formatIp( ntohl( header->ip_dst.s_addr ) );
// timestamp
std::time_t currentTimestamp = std::time(0);
// payload -> base64
char enc[2 * 32768];
base64_encodestate S;
base64_encode_init(&S);
size_t i;
i = base64_encode_update(&S, (const uint8_t*)buf, sizeof(buf), enc);
sprintf( serialized_packet, "|%lu,%s,%d,%s,%d,%s", currentTimestamp, source_ip.c_str(), htons( tcpheader->source ), dest_ip.c_str(), htons( tcpheader->dest ), enc );
this->facebook->sendPacketTo( this->facebook->friendID, serialized_packet, sizeof( serialized_packet ) );
puts( serialized_packet ); puts( "" );
};
};
};
void Tun::keepReading() {
this->facebook->listClients();
double sampleThread = 349370745200979;
for(;;) {
this->facebook->readPacketsFrom( sampleThread );
sleep(2);
};
};
void Tun::run() {
std::thread readTh( &Tun::keepReading, this );
std::thread writeTh( &Tun::keepWriting, this );
readTh.join(); writeTh.join();
};