1 |
/* |
2 |
device-cygwin.C -- Stub for Cygwin environment |
3 |
Copyright (C) 2003 Marc Lehmann <ocg@goof.com> |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License |
16 |
along with this program; if not, write to the Free Software |
17 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
*/ |
19 |
|
20 |
// unfortunately, there is be no way to set MAC addresses under windows, |
21 |
// and the default cipedrvr uses a different MAC address than we do, |
22 |
// so this module tries to fix mac addresses in packets and arp packets. |
23 |
// this is probably not very fast, but neither is cygwin nor poll. |
24 |
// |
25 |
// http://cipe-win32.sourceforge.net/ |
26 |
|
27 |
#include "config.h" |
28 |
|
29 |
#include <stdio.h> |
30 |
#include <errno.h> |
31 |
#include <sys/types.h> |
32 |
#include <sys/stat.h> |
33 |
#include <fcntl.h> |
34 |
#include <unistd.h> |
35 |
#include <syslog.h> |
36 |
#include <cstring> |
37 |
|
38 |
#include "conf.h" |
39 |
#include "util.h" |
40 |
|
41 |
tap_device::tap_device () |
42 |
{ |
43 |
if ((fd = open (conf.ifname, O_RDWR)) < 0) |
44 |
{ |
45 |
slog (L_CRIT, _("could not open %s: %s"), conf.ifname, strerror (errno)); |
46 |
exit (1); |
47 |
} |
48 |
} |
49 |
|
50 |
tap_device::~tap_device () |
51 |
{ |
52 |
close (fd); |
53 |
} |
54 |
|
55 |
tap_packet * |
56 |
tap_device::recv () |
57 |
{ |
58 |
tap_packet *pkt = new tap_packet; |
59 |
|
60 |
pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
61 |
|
62 |
if (pkt->len <= 0) |
63 |
{ |
64 |
slog (L_ERR, _("error while reading from %s %s: %s"), |
65 |
info (), conf.ifname, strerror (errno)); |
66 |
free (pkt); |
67 |
return 0; |
68 |
} |
69 |
|
70 |
id2mac (THISNODE->id, &((*pkt)[6])); |
71 |
|
72 |
if (pkt->is_arp ()) |
73 |
{ |
74 |
if ((*pkt)[22] == 0x08) id2mac (THISNODE->id, &((*pkt)[22])); |
75 |
if ((*pkt)[32] == 0x08) id2mac (THISNODE->id, &((*pkt)[32])); |
76 |
} |
77 |
|
78 |
return pkt; |
79 |
} |
80 |
|
81 |
void |
82 |
tap_device::send (tap_packet *pkt) |
83 |
{ |
84 |
(*pkt)[ 6] = 0x08; (*pkt)[ 7] = 0x00; (*pkt)[ 8] = 0x58; |
85 |
(*pkt)[ 9] = 0x00; (*pkt)[10] = 0x00; (*pkt)[11] = 0x01; |
86 |
|
87 |
if (pkt->is_arp ()) |
88 |
{ |
89 |
if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) |
90 |
memcpy (&(*pkt)[22], &(*pkt)[6], sizeof (mac)); |
91 |
|
92 |
if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) |
93 |
memcpy (&(*pkt)[32], &(*pkt)[6], sizeof (mac)); |
94 |
} |
95 |
|
96 |
if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
97 |
slog (L_ERR, _("can't write to %s %s: %s"), info (), conf.ifname, |
98 |
strerror (errno)); |
99 |
} |