| 1 |
/* |
| 2 |
device-darwin.C -- device driver for mac os x |
| 3 |
Copyright (C) 2003-2008 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 it |
| 8 |
under the terms of the GNU General Public License as published by the |
| 9 |
Free Software Foundation; either version 3 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, but |
| 13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 15 |
Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with this program; if not, see <http://www.gnu.org/licenses/>. |
| 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. |
| 30 |
*/ |
| 31 |
|
| 32 |
// uses the kernel driver at: |
| 33 |
// http://www-user.rhrk.uni-kl.de/~nissler/tuntap/ |
| 34 |
|
| 35 |
#include <cstdio> |
| 36 |
#include <cstring> |
| 37 |
#include <cerrno> |
| 38 |
|
| 39 |
#include <sys/types.h> |
| 40 |
#include <sys/stat.h> |
| 41 |
#include <sys/ioctl.h> |
| 42 |
#include <unistd.h> |
| 43 |
#include <syslog.h> |
| 44 |
#include <fcntl.h> |
| 45 |
|
| 46 |
#include "conf.h" |
| 47 |
|
| 48 |
// following headers used by cygwin (maybe others) |
| 49 |
#include "netcompat.h" |
| 50 |
#include <signal.h> |
| 51 |
|
| 52 |
#define DEFAULT_DEVICE "/dev/tap0" |
| 53 |
|
| 54 |
const char * |
| 55 |
tap_device::info () |
| 56 |
{ |
| 57 |
return _("darwin tap driver"); |
| 58 |
} |
| 59 |
|
| 60 |
const char * |
| 61 |
tap_device::if_up () |
| 62 |
{ |
| 63 |
return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; |
| 64 |
} |
| 65 |
|
| 66 |
tap_device::tap_device () |
| 67 |
{ |
| 68 |
const char *device = conf.ifname ? conf.ifname : DEFAULT_DEVICE; |
| 69 |
|
| 70 |
if ((fd = open (device, O_RDWR | O_NONBLOCK)) < 0) |
| 71 |
{ |
| 72 |
slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
| 73 |
exit (EXIT_FAILURE); |
| 74 |
} |
| 75 |
|
| 76 |
slog (L_DEBUG, _("interface %s on %s initialized"), info (), device); |
| 77 |
|
| 78 |
strcpy (ifrname, rindex(device, '/') ? rindex(device, '/') + 1 : device); |
| 79 |
} |
| 80 |
|
| 81 |
tap_device::~tap_device () |
| 82 |
{ |
| 83 |
close (fd); |
| 84 |
} |
| 85 |
|
| 86 |
tap_packet * |
| 87 |
tap_device::recv () |
| 88 |
{ |
| 89 |
tap_packet *pkt = new tap_packet; |
| 90 |
|
| 91 |
pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
| 92 |
|
| 93 |
if (pkt->len <= 0) |
| 94 |
{ |
| 95 |
delete pkt; |
| 96 |
slog (L_ERR, _("error while reading from %s %s: %s"), |
| 97 |
info (), DEFAULT_DEVICE, strerror (errno)); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
return pkt; |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
tap_device::send (tap_packet *pkt) |
| 106 |
{ |
| 107 |
if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
| 108 |
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
| 109 |
strerror (errno)); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|