tun_device: add a getter for the underlying file descriptor
[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 * Bring the TUN device up
70 *
71 * @return TRUE if operation successful
72 */
73 bool (*up)(tun_device_t *this);
74
75 /**
76 * Set the MTU for this TUN device
77 *
78 * @param mtu new MTU
79 * @return TRUE if operation successful
80 */
81 bool (*set_mtu)(tun_device_t *this, int mtu);
82
83 /**
84 * Get the current MTU for this TUN device
85 *
86 * @return current MTU
87 */
88 int (*get_mtu)(tun_device_t *this);
89
90 /**
91 * Get the interface name of this device
92 *
93 * @return interface name
94 */
95 char *(*get_name)(tun_device_t *this);
96
97 /**
98 * Get the underlying tun file descriptor.
99 *
100 * @return file descriptor of this tun device
101 */
102 int (*get_fd)(tun_device_t *this);
103
104 /**
105 * Destroy a tun_device_t
106 */
107 void (*destroy)(tun_device_t *this);
108
109 };
110
111 /**
112 * Create a TUN device using the given name template.
113 *
114 * @param name_tmpl name template, defaults to "tun%d" if not given
115 * @return TUN device
116 */
117 tun_device_t *tun_device_create(const char *name_tmpl);
118
119 #endif /** TUN_DEVICE_H_ @}*/