Skip to content

Commit 2965b92

Browse files
committed
VPN
C Program implementation
0 parents  commit 2965b92

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# VPN

vpnclient.c

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
#include <string.h>
5+
#include <arpa/inet.h>
6+
#include <linux/if.h>
7+
#include <linux/if_tun.h>
8+
#include <sys/ioctl.h>
9+
10+
#define BUFF_SIZE 2000
11+
#define PORT_NUMBER 55555
12+
#define SERVER_IP "10.0.2.25"
13+
struct sockaddr_in peerAddr;
14+
15+
int createTunDevice() {
16+
int tunfd;
17+
struct ifreq ifr;
18+
memset(&ifr, 0, sizeof(ifr));
19+
20+
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
21+
22+
tunfd = open("/dev/net/tun", O_RDWR);
23+
ioctl(tunfd, TUNSETIFF, &ifr);
24+
25+
return tunfd;
26+
}
27+
28+
/*int connectToUDPServer(){
29+
int sockfd;
30+
char *hello="Hello";
31+
32+
memset(&peerAddr, 0, sizeof(peerAddr));
33+
peerAddr.sin_family = AF_INET;
34+
peerAddr.sin_port = htons(PORT_NUMBER);
35+
peerAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
36+
37+
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
38+
39+
// Send a hello message to "connect" with the VPN server
40+
sendto(sockfd, hello, strlen(hello), 0,
41+
(struct sockaddr *) &peerAddr, sizeof(peerAddr));
42+
43+
return sockfd;
44+
}
45+
*/
46+
int connectToTCPServer(){
47+
int sockfd;
48+
char *hello="Hello";
49+
50+
memset(&peerAddr, 0, sizeof(peerAddr));
51+
peerAddr.sin_family = AF_INET;
52+
peerAddr.sin_port = htons(443);
53+
peerAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
54+
55+
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
56+
57+
// Send a hello message to "connect" with the VPN server
58+
sendto(sockfd, hello, strlen(hello), 0,
59+
(struct sockaddr *) &peerAddr, sizeof(peerAddr));
60+
61+
return sockfd;
62+
}
63+
64+
65+
void tunSelected(int tunfd, int sockfd){
66+
int len;
67+
char buff[BUFF_SIZE];
68+
69+
printf("Got a packet from TUN\n");
70+
71+
bzero(buff, BUFF_SIZE);
72+
len = read(tunfd, buff, BUFF_SIZE);
73+
sendto(sockfd, buff, len, 0, (struct sockaddr *) &peerAddr,
74+
sizeof(peerAddr));
75+
}
76+
77+
void socketSelected (int tunfd, int sockfd){
78+
int len;
79+
char buff[BUFF_SIZE];
80+
81+
printf("Got a packet from the tunnel\n");
82+
83+
bzero(buff, BUFF_SIZE);
84+
len = recvfrom(sockfd, buff, BUFF_SIZE, 0, NULL, NULL);
85+
write(tunfd, buff, len);
86+
87+
}
88+
int main (int argc, char * argv[]) {
89+
int tunfd, sockfd;
90+
91+
tunfd = createTunDevice();
92+
sockfd = connectToUDPServer();
93+
94+
// Enter the main loop
95+
while (1) {
96+
fd_set readFDSet;
97+
98+
FD_ZERO(&readFDSet);
99+
FD_SET(sockfd, &readFDSet);
100+
FD_SET(tunfd, &readFDSet);
101+
select(FD_SETSIZE, &readFDSet, NULL, NULL, NULL);
102+
103+
if (FD_ISSET(tunfd, &readFDSet)) tunSelected(tunfd, sockfd);
104+
if (FD_ISSET(sockfd, &readFDSet)) socketSelected(tunfd, sockfd);
105+
}
106+
}
107+

vpnserver.c

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
#include <string.h>
5+
#include <arpa/inet.h>
6+
#include <linux/if.h>
7+
#include <linux/if_tun.h>
8+
#include <sys/ioctl.h>
9+
10+
#define PORT_NUMBER 55555
11+
#define BUFF_SIZE 2000
12+
13+
struct sockaddr_in peerAddr;
14+
15+
int createTunDevice() {
16+
int tunfd;
17+
struct ifreq ifr;
18+
memset(&ifr, 0, sizeof(ifr));
19+
20+
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
21+
22+
tunfd = open("/dev/net/tun", O_RDWR);
23+
ioctl(tunfd, TUNSETIFF, &ifr);
24+
25+
return tunfd;
26+
}
27+
28+
int initUDPServer() {
29+
int sockfd;
30+
struct sockaddr_in server;
31+
char buff[100];
32+
33+
memset(&server, 0, sizeof(server));
34+
server.sin_family = AF_INET;
35+
server.sin_addr.s_addr = htonl(INADDR_ANY);
36+
server.sin_port = htons(PORT_NUMBER);
37+
38+
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
39+
bind(sockfd, (struct sockaddr*) &server, sizeof(server));
40+
41+
// Wait for the VPN client to "connect".
42+
bzero(buff, 100);
43+
int peerAddrLen = sizeof(struct sockaddr_in);
44+
int len = recvfrom(sockfd, buff, 100, 0,
45+
(struct sockaddr *) &peerAddr, &peerAddrLen);
46+
47+
printf("Connected with the client: %s\n", buff);
48+
return sockfd;
49+
}
50+
51+
void tunSelected(int tunfd, int sockfd){
52+
int len;
53+
char buff[BUFF_SIZE];
54+
55+
printf("Got a packet from TUN\n");
56+
57+
bzero(buff, BUFF_SIZE);
58+
len = read(tunfd, buff, BUFF_SIZE);
59+
sendto(sockfd, buff, len, 0, (struct sockaddr *) &peerAddr,
60+
sizeof(peerAddr));
61+
}
62+
63+
void socketSelected (int tunfd, int sockfd){
64+
int len;
65+
char buff[BUFF_SIZE];
66+
67+
printf("Got a packet from the tunnel\n");
68+
69+
bzero(buff, BUFF_SIZE);
70+
len = recvfrom(sockfd, buff, BUFF_SIZE, 0, NULL, NULL);
71+
write(tunfd, buff, len);
72+
73+
}
74+
int main (int argc, char * argv[]) {
75+
int tunfd, sockfd;
76+
77+
tunfd = createTunDevice();
78+
sockfd = initUDPServer();
79+
80+
// Enter the main loop
81+
while (1) {
82+
fd_set readFDSet;
83+
84+
FD_ZERO(&readFDSet);
85+
FD_SET(sockfd, &readFDSet);
86+
FD_SET(tunfd, &readFDSet);
87+
select(FD_SETSIZE, &readFDSet, NULL, NULL, NULL);
88+
89+
if (FD_ISSET(tunfd, &readFDSet)) tunSelected(tunfd, sockfd);
90+
if (FD_ISSET(sockfd, &readFDSet)) socketSelected(tunfd, sockfd);
91+
}
92+
}
93+

0 commit comments

Comments
 (0)