Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tuntap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Tristan Le Guern <leguern AT medu DOT se>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <sys/types.h>
18 #if defined Windows
19 # include <In6addr.h>
20 #else /* Unix */
21 # include <sys/socket.h>
22 #endif
23 
24 #if !defined Windows /* Unix :) */
25 # if defined __linux__
26 # include <linux/if.h>
27 # else
28 # include <net/if.h>
29 # endif
30 # include <netinet/in.h>
31 # include <netinet/if_ether.h>
32 #endif
33 
34 #include <stdint.h>
35 
36 #ifndef LIBTUNTAP_H_
37 # define LIBTUNTAP_H_
38 
39 /*
40  * Uniformize macros
41  * - ETHER_ADDR_LEN: Magic number from IEEE 802.3
42  * - IF_NAMESIZE: Length of interface external name
43  * - IF_DESCRSIZE: Length of interface description
44  * - TUNSDEBUG: ioctl flag to enable the debug mode of a tun device
45  * - TUNFD_INVALID_VALUE: Invalid value for tun_fd
46  */
47 # if defined ETH_ALEN /* Linux */
48 # define ETHER_ADDR_LEN ETH_ALEN
49 # elif defined Windows
50 # define ETHER_ADDR_LEN 6
51 # endif
52 
53 # if defined IFNAMSIZ && !defined IF_NAMESIZE
54 # define IF_NAMESIZE IFNAMSIZ /* Historical BSD name */
55 # elif !defined IF_NAMESIZE
56 # define IF_NAMESIZE 16
57 # endif
58 
59 # define IF_DESCRSIZE 50 /* XXX: Tests needed on NetBSD and OpenBSD */
60 
61 # if defined TUNSETDEBUG
62 # define TUNSDEBUG TUNSETDEBUG
63 # endif
64 
65 # if defined Windows
66 # define TUNFD_INVALID_VALUE INVALID_HANDLE_VALUE
67 # else /* Unix */
68 # define TUNFD_INVALID_VALUE -1
69 # endif
70 
71 /*
72  * Uniformize types
73  * - t_tun: tun device file descriptor
74  * - t_tun_in_addr: struct in_addr/IN_ADDR
75  * - t_tun_in6_addr: struct in6_addr/IN6_ADDR
76  */
77 # if defined Windows
78 typedef HANDLE t_tun;
79 typedef IN_ADDR t_tun_in_addr;
80 typedef IN6_ADDR t_tun_in6_addr;
81 # else /* Unix */
82 typedef int t_tun;
83 typedef struct in_addr t_tun_in_addr;
84 typedef struct in6_addr t_tun_in6_addr;
85 # endif
86 
87 /*
88  * Windows helpers
89  */
90 # if defined Windows
91 # define snprintf(x, y, z, ...) _snprintf_s((x), (y), (y), (z), __VA_ARGS__);
92 # define strncat(x, y, z) strncat_s((x), _countof(x), (y), (z));
93 # define strdup(x) _strdup(x)
94 # endif
95 
96 # define TUNTAP_ID_MAX 256
97 # define TUNTAP_ID_ANY 257
98 
99 # define TUNTAP_MODE_ETHERNET 0x0001
100 # define TUNTAP_MODE_TUNNEL 0x0002
101 # define TUNTAP_MODE_PERSIST 0x0004
102 
103 # define TUNTAP_LOG_NONE 0x0000
104 # define TUNTAP_LOG_DEBUG 0x0001
105 # define TUNTAP_LOG_INFO 0x0002
106 # define TUNTAP_LOG_NOTICE 0x0004
107 # define TUNTAP_LOG_WARN 0x0008
108 # define TUNTAP_LOG_ERR 0x0016
109 
110 /* Versioning: 0xMMmm, with 'M' for major and 'm' for minor */
111 # define TUNTAP_VERSION_MAJOR 0
112 # define TUNTAP_VERSION_MINOR 3
113 # define TUNTAP_VERSION ((TUNTAP_VERSION_MAJOR<<8)|TUNTAP_VERSION_MINOR)
114 
115 # define TUNTAP_GET_FD(x) (x)->tun_fd
116 
117 /* Handle Windows symbols export */
118 # if defined Windows
119 # if defined(tuntap_EXPORTS) /* CMake generated goo */
120 # define TUNTAP_EXPORT __declspec(dllexport)
121 # else
122 # define TUNTAP_EXPORT __declspec(dllimport)
123 # endif
124 # else /* Unix */
125 # define TUNTAP_EXPORT
126 # endif
127 
128 # ifdef __cplusplus
129 extern "C" {
130 # endif
131 
132 struct device {
135  int flags; /* ifr.ifr_flags on Unix */
136  unsigned char hwaddr[ETHER_ADDR_LEN];
138 };
139 
140 /* User definable log callback */
141 typedef void (*t_tuntap_log)(int, const char *);
143 
144 /* Portable "public" functions */
145 TUNTAP_EXPORT struct device *tuntap_init(void);
146 TUNTAP_EXPORT int tuntap_version(void);
147 TUNTAP_EXPORT void tuntap_destroy(struct device *);
148 TUNTAP_EXPORT void tuntap_release(struct device *);
149 TUNTAP_EXPORT int tuntap_start(struct device *, int, int);
150 TUNTAP_EXPORT char *tuntap_get_ifname(struct device *);
151 TUNTAP_EXPORT int tuntap_set_ifname(struct device *, const char *);
152 TUNTAP_EXPORT char *tuntap_get_hwaddr(struct device *);
153 TUNTAP_EXPORT int tuntap_set_hwaddr(struct device *, const char *);
154 TUNTAP_EXPORT int tuntap_set_descr(struct device *, const char *);
155 TUNTAP_EXPORT int tuntap_up(struct device *);
156 TUNTAP_EXPORT int tuntap_down(struct device *);
157 TUNTAP_EXPORT int tuntap_get_mtu(struct device *);
158 TUNTAP_EXPORT int tuntap_set_mtu(struct device *, int);
159 TUNTAP_EXPORT int tuntap_set_ip(struct device *, const char *, int);
160 TUNTAP_EXPORT int tuntap_read(struct device *, void *, size_t);
161 TUNTAP_EXPORT int tuntap_write(struct device *, void *, size_t);
163 TUNTAP_EXPORT int tuntap_set_nonblocking(struct device *dev, int);
164 TUNTAP_EXPORT int tuntap_set_debug(struct device *dev, int);
165 
166 /* Logging functions */
168 void tuntap_log_default(int, const char *);
169 void tuntap_log_hexdump(void *, size_t);
170 void tuntap_log_chksum(void *, int);
171 
172 /* OS specific functions */
173 int tuntap_sys_start(struct device *, int, int);
174 void tuntap_sys_destroy(struct device *);
175 int tuntap_sys_set_hwaddr(struct device *, struct ether_addr *);
176 int tuntap_sys_set_ipv4(struct device *, t_tun_in_addr *, uint32_t);
177 int tuntap_sys_set_ipv6(struct device *, t_tun_in6_addr *, uint32_t);
178 int tuntap_sys_set_ifname(struct device *, const char *, size_t);
179 int tuntap_sys_set_descr(struct device *, const char *, size_t);
180 
181 # ifdef __cplusplus
182 }
183 # endif
184 
185 #endif
void tuntap_log_default(int, const char *)
Definition: tuntap_log.c:37
int t_tun
Definition: tuntap.h:82
int tuntap_sys_set_ifname(struct device *, const char *, size_t)
Definition: tuntap-unix-aix.c:75
TUNTAP_EXPORT void tuntap_log_set_cb(t_tuntap_log cb)
Copyright (c) 2012, PICHOT Fabien Paul Leonard <pichot.fabienATgmail.com> Copyright (c) 2012...
Definition: tuntap_log.c:29
TUNTAP_EXPORT int tuntap_set_debug(struct device *dev, int)
Definition: tuntap-unix.c:321
int flags
Definition: tuntap.h:135
#define TUNTAP_EXPORT
Definition: tuntap.h:125
TUNTAP_EXPORT char * tuntap_get_ifname(struct device *)
Definition: tuntap.c:59
TUNTAP_EXPORT int tuntap_read(struct device *, void *, size_t)
Definition: tuntap-unix.c:254
TUNTAP_EXPORT struct device * tuntap_init(void)
Definition: tuntap.c:36
unsigned char hwaddr[ETHER_ADDR_LEN]
Definition: tuntap.h:136
int tuntap_sys_set_hwaddr(struct device *, struct ether_addr *)
Definition: tuntap-unix-aix.c:53
TUNTAP_EXPORT char * tuntap_get_hwaddr(struct device *)
Definition: tuntap-unix.c:140
Definition: tuntap.h:132
TUNTAP_EXPORT t_tuntap_log tuntap_log
Definition: tuntap.h:142
void(* t_tuntap_log)(int, const char *)
Definition: tuntap.h:141
TUNTAP_EXPORT int tuntap_set_mtu(struct device *, int)
Definition: tuntap-unix.c:233
TUNTAP_EXPORT int tuntap_up(struct device *)
Definition: tuntap-unix.c:179
TUNTAP_EXPORT int tuntap_get_readable(struct device *)
Definition: tuntap-unix.c:290
void tuntap_sys_destroy(struct device *)
Definition: tuntap-unix-aix.c:48
TUNTAP_EXPORT void tuntap_destroy(struct device *)
Definition: tuntap.c:53
TUNTAP_EXPORT int tuntap_get_mtu(struct device *)
Definition: tuntap-unix.c:213
TUNTAP_EXPORT int tuntap_start(struct device *, int, int)
Definition: tuntap-unix.c:46
int ctrl_sock
Definition: tuntap.h:134
TUNTAP_EXPORT int tuntap_write(struct device *, void *, size_t)
Definition: tuntap-unix.c:272
struct in6_addr t_tun_in6_addr
Definition: tuntap.h:84
int tuntap_sys_set_ipv6(struct device *, t_tun_in6_addr *, uint32_t)
Definition: tuntap-unix-aix.c:66
void tuntap_log_chksum(void *, int)
Definition: tuntap_log.c:125
char if_name[IF_NAMESIZE]
Definition: tuntap.h:137
TUNTAP_EXPORT int tuntap_set_descr(struct device *, const char *)
Definition: tuntap-unix.c:95
TUNTAP_EXPORT int tuntap_set_hwaddr(struct device *, const char *)
Definition: tuntap-unix.c:148
#define IF_NAMESIZE
Definition: tuntap.h:56
struct in_addr t_tun_in_addr
Definition: tuntap.h:83
int tuntap_sys_start(struct device *, int, int)
Definition: tuntap-unix-aix.c:43
TUNTAP_EXPORT int tuntap_version(void)
Definition: tuntap.c:64
int tuntap_sys_set_ipv4(struct device *, t_tun_in_addr *, uint32_t)
Definition: tuntap-unix-aix.c:58
t_tun tun_fd
Definition: tuntap.h:133
void tuntap_log_hexdump(void *, size_t)
Definition: tuntap_log.c:69
TUNTAP_EXPORT void tuntap_release(struct device *)
Definition: tuntap-unix.c:88
TUNTAP_EXPORT int tuntap_down(struct device *)
Definition: tuntap-unix.c:196
int tuntap_sys_set_descr(struct device *, const char *, size_t)
Definition: tuntap-unix-aix.c:85
TUNTAP_EXPORT int tuntap_set_nonblocking(struct device *dev, int)
Definition: tuntap-unix.c:303
TUNTAP_EXPORT int tuntap_set_ifname(struct device *, const char *)
Definition: tuntap-unix.c:116
TUNTAP_EXPORT int tuntap_set_ip(struct device *, const char *, int)
Definition: tuntap.c:69