-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtun-client.c
More file actions
executable file
·121 lines (98 loc) · 3.21 KB
/
tun-client.c
File metadata and controls
executable file
·121 lines (98 loc) · 3.21 KB
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
#include<stdio.h> //printf
#include<string.h> //strlen
#include<sys/socket.h> //socket
#include<arpa/inet.h> //inet_addr
#include <fcntl.h> /* O_RDWR */
//#include <string.h> /* memset(), memcpy() */
//#include <stdio.h> /* perror(), printf(), fprintf() */
#include <stdlib.h> /* exit(), malloc(), free() */
#include <sys/ioctl.h> /* ioctl() */
/* includes for struct ifreq, etc */
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
int tun_open(char *devname)
{
struct ifreq ifr;
int fd, err;
if ( (fd = open("/dev/net/tun", O_RDWR)) == -1 ) {
perror("open /dev/net/tun");
exit(1);
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = (IFF_TUN | IFF_NO_PI);
strncpy(ifr.ifr_name, devname, IFNAMSIZ);
/* ioctl will use if_name as the name of TUN
* interface to open: "tun0", etc. */
if ( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) == -1 ) {
perror("ioctl TUNSETIFF");close(fd);exit(1);
}
/* After the ioctl call the fd is "connected" to tun device
* specified
* by devname */
return fd;
}
int main(int argc , char *argv[])
{
//-------------------------------------------------------------------------
// Prepare the connection to the 'asa1' TUN interface first.
int fd, nbytes;
char buf[1600];
// opening first TUN interface
fd = tun_open("asa1") ;
printf("Device asa1 opened\n");
//-------------------------------------------------------------------------
// preparing the TCP connection to the tun-router-server
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("172.16.215.130");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
//keep communicating with server
while(1)
{
// read from the TUN interface file descriptor
nbytes = read(fd, buf, sizeof(buf));
printf("Read %d bytes from asa1\n", nbytes);
// send to the remote TCP server
if( send(sock, buf, nbytes, 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the remote TCP server
nbytes = 0;
if( (nbytes = recv(sock , server_reply , 2000 , 0)) < 0)
{
puts("recv failed");
break;
}
printf("Server replied with %d bytes\n", nbytes);
//puts(server_reply);
// should write the data back to the TUN interface file descriptor
nbytes = write(fd, server_reply, nbytes);
//nbytes = write(fd, buf, nbytes);
printf("Wrote %d bytes to asa1\n", nbytes);
memset(message, 0, sizeof(message));
memset(server_reply, 0, sizeof(server_reply));
//break;
}
close(sock);
return 0;
}