1 /* vim: set expandtab ts=4 sw=4: */
2 /*
3  * You may redistribute this program and/or modify it under the terms of
4  * the GNU General Public License as published by the Free Software Foundation,
5  * either version 3 of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
14  */
15 #ifndef TUNMessageType_H
16 #define TUNMessageType_H
17 
18 #include "util/Defined.h"
19 #include "wire/Message.h"
20 
Er_DEFUN(void TUNMessageType_push (struct Message * message,uint16_t ethertype))21 static inline Er_DEFUN(void TUNMessageType_push(struct Message* message,
22                                        uint16_t ethertype))
23 {
24     Er(Message_eshift(message, 4));
25     ((uint16_t*) message->bytes)[0] = 0;
26     ((uint16_t*) message->bytes)[1] = ethertype;
27     Er_ret();
28 }
29 
Er_DEFUN(uint16_t TUNMessageType_pop (struct Message * message))30 static inline Er_DEFUN(uint16_t TUNMessageType_pop(struct Message* message))
31 {
32     Er(Message_eshift(message, -4));
33     Er_ret( ((uint16_t*) message->bytes)[-1] );
34 }
35 
36 enum TUNMessageType {
37     // Ethertype header, used by linux
38     TUNMessageType_ETHERTYPE = 0,
39 
40     // No header, used by android and sunos, ipv4 and ipv6 only
41     TUNMessageType_NONE  = 1,
42 
43     // address family header, used by BSD
44     TUNMessageType_AF  = 2,
45 };
46 
TUNMessageType_guess()47 static inline enum TUNMessageType TUNMessageType_guess()
48 {
49     if (Defined(android) || Defined(sunos)) {
50         return TUNMessageType_NONE;
51     } else if (Defined(linux) || Defined(win32)) {
52         return TUNMessageType_ETHERTYPE;
53     } else {
54         return TUNMessageType_AF;
55     }
56 }
57 
58 #endif
59