1 | /* |
1 | /* |
2 | device-linux.C -- Interaction with Linux tun/tap device |
2 | device-linux.C -- Interaction with Linux tun/tap device |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
41 | |
42 | |
42 | #include "gettext.h" |
43 | #include "gettext.h" |
43 | |
44 | |
44 | #include "conf.h" |
45 | #include "conf.h" |
45 | |
46 | |
|
|
47 | #if TEST_ETHEREMU |
|
|
48 | # include "ether_emu.C" |
|
|
49 | #endif |
|
|
50 | |
46 | const char * |
51 | const char * |
47 | tap_device::info () |
52 | tap_device::info () |
48 | { |
53 | { |
49 | return _("Linux tun/tap device"); |
54 | return _("Linux tun/tap device"); |
50 | } |
55 | } |
… | |
… | |
62 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
67 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
63 | exit (1); |
68 | exit (1); |
64 | } |
69 | } |
65 | |
70 | |
66 | memset (&ifr, 0, sizeof (ifr)); |
71 | memset (&ifr, 0, sizeof (ifr)); |
|
|
72 | #if TEST_ETHEREMU |
|
|
73 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
|
|
74 | #else |
67 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
75 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
|
|
76 | #endif |
68 | |
77 | |
69 | if (conf.ifname) |
78 | if (conf.ifname) |
70 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
79 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
71 | else |
80 | else |
72 | ifr.ifr_name[0] = 0; |
81 | ifr.ifr_name[0] = 0; |
… | |
… | |
96 | tap_packet * |
105 | tap_packet * |
97 | tap_device::recv () |
106 | tap_device::recv () |
98 | { |
107 | { |
99 | tap_packet *pkt = new tap_packet; |
108 | tap_packet *pkt = new tap_packet; |
100 | |
109 | |
|
|
110 | #if TEST_ETHEREMU |
|
|
111 | pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14); |
|
|
112 | #else |
101 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
113 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
|
|
114 | #endif |
102 | |
115 | |
103 | if (pkt->len <= 0) |
116 | if (pkt->len <= 0) |
104 | { |
117 | { |
105 | delete pkt; |
118 | delete pkt; |
106 | slog (L_ERR, _("error while reading from %s %s: %s"), |
119 | slog (L_ERR, _("error while reading from %s %s: %s"), |
107 | info (), DEFAULT_DEVICE, strerror (errno)); |
120 | info (), DEFAULT_DEVICE, strerror (errno)); |
108 | return 0; |
121 | return 0; |
109 | } |
122 | } |
110 | |
123 | |
|
|
124 | #if TEST_ETHEREMU |
|
|
125 | pkt->len += 14; |
|
|
126 | |
|
|
127 | // assume ipv4 |
|
|
128 | (*pkt)[12] = 0x08; |
|
|
129 | (*pkt)[13] = 0x00; |
|
|
130 | |
|
|
131 | if (!ether_emu.tun_to_tap (pkt)) |
|
|
132 | { |
|
|
133 | delete pkt; |
|
|
134 | return 0; |
|
|
135 | } |
|
|
136 | #endif |
|
|
137 | |
111 | return pkt; |
138 | return pkt; |
112 | } |
139 | } |
113 | |
140 | |
114 | void |
141 | void |
115 | tap_device::send (tap_packet *pkt) |
142 | tap_device::send (tap_packet *pkt) |
116 | { |
143 | { |
|
|
144 | #if TEST_ETHEREMU |
|
|
145 | if (ether_emu.tap_to_tun (pkt) && |
|
|
146 | write (fd, &((*pkt)[14]), pkt->len - 14) < 0) |
|
|
147 | #else |
117 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
148 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
|
|
149 | #endif |
118 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
150 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
119 | strerror (errno)); |
151 | strerror (errno)); |
120 | } |
152 | } |
121 | |
153 | |