-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathtuntap.c
More file actions
130 lines (112 loc) · 2.91 KB
/
tuntap.c
File metadata and controls
130 lines (112 loc) · 2.91 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
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2012 Tristan Le Guern <tleguern@bouledef.eu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#if defined Windows
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#endif
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "private.h"
#include "tuntap.h"
struct device *
tuntap_init(void)
{
struct device *dev = NULL;
if ((dev = (struct device *)malloc(sizeof(*dev))) == NULL) {
return NULL;
}
(void)memset(dev->if_name, '\0', sizeof dev->if_name);
(void)memset(dev->hwaddr, '\0', sizeof dev->hwaddr);
dev->tun_fd = TUNFD_INVALID_VALUE;
dev->ctrl_sock = -1;
dev->flags = 0;
tuntap_log = tuntap_log_default;
dev->sys = NULL;
return dev;
}
void
tuntap_destroy(struct device *dev)
{
tuntap_sys_destroy(dev);
tuntap_release(dev);
}
char *
tuntap_get_ifname(struct device *dev)
{
return dev->if_name;
}
int
tuntap_version(void)
{
return TUNTAP_VERSION;
}
int
tuntap_set_ip(struct device *dev, const char *addr, int netmask)
{
t_tun_in_addr baddr4;
t_tun_in6_addr baddr6;
uint32_t mask;
/* Only accept started device */
if (dev->tun_fd == TUNFD_INVALID_VALUE) {
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
if (addr == NULL) {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'addr'");
return -1;
}
if (netmask < 0 || netmask > 128) {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'netmask'");
return -1;
}
/*
* Destination address parsing: we try IPv4 first and fall back to
* IPv6 if inet_pton return 0
*/
(void)memset(&baddr4, '\0', sizeof baddr4);
(void)memset(&baddr6, '\0', sizeof baddr6);
if (inet_pton(AF_INET, addr, &(baddr4)) == 1) {
/* Netmask */
if (netmask == 32) {
mask = htonl(INADDR_NONE);
} else {
mask = ~0;
mask = ~(mask >> netmask);
mask = htonl(mask);
}
return tuntap_sys_set_ipv4(dev, &baddr4, mask);
} else if (inet_pton(AF_INET6, addr, &(baddr6)) == 1) {
/* ipv6 prefix no need to convert */
return tuntap_sys_set_ipv6(dev, &baddr6, netmask);
} else {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameters");
return -1;
}
/* NOTREACHED */
return -1;
}
t_tun
tuntap_get_fd(struct device *dev)
{
return dev->tun_fd;
}