Compare commits

..

No commits in common. "master" and "v0.4.2" have entirely different histories.

34 changed files with 401 additions and 931 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/hans
/build

19
CHANGES
View File

@ -1,22 +1,5 @@
Release 1.0 (November 2016)
---------------------------
* Added cygwin support
* Remove supplementary groups when dropping privileges
* Fixed compile error on FreeBSD
Release 0.4.4 (February 2014)
-----------------------------
* Fixed writing beyond array bounds
* Fixed some smaller bugs
* Clean exit on signal
* Improved error reporting
Release 0.4.3 (Mai 2013)
------------------------
* Fixed tunemu compiling issue
Release 0.4.2 (Mai 2013)
------------------------
-----------------
* Fixed compilation issue on Linux
* Changed the license of tunemu from GPL to BSD
* Minor changes

View File

@ -1,13 +1,12 @@
LDFLAGS = `sh osflags ld $(MODE)`
CFLAGS = -c -g `sh osflags c $(MODE)`
CPPFLAGS = -c -g -std=c++98 -pedantic -Wall -Wextra -Wno-sign-compare -Wno-missing-field-initializers `sh osflags c $(MODE)`
TUN_DEV_FILE = `sh osflags dev $(MODE)`
GCC = gcc
GPP = g++
.PHONY: directories
all: directories hans
all: hans
directories: build_dir
@ -16,47 +15,48 @@ build_dir:
tunemu.o: directories build/tunemu.o
hans: build/tun.o build/sha1.o build/main.o build/client.o build/server.o build/auth.o build/worker.o build/time.o build/tun_dev.o build/echo.o build/exception.o build/utility.o
hans: directories build/tun.o build/sha1.o build/main.o build/client.o build/server.o build/auth.o build/worker.o build/time.o build/tun_dev.o build/echo.o build/exception.o build/utility.o
$(GPP) -o hans build/tun.o build/sha1.o build/main.o build/client.o build/server.o build/auth.o build/worker.o build/time.o build/tun_dev.o build/echo.o build/exception.o build/utility.o $(LDFLAGS)
build/utility.o: src/utility.cpp src/utility.h
$(GPP) -c src/utility.cpp -o $@ -o $@ $(CPPFLAGS)
$(GPP) -c src/utility.cpp -o $@ -o $@ $(CFLAGS)
build/exception.o: src/exception.cpp src/exception.h
$(GPP) -c src/exception.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/exception.cpp -o $@ $(CFLAGS)
build/echo.o: src/echo.cpp src/echo.h src/exception.h
$(GPP) -c src/echo.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/echo.cpp -o $@ $(CFLAGS)
build/tun.o: src/tun.cpp src/tun.h src/exception.h src/utility.h src/tun_dev.h
$(GPP) -c src/tun.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/tun.cpp -o $@ $(CFLAGS)
build/tun_dev.o:
$(GCC) -c $(TUN_DEV_FILE) -o build/tun_dev.o -o $@ $(CFLAGS)
build/sha1.o: src/sha1.cpp src/sha1.h
$(GPP) -c src/sha1.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/sha1.cpp -o $@ $(CFLAGS)
build/main.o: src/main.cpp src/client.h src/server.h src/exception.h src/worker.h src/auth.h src/time.h src/echo.h src/tun.h src/tun_dev.h
$(GPP) -c src/main.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/main.cpp -o $@ $(CFLAGS)
build/client.o: src/client.cpp src/client.h src/server.h src/exception.h src/config.h src/worker.h src/auth.h src/time.h src/echo.h src/tun.h src/tun_dev.h
$(GPP) -c src/client.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/client.cpp -o $@ $(CFLAGS)
build/server.o: src/server.cpp src/server.h src/client.h src/utility.h src/config.h src/worker.h src/auth.h src/time.h src/echo.h src/tun.h src/tun_dev.h
$(GPP) -c src/server.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/server.cpp -o $@ $(CFLAGS)
build/auth.o: src/auth.cpp src/auth.h src/sha1.h src/utility.h
$(GPP) -c src/auth.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/auth.cpp -o $@ $(CFLAGS)
build/worker.o: src/worker.cpp src/worker.h src/tun.h src/exception.h src/time.h src/echo.h src/tun_dev.h src/config.h
$(GPP) -c src/worker.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/worker.cpp -o $@ $(CFLAGS)
build/time.o: src/time.cpp src/time.h
$(GPP) -c src/time.cpp -o $@ $(CPPFLAGS)
$(GPP) -c src/time.cpp -o $@ $(CFLAGS)
clean:
rm -rf build hans
rm -f build/tun.o build/sha1.o build/main.o build/client.o build/server.o build/auth.o build/worker.o build/time.o build/tun_dev.o build/echo.o build/exception.o build/utility.o build/tunemu.o hans
rm -df build
build/tunemu.o: src/tunemu.h src/tunemu.c
$(GCC) -c src/tunemu.c -o build/tunemu.o

11
osflags
View File

