Improved tun error reporting

This commit is contained in:
Friedrich Schöller 2013-05-13 13:58:05 +02:00
parent 3459887cf7
commit b58ad1bee8
10 changed files with 62 additions and 25 deletions

View File

@ -30,6 +30,11 @@ Exception::Exception(const char *msg)
this->msg = msg; this->msg = msg;
} }
Exception::Exception(const string msg)
{
this->msg = msg;
}
Exception::Exception(const char *msg, bool appendSystemError) Exception::Exception(const char *msg, bool appendSystemError)
{ {
if (appendSystemError) if (appendSystemError)

View File

@ -23,8 +23,9 @@ class Exception
{ {
public: public:
Exception(const char *msg); Exception(const char *msg);
Exception(const std::string msg);
Exception(const char *msg, bool appendSystemError); Exception(const char *msg, bool appendSystemError);
const char *errorMessage() const { return msg.c_str(); } const char *errorMessage() const { return msg.c_str(); }
protected: protected:
std::string msg; std::string msg;

View File

@ -28,7 +28,6 @@
#include <syslog.h> #include <syslog.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
typedef ip IpHeader; typedef ip IpHeader;
@ -49,7 +48,7 @@ Tun::Tun(const char *device, int mtu)
fd = tun_open(this->device); fd = tun_open(this->device);
if (fd == -1) if (fd == -1)
throw Exception("could not create tunnel device"); throw Exception(string("could not create tunnel device: ") + tun_last_error());
char cmdline[512]; char cmdline[512];
snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s mtu %u", this->device, mtu); 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) void Tun::write(const char *buffer, int length)
{ {
if (tun_write(fd, (char *)buffer, length) == -1) 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 Tun::read(char *buffer)
{ {
int length = tun_read(fd, buffer, mtu); int length = tun_read(fd, buffer, mtu);
if (length == -1) 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; return length;
} }

View File

@ -24,4 +24,5 @@ extern "C"
int tun_close(int fd, char *dev); int tun_close(int fd, char *dev);
int tun_write(int fd, char *buf, int len); int tun_write(int fd, char *buf, int len);
int tun_read(int fd, char *buf, int len); int tun_read(int fd, char *buf, int len);
const char *tun_last_error();
} }

View File

@ -1,19 +1,20 @@
/* /*
VTun - Virtual Tunnel over TCP/IP network. * Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
Copyright (C) 1998-2000 Maxim Krasnyansky <max_mk@yahoo.com> *
* This program is free software: you can redistribute it and/or modify
VTun has been derived from VPPP package by Maxim Krasnyansky. * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
This program is free software; you can redistribute it and/or modify * (at your option) any later version.
it under the terms of the GNU General Public License as published by *
the Free Software Foundation; either version 2 of the License, or * This program is distributed in the hope that it will be useful,
(at your option) any later version. * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
This program is distributed in the hope that it will be useful, * GNU General Public License for more details.
but WITHOUT ANY WARRANTY; without even the implied warranty of *
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * You should have received a copy of the GNU General Public License
GNU General Public License for more details. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/ */
#include "tunemu.h" #include "tunemu.h"
@ -22,10 +23,7 @@
int tun_open(char *dev) int tun_open(char *dev)
{ {
int fd = tunemu_open(dev); return tunemu_open(dev);
if (fd < 0)
syslog(LOG_ERR, tunemu_error);
return fd;
} }
int tun_close(int fd, char *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); return tunemu_read(fd, buf, len);
} }
const char *tun_last_error()
{
return tunemu_error;
}

View File

@ -28,6 +28,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <errno.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if_tun.h> #include <net/if_tun.h>
@ -81,3 +82,8 @@ int tun_read(int fd, char *buf, int len)
{ {
return read(fd, buf, len); return read(fd, buf, len);
} }
const char *tun_last_error()
{
return strerror(errno);
}

View File

@ -28,6 +28,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <errno.h>
/* #include "vtun.h" /* #include "vtun.h"
#include "lib.h" */ #include "lib.h" */
@ -72,3 +73,8 @@ int tun_read(int fd, char *buf, int len)
{ {
return read(fd, buf, len); return read(fd, buf, len);
} }
const char *tun_last_error()
{
return strerror(errno);
}

View File

@ -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 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); } int tap_read(int fd, char *buf, int len) { return read(fd, buf, len); }
const char *tun_last_error()
{
return strerror(errno);
}

View File

@ -28,6 +28,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/uio.h> #include <sys/uio.h>
@ -98,3 +99,8 @@ int tun_read(int fd, char *buf, int len)
else else
return rlen; return rlen;
} }
const char *tun_last_error()
{
return strerror(errno);
}

View File

@ -175,3 +175,8 @@ int tun_read(int fd, char *buf, int len)
sbuf.buf = buf; sbuf.buf = buf;
return getmsg(fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; return getmsg(fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
} }
const char *tun_last_error()
{
return strerror(errno);
}