-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvpnclient.c
99 lines (74 loc) · 2.12 KB
/
vpnclient.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
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#define BUFF_SIZE 2000
#define PORT_NUMBER 4433
#define SERVER_IP "127.0.0.1"
struct sockaddr_in peerAddr;
int createTunDevice() {
int tunfd;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
tunfd = open("/dev/net/tun", O_RDWR);
ioctl(tunfd, TUNSETIFF, &ifr);
return tunfd;
}
int connectToTCPServer(){
int sockfd;
char *hello="Hello";
memset(&peerAddr, 0, sizeof(peerAddr));
peerAddr.sin_family = AF_INET;
peerAddr.sin_port = htons(PORT_NUMBER);
peerAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
// connect() is used in TCP to attempt to make a connection to socket
if (connect(sockfd, (struct sockaddr *)&peerAddr, sizeof(peerAddr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
// send() is used instead of sendto() for tcp
send(sockfd, hello, strlen(hello), 0);
return sockfd;
}
void tunSelected(int tunfd, int sockfd){
int len;
char buff[BUFF_SIZE];
printf("Got a packet from TUN\n");
bzero(buff, BUFF_SIZE);
len = read(tunfd, buff, BUFF_SIZE);
send(sockfd, buff, len, 0);
}
void socketSelected (int tunfd, int sockfd){
int len;
char buff[BUFF_SIZE];
printf("Got a packet from the tunnel\n");
bzero(buff, BUFF_SIZE);
len = read(sockfd, buff, BUFF_SIZE);
write(tunfd, buff, len);
}
int main (int argc, char * argv[]) {
int tunfd, sockfd;
tunfd = createTunDevice();
sockfd = connectToTCPServer();
// Enter the main loop
while (1) {
fd_set readFDSet;
FD_ZERO(&readFDSet);
FD_SET(sockfd, &readFDSet);
FD_SET(tunfd, &readFDSet);
select(FD_SETSIZE, &readFDSet, NULL, NULL, NULL);
if (FD_ISSET(tunfd, &readFDSet)) tunSelected(tunfd, sockfd);
if (FD_ISSET(sockfd, &readFDSet)) socketSelected(tunfd, sockfd);
}
}