-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwg_tun.c
More file actions
54 lines (49 loc) · 1.04 KB
/
Copy pathwg_tun.c
File metadata and controls
54 lines (49 loc) · 1.04 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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include "wg.h"
int
wg_tun_open(const char *dev_name)
{
struct ifreq ifr;
int fd;
fd = open("/dev/net/tun", O_RDWR);
if (fd < 0) {
perror("open /dev/net/tun");
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if (dev_name && *dev_name) {
strncpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1);
}
if (ioctl(fd, TUNSETIFF, &ifr) < 0) {
perror("ioctl TUNSETIFF");
close(fd);
return -1;
}
fprintf(stderr, "TUN device created: %s\n", ifr.ifr_name);
return fd;
}
ssize_t
wg_tun_read(int fd, uint8_t *buf, size_t size)
{
ssize_t n = read(fd, buf, size);
if (n < 0) {
perror("tun read");
}
return n;
}
ssize_t
wg_tun_write(int fd, const uint8_t *buf, size_t size)
{
ssize_t n = write(fd, buf, size);
if (n < 0) {
perror("tun write");
}
return n;
}