@ -14,12 +14,11 @@ ld)
esac
;;
c)
FLAGS=-D$OS
case $OS in
LINUX)
echo $FLAGS -DHAVE_LINUX_IF_TUN_H -DLINUX
;;
CYGWIN*)
echo $FLAGS -DWIN32
echo $FLAGS -DHAVE_LINUX_IF_TUN_H
;;
*)
echo $FLAGS
@ -44,12 +43,10 @@ dev)
echo src/tun_dev_generic.c
fi
;;
CYGWIN*)
echo src/tun_dev_cygwin.c
;;
*)
echo src/tun_dev_generic.c
;;
esac
;;
esac

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "auth.h"
@ -23,9 +23,10 @@
#include <arpa/inet.h>
Auth::Auth(const std::string &passphrase)
: passphrase(passphrase)
{ }
Auth::Auth(const char *passphrase)
{
this->passphrase = passphrase;
}
Auth::Response Auth::getResponse(const Challenge &challenge) const
{
@ -33,11 +34,11 @@ Auth::Response Auth::getResponse(const Challenge &challenge) const
Response response;
hasher << passphrase.data();
hasher << passphrase.c_str();
hasher.Input(&challenge[0], challenge.size());
hasher.Result((unsigned int *)response.data);
for (int i = 0; i < 5; i++)
response.data[i] = htonl(response.data[i]);

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef AUTH_H
@ -36,7 +36,7 @@ public:
bool operator==(const Response &other) const { return memcmp(this, &other, sizeof(Response)) == 0; }
};
Auth(const std::string &passphrase);
Auth(const char *passphrase);
Challenge generateChallenge(int length) const;
Response getResponse(const Challenge &challenge) const;

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "client.h"
@ -28,15 +28,14 @@
#include <netinet/in.h>
#include <syslog.h>
using std::vector;
using std::string;
using namespace std;
const Worker::TunnelHeader::Magic Client::magic("hanc");
Client::Client(int tunnelMtu, const string *deviceName, uint32_t serverIp,
int maxPolls, const string &passphrase, uid_t uid, gid_t gid,
Client::Client(int tunnelMtu, const char *deviceName, uint32_t serverIp,
int maxPolls, const char *passphrase, uid_t uid, gid_t gid,
bool changeEchoId, bool changeEchoSeq, uint32_t desiredIp)
: Worker(tunnelMtu, deviceName, false, uid, gid), auth(passphrase)
: Worker(tunnelMtu, deviceName, false, uid, gid), auth(passphrase)
{
this->serverIp = serverIp;
this->clientIp = INADDR_NONE;
@ -90,7 +89,7 @@ void Client::sendChallengeResponse(int dataLength)
setTimeout(5000);
}
bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, uint16_t, uint16_t)
bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, uint16_t id, uint16_t seq)
{
if (realIp != serverIp || !reply)
return false;
@ -101,7 +100,7 @@ bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t
switch (header.type)
{
case TunnelHeader::TYPE_RESET_CONNECTION:
syslog(LOG_DEBUG, "reset received");
syslog(LOG_DEBUG, "reset reveiced");
sendConnectionRequest();
return true;
@ -114,7 +113,7 @@ bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t
case TunnelHeader::TYPE_CHALLENGE:
if (state == STATE_CONNECTION_REQUEST_SENT)
{
syslog(LOG_DEBUG, "authentication request received");
syslog(LOG_DEBUG, "challenge received");
sendChallengeResponse(dataLength);
return true;
}
@ -138,7 +137,7 @@ bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t
clientIp = ip;
desiredIp = ip;
tun.setIp(ip, (ip & 0xffffff00) + 1);
tun->setIp(ip, (ip & 0xffffff00) + 1, false);
}
state = STATE_ESTABLISHED;
@ -161,8 +160,6 @@ bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t
return true;
}
break;
default:
break;
}
syslog(LOG_DEBUG, "invalid packet type: %d, state: %d", header.type, state);
@ -170,7 +167,7 @@ bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t
return true;
}
void Client::sendEchoToServer(Worker::TunnelHeader::Type type, int dataLength)
void Client::sendEchoToServer(int type, int dataLength)
{
if (maxPolls == 0 && state == STATE_ESTABLISHED)
setTimeout(KEEP_ALIVE_INTERVAL);
@ -211,7 +208,7 @@ void Client::handleDataFromServer(int dataLength)
sendEchoToServer(TunnelHeader::TYPE_POLL, 0);
}
void Client::handleTunData(int dataLength, uint32_t, uint32_t)
void Client::handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp)
{
if (state != STATE_ESTABLISHED)
return;

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef CLIENT_H
@ -27,10 +27,9 @@
class Client : public Worker
{
public:
Client(int tunnelMtu, const std::string *deviceName, uint32_t serverIp,
int maxPolls, const std::string &passphrase, uid_t uid, gid_t gid,
Client(int tunnelMtu, const char *deviceName, uint32_t serverIp,
int maxPolls, const char *passphrase, uid_t uid, gid_t gid,
bool changeEchoId, bool changeEchoSeq, uint32_t desiredIp);
virtual ~Client();
@ -54,7 +53,7 @@ protected:
void startPolling();
void sendEchoToServer(Worker::TunnelHeader::Type type, int dataLength);
void sendEchoToServer(int type, int dataLength);
void sendChallengeResponse(int dataLength);
void sendConnectionRequest();

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#define MAX_BUFFERED_PACKETS 20
@ -24,5 +24,5 @@
#define CHALLENGE_SIZE 20
// #define DEBUG_ONLY(a) a
//#define DEBUG_ONLY(a) a
#define DEBUG_ONLY(a)

View File

@ -1,27 +1,26 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "echo.h"
#include "exception.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
@ -42,13 +41,16 @@ Echo::Echo(int maxPayloadSize)
throw Exception("creating icmp socket", true);
bufferSize = maxPayloadSize + headerSize();
sendBuffer.resize(bufferSize);
receiveBuffer.resize(bufferSize);
sendBuffer = new char[bufferSize];
receiveBuffer = new char[bufferSize];
}
Echo::~Echo()
{
close(fd);
delete[] sendBuffer;
delete[] receiveBuffer;
}
int Echo::headerSize()
@ -65,15 +67,15 @@ void Echo::send(int payloadLength, uint32_t realIp, bool reply, uint16_t id, uin
if (payloadLength + sizeof(IpHeader) + sizeof(EchoHeader) > bufferSize)
throw Exception("packet too big");
EchoHeader *header = (EchoHeader *)(sendBuffer.data() + sizeof(IpHeader));
EchoHeader *header = (EchoHeader *)(sendBuffer + sizeof(IpHeader));
header->type = reply ? 0: 8;
header->code = 0;
header->id = htons(id);
header->seq = htons(seq);
header->chksum = 0;
header->chksum = icmpChecksum(sendBuffer.data() + sizeof(IpHeader), payloadLength + sizeof(EchoHeader));
header->chksum = icmpChecksum(sendBuffer + sizeof(IpHeader), payloadLength + sizeof(EchoHeader));
int result = sendto(fd, sendBuffer.data() + sizeof(IpHeader), payloadLength + sizeof(EchoHeader), 0, (struct sockaddr *)&target, sizeof(struct sockaddr_in));
int result = sendto(fd, sendBuffer + sizeof(IpHeader), payloadLength + sizeof(EchoHeader), 0, (struct sockaddr *)&target, sizeof(struct sockaddr_in));
if (result == -1)
syslog(LOG_ERR, "error sending icmp packet: %s", strerror(errno));
}
@ -83,7 +85,7 @@ int Echo::receive(uint32_t &realIp, bool &reply, uint16_t &id, uint16_t &seq)
struct sockaddr_in source;
int source_addr_len = sizeof(struct sockaddr_in);
int dataLength = recvfrom(fd, receiveBuffer.data(), bufferSize, 0, (struct sockaddr *)&source, (socklen_t *)&source_addr_len);
int dataLength = recvfrom(fd, receiveBuffer, bufferSize, 0, (struct sockaddr *)&source, (socklen_t *)&source_addr_len);
if (dataLength == -1)
{
syslog(LOG_ERR, "error receiving icmp packet: %s", strerror(errno));
@ -93,7 +95,7 @@ int Echo::receive(uint32_t &realIp, bool &reply, uint16_t &id, uint16_t &seq)
if (dataLength < sizeof(IpHeader) + sizeof(EchoHeader))
return -1;
EchoHeader *header = (EchoHeader *)(receiveBuffer.data() + sizeof(IpHeader));
EchoHeader *header = (EchoHeader *)(receiveBuffer + sizeof(IpHeader));
if ((header->type != 0 && header->type != 8) || header->code != 0)
return -1;
@ -109,23 +111,13 @@ uint16_t Echo::icmpChecksum(const char *data, int length)
{
uint16_t *data16 = (uint16_t *)data;
uint32_t sum = 0;
for (sum = 0; length > 1; length -= 2)
sum += *data16++;
if (length == 1)
sum += *(unsigned char *)data16;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
char *Echo::sendPayloadBuffer()
{
return sendBuffer.data() + headerSize();
}
char *Echo::receivePayloadBuffer()
{
return receiveBuffer.data() + headerSize();
}

View File

@ -1,27 +1,26 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef ECHO_H
#define ECHO_H
#include <string>
#include <vector>
#include <stdint.h>
class Echo
@ -35,8 +34,8 @@ public:
void send(int payloadLength, uint32_t realIp, bool reply, uint16_t id, uint16_t seq);
int receive(uint32_t &realIp, bool &reply, uint16_t &id, uint16_t &seq);
char *sendPayloadBuffer();
char *receivePayloadBuffer();
char *sendPayloadBuffer() { return sendBuffer + headerSize(); }
char *receivePayloadBuffer() { return receiveBuffer + headerSize(); }
static int headerSize();
protected:
@ -53,8 +52,7 @@ protected:
int fd;
int bufferSize;
std::vector<char> sendBuffer;
std::vector<char> receiveBuffer;
char *sendBuffer, *receiveBuffer;
};
#endif

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "exception.h"
@ -23,17 +23,17 @@
#include <stdio.h>
#include <string.h>
using std::string;
using namespace std;
Exception::Exception(const string &msg)
Exception::Exception(const char *msg)
{
this->msg = msg;
}
Exception::Exception(const string &msg, bool appendSystemError)
Exception::Exception(const char *msg, bool appendSystemError)
{
if (appendSystemError)
this->msg = msg + ": " + strerror(errno);
this->msg = string(msg) + ": " + strerror(errno);
else
this->msg = msg;
}

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include <string>
@ -22,10 +22,10 @@
class Exception
{
public:
Exception(const std::string &msg);
Exception(const std::string &msg, bool appendSystemError);
const std::string &errorMessage() const { return msg; }
Exception(const char *msg);
Exception(const char *msg, bool appendSystemError);
const char *errorMessage() const { return msg.c_str(); }
protected:
std::string msg;
};

View File

@ -1,27 +1,27 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "client.h"
#include "server.h"
#include "exception.h"
#include <iostream>
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
@ -34,70 +34,41 @@
#include <syslog.h>
#include <unistd.h>
#include <sys/socket.h>
#include <signal.h>
#include <memory>
#ifndef AI_V4MAPPED // Not supported on OpenBSD 6.0
#define AI_V4MAPPED 0
#endif
using std::string;
static Worker *worker = NULL;
static void sig_term_handler(int)
void usage()
{
syslog(LOG_INFO, "SIGTERM received");
if (worker)
worker->stop();
}
static void sig_int_handler(int)
{
syslog(LOG_INFO, "SIGINT received");
if (worker)
worker->stop();
}
static void usage()
{
std::cerr <<
"Hans - IP over ICMP version 1.0\n\n"
printf(
"Hans - IP over ICMP version 0.4.1\n\n"
"RUN AS SERVER\n"
" hans -s network [-fvr] [-p password] [-u unprivileged_user] [-d tun_device] [-m reference_mtu] [-a ip]\n\n"
"RUN AS CLIENT\n"
" hans -c server [-fv] [-p passphrase] [-u user] [-d tun_device]\n"
" [-m reference_mtu] [-w polls]\n\n"
"RUN AS SERVER (linux only)\n"
" hans -s network [-fvr] [-p passphrase] [-u user] [-d tun_device]\n"
" [-m reference_mtu] [-a ip]\n\n"
" hans -c server [-fv] [-p password] [-u unprivileged_user] [-d tun_device] [-m reference_mtu] [-w polls]\n\n"
"ARGUMENTS\n"
" -c server Run as client. Connect to given server address.\n"
" -s network Run as server. Use given network address on virtual interfaces.\n"
" -p passphrase Set passphrase.\n"
" -u username Change user under which the program runs.\n"
" -a ip Request assignment of given tunnel ip address from the server.\n"
" -r Respond to ordinary pings in server mode.\n"
" -d device Use given tun device.\n"
" -m mtu Set maximum echo packet size. This should correspond to the MTU\n"
" of the network between client and server, which is usually 1500\n"
" over Ethernet. Has to be the same on client and server. Defaults\n"
" to 1500.\n"
" -w polls Number of echo requests the client sends in advance for the\n"
" server to reply to. 0 disables polling, which is the best choice\n"
" if the network allows unlimited echo replies. Defaults to 10.\n"
" -i Change echo id on every echo request. May help with buggy\n"
" routers. May impact performance with others.\n"
" -q Change echo sequence number on every echo request. May help with\n"
" buggy routers. May impact performance with others.\n"
" -s network Run as a server with the given network address for the virtual interface.\n"
" -c server Connect to a server.\n"
" -f Run in foreground.\n"
" -v Print debug information.\n";
" -v Print debug information.\n"
" -r Respond to ordinary pings. Only in server mode.\n"
" -p password Use a password.\n"
" -u username Set the user under which the program should run.\n"
" -d device Use the given tun device.\n"
" -m mtu Use this mtu to calculate the tunnel mtu.\n"
" The generated echo packets will not be bigger than this value.\n"
" Has to be the same on client and server. Defaults to 1500.\n"
" -w polls Number of echo requests the client sends to the server for polling.\n"
" 0 disables polling. Defaults to 10.\n"
" -i Change the echo id for every echo request.\n"
" -q Change the echo sequence number for every echo request.\n"
" -a ip Try to get assigned the given tunnel ip address.\n"
);
}
int main(int argc, char *argv[])
{
string serverName;
string userName;
string passphrase;
string device;
const char *serverName;
const char *userName = NULL;
const char *password = "";
const char *device = NULL;
bool isServer = false;
bool isClient = false;
bool foreground = false;
@ -111,7 +82,7 @@ int main(int argc, char *argv[])
bool changeEchoId = false;
bool changeEchoSeq = false;
bool verbose = false;
openlog(argv[0], LOG_PERROR, LOG_DAEMON);
int c;
@ -128,8 +99,8 @@ int main(int argc, char *argv[])
device = optarg;
break;
case 'p':
passphrase = optarg;
memset(optarg, 0, strlen(optarg));
password = strdup(optarg);
memset(optarg, 0, strlen(optarg));
break;
case 'c':
isClient = true;
@ -139,7 +110,7 @@ int main(int argc, char *argv[])
isServer = true;
network = ntohl(inet_addr(optarg));
if (network == INADDR_NONE)
std::cerr << "invalid network\n";
printf("invalid network\n");
break;
case 'm':
mtu = atoi(optarg);
@ -172,9 +143,8 @@ int main(int argc, char *argv[])
if (mtu < 68)
{
// RFC 791: Every internet module must be able to forward a datagram of
// 68 octets without further fragmentation.
std::cerr << "mtu too small\n";
// RFC 791: Every internet module must be able to forward a datagram of 68 octets without further fragmentation.
printf("mtu too small\n");
return 1;
}
@ -187,13 +157,9 @@ int main(int argc, char *argv[])
return 1;
}
if (!userName.empty())
if (userName != NULL)
{
#ifdef WIN32
syslog(LOG_ERR, "dropping privileges is not supported on Windows");
return 1;
#endif
passwd *pw = getpwnam(userName.data());
passwd *pw = getpwnam(userName);
if (pw != NULL)
{
@ -210,39 +176,30 @@ int main(int argc, char *argv[])
if (!verbose)
setlogmask(LOG_UPTO(LOG_INFO));
signal(SIGTERM, sig_term_handler);
signal(SIGINT, sig_int_handler);
try
{
Worker *worker;
if (isServer)
{
worker = new Server(mtu, device.empty() ? NULL : &device, passphrase,
network, answerPing, uid, gid, 5000);
worker = new Server(mtu, device, password, network, answerPing, uid, gid, 5000);
}
else
{
struct addrinfo hints = {0};
struct addrinfo *res = NULL;
hints.ai_family = AF_INET;
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
int err = getaddrinfo(serverName.data(), NULL, &hints, &res);
if (err)
uint32_t serverIp = inet_addr(serverName);
if (serverIp == INADDR_NONE)
{
syslog(LOG_ERR, "getaddrinfo: %s", gai_strerror(err));
return 1;
struct hostent* he = gethostbyname(serverName);
if (!he)
{
syslog(LOG_ERR, "gethostbyname: %s", hstrerror(h_errno));
return 1;
}
serverIp = *(uint32_t *)he->h_addr;
}
sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(res->ai_addr);
uint32_t serverIp = sockaddr->sin_addr.s_addr;
worker = new Client(mtu, device.empty() ? NULL : &device,
ntohl(serverIp), maxPolls, passphrase, uid, gid,
changeEchoId, changeEchoSeq, clientIp);
freeaddrinfo(res);
worker = new Client(mtu, device, ntohl(serverIp), maxPolls, password, uid, gid, changeEchoId, changeEchoSeq, clientIp);
}
if (!foreground)
@ -255,8 +212,7 @@ int main(int argc, char *argv[])
}
catch (Exception e)
{
syslog(LOG_ERR, "%s", e.errorMessage().data());
delete worker;
syslog(LOG_ERR, "%s", e.errorMessage());
return 1;
}

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "server.h"
@ -25,25 +25,21 @@
#include <string.h>
#include <arpa/inet.h>
#include <syslog.h>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
using namespace std;
#define FIRST_ASSIGNED_IP_OFFSET 100
const Worker::TunnelHeader::Magic Server::magic("hans");
Server::Server(int tunnelMtu, const string *deviceName, const string &passphrase,
uint32_t network, bool answerEcho, uid_t uid, gid_t gid, int pollTimeout)
Server::Server(int tunnelMtu, const char *deviceName, const char *passphrase, uint32_t network, bool answerEcho, uid_t uid, gid_t gid, int pollTimeout)
: Worker(tunnelMtu, deviceName, answerEcho, uid, gid), auth(passphrase)
{
this->network = network & 0xffffff00;
this->pollTimeout = pollTimeout;
this->latestAssignedIpOffset = FIRST_ASSIGNED_IP_OFFSET - 1;
tun.setIp(this->network + 1, this->network + 2);
tun->setIp(this->network + 1, this->network + 2, true);
dropPrivileges();
}
@ -63,8 +59,7 @@ void Server::handleUnknownClient(const TunnelHeader &header, int dataLength, uin
if (header.type != TunnelHeader::TYPE_CONNECTION_REQUEST || dataLength != sizeof(ClientConnectData))
{
syslog(LOG_DEBUG, "invalid request (type %d) from %s", header.type,
Utility::formatIp(realIp).c_str());
syslog(LOG_DEBUG, "invalid request %s", Utility::formatIp(realIp).c_str());
sendReset(&client);
return;
}
@ -75,9 +70,7 @@ void Server::handleUnknownClient(const TunnelHeader &header, int dataLength, uin
client.state = ClientData::STATE_NEW;
client.tunnelIp = reserveTunnelIp(connectData->desiredIp);
syslog(LOG_DEBUG, "new client %s with tunnel address %s\n",
Utility::formatIp(client.realIp).data(),
Utility::formatIp(client.tunnelIp).data());
syslog(LOG_DEBUG, "new client: %s (%s)\n", Utility::formatIp(client.realIp).c_str(), Utility::formatIp(client.tunnelIp).c_str());
if (client.tunnelIp != 0)
{
@ -85,9 +78,9 @@ void Server::handleUnknownClient(const TunnelHeader &header, int dataLength, uin
sendChallenge(&client);
// add client to list
clientList.push_front(client);
clientRealIpMap[realIp] = clientList.begin();
clientTunnelIpMap[client.tunnelIp] = clientList.begin();
clientList.push_back(client);
clientRealIpMap[realIp] = clientList.size() - 1;
clientTunnelIpMap[client.tunnelIp] = clientList.size() - 1;
}
else
{
@ -98,8 +91,7 @@ void Server::handleUnknownClient(const TunnelHeader &header, int dataLength, uin
void Server::sendChallenge(ClientData *client)
{
syslog(LOG_DEBUG, "sending authentication request to %s\n",
Utility::formatIp(client->realIp).data());
syslog(LOG_DEBUG, "sending challenge to: %s\n", Utility::formatIp(client->realIp).c_str());
memcpy(echoSendPayloadBuffer(), &client->challenge[0], client->challenge.size());
sendEchoToClient(client, TunnelHeader::TYPE_CHALLENGE, client->challenge.size());
@ -109,18 +101,16 @@ void Server::sendChallenge(ClientData *client)
void Server::removeClient(ClientData *client)
{
syslog(LOG_DEBUG, "removing client %s with tunnel ip %s\n",
Utility::formatIp(client->realIp).data(),
Utility::formatIp(client->tunnelIp).data());
syslog(LOG_DEBUG, "removing client: %s (%s)\n", Utility::formatIp(client->realIp).c_str(), Utility::formatIp(client->tunnelIp).c_str());
releaseTunnelIp(client->tunnelIp);
ClientList::iterator it = clientRealIpMap[client->realIp];
int nr = clientRealIpMap[client->realIp];
clientRealIpMap.erase(client->realIp);
clientTunnelIpMap.erase(client->tunnelIp);
clientList.erase(it);
clientList.erase(clientList.begin() + nr);
}
void Server::checkChallenge(ClientData *client, int length)
@ -129,8 +119,7 @@ void Server::checkChallenge(ClientData *client, int length)
if (length != sizeof(Auth::Response) || memcmp(&rightResponse, echoReceivePayloadBuffer(), length) != 0)
{
syslog(LOG_DEBUG, "wrong challenge response from %s\n",
Utility::formatIp(client->realIp).data());
syslog(LOG_DEBUG, "wrong challenge response\n");
sendEchoToClient(client, TunnelHeader::TYPE_CHALLENGE_ERROR, 0);
@ -145,14 +134,12 @@ void Server::checkChallenge(ClientData *client, int length)
client->state = ClientData::STATE_ESTABLISHED;
syslog(LOG_INFO, "connection established to %s",
Utility::formatIp(client->realIp).data());
syslog(LOG_INFO, "connection established: %s", Utility::formatIp(client->realIp).c_str());
}
void Server::sendReset(ClientData *client)
{
syslog(LOG_DEBUG, "sending reset to %s",
Utility::formatIp(client->realIp).data());
syslog(LOG_DEBUG, "sending reset: %s", Utility::formatIp(client->realIp).c_str());
sendEchoToClient(client, TunnelHeader::TYPE_RESET_CONNECTION, 0);
}
@ -185,7 +172,7 @@ bool Server::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t
while (client->pollIds.size() > 1)
client->pollIds.pop();
syslog(LOG_DEBUG, "reconnecting %s", Utility::formatIp(realIp).data());
syslog(LOG_DEBUG, "reconnecting %s", Utility::formatIp(realIp).c_str());
sendReset(client);
removeClient(client);
return true;
@ -211,35 +198,32 @@ bool Server::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t
break;
case TunnelHeader::TYPE_POLL:
return true;
default:
break;
}
syslog(LOG_DEBUG, "invalid packet from: %s, type: %d, state: %d",
Utility::formatIp(realIp).data(), header.type, client->state);
syslog(LOG_DEBUG, "invalid packet from: %s, type: %d, state: %d", Utility::formatIp(realIp).c_str(), header.type, client->state);
return true;
}
Server::ClientData *Server::getClientByTunnelIp(uint32_t ip)
{
ClientIpMap::iterator it = clientTunnelIpMap.find(ip);
if (it == clientTunnelIpMap.end())
ClientIpMap::iterator clientMapIterator = clientTunnelIpMap.find(ip);
if (clientMapIterator == clientTunnelIpMap.end())
return NULL;
return &*it->second;
return &clientList[clientMapIterator->second];
}
Server::ClientData *Server::getClientByRealIp(uint32_t ip)
{
ClientIpMap::iterator it = clientRealIpMap.find(ip);
if (it == clientRealIpMap.end())
ClientIpMap::iterator clientMapIterator = clientRealIpMap.find(ip);
if (clientMapIterator == clientRealIpMap.end())
return NULL;
return &*it->second;
return &clientList[clientMapIterator->second];
}
void Server::handleTunData(int dataLength, uint32_t, uint32_t destIp)
void Server::handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp)
{
if (destIp == network + 255) // ignore broadcasts
return;
@ -248,8 +232,7 @@ void Server::handleTunData(int dataLength, uint32_t, uint32_t destIp)
if (client == NULL)
{
syslog(LOG_DEBUG, "data received for unknown client %s\n",
Utility::formatIp(destIp).data());
syslog(LOG_DEBUG, "unknown client: %s\n", Utility::formatIp(destIp).c_str());
return;
}
@ -263,7 +246,7 @@ void Server::pollReceived(ClientData *client, uint16_t echoId, uint16_t echoSeq)
client->pollIds.push(ClientData::EchoId(echoId, echoSeq));
if (client->pollIds.size() > maxSavedPolls)
client->pollIds.pop();
DEBUG_ONLY(cout << "poll -> " << client->pollIds.size() << endl);
DEBUG_ONLY(printf("poll -> %d\n", client->pollIds.size()));
if (client->pendingPackets.size() > 0)
{
@ -271,14 +254,14 @@ void Server::pollReceived(ClientData *client, uint16_t echoId, uint16_t echoSeq)
memcpy(echoSendPayloadBuffer(), &packet.data[0], packet.data.size());
client->pendingPackets.pop();
DEBUG_ONLY(cout << "pending packet: " << packet.data.size() << " bytes\n");
DEBUG_ONLY(printf("pending packet: %d bytes\n", packet.data.size()));
sendEchoToClient(client, packet.type, packet.data.size());
}
client->lastActivity = now;
}
void Server::sendEchoToClient(ClientData *client, TunnelHeader::Type type, int dataLength)
void Server::sendEchoToClient(ClientData *client, int type, int dataLength)
{
if (client->maxPolls == 0)
{
@ -291,7 +274,7 @@ void Server::sendEchoToClient(ClientData *client, TunnelHeader::Type type, int d
ClientData::EchoId echoId = client->pollIds.front();
client->pollIds.pop();
DEBUG_ONLY(cout << "sending -> " << client->pollIds.size() << endl);
DEBUG_ONLY(printf("sending -> %d\n", client->pollIds.size()));
sendEcho(magic, type, dataLength, client->realIp, true, echoId.id, echoId.seq);
return;
}
@ -299,11 +282,10 @@ void Server::sendEchoToClient(ClientData *client, TunnelHeader::Type type, int d
if (client->pendingPackets.size() == MAX_BUFFERED_PACKETS)
{
client->pendingPackets.pop();
syslog(LOG_WARNING, "packet to %s dropped",
Utility::formatIp(client->tunnelIp).data());
syslog(LOG_WARNING, "packet dropped to %s", Utility::formatIp(client->tunnelIp).c_str());
}
DEBUG_ONLY(cout << "packet queued: " << dataLength << " bytes\n");
DEBUG_ONLY(printf("packet queued: %d bytes\n", dataLength));
client->pendingPackets.push(Packet());
Packet &packet = client->pendingPackets.back();
@ -319,16 +301,15 @@ void Server::releaseTunnelIp(uint32_t tunnelIp)
void Server::handleTimeout()
{
ClientList::iterator it = clientList.begin();
while (it != clientList.end())
for (int i = 0; i < clientList.size(); i++)
{
ClientData &client = *it++;
ClientData *client = &clientList[i];
if (client.lastActivity + KEEP_ALIVE_INTERVAL * 2 < now)
if (client->lastActivity + KEEP_ALIVE_INTERVAL * 2 < now)
{
syslog(LOG_DEBUG, "client %s timed out\n",
Utility::formatIp(client.realIp).data());
removeClient(&client);
syslog(LOG_DEBUG, "client timeout: %s\n", Utility::formatIp(client->realIp).c_str());
removeClient(client);
i--;
}
}

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef SERVER_H
@ -26,29 +26,28 @@
#include <map>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <string>
class Server : public Worker
{
public:
Server(int tunnelMtu, const std::string *deviceName, const std::string &passphrase,
uint32_t network, bool answerEcho, uid_t uid, gid_t gid, int pollTimeout);
Server(int tunnelMtu, const char *deviceName, const char *passphrase, uint32_t network, bool answerEcho, uid_t uid, gid_t gid, int pollTimeout);
virtual ~Server();
// change some time:
// struct __attribute__ ((__packed__)) ClientConnectData
struct ClientConnectData
{
uint8_t maxPolls;
uint32_t desiredIp;
};
static const TunnelHeader::Magic magic;
static const Worker::TunnelHeader::Magic magic;
protected:
struct Packet
{
TunnelHeader::Type type;
int type;
std::vector<char> data;
};
@ -83,8 +82,8 @@ protected:
Auth::Challenge challenge;
};
typedef std::list<ClientData> ClientList;
typedef std::map<uint32_t, ClientList::iterator> ClientIpMap;
typedef std::vector<ClientData> ClientList;
typedef std::map<uint32_t, int> ClientIpMap;
virtual bool handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, uint16_t id, uint16_t seq);
virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp);
@ -101,7 +100,7 @@ protected:
void checkChallenge(ClientData *client, int dataLength);
void sendReset(ClientData *client);
void sendEchoToClient(ClientData *client, TunnelHeader::Type type, int dataLength);
void sendEchoToClient(ClientData *client, int type, int dataLength);
void pollReceived(ClientData *client, uint16_t echoId, uint16_t echoSeq);

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "time.h"
@ -32,13 +32,13 @@ Time Time::operator-(const Time &other) const
Time result;
result.tv.tv_sec = tv.tv_sec - other.tv.tv_sec;
result.tv.tv_usec = tv.tv_usec - other.tv.tv_usec;
if (result.tv.tv_usec < 0)
{
result.tv.tv_usec += 1000000;
result.tv.tv_sec -= 1;
}
return result;
}
@ -47,13 +47,13 @@ Time Time::operator+(const Time &other) const
Time result;
result.tv.tv_sec = tv.tv_sec + other.tv.tv_sec;
result.tv.tv_usec = tv.tv_usec + other.tv.tv_usec;
if (result.tv.tv_usec >= 1000000)
{
result.tv.tv_usec -= 1000000;
result.tv.tv_sec += 1;
}
return result;
}

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef TIME_H

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "tun.h"
@ -22,114 +22,82 @@
#include "utility.h"
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sstream>
#ifdef WIN32
#include <w32api/windows.h>
#endif
typedef ip IpHeader;
using std::string;
using namespace std;
#ifdef WIN32
static void winsystem(char *cmd)
{
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
if (CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
WaitForSingleObject(processInfo.hProcess, INFINITE);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
}
#endif
#define ip_path "/sbin/ip "
Tun::Tun(const string *device, int mtu)
Tun::Tun(const char *device, int mtu)
{
this->mtu = mtu;
if (device)
this->device = *device;
this->device.resize(VTUN_DEV_LEN);
fd = tun_open(&this->device[0]);
this->device.resize(strlen(this->device.data()));
if (device != NULL)
{
strncpy(this->device, device, VTUN_DEV_LEN);
this->device[VTUN_DEV_LEN] = 0;
}
else
this->device[0] = 0;
fd = tun_open(this->device);
if (fd == -1)
throw Exception(string("could not create tunnel device: ") + tun_last_error());
throw Exception("could not create tunnel device");
syslog(LOG_INFO, "opened tunnel device: %s", this->device.data());
std::stringstream cmdline;
#ifdef WIN32
cmdline << "netsh interface ipv4 set subinterface \"" << this->device
<< "\" mtu=" << mtu;
winsystem(cmdline.str().data());
#else
// ip link set dev <device> mtu <mtu>
cmdline << ip_path << "link set dev " << this->device << " mtu " << mtu;
// cmdline << "/sbin/ifconfig " << this->device << " mtu " << mtu;
if (system(cmdline.str().data()) != 0)
char cmdline[512];
snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s mtu %u", this->device, mtu);
if (system(cmdline) != 0)
syslog(LOG_ERR, "could not set tun device mtu");
#endif
}
Tun::~Tun()
{
tun_close(fd, &device[0]);
tun_close(fd, device);
}
void Tun::setIp(uint32_t ip, uint32_t destIp)
void Tun::setIp(uint32_t ip, uint32_t destIp, bool includeSubnet)
{
std::stringstream cmdline;
char cmdline[512];
string ips = Utility::formatIp(ip);
string destIps = Utility::formatIp(destIp);
#ifdef WIN32
cmdline << "netsh interface ip set address name=\"" << device << "\" "
<< "static " << ips << " 255.255.255.0";
winsystem(cmdline.str().data());
if (!tun_set_ip(fd, ip, ip & 0xffffff00, 0xffffff00))
syslog(LOG_ERR, "could not set tun device driver ip address: %s", tun_last_error());
#elif LINUX
// ip addr add <ip>/24 dev <device>
cmdline << ip_path << "addr add " << ips << "/24 dev " << device;
// cmdline << "/sbin/ifconfig " << device << " " << ips << " netmask 255.255.255.0";
if (system(cmdline.str().data()) != 0)
syslog(LOG_ERR, "could not set tun device ip address");
#ifdef LINUX
snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s %s netmask 255.255.255.0", device, ips.c_str());
#else
cmdline << "/sbin/ifconfig " << device << " " << ips << " " << destIps
<< " netmask 255.255.255.255";
if (system(cmdline.str().data()) != 0)
snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s %s %s netmask 255.255.255.255", device, ips.c_str(), destIps.c_str());
#endif
if (system(cmdline) != 0)
syslog(LOG_ERR, "could not set tun device ip address");
#ifndef LINUX
if (includeSubnet)
{
snprintf(cmdline, sizeof(cmdline), "/sbin/route add %s/24 %s", destIps.c_str(), destIps.c_str());
if (system(cmdline) != 0)
syslog(LOG_ERR, "could not add route");
}
#endif
}
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, tun_last_error());
syslog(LOG_ERR, "error writing %d bytes to tun: %s", length, strerror(errno));
}
int Tun::read(char *buffer)
{
int length = tun_read(fd, buffer, mtu);
if (length == -1)
syslog(LOG_ERR, "error reading from tun: %s", tun_last_error());
syslog(LOG_ERR, "error reading from tun: %s", strerror(errno));
return length;
}

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef TUN_H
@ -28,7 +28,7 @@
class Tun
{
public:
Tun(const std::string *device, int mtu);
Tun(const char *device, int mtu);
~Tun();
int getFd() { return fd; }
@ -38,9 +38,9 @@ public:
void write(const char *buffer, int length);
void setIp(uint32_t ip, uint32_t destIp);
void setIp(uint32_t ip, uint32_t destIp, bool includeSubnet);
protected:
std::string device;
char device[VTUN_DEV_LEN];
int mtu;
int fd;

View File

@ -1,50 +1,27 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2013 Friedrich Schöller <hans@schoeller.se>
* 1998-2000 Maxim Krasnyansky <max_mk@yahoo.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
/*
VTun - Virtual Tunnel over TCP/IP network.
Copyright (C) 1998-2000 Maxim Krasnyansky <max_mk@yahoo.com>
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.
*/
#ifdef WIN32
#include <stdint.h>
#include <stdbool.h>
#define VTUN_DEV_LEN 100
#else
#define VTUN_DEV_LEN 20
#endif
#ifdef __cplusplus
extern "C" {
#endif
extern "C"
{
int tun_open(char *dev);
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();
#ifdef WIN32
bool tun_set_ip(int fd, uint32_t local, uint32_t network, uint32_t netmask);
#endif
#ifdef __cplusplus
}
#endif

View File

@ -1,318 +0,0 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2013 Friedrich Schöller <hans@schoeller.se>
* 2002-2005 Ivo Timmermans,
* 2002-2011 Guus Sliepen <guus@tinc-vpn.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#include "tun_dev.h"
#include <unistd.h>
#include <stdbool.h>
#include <syslog.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <w32api/windows.h>
#include <w32api/winioctl.h>
#define TAP_WIN_CONTROL_CODE(request,method) CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
#define TAP_WIN_IOCTL_GET_MAC TAP_WIN_CONTROL_CODE (1, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_GET_VERSION TAP_WIN_CONTROL_CODE (2, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_GET_MTU TAP_WIN_CONTROL_CODE (3, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_GET_INFO TAP_WIN_CONTROL_CODE (4, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT TAP_WIN_CONTROL_CODE (5, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_SET_MEDIA_STATUS TAP_WIN_CONTROL_CODE (6, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_CONFIG_DHCP_MASQ TAP_WIN_CONTROL_CODE (7, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_GET_LOG_LINE TAP_WIN_CONTROL_CODE (8, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT TAP_WIN_CONTROL_CODE (9, METHOD_BUFFERED)
#define TAP_WIN_IOCTL_CONFIG_TUN TAP_WIN_CONTROL_CODE (10, METHOD_BUFFERED)
#define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
#define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
#define USERMODEDEVICEDIR "\\\\.\\Global\\"
#define SYSDEVICEDIR "\\Device\\"
#define USERDEVICEDIR "\\DosDevices\\Global\\"
#define TAP_WIN_SUFFIX ".tap"
struct adapter_info
{
int reader_read_fd, reader_write_fd;
HANDLE reader_thread;
HANDLE adapter_handle;
};
#define ERROR_BUFFER_SIZE 1024
char error_buffer[ERROR_BUFFER_SIZE];
static void error(char *format, ...)
{
va_list vl;
va_start(vl, format);
vsnprintf(error_buffer, ERROR_BUFFER_SIZE, format, vl);
va_end(vl);
}
static void noerror(void)
{
*error_buffer = 0;
}
static const char *winerror(int err)
{
static char buf[1024], *ptr;
ptr = buf + sprintf(buf, "(%d) ", err);
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), ptr, sizeof(buf) - (ptr - buf), NULL)) {
strcpy(ptr, "(unable to format errormessage)");
};
if((ptr = strchr(buf, '\r')))
*ptr = '\0';
return buf;
}
static struct adapter_info *get_adapter_info_from_fd(int fd)
{
static struct adapter_info single_adapter_info = {
.reader_read_fd = -1,
.reader_write_fd = -1,
.reader_thread = INVALID_HANDLE_VALUE,
.adapter_handle = INVALID_HANDLE_VALUE
};
return &single_adapter_info;
}
static HANDLE open_tap_adapter(char *name)
{
HKEY connections_key, adapter_key;
int adapter_index, error_code;
char regpath[1024];
char adapter_id[VTUN_DEV_LEN];
char adapter_path[1024];
char adapter_name[1024];
HANDLE adapter_handle = INVALID_HANDLE_VALUE;
DWORD len;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &connections_key) != ERROR_SUCCESS)
{
error("opening registry: %s", winerror(GetLastError()));
return INVALID_HANDLE_VALUE;
}
for (adapter_index = 0; ; adapter_index++)
{
len = sizeof(adapter_id);
if (RegEnumKeyEx(connections_key, adapter_index, adapter_id, &len, 0, 0, 0, NULL) != ERROR_SUCCESS)
break;
snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapter_id);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &adapter_key) != ERROR_SUCCESS)
continue;
len = sizeof(adapter_name);
if (RegQueryValueEx(adapter_key, "Name", 0, 0, adapter_name, &len) != ERROR_SUCCESS)
{
RegCloseKey(adapter_key);
continue;
}
RegCloseKey(adapter_key);
if (name && name[0] && strcmp(name, adapter_name) && strcmp(name, adapter_id))
continue;
snprintf(adapter_path, sizeof(adapter_path), USERMODEDEVICEDIR "%s" TAP_WIN_SUFFIX, adapter_id);
adapter_handle = CreateFile(adapter_path, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
if (adapter_handle != INVALID_HANDLE_VALUE)
break;
}
RegCloseKey(connections_key);
if (adapter_handle == INVALID_HANDLE_VALUE)
{
error("could not open tap adapter");
return INVALID_HANDLE_VALUE;
}
strncpy(name, adapter_name, VTUN_DEV_LEN);
noerror();
return adapter_handle;
}
static __stdcall DWORD reader_thread(LPVOID ptr)
{
struct adapter_info *adapter_info = ptr;
char buf[0xffff]; // maximum IPv4 packet size
OVERLAPPED overlapped;
DWORD len;
int wait_result;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = CreateEvent(NULL, true, false, NULL);
while (true)
{
if (!ReadFile(adapter_info->adapter_handle, buf, sizeof(buf), &len, &overlapped))
{
if (GetLastError() != ERROR_IO_PENDING)
{
syslog(LOG_ERR, "error reading from tap adapter: %s", winerror(GetLastError()));
return 1;
}
wait_result = WaitForSingleObjectEx(overlapped.hEvent, INFINITE, false);
if (wait_result != WAIT_OBJECT_0)
{
syslog(LOG_ERR, "error waiting for tap adapter: %s", winerror(GetLastError()));
return 1;
}
if (!GetOverlappedResult(adapter_info->adapter_handle, &overlapped, &len, true))
{
syslog(LOG_ERR, "error getting tap adapter reading result: %s", winerror(GetLastError()));
return 1;
}
}
write(adapter_info->reader_write_fd, buf, len);
}
}
int tun_open(char *dev)
{
struct adapter_info *adapter_info;
int socket_pair[2];
if (socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, socket_pair))
{
error("creating socket pair: %s", strerror(errno));
return -1;
}
adapter_info = get_adapter_info_from_fd(socket_pair[0]);
adapter_info->reader_read_fd = socket_pair[0];
adapter_info->reader_write_fd = socket_pair[1];
adapter_info->adapter_handle = open_tap_adapter(dev);
if (adapter_info->adapter_handle == INVALID_HANDLE_VALUE)
{
tun_close(adapter_info->reader_read_fd, NULL);
return -1;
}
adapter_info->reader_thread = CreateThread(NULL, 0, reader_thread, adapter_info, 0, NULL);
if (adapter_info->reader_thread == INVALID_HANDLE_VALUE)
{
error("reader thread creation: %s", winerror(GetLastError()));
tun_close(adapter_info->reader_read_fd, NULL);
return -1;
}
return adapter_info->reader_read_fd;
}
int tun_close(int fd, char *dev)
{
struct adapter_info *adapter_info = get_adapter_info_from_fd(fd);
if (adapter_info->reader_thread != INVALID_HANDLE_VALUE)
{
TerminateThread(adapter_info->reader_thread, 0);
adapter_info->reader_thread = INVALID_HANDLE_VALUE;
}
close(adapter_info->reader_read_fd);
adapter_info->reader_read_fd = -1;
close(adapter_info->reader_write_fd);
adapter_info->reader_write_fd = -1;
if (adapter_info->adapter_handle != INVALID_HANDLE_VALUE)
{
CloseHandle(adapter_info->adapter_handle);
adapter_info->adapter_handle = INVALID_HANDLE_VALUE;
}
return 0;
}
int tun_write(int fd, char *buf, int len)
{
struct adapter_info *adapter_info = get_adapter_info_from_fd(fd);
OVERLAPPED overlapped;
DWORD written;
memset(&overlapped, 0, sizeof(overlapped));
if (!WriteFile(adapter_info->adapter_handle, buf, len, &written, &overlapped))
{
error("tap write: %s", winerror(GetLastError()));
return -1;
}
return written;
}
int tun_read(int fd, char *buf, int len)
{
len = read(fd, buf, len);
if (len == -1)
error("reader read: %s", strerror(errno));
return len;
}
const char *tun_last_error()
{
return error_buffer;
}
bool tun_set_ip(int fd, uint32_t local, uint32_t network, uint32_t netmask)
{
struct adapter_info *adapter_info = get_adapter_info_from_fd(fd);
uint32_t addresses[3];
DWORD status;
DWORD len;
addresses[0] = htonl(local);
addresses[1] = htonl(network);
addresses[2] = htonl(netmask);
if (!DeviceIoControl(adapter_info->adapter_handle, TAP_WIN_IOCTL_CONFIG_TUN,
&addresses, sizeof(addresses), &addresses, sizeof(addresses), &len, NULL))
{
error("configuring tap addresses: %s", winerror(GetLastError()));
return false;
}
status = true;
if (!DeviceIoControl(adapter_info->adapter_handle, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
&status, sizeof(status), &status, sizeof(status), &len, NULL))
{
error("enabling tap device: %s", winerror(GetLastError()));
return false;
}
noerror();
return true;
}

View File

@ -1,20 +1,19 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
* 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 <http://www.gnu.org/licenses/>.
*
/*
VTun - Virtual Tunnel over TCP/IP network.
Copyright (C) 1998-2000 Maxim Krasnyansky <max_mk@yahoo.com>
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.
*/
#include "tunemu.h"
@ -23,7 +22,10 @@
int tun_open(char *dev)
{
return tunemu_open(dev);
int fd = tunemu_open(dev);
if (fd < 0)
syslog(LOG_ERR, tunemu_error);
return fd;
}
int tun_close(int fd, char *dev)
@ -40,8 +42,3 @@ int tun_read(int fd, char *buf, int len)
{
return tunemu_read(fd, buf, len);
}
const char *tun_last_error()
{
return tunemu_error;
}

View File

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

View File

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

View File

@ -130,8 +130,3 @@ 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);
}

