1 | /* |
1 | /* |
2 | device.h -- generic header for device.c |
2 | device.h -- generic header for device.c |
|
|
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
4 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify it |
5 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
6 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
7 | (at your option) any later version. |
10 | option) any later version. |
8 | |
11 | |
9 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, but |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
12 | GNU General Public License for more details. |
15 | Public License for more details. |
13 | |
16 | |
14 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License along |
15 | along with this program; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
17 | */ |
30 | */ |
18 | |
31 | |
19 | #ifndef VPE_DEVICE_H__ |
32 | #ifndef GVPE_DEVICE_H__ |
20 | #define VPE_DEVICE_H__ |
33 | #define GVPE_DEVICE_H__ |
21 | |
34 | |
22 | #include <net/if.h> |
35 | #include "config.h" |
23 | |
36 | |
24 | #include "gettext.h" |
37 | #define IFNAMESIZE 256 // be conservative |
25 | |
38 | |
26 | #include "global.h" |
39 | #include "global.h" |
|
|
40 | #include "util.h" |
27 | |
41 | |
28 | struct net_packet { |
42 | struct net_packet |
|
|
43 | { |
29 | u32 len; // actually u16, but padding... |
44 | u32 len; // actually u16, but padding... |
|
|
45 | // upto 4 bytes at the end might get overwritten when decrypting, |
|
|
46 | // which means len, above, is regularly overwritten with part |
|
|
47 | // of the random prefix when RAND_BYTES=12. |
30 | |
48 | |
31 | u8 &operator[] (u16 offset) const; |
49 | u8 &operator[] (u16 offset) const; |
|
|
50 | u8 *at (u16 offset) const; |
32 | |
51 | |
33 | void unshift_hdr (u16 hdrsize) |
52 | void unshift_hdr (u16 hdrsize) |
34 | { |
53 | { |
35 | memmove ((void *)&(*this)[hdrsize], (void *)&(*this)[0], len); |
54 | memmove ((void *)&(*this)[hdrsize], (void *)&(*this)[0], len); |
36 | len += hdrsize; |
55 | len += hdrsize; |
… | |
… | |
40 | { |
59 | { |
41 | len -= hdrsize; |
60 | len -= hdrsize; |
42 | memmove ((void *)&(*this)[0], (void *)&(*this)[hdrsize], len); |
61 | memmove ((void *)&(*this)[0], (void *)&(*this)[hdrsize], len); |
43 | } |
62 | } |
44 | |
63 | |
|
|
64 | u16 ipv4_hdr_len () const |
|
|
65 | { |
|
|
66 | return ((*this)[0] & 15) << 2; |
|
|
67 | } |
|
|
68 | |
45 | void set (const net_packet &pkt) |
69 | void set (const net_packet &pkt) |
46 | { |
70 | { |
47 | len = pkt.len; |
71 | len = pkt.len; |
48 | memcpy (&((*this)[0]), &(pkt[0]), len); |
72 | memcpy (&((*this)[0]), &(pkt[0]), len); |
49 | } |
73 | } |
50 | |
74 | |
|
|
75 | bool is_ipv4 () const |
|
|
76 | { |
|
|
77 | return (*this)[12] == 0x08 && (*this)[13] == 0x00 // IP |
|
|
78 | && ((*this)[14] & 0xf0) == 0x40; // IPv4 |
|
|
79 | } |
|
|
80 | |
|
|
81 | u32 &ipv4_src () const |
|
|
82 | { |
|
|
83 | return *(u32 *)&(*this)[26]; |
|
|
84 | } |
|
|
85 | |
|
|
86 | u32 &ipv4_dst () const |
|
|
87 | { |
|
|
88 | return *(u32 *)&(*this)[30]; |
|
|
89 | } |
|
|
90 | |
51 | bool is_arp () |
91 | bool is_arp () const |
52 | { |
92 | { |
53 | return (*this)[12] == 0x08 && (*this)[13] == 0x06 // 0806 protocol |
93 | return (*this)[12] == 0x08 && (*this)[13] == 0x06 // 0806 protocol |
54 | && (*this)[14] == 0x00 && (*this)[15] == 0x01 // 0001 hw_format |
94 | && (*this)[14] == 0x00 && (*this)[15] == 0x01 // 0001 hw_format |
55 | && (*this)[16] == 0x08 && (*this)[17] == 0x00 // 0800 prot_format |
95 | && (*this)[16] == 0x08 && (*this)[17] == 0x00 // 0800 prot_format |
56 | && (*this)[18] == 0x06 && (*this)[19] == 0x04; // 06 hw_len 04 prot_len |
96 | && (*this)[18] == 0x06 && (*this)[19] == 0x04; // 06 hw_len 04 prot_len |
… | |
… | |
58 | |
98 | |
59 | void *operator new (size_t s); |
99 | void *operator new (size_t s); |
60 | void operator delete (void *p); |
100 | void operator delete (void *p); |
61 | }; |
101 | }; |
62 | |
102 | |
63 | struct data_packet : net_packet { |
103 | struct data_packet : net_packet |
|
|
104 | { |
64 | u8 data_[MAXSIZE]; |
105 | u8 data_[MAXSIZE]; |
65 | }; |
106 | }; |
66 | |
107 | |
67 | inline |
108 | inline |
68 | u8 &net_packet::operator[] (u16 offset) const |
109 | u8 &net_packet::operator[] (u16 offset) const |
69 | { |
110 | { |
70 | return ((data_packet *)this)->data_[offset]; |
111 | return ((data_packet *)this)->data_[offset]; |
71 | } |
112 | } |
72 | |
113 | |
73 | typedef u8 mac[6]; |
114 | inline |
|
|
115 | u8 *net_packet::at (u16 offset) const |
|
|
116 | { |
|
|
117 | return &((*this)[offset]); |
|
|
118 | } |
74 | |
119 | |
75 | struct tap_packet : net_packet { |
120 | struct tap_packet : net_packet |
|
|
121 | { |
76 | mac dst; |
122 | mac dst; |
77 | mac src; |
123 | mac src; |
78 | u8 data[MAXSIZE - 12]; |
124 | u8 data[MAXSIZE - 12]; |
79 | }; |
125 | }; |
80 | |
126 | |
81 | struct tap_device { |
127 | struct tap_device |
|
|
128 | { |
82 | int fd; |
129 | int fd; |
83 | |
130 | |
84 | // linux tuntap |
131 | // network interface name or identifier |
85 | char ifrname[IFNAMSIZ + 1]; |
132 | char ifrname[IFNAMESIZE + 1]; |
86 | |
133 | |
87 | char *device; |
134 | char *device; |
88 | |
135 | |
89 | tap_device (); |
136 | tap_device (); |
90 | ~tap_device (); |
137 | ~tap_device (); |
91 | |
138 | |
|
|
139 | //bool open (); |
|
|
140 | //void close (); |
|
|
141 | |
92 | const char *interface () { return ifrname; } |
142 | const char *interface () { return ifrname; } |
93 | const char *info () { return _("Linux tun/tap device"); } |
143 | const char *info (); |
|
|
144 | const char *if_up (); |
94 | |
145 | |
95 | tap_packet *recv (); |
146 | tap_packet *recv (); |
96 | void send (tap_packet *pkt); |
147 | void send (tap_packet *pkt); |
97 | }; |
148 | }; |
98 | |
149 | |
|
|
150 | //extern tap_device *tap_device (); |
|
|
151 | |
99 | #endif |
152 | #endif |
100 | |
153 | |