1 |
/* |
2 |
device.h -- generic header for device.c |
3 |
Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 |
|
5 |
This file is part of GVPE. |
6 |
|
7 |
GVPE is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with gvpe; if not, write to the Free Software |
19 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
#ifndef GVPE_DEVICE_H__ |
23 |
#define GVPE_DEVICE_H__ |
24 |
|
25 |
#include "config.h" |
26 |
|
27 |
#define IFNAMESIZE 256 // be conservative |
28 |
|
29 |
#include "global.h" |
30 |
#include "util.h" |
31 |
|
32 |
struct net_packet { |
33 |
u32 len; // actually u16, but padding... |
34 |
|
35 |
u8 &operator[] (u16 offset) const; |
36 |
u8 *at (u16 offset) const; |
37 |
|
38 |
void unshift_hdr (u16 hdrsize) |
39 |
{ |
40 |
memmove ((void *)&(*this)[hdrsize], (void *)&(*this)[0], len); |
41 |
len += hdrsize; |
42 |
} |
43 |
|
44 |
void skip_hdr (u16 hdrsize) |
45 |
{ |
46 |
len -= hdrsize; |
47 |
memmove ((void *)&(*this)[0], (void *)&(*this)[hdrsize], len); |
48 |
} |
49 |
|
50 |
void set (const net_packet &pkt) |
51 |
{ |
52 |
len = pkt.len; |
53 |
memcpy (&((*this)[0]), &(pkt[0]), len); |
54 |
} |
55 |
|
56 |
bool is_ipv4 () const |
57 |
{ |
58 |
return (*this)[12] == 0x08 && (*this)[13] == 0x00 // IP |
59 |
&& ((*this)[14] & 0xf0) == 0x40; // IPv4 |
60 |
} |
61 |
|
62 |
u32 &ipv4_src () const |
63 |
{ |
64 |
return *(u32 *)&(*this)[26]; |
65 |
} |
66 |
|
67 |
u32 &ipv4_dst () const |
68 |
{ |
69 |
return *(u32 *)&(*this)[30]; |
70 |
} |
71 |
|
72 |
bool is_arp () const |
73 |
{ |
74 |
return (*this)[12] == 0x08 && (*this)[13] == 0x06 // 0806 protocol |
75 |
&& (*this)[14] == 0x00 && (*this)[15] == 0x01 // 0001 hw_format |
76 |
&& (*this)[16] == 0x08 && (*this)[17] == 0x00 // 0800 prot_format |
77 |
&& (*this)[18] == 0x06 && (*this)[19] == 0x04; // 06 hw_len 04 prot_len |
78 |
} |
79 |
|
80 |
void *operator new (size_t s); |
81 |
void operator delete (void *p); |
82 |
}; |
83 |
|
84 |
struct data_packet : net_packet { |
85 |
u8 data_[MAXSIZE]; |
86 |
}; |
87 |
|
88 |
inline |
89 |
u8 &net_packet::operator[] (u16 offset) const |
90 |
{ |
91 |
return ((data_packet *)this)->data_[offset]; |
92 |
} |
93 |
|
94 |
inline |
95 |
u8 *net_packet::at (u16 offset) const |
96 |
{ |
97 |
return &((*this)[offset]); |
98 |
} |
99 |
|
100 |
struct tap_packet : net_packet { |
101 |
mac dst; |
102 |
mac src; |
103 |
u8 data[MAXSIZE - 12]; |
104 |
}; |
105 |
|
106 |
struct tap_device { |
107 |
int fd; |
108 |
|
109 |
// network interface name or identifier |
110 |
char ifrname[IFNAMESIZE + 1]; |
111 |
|
112 |
char *device; |
113 |
|
114 |
tap_device (); |
115 |
~tap_device (); |
116 |
|
117 |
//bool open (); |
118 |
//void close (); |
119 |
|
120 |
const char *interface () { return ifrname; } |
121 |
const char *info (); |
122 |
|
123 |
tap_packet *recv (); |
124 |
void send (tap_packet *pkt); |
125 |
}; |
126 |
|
127 |
//extern tap_device *tap_device (); |
128 |
|
129 |
#endif |
130 |
|