-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate_tun_device.h
56 lines (46 loc) · 1.57 KB
/
create_tun_device.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
#ifndef __CREATE_TUN_DEVICE_H
#define __CREATE_TUN_DEVICE_H
#include <string.h>
#include <net/if.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/if_tun.h>
/** Arguments taken by the function:
*
* @param char *dev the name of an interface (or '\0'). MUST have enough
* space to hold the interface name if '\0' is passed
* @param int flags interface flags (eg, IFF_TUN etc.)
* @return int file descriptor (if positive) or error code (negative)
*/
static int create_tun_device( char *dev, int flags ) {
struct ifreq ifr;
int fd, err;
char *clonedev = "/dev/net/tun";
/* open the clone device */
if( (fd = open(clonedev, O_RDWR)) < 0 ) {
return fd;
}
/* preparation of the struct ifr, of type "struct ifreq" */
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags; /* IFF_TUN or IFF_TAP, plus maybe IFF_NO_PI */
if( *dev ) {
/* if a device name was specified, put it in the structure; otherwise,
* the kernel will try to allocate the "next" device of the
* specified type */
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
/* try to create the device */
if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
close(fd);
return err;
}
/* if the operation was successful, write back the name of the
* interface to the variable "dev", so the caller can know
* it. Note that the caller MUST reserve space in *dev (see calling
* code below) */
strcpy(dev, ifr.ifr_name);
/* this is the special file descriptor that the caller will use to talk
* with the virtual interface */
return fd;
}
#endif