View File

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

View File

@ -175,8 +175,3 @@ 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);
}

View File

@ -1,7 +1,7 @@
/*
* tunemu - Tun device emulation for Darwin
*
* Copyright (c) 2009-2013 Friedrich Schöller <hans@schoeller.se>
* Copyright (c) 2009-2010 Friedrich Schöller <hans@schoeller.se>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,18 +28,20 @@
#include "tunemu.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/io.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <util.h>
#include <pcap.h>
#include <stdarg.h>
#include <errno.h>
#include <stdint.h>
#include <stdint.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>

View File

@ -1,7 +1,7 @@
/*
* tunemu - Tun device emulation for Darwin
*
* Copyright (c) 2009-2013 Friedrich Schöller <hans@schoeller.se>
* Copyright (c) 2009-2010 Friedrich Schöller <hans@schoeller.se>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "utility.h"
@ -22,16 +22,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sstream>
std::string Utility::formatIp(uint32_t ip)
using namespace std;
string Utility::formatIp(uint32_t ip)
{
std::stringstream s;
s << ((ip >> 24) & 0xff) << '.'
<< ((ip >> 16) & 0xff) << '.'
<< ((ip >> 8) & 0xff) << '.'
<< ((ip >> 0) & 0xff);
return s.str();
char buffer[16];
sprintf(buffer, "%d.%d.%d.%d", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
return buffer;
}
int Utility::rand()

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef UTILITY_H

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#include "worker.h"
@ -27,11 +27,8 @@
#include <sys/types.h>
#include <unistd.h>
#include <sys/select.h>
#include <grp.h>
#include <iostream>
using std::cout;
using std::endl;
using namespace std;
Worker::TunnelHeader::Magic::Magic(const char *magic)
{
@ -49,37 +46,54 @@ bool Worker::TunnelHeader::Magic::operator!=(const Magic &other) const
return memcmp(data, other.data, sizeof(data)) != 0;
}
Worker::Worker(int tunnelMtu, const std::string *deviceName, bool answerEcho,
uid_t uid, gid_t gid)
: echo(tunnelMtu + sizeof(TunnelHeader)), tun(deviceName, tunnelMtu)
Worker::Worker(int tunnelMtu, const char *deviceName, bool answerEcho, uid_t uid, gid_t gid)
{
this->tunnelMtu = tunnelMtu;
this->answerEcho = answerEcho;
this->uid = uid;
this->gid = gid;
this->privilegesDropped = false;
echo = NULL;
tun = NULL;
try
{
echo = new Echo(tunnelMtu + sizeof(TunnelHeader));
tun = new Tun(deviceName, tunnelMtu);
}
catch (...)
{
delete echo;
delete tun;
throw;
}
}
void Worker::sendEcho(const TunnelHeader::Magic &magic, TunnelHeader::Type type,
int length, uint32_t realIp, bool reply, uint16_t id, uint16_t seq)
Worker::~Worker()
{
delete echo;
delete tun;
}
void Worker::sendEcho(const TunnelHeader::Magic &magic, int type, int length, uint32_t realIp, bool reply, uint16_t id, uint16_t seq)
{
if (length > payloadBufferSize())
throw Exception("packet too big");
TunnelHeader *header = (TunnelHeader *)echo.sendPayloadBuffer();
TunnelHeader *header = (TunnelHeader *)echo->sendPayloadBuffer();
header->magic = magic;
header->type = type;
DEBUG_ONLY(
cout << "sending: type " << type << ", length " << length
<< ", id " << id << ", seq " << seq << endl);
DEBUG_ONLY(printf("sending: type %d, length %d, id %d, seq %d\n", type, length, id, seq));
echo.send(length + sizeof(TunnelHeader), realIp, reply, id, seq);
echo->send(length + sizeof(TunnelHeader), realIp, reply, id, seq);
}
void Worker::sendToTun(int length)
{
tun.write(echoReceivePayloadBuffer(), length);
tun->write(echoReceivePayloadBuffer(), length);
}
void Worker::setTimeout(Time delta)
@ -92,7 +106,7 @@ void Worker::run()
now = Time::now();
alive = true;
int maxFd = echo.getFd() > tun.getFd() ? echo.getFd() : tun.getFd();
int maxFd = echo->getFd() > tun->getFd() ? echo->getFd() : tun->getFd();
while (alive)
{
@ -100,8 +114,8 @@ void Worker::run()
Time timeout;
FD_ZERO(&fs);
FD_SET(tun.getFd(), &fs);
FD_SET(echo.getFd(), &fs);
FD_SET(tun->getFd(), &fs);
FD_SET(echo->getFd(), &fs);
if (nextTimeout != Time::ZERO)
{
@ -111,15 +125,9 @@ void Worker::run()
}
// wait for data or timeout
timeval *timeval = nextTimeout != Time::ZERO ? &timeout.getTimeval() : NULL;
int result = select(maxFd + 1 , &fs, NULL, NULL, timeval);
int result = select(maxFd + 1 , &fs, NULL, NULL, nextTimeout != Time::ZERO ? &timeout.getTimeval() : NULL);
if (result == -1)
{
if (alive)
throw Exception("select", true);
else
return;
}
throw Exception("select", true);
now = Time::now();
// timeout
@ -131,43 +139,40 @@ void Worker::run()
}
// icmp data
if (FD_ISSET(echo.getFd(), &fs))
if (FD_ISSET(echo->getFd(), &fs))
{
bool reply;
uint16_t id, seq;
uint32_t ip;
int dataLength = echo.receive(ip, reply, id, seq);
int dataLength = echo->receive(ip, reply, id, seq);
if (dataLength != -1)
{
bool valid = dataLength >= sizeof(TunnelHeader);
if (valid)
{
TunnelHeader *header = (TunnelHeader *)echo.receivePayloadBuffer();
TunnelHeader *header = (TunnelHeader *)echo->receivePayloadBuffer();
DEBUG_ONLY(
cout << "received: type " << header->type
<< ", length " << dataLength - sizeof(TunnelHeader)
<< ", id " << id << ", seq " << seq << endl);
DEBUG_ONLY(printf("received: type %d, length %d, id %d, seq %d\n", header->type, dataLength - sizeof(TunnelHeader), id, seq));
valid = handleEchoData(*header, dataLength - sizeof(TunnelHeader), ip, reply, id, seq);
}
if (!valid && !reply && answerEcho)
{
memcpy(echo.sendPayloadBuffer(), echo.receivePayloadBuffer(), dataLength);
echo.send(dataLength, ip, true, id, seq);
memcpy(echo->sendPayloadBuffer(), echo->receivePayloadBuffer(), dataLength);
echo->send(dataLength, ip, true, id, seq);
}
}
}
// data from tun
if (FD_ISSET(tun.getFd(), &fs))
if (FD_ISSET(tun->getFd(), &fs))
{
uint32_t sourceIp, destIp;
int dataLength = tun.read(echoSendPayloadBuffer(), sourceIp, destIp);
int dataLength = tun->read(echoSendPayloadBuffer(), sourceIp, destIp);
if (dataLength == 0)
throw Exception("tunnel closed");
@ -178,24 +183,13 @@ void Worker::run()
}
}
void Worker::stop()
{
alive = false;
}
void Worker::dropPrivileges()
{
if (uid <= 0 || privilegesDropped)
return;
#ifdef WIN32
throw Exception("dropping privileges not supported");
#else
syslog(LOG_INFO, "dropping privileges");
if (setgroups(0, NULL) == -1)
throw Exception("setgroups", true);
if (setgid(gid) == -1)
throw Exception("setgid", true);
@ -203,24 +197,4 @@ void Worker::dropPrivileges()
throw Exception("setuid", true);
privilegesDropped = true;
#endif
}
bool Worker::handleEchoData(const TunnelHeader &, int, uint32_t, bool, uint16_t, uint16_t)
{
return true;
}
void Worker::handleTunData(int, uint32_t, uint32_t) { }
void Worker::handleTimeout() { }
char *Worker::echoSendPayloadBuffer()
{
return echo.sendPayloadBuffer() + sizeof(TunnelHeader);
}
char *Worker::echoReceivePayloadBuffer()
{
return echo.receivePayloadBuffer() + sizeof(TunnelHeader);
}

View File

@ -1,20 +1,20 @@
/*
* Hans - IP over ICMP
* Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef WORKER_H
@ -30,12 +30,10 @@
class Worker
{
public:
Worker(int tunnelMtu, const std::string *deviceName, bool answerEcho,
uid_t uid, gid_t gid);
virtual ~Worker() { }
Worker(int tunnelMtu, const char *deviceName, bool answerEcho, uid_t uid, gid_t gid);
virtual ~Worker();
virtual void run();
virtual void stop();
static int headerSize() { return sizeof(TunnelHeader); }
@ -53,44 +51,41 @@ protected:
char data[4];
};
enum Type
{
TYPE_RESET_CONNECTION = 1,
TYPE_CONNECTION_REQUEST = 2,
TYPE_CHALLENGE = 3,
TYPE_CHALLENGE_RESPONSE = 4,
TYPE_CONNECTION_ACCEPT = 5,
TYPE_CHALLENGE_ERROR = 6,
TYPE_DATA = 7,
TYPE_POLL = 8,
TYPE_SERVER_FULL = 9
};
Magic magic;
uint8_t type;
enum Type
{
TYPE_RESET_CONNECTION = 1,
TYPE_CONNECTION_REQUEST = 2,
TYPE_CHALLENGE = 3,
TYPE_CHALLENGE_RESPONSE = 4,
TYPE_CONNECTION_ACCEPT = 5,
TYPE_CHALLENGE_ERROR = 6,
TYPE_DATA = 7,
TYPE_POLL = 8,
TYPE_SERVER_FULL = 9
};
}; // size = 5
virtual bool handleEchoData(const TunnelHeader &header, int dataLength,
uint32_t realIp, bool reply, uint16_t id, uint16_t seq);
virtual void handleTunData(int dataLength, uint32_t sourceIp,
uint32_t destIp); // to echoSendPayloadBuffer
virtual void handleTimeout();
virtual bool handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, uint16_t id, uint16_t seq) { return true; }
virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp) { } // to echoSendPayloadBuffer
virtual void handleTimeout() { }
void sendEcho(const TunnelHeader::Magic &magic, TunnelHeader::Type type,
int length, uint32_t realIp, bool reply, uint16_t id, uint16_t seq);
void sendEcho(const TunnelHeader::Magic &magic, int type, int length, uint32_t realIp, bool reply, uint16_t id, uint16_t seq);
void sendToTun(int length); // from echoReceivePayloadBuffer
void setTimeout(Time delta);
char *echoSendPayloadBuffer();
char *echoReceivePayloadBuffer();
char *echoSendPayloadBuffer() { return echo->sendPayloadBuffer() + sizeof(TunnelHeader); }
char *echoReceivePayloadBuffer() { return echo->receivePayloadBuffer() + sizeof(TunnelHeader); }
int payloadBufferSize() { return tunnelMtu; }
void dropPrivileges();
Echo echo;
Tun tun;
Echo *echo;
Tun *tun;
bool alive;
bool answerEcho;
int tunnelMtu;