diff --git a/src/exception.cpp b/src/exception.cpp index 9fb6783..1c7318b 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -30,6 +30,11 @@ Exception::Exception(const char *msg) this->msg = msg; } +Exception::Exception(const string msg) +{ + this->msg = msg; +} + Exception::Exception(const char *msg, bool appendSystemError) { if (appendSystemError) diff --git a/src/exception.h b/src/exception.h index 8410c8c..d873ebd 100644 --- a/src/exception.h +++ b/src/exception.h @@ -23,8 +23,9 @@ class Exception { public: Exception(const char *msg); + Exception(const std::string msg); Exception(const char *msg, bool appendSystemError); - + const char *errorMessage() const { return msg.c_str(); } protected: std::string msg; diff --git a/src/tun.cpp b/src/tun.cpp index 4139a23..7a6937a 100644 --- a/src/tun.cpp +++ b/src/tun.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include typedef ip IpHeader; @@ -49,7 +48,7 @@ Tun::Tun(const char *device, int mtu) fd = tun_open(this->device); if (fd == -1) - throw Exception("could not create tunnel device"); + throw Exception(string("could not create tunnel device: ") + tun_last_error()); char cmdline[512]; snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s mtu %u", this->device, mtu); @@ -90,14 +89,14 @@ void Tun::setIp(uint32_t ip, uint32_t destIp, bool includeSubnet) void Tun::write(const char *buffer, int length) { if (tun_write(fd, (char *)buffer, length) == -1) - syslog(LOG_ERR, "error writing %d bytes to tun: %s", length, strerror(errno)); + syslog(LOG_ERR, "error writing %d bytes to tun: %s", length, tun_last_error()); } int Tun::read(char *buffer) { int length = tun_read(fd, buffer, mtu); if (length == -1) - syslog(LOG_ERR, "error reading from tun: %s", strerror(errno)); + syslog(LOG_ERR, "error reading from tun: %s", tun_last_error()); return length; } diff --git a/src/tun_dev.h b/src/tun_dev.h index 667750d..c5eac51 100644 --- a/src/tun_dev.h +++ b/src/tun_dev.h @@ -24,4 +24,5 @@ extern "C" int tun_close(int fd, char *dev); int tun_write(int fd, char *buf, int len); int tun_read(int fd, char *buf, int len); + const char *tun_last_error(); } diff --git a/src/tun_dev_darwin_emu.c b/src/tun_dev_darwin_emu.c index 88a2b50..0730306 100644 --- a/src/tun_dev_darwin_emu.c +++ b/src/tun_dev_darwin_emu.c @@ -1,19 +1,20 @@ -/* - VTun - Virtual Tunnel over TCP/IP network. - - Copyright (C) 1998-2000 Maxim Krasnyansky - - VTun has been derived from VPPP package by Maxim Krasnyansky. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. +/* + * Hans - IP over ICMP + * Copyright (C) 2009 Friedrich Schöller + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * */ #include "tunemu.h" @@ -22,10 +23,7 @@ int tun_open(char *dev) { - int fd = tunemu_open(dev); - if (fd < 0) - syslog(LOG_ERR, tunemu_error); - return fd; + return tunemu_open(dev); } int tun_close(int fd, char *dev) @@ -42,3 +40,8 @@ int tun_read(int fd, char *buf, int len) { return tunemu_read(fd, buf, len); } + +const char *tun_last_error() +{ + return tunemu_error; +} diff --git a/src/tun_dev_freebsd.c b/src/tun_dev_freebsd.c index b626591..0b3269b 100644 --- a/src/tun_dev_freebsd.c +++ b/src/tun_dev_freebsd.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -81,3 +82,8 @@ int tun_read(int fd, char *buf, int len) { return read(fd, buf, len); } + +const char *tun_last_error() +{ + return strerror(errno); +} diff --git a/src/tun_dev_generic.c b/src/tun_dev_generic.c index 57cbaf0..5a767c7 100644 --- a/src/tun_dev_generic.c +++ b/src/tun_dev_generic.c @@ -28,6 +28,7 @@ #include #include #include +#include /* #include "vtun.h" #include "lib.h" */ @@ -72,3 +73,8 @@ int tun_read(int fd, char *buf, int len) { return read(fd, buf, len); } + +const char *tun_last_error() +{ + return strerror(errno); +} diff --git a/src/tun_dev_linux.c b/src/tun_dev_linux.c index daaeb82..0179eac 100644 --- a/src/tun_dev_linux.c +++ b/src/tun_dev_linux.c @@ -130,3 +130,8 @@ int tap_write(int fd, char *buf, int len) { return write(fd, buf, len); } int tun_read(int fd, char *buf, int len) { return read(fd, buf, len); } int tap_read(int fd, char *buf, int len) { return read(fd, buf, len); } + +const char *tun_last_error() +{ + return strerror(errno); +} diff --git a/src/tun_dev_openbsd.c b/src/tun_dev_openbsd.c index b48216a..b20f4b7 100644 --- a/src/tun_dev_openbsd.c +++ b/src/tun_dev_openbsd.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -98,3 +99,8 @@ int tun_read(int fd, char *buf, int len) else return rlen; } + +const char *tun_last_error() +{ + return strerror(errno); +} diff --git a/src/tun_dev_svr4.c b/src/tun_dev_svr4.c index 538aed4..33ff378 100644 --- a/src/tun_dev_svr4.c +++ b/src/tun_dev_svr4.c @@ -175,3 +175,8 @@ int tun_read(int fd, char *buf, int len) sbuf.buf = buf; return getmsg(fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; } + +const char *tun_last_error() +{ + return strerror(errno); +}