added documentation; minor changes

This commit is contained in:
Friedrich Schöller 2009-07-24 00:23:59 +02:00
parent f20f1d50df
commit 735b71804d
7 changed files with 59 additions and 25 deletions

16
README Normal file
View File

@ -0,0 +1,16 @@
### Compiling ###
make
### Running as a server ###
./hans -s 10.1.2.0 -r -p password -u nobody
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
### Running as a client ###
./hans -c server_address -p password -u nobody
### Help ###
./hans

View File

@ -78,9 +78,9 @@ void Client::sendChallengeResponse(int dataLength)
setTimeout(5000); setTimeout(5000);
} }
bool Client::handleEchoData(TunnelHeader header, int dataLength, uint32_t realIp, bool reply, int id, int seq) bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, int id, int seq)
{ {
if (realIp != serverIp || !reply || id != ICMP_ID) if (realIp != serverIp || !reply)
return false; return false;
if (header.magic != Server::magic) if (header.magic != Server::magic)

View File

@ -43,7 +43,7 @@ protected:
STATE_ESTABLISHED STATE_ESTABLISHED
}; };
virtual bool handleEchoData(TunnelHeader header, int dataLength, uint32_t realIp, bool reply, int id, int seq); virtual bool handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, int id, int seq);
virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp); virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp);
virtual void handleTimeout(); virtual void handleTimeout();

View File

@ -33,8 +33,30 @@
void usage() void usage()
{ {
printf( printf(
"usage: hans -s network [-fhr] [-p password] [-u unprivileged_user] [-d tun_device] [-m mtu]\n" "Hans - IP over ICMP version 0.1 dev\n\n"
" hans -c server [-fhw] [-p password] [-u unprivileged_user] [-d tun_device] [-m mtu]\n"); "RUN AS SERVER\n"
" hans -s network [-fr] [-p password] [-u unprivileged_user] [-d tun_device] [-m reference_mtu]\n\n"
"RUN AS CLIENT\n"
" hans -c server [-f] [-p password] [-u unprivileged_user] [-d tun_device] [-m reference_mtu] [-w polls]\n\n"
"ARGUMENTS\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"
" -r Respond to ordinary pings. Only in server mode.\n"
" Use this when you disable echo replies of your operating system, which is a good idea.\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 ICMP packets will not be bigger than this value.\n"
" Has to be the same on client and server.\n"
" In most cases you don't want to set this. Defaults to 1500.\n"
" -w polls Number of echo requests the client sends to the server for polling.\n"
" If your network allows unlimited echo replies set this to 0 to disable polling.\n"
" The default value of 10 is regarded as pretty high.\n"
" Set this to a lower value if you experience packet loss through the tunnel.\n"
" Set this to 1 in extreme cases, when your network allows only one echo reply per request.\n"
" A low value will decrease the performance of the tunnel.\n");
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -56,15 +78,12 @@ int main(int argc, char *argv[])
openlog(argv[0], LOG_PERROR, LOG_DAEMON); openlog(argv[0], LOG_PERROR, LOG_DAEMON);
int c; int c;
while ((c = getopt(argc, argv, "fhru:d:p:s:c:m:w:")) != -1) while ((c = getopt(argc, argv, "fru:d:p:s:c:m:w:")) != -1)
{ {
switch(c) { switch(c) {
case 'f': case 'f':
foreground = true; foreground = true;
break; break;
case 'h':
usage();
return 0;
case 'u': case 'u':
userName = optarg; userName = optarg;
break; break;
@ -100,19 +119,16 @@ int main(int argc, char *argv[])
mtu -= Echo::headerSize() + Worker::headerSize(); mtu -= Echo::headerSize() + Worker::headerSize();
if (isClient == isServer) if (mtu < 68)
{ {
usage(); // 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; return 1;
} }
if (network == INADDR_NONE && isServer) if ((isClient == isServer) ||
{ (isServer && network == INADDR_NONE) ||
usage(); (maxPolls < 0 || maxPolls > 255))
return 1;
}
if (maxPolls < 0 || maxPolls > 255)
{ {
usage(); usage();
return 1; return 1;

View File

@ -49,7 +49,7 @@ Server::~Server()
} }
void Server::handleUnknownClient(TunnelHeader &header, int dataLength, uint32_t realIp) void Server::handleUnknownClient(const TunnelHeader &header, int dataLength, uint32_t realIp)
{ {
ClientData client; ClientData client;
client.realIp = realIp; client.realIp = realIp;
@ -146,9 +146,9 @@ void Server::sendReset(ClientData *client)
sendEchoToClient(client, TunnelHeader::TYPE_RESET_CONNECTION, 0); sendEchoToClient(client, TunnelHeader::TYPE_RESET_CONNECTION, 0);
} }
bool Server::handleEchoData(TunnelHeader header, int dataLength, uint32_t realIp, bool reply, int id, int seq) bool Server::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, int id, int seq)
{ {
if (reply || id != ICMP_ID) if (reply)
return false; return false;
if (header.magic != Client::magic) if (header.magic != Client::magic)

View File

@ -74,7 +74,7 @@ protected:
typedef std::vector<ClientData> ClientList; typedef std::vector<ClientData> ClientList;
typedef std::map<uint32_t, int> ClientIpMap; typedef std::map<uint32_t, int> ClientIpMap;
virtual bool handleEchoData(TunnelHeader header, int dataLength, uint32_t realIp, bool reply, int id, int seq); virtual bool handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, int id, int seq);
virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp); virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp);
virtual void handleTimeout(); virtual void handleTimeout();
@ -82,7 +82,7 @@ protected:
void serveTun(ClientData *client); void serveTun(ClientData *client);
void handleUnknownClient(TunnelHeader &header, int dataLength, uint32_t realIp); void handleUnknownClient(const TunnelHeader &header, int dataLength, uint32_t realIp);
void removeClient(ClientData *client); void removeClient(ClientData *client);
void sendChallenge(ClientData *client); void sendChallenge(ClientData *client);

View File

@ -41,7 +41,9 @@ protected:
{ {
struct Magic struct Magic
{ {
Magic() { }
Magic(const char *magic); Magic(const char *magic);
bool operator==(const Magic &other) const; bool operator==(const Magic &other) const;
bool operator!=(const Magic &other) const; bool operator!=(const Magic &other) const;
@ -65,11 +67,11 @@ protected:
}; };
}; // size = 5 }; // size = 5
virtual bool handleEchoData(TunnelHeader header, int dataLength, uint32_t realIp, bool reply, int id, int seq) { return true; } virtual bool handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, int id, int seq) { return true; }
virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp) { } virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp) { }
virtual void handleTimeout() { } virtual void handleTimeout() { }
void sendEcho(const TunnelHeader::Magic &magic, int type, int length, uint32_t realIp, bool reply, int id, int seq); void sendEcho(const TunnelHeader::Magic &magic, uint16_t pollSequence, int type, int length, uint32_t realIp, bool reply, int id, int seq);
void sendToTun(int length); void sendToTun(int length);
void setTimeout(Time delta); void setTimeout(Time delta);