Merge branch 'tun-vip'
[strongswan.git] / src / libstrongswan / networking / tun_device.h
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup tun_device tun_device
20 * @{ @ingroup networking
21 */
22
23 #ifndef TUN_DEVICE_H_
24 #define TUN_DEVICE_H_
25
26 #include <library.h>
27 #include <networking/host.h>
28
29 typedef struct tun_device_t tun_device_t;
30
31 /**
32 * Class to create TUN devices
33 *
34 * Creating such a device requires the CAP_NET_ADMIN capability.
35 *
36 * @note The implementation is currently very Linux specific
37 */
38 struct tun_device_t {
39
40 /**
41 * Read a packet from the TUN device
42 *
43 * @note This call blocks until a packet is available. It is a thread
44 * cancellation point.
45 *
46 * @param packet the packet read from the device
47 * @return TRUE if successful
48 */
49 bool (*read_packet)(tun_device_t *this, chunk_t *packet);
50
51 /**
52 * Write a packet to the TUN device
53 *
54 * @param packet the packet to write to the TUN device
55 * @return TRUE if successful
56 */
57 bool (*write_packet)(tun_device_t *this, chunk_t packet);
58
59 /**
60 * Set the IP address of the device
61 *
62 * @param addr the desired interface address
63 * @param netmask the netmask to use
64 * @return TRUE if operation successful
65 */
66 bool (*set_address)(tun_device_t *this, host_t *addr, u_int8_t netmask);
67
68 /**
69 * Get the IP address previously assigned to using set_address().
70 *
71 * @param netmask pointer receiving the configured netmask, or NULL
72 * @return address previously set, NULL if none
73 */
74 host_t* (*get_address)(tun_device_t *this, u_int8_t *netmask);
75
76 /**
77 * Bring the TUN device up
78 *
79 * @return TRUE if operation successful
80 */
81 bool (*up)(tun_device_t *this);
82
83 /**
84 * Set the MTU for this TUN device
85 *
86 * @param mtu new MTU
87 * @return TRUE if operation successful
88 */
89 bool (*set_mtu)(tun_device_t *this, int mtu);
90
91 /**
92 * Get the current MTU for this TUN device
93 *
94 * @return current MTU
95 */
96 int (*get_mtu)(tun_device_t *this);
97
98 /**
99 * Get the interface name of this device
100 *
101 * @return interface name
102 */
103 char *(*get_name)(tun_device_t *this);
104
105 /**
106 * Get the underlying tun file descriptor.
107 *
108 * @return file descriptor of this tun device
109 */
110 int (*get_fd)(tun_device_t *this);
111
112 /**
113 * Destroy a tun_device_t
114 */
115 void (*destroy)(tun_device_t *this);
116
117 };
118
119 /**
120 * Create a TUN device using the given name template.
121 *
122 * @param name_tmpl name template, defaults to "tun%d" if not given
123 * @return TUN device
124 */
125 tun_device_t *tun_device_create(const char *name_tmpl);
126
127 #endif /** TUN_DEVICE_H_ @}*/