From ac2147483cfcbb6f01123d65e67b7bb363bddc8f Mon Sep 17 00:00:00 2001 From: Othmar Gsenger Date: Fri, 28 Nov 2008 18:45:57 +0000 Subject: added some windows support (not working now) try to build with following Options: NOCRYPT;NODAEMON;NOEXEC;NOPACKED;NOSYSLOG;NOROUTING;NOSIGNALCONTROLLER --- src/PracticalSocket.cpp | 480 ------------------------------------------------ src/PracticalSocket.h | 401 ---------------------------------------- src/anytun.cpp | 23 ++- src/anytun.sln | 20 ++ src/anytun.vcproj | 440 ++++++++++++++++++++++++++++++++++++++++++++ src/cipher.h | 3 +- src/connectionList.cpp | 8 + src/connectionList.h | 3 +- src/daemon.hpp | 8 + src/encryptedPacket.cpp | 2 +- src/keyDerivation.cpp | 3 +- src/log.cpp | 9 +- src/log.h | 33 ++++ src/plainPacket.h | 6 +- 14 files changed, 541 insertions(+), 898 deletions(-) delete mode 100644 src/PracticalSocket.cpp delete mode 100644 src/PracticalSocket.h create mode 100644 src/anytun.sln create mode 100644 src/anytun.vcproj (limited to 'src') diff --git a/src/PracticalSocket.cpp b/src/PracticalSocket.cpp deleted file mode 100644 index 6f53da6..0000000 --- a/src/PracticalSocket.cpp +++ /dev/null @@ -1,480 +0,0 @@ -/* - * anytun - * - * The secure anycast tunneling protocol (satp) defines a protocol used - * for communication between any combination of unicast and anycast - * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel - * mode and allows tunneling of every ETHER TYPE protocol (e.g. - * ethernet, ip, arp ...). satp directly includes cryptography and - * message authentication based on the methodes used by SRTP. It is - * intended to deliver a generic, scaleable and secure solution for - * tunneling and relaying of packets of any protocol. - * - * - * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, - * Christian Pointner - * - * This file is part of Anytun. - * - * Anytun is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 3 as - * published by the Free Software Foundation. - * - * Anytun 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 anytun. If not, see . - */ - -#include "datatypes.h" - -// this is from: http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/ -// and this is their header: -/* - * C++ sockets on Unix and Windows - * Copyright (C) 2002 - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "PracticalSocket.h" - -#ifdef WIN32 - #include // For socket(), connect(), send(), and recv() - typedef int socklen_t; - typedef char raw_type; // Type used for raw data on this platform -#else - #include // For data types - #include // For socket(), connect(), send(), and recv() - #include // For gethostbyname() - #include // For inet_addr() - #include // For close() - #include // For sockaddr_in - #include - typedef void raw_type; // Type used for raw data on this platform -#endif - -#include // for strerror_r -#include // For errno - -using namespace std; - -#ifdef WIN32 -static bool initialized = false; -#endif - -// SocketException Code - -SocketException::SocketException(const string &message, bool inclSysMsg) - throw() : userMessage(message) { - if (inclSysMsg) { - userMessage.append(": "); - char buf[STERROR_TEXT_MAX]; - buf[0] = 0; - strerror_r(errno, buf, STERROR_TEXT_MAX); - userMessage.append(buf); - } -} - -SocketException::~SocketException() throw() { -} - -const char *SocketException::what() const throw() { - return userMessage.c_str(); -} - -// Function to fill in address structure given an address and port -static void fillAddr(const string &address, unsigned short port, - sockaddr_in &addr) { - memset(&addr, 0, sizeof(addr)); // Zero out address structure - addr.sin_family = AF_INET; // Internet address - - hostent *host; // Resolve name - if ((host = gethostbyname(address.c_str())) == NULL) { - // strerror() will not work for gethostbyname() and hstrerror() - // is supposedly obsolete - throw SocketException("Failed to resolve name (gethostbyname())"); - } - addr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]); - - addr.sin_port = htons(port); // Assign port in network byte order -} - -// Socket Code - -Socket::Socket(int type, int protocol) throw(SocketException) { - #ifdef WIN32 - if (!initialized) { - WORD wVersionRequested; - WSADATA wsaData; - - wVersionRequested = MAKEWORD(2, 0); // Request WinSock v2.0 - if (WSAStartup(wVersionRequested, &wsaData) != 0) { // Load WinSock DLL - throw SocketException("Unable to load WinSock DLL"); - } - initialized = true; - } - #endif - - // Make a new socket - if ((sockDesc = socket(PF_INET, type, protocol)) < 0) { - throw SocketException("Socket creation failed (socket())", true); - } -} - -Socket::Socket(int sockDesc) { - this->sockDesc = sockDesc; -} - -Socket::~Socket() { - #ifdef WIN32 - ::closesocket(sockDesc); - #else - ::close(sockDesc); - #endif - sockDesc = -1; -} - -string Socket::getLocalAddress() throw(SocketException) { - sockaddr_in addr; - unsigned int addr_len = sizeof(addr); - - if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { - throw SocketException("Fetch of local address failed (getsockname())", true); - } - return inet_ntoa(addr.sin_addr); -} - -unsigned short Socket::getLocalPort() throw(SocketException) { - sockaddr_in addr; - unsigned int addr_len = sizeof(addr); - - if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { - throw SocketException("Fetch of local port failed (getsockname())", true); - } - return ntohs(addr.sin_port); -} - -void Socket::setLocalPort(unsigned short localPort) throw(SocketException) { - // Bind the socket to its port - sockaddr_in localAddr; - memset(&localAddr, 0, sizeof(localAddr)); - localAddr.sin_family = AF_INET; - localAddr.sin_addr.s_addr = htonl(INADDR_ANY); - localAddr.sin_port = htons(localPort); - - if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { - throw SocketException("Set of local port failed (bind())", true); - } -} - -void Socket::setLocalAddressAndPort(const string &localAddress, - unsigned short localPort) throw(SocketException) { - // Get the address of the requested host - sockaddr_in localAddr; - fillAddr(localAddress, localPort, localAddr); - - if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { - throw SocketException("Set of local address and port failed (bind())", true); - } -} - -void Socket::setSocketOpt(int optionName, const void* optionValue, socklen_t optionLen) - throw(SocketException) -{ - if (::setsockopt(sockDesc, SOL_SOCKET, optionName, optionValue, optionLen) < 0) { - throw SocketException("setSockopt failed", true); - } -} - -void Socket::cleanUp() throw(SocketException) { - #ifdef WIN32 - if (WSACleanup() != 0) { - throw SocketException("WSACleanup() failed"); - } - #endif -} - -unsigned short Socket::resolveService(const string &service, - const string &protocol) { - struct servent *serv; /* Structure containing service information */ - - if ((serv = getservbyname(service.c_str(), protocol.c_str())) == NULL) - return atoi(service.c_str()); /* Service is port number */ - else - return ntohs(serv->s_port); /* Found port (network byte order) by name */ -} - -// CommunicatingSocket Code - -CommunicatingSocket::CommunicatingSocket(int type, int protocol) - throw(SocketException) : Socket(type, protocol) { -} - -CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD) { -} - -void CommunicatingSocket::connect(const string &foreignAddress, - unsigned short foreignPort) throw(SocketException) { - // Get the address of the requested host - sockaddr_in destAddr; - fillAddr(foreignAddress, foreignPort, destAddr); - - // Try to connect to the given port - if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) { - throw SocketException("Connect failed (connect())", true); - } -} - -void CommunicatingSocket::send(const void *buffer, int bufferLen) - throw(SocketException) { - if (::send(sockDesc, (raw_type *) buffer, bufferLen, 0) < 0) { - throw SocketException("Send failed (send())", true); - } -} - -int CommunicatingSocket::recv(void *buffer, int bufferLen) - throw(SocketException) { - int rtn; - if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0) { - throw SocketException("Received failed (recv())", true); - } - - return rtn; -} - -int CommunicatingSocket::recvNonBlocking(void *buffer, int bufferLen, int timeOut) - throw(SocketException) -{ - struct pollfd pfd[1]; - pfd[0].fd = sockDesc; - pfd[0].events = POLLIN; - int rtn = poll(pfd,1,timeOut); - if(rtn > 0) { - if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0) { - throw SocketException("non blocking receive failed", true); - } - if(!rtn) { - throw SocketException("connection closed by peer", false); - } - } - return rtn; -} - -string CommunicatingSocket::getForeignAddress() - throw(SocketException) { - sockaddr_in addr; - unsigned int addr_len = sizeof(addr); - - if (getpeername(sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) { - throw SocketException("Fetch of foreign address failed (getpeername())", true); - } - return inet_ntoa(addr.sin_addr); -} - -unsigned short CommunicatingSocket::getForeignPort() throw(SocketException) { - sockaddr_in addr; - unsigned int addr_len = sizeof(addr); - - if (getpeername(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { - throw SocketException("Fetch of foreign port failed (getpeername())", true); - } - return ntohs(addr.sin_port); -} - -// TCPSocket Code - -TCPSocket::TCPSocket() - throw(SocketException) : CommunicatingSocket(SOCK_STREAM, - IPPROTO_TCP) { -} - -TCPSocket::TCPSocket(const string &foreignAddress, unsigned short foreignPort) - throw(SocketException) : CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) { - connect(foreignAddress, foreignPort); -} - -TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) { -} - -// TCPServerSocket Code - -TCPServerSocket::TCPServerSocket(unsigned short localPort, int queueLen) - throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { - const int opt = 1; - setSocketOpt(SO_REUSEADDR, &opt, sizeof(opt)); - setLocalPort(localPort); - setListen(queueLen); -} - -TCPServerSocket::TCPServerSocket(const string &localAddress, - unsigned short localPort, int queueLen) - throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { - const int opt = 1; - setSocketOpt(SO_REUSEADDR, &opt, sizeof(opt)); - setLocalAddressAndPort(localAddress, localPort); - setListen(queueLen); -} - -TCPSocket *TCPServerSocket::accept() throw(SocketException) { - int newConnSD; - if ((newConnSD = ::accept(sockDesc, NULL, 0)) < 0) { - throw SocketException("Accept failed (accept())", true); - } - - return new TCPSocket(newConnSD); -} - -void TCPServerSocket::setListen(int queueLen) throw(SocketException) { - if (listen(sockDesc, queueLen) < 0) { - throw SocketException("Set listening socket failed (listen())", true); - } -} - -// UDPSocket Code - -UDPSocket::UDPSocket() throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, - IPPROTO_UDP) { - - const int opt = 1; - setSocketOpt(SO_REUSEADDR, &opt, sizeof(opt)); - setBroadcast(); -} - -UDPSocket::UDPSocket(unsigned short localPort) throw(SocketException) : - CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) { - const int opt = 1; - setSocketOpt(SO_REUSEADDR, &opt, sizeof(opt)); - setLocalPort(localPort); - setBroadcast(); -} - -UDPSocket::UDPSocket(const string &localAddress, unsigned short localPort) - throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) { - const int opt = 1; - setSocketOpt(SO_REUSEADDR, &opt, sizeof(opt)); - setLocalAddressAndPort(localAddress, localPort); - setBroadcast(); -} - -void UDPSocket::setBroadcast() { - // If this fails, we'll hear about it when we try to send. This will allow - // system that cannot broadcast to continue if they don't plan to broadcast - int broadcastPermission = 1; - setsockopt(sockDesc, SOL_SOCKET, SO_BROADCAST, - (raw_type *) &broadcastPermission, sizeof(broadcastPermission)); -} - -void UDPSocket::disconnect() throw(SocketException) { - sockaddr_in nullAddr; - memset(&nullAddr, 0, sizeof(nullAddr)); - nullAddr.sin_family = AF_UNSPEC; - - // Try to disconnect - if (::connect(sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0) { - #ifdef WIN32 - if (errno != WSAEAFNOSUPPORT) { - #else - if (errno != EAFNOSUPPORT) { - #endif - throw SocketException("Disconnect failed (connect())", true); - } - } -} - -void UDPSocket::sendTo(const void *buffer, int bufferLen, - const string &foreignAddress, unsigned short foreignPort) - throw(SocketException) { - sockaddr_in destAddr; - fillAddr(foreignAddress, foreignPort, destAddr); - - // Write out the whole buffer as a single message. - if (sendto(sockDesc, (raw_type *) buffer, bufferLen, 0, - (sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen) { - throw SocketException("Send failed (sendto())", true); - } -} - -int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress, - unsigned short &sourcePort) throw(SocketException) { - sockaddr_in clntAddr; - socklen_t addrLen = sizeof(clntAddr); - int rtn; - if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, 0, - (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0) { - throw SocketException("Receive failed (recvfrom())", true); - } - sourceAddress = inet_ntoa(clntAddr.sin_addr); - sourcePort = ntohs(clntAddr.sin_port); - - return rtn; -} - -int UDPSocket::recvFromNonBlocking(void *buffer, int bufferLen, string &sourceAddress, - unsigned short &sourcePort, int timeOut) throw(SocketException) { - sockaddr_in clntAddr; - socklen_t addrLen = sizeof(clntAddr); - struct pollfd pfd[1]; - pfd[0].fd = sockDesc; - pfd[0].events = POLLIN; - int rtn = poll(pfd,1,timeOut); - if(rtn > 0) { - if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, 0, - (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0) { - throw SocketException("Receive failed (recvfrom())", true); - } - if(!rtn) { - throw SocketException("connection closed by peer", false); - } - } - sourceAddress = inet_ntoa(clntAddr.sin_addr); - sourcePort = ntohs(clntAddr.sin_port); - - return rtn; -} - -void UDPSocket::setMulticastTTL(unsigned char multicastTTL) throw(SocketException) { - if (setsockopt(sockDesc, IPPROTO_IP, IP_MULTICAST_TTL, - (raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0) { - throw SocketException("Multicast TTL set failed (setsockopt())", true); - } -} - -void UDPSocket::joinGroup(const string &multicastGroup) throw(SocketException) { - struct ip_mreq multicastRequest; - - multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); - multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); - if (setsockopt(sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, - (raw_type *) &multicastRequest, - sizeof(multicastRequest)) < 0) { - throw SocketException("Multicast group join failed (setsockopt())", true); - } -} - -void UDPSocket::leaveGroup(const string &multicastGroup) throw(SocketException) { - struct ip_mreq multicastRequest; - - multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); - multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); - if (setsockopt(sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, - (raw_type *) &multicastRequest, - sizeof(multicastRequest)) < 0) { - throw SocketException("Multicast group leave failed (setsockopt())", true); - } -} diff --git a/src/PracticalSocket.h b/src/PracticalSocket.h deleted file mode 100644 index fd266f6..0000000 --- a/src/PracticalSocket.h +++ /dev/null @@ -1,401 +0,0 @@ -/* - * anytun - * - * The secure anycast tunneling protocol (satp) defines a protocol used - * for communication between any combination of unicast and anycast - * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel - * mode and allows tunneling of every ETHER TYPE protocol (e.g. - * ethernet, ip, arp ...). satp directly includes cryptography and - * message authentication based on the methodes used by SRTP. It is - * intended to deliver a generic, scaleable and secure solution for - * tunneling and relaying of packets of any protocol. - * - * - * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, - * Christian Pointner - * - * This file is part of Anytun. - * - * Anytun is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 3 as - * published by the Free Software Foundation. - * - * Anytun 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 anytun. If not, see . - */ - -// this is from: http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/ -// and this is their header: -/* - * C++ sockets on Unix and Windows - * Copyright (C) 2002 - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef __PRACTICALSOCKET_INCLUDED__ -#define __PRACTICALSOCKET_INCLUDED__ - -#include // For string -#include // For exception class - -using namespace std; - -/** - * Signals a problem with the execution of a socket call. - */ -class SocketException : public exception { -public: - /** - * Construct a SocketException with a explanatory message. - * @param message explanatory message - * @param incSysMsg true if system message (from strerror(errno)) - * should be postfixed to the user provided message - */ - SocketException(const string &message, bool inclSysMsg = false) throw(); - - /** - * Provided just to guarantee that no exceptions are thrown. - */ - ~SocketException() throw(); - - /** - * Get the exception message - * @return exception message - */ - const char *what() const throw(); - -private: - string userMessage; // Exception message -}; - -/** - * Base class representing basic communication endpoint - */ -class Socket { -public: - /** - * Close and deallocate this socket - */ - ~Socket(); - - /** - * Get the local address - * @return local address of socket - * @exception SocketException thrown if fetch fails - */ - string getLocalAddress() throw(SocketException); - - /** - * Get the local port - * @return local port of socket - * @exception SocketException thrown if fetch fails - */ - unsigned short getLocalPort() throw(SocketException); - - /** - * Set the local port to the specified port and the local address - * to any interface - * @param localPort local port - * @exception SocketException thrown if setting local port fails - */ - void setLocalPort(unsigned short localPort) throw(SocketException); - - /** - * Set the local port to the specified port and the local address - * to the specified address. If you omit the port, a random port - * will be selected. - * @param localAddress local address - * @param localPort local port - * @exception SocketException thrown if setting local port or address fails - */ - void setLocalAddressAndPort(const string &localAddress, - unsigned short localPort = 0) throw(SocketException); - - - void setSocketOpt(int optionName, const void* optionValue, socklen_t optionLen) - throw(SocketException); - - /** - * If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore - * this in our sample client code but include it in the library for - * completeness. If you are running on Windows and you are concerned - * about DLL resource consumption, call this after you are done with all - * Socket instances. If you execute this on Windows while some instance of - * Socket exists, you are toast. For portability of client code, this is - * an empty function on non-Windows platforms so you can always include it. - * @param buffer buffer to receive the data - * @param bufferLen maximum number of bytes to read into buffer - * @return number of bytes read, 0 for EOF, and -1 for error - * @exception SocketException thrown WinSock clean up fails - */ - static void cleanUp() throw(SocketException); - - /** - * Resolve the specified service for the specified protocol to the - * corresponding port number in host byte order - * @param service service to resolve (e.g., "http") - * @param protocol protocol of service to resolve. Default is "tcp". - */ - static unsigned short resolveService(const string &service, - const string &protocol = "tcp"); - -private: - // Prevent the user from trying to use value semantics on this object - Socket(const Socket &sock); - void operator=(const Socket &sock); - -protected: - int sockDesc; // Socket descriptor - Socket(int type, int protocol) throw(SocketException); - Socket(int sockDesc); -}; - -/** - * Socket which is able to connect, send, and receive - */ -class CommunicatingSocket : public Socket { -public: - /** - * Establish a socket connection with the given foreign - * address and port - * @param foreignAddress foreign address (IP address or name) - * @param foreignPort foreign port - * @exception SocketException thrown if unable to establish connection - */ - void connect(const string &foreignAddress, unsigned short foreignPort) - throw(SocketException); - - /** - * Write the given buffer to this socket. Call connect() before - * calling send() - * @param buffer buffer to be written - * @param bufferLen number of bytes from buffer to be written - * @exception SocketException thrown if unable to send data - */ - void send(const void *buffer, int bufferLen) throw(SocketException); - - /** - * Read into the given buffer up to bufferLen bytes data from this - * socket. Call connect() before calling recv() - * @param buffer buffer to receive the data - * @param bufferLen maximum number of bytes to read into buffer - * @return number of bytes read, 0 for EOF, and -1 for error - * @exception SocketException thrown if unable to receive data - */ - int recv(void *buffer, int bufferLen) throw(SocketException); - - /** - * Read into the given buffer up to bufferLen bytes data from this - * socket. Call connect() before recvNonBlocking(). - * @param buffer buffer to receive the data - * @param bufferLen maximum number of bytes to read into buffer - * @param timeout timout in ms - * @return number of bytes read, 0 for timeout, and -1 for error - * @exception SocketException thrown if unable to receive data - */ - int recvNonBlocking(void *buffer, int bufferLen, int timeout) throw(SocketException); - - /** - * Get the foreign address. Call connect() before calling recv() - * @return foreign address - * @exception SocketException thrown if unable to fetch foreign address - */ - string getForeignAddress() throw(SocketException); - - /** - * Get the foreign port. Call connect() before calling recv() - * @return foreign port - * @exception SocketException thrown if unable to fetch foreign port - */ - unsigned short getForeignPort() throw(SocketException); - -protected: - CommunicatingSocket(int type, int protocol) throw(SocketException); - CommunicatingSocket(int newConnSD); -}; - -/** - * TCP socket for communication with other TCP sockets - */ -class TCPSocket : public CommunicatingSocket { -public: - /** - * Construct a TCP socket with no connection - * @exception SocketException thrown if unable to create TCP socket - */ - TCPSocket() throw(SocketException); - - /** - * Construct a TCP socket with a connection to the given foreign address - * and port - * @param foreignAddress foreign address (IP address or name) - * @param foreignPort foreign port - * @exception SocketException thrown if unable to create TCP socket - */ - TCPSocket(const string &foreignAddress, unsigned short foreignPort) - throw(SocketException); - -private: - // Access for TCPServerSocket::accept() connection creation - friend class TCPServerSocket; - TCPSocket(int newConnSD); -}; - -/** - * TCP socket class for servers - */ -class TCPServerSocket : public Socket { -public: - /** - * Construct a TCP socket for use with a server, accepting connections - * on the specified port on any interface - * @param localPort local port of server socket, a value of zero will - * give a system-assigned unused port - * @param queueLen maximum queue length for outstanding - * connection requests (default 5) - * @exception SocketException thrown if unable to create TCP server socket - */ - TCPServerSocket(unsigned short localPort, int queueLen = 5) - throw(SocketException); - - /** - * Construct a TCP socket for use with a server, accepting connections - * on the specified port on the interface specified by the given address - * @param localAddress local interface (address) of server socket - * @param localPort local port of server socket - * @param queueLen maximum queue length for outstanding - * connection requests (default 5) - * @exception SocketException thrown if unable to create TCP server socket - */ - TCPServerSocket(const string &localAddress, unsigned short localPort, - int queueLen = 5) throw(SocketException); - - /** - * Blocks until a new connection is established on this socket or error - * @return new connection socket - * @exception SocketException thrown if attempt to accept a new connection fails - */ - TCPSocket *accept() throw(SocketException); - -private: - void setListen(int queueLen) throw(SocketException); -}; - -/** - * UDP socket class - */ -class UDPSocket : public CommunicatingSocket { -public: - /** - * Construct a UDP socket - * @exception SocketException thrown if unable to create UDP socket - */ - UDPSocket() throw(SocketException); - - /** - * Construct a UDP socket with the given local port - * @param localPort local port - * @exception SocketException thrown if unable to create UDP socket - */ - UDPSocket(unsigned short localPort) throw(SocketException); - - /** - * Construct a UDP socket with the given local port and address - * @param localAddress local address - * @param localPort local port - * @exception SocketException thrown if unable to create UDP socket - */ - UDPSocket(const string &localAddress, unsigned short localPort) - throw(SocketException); - - /** - * Unset foreign address and port - * @return true if disassociation is successful - * @exception SocketException thrown if unable to disconnect UDP socket - */ - void disconnect() throw(SocketException); - - /** - * Send the given buffer as a UDP datagram to the - * specified address/port - * @param buffer buffer to be written - * @param bufferLen number of bytes to write - * @param foreignAddress address (IP address or name) to send to - * @param foreignPort port number to send to - * @return true if send is successful - * @exception SocketException thrown if unable to send datagram - */ - void sendTo(const void *buffer, int bufferLen, const string &foreignAddress, - unsigned short foreignPort) throw(SocketException); - - /** - * Read read up to bufferLen bytes data from this socket. The given buffer - * is where the data will be placed - * @param buffer buffer to receive data - * @param bufferLen maximum number of bytes to receive - * @param sourceAddress address of datagram source - * @param sourcePort port of data source - * @return number of bytes received and -1 for error - * @exception SocketException thrown if unable to receive datagram - */ - int recvFrom(void *buffer, int bufferLen, string &sourceAddress, - unsigned short &sourcePort) throw(SocketException); - - /** - * Read read up to bufferLen bytes data from this socket. The given buffer - * is where the data will be placed - * @param buffer buffer to receive data - * @param bufferLen maximum number of bytes to receive - * @param sourceAddress address of datagram source - * @param sourcePort port of data source - * @param timeout int ms - * @return number of bytes received and -1 for error - * @exception SocketException thrown if unable to receive datagram - */ - int recvFromNonBlocking(void *buffer, int bufferLen, string &sourceAddress, - unsigned short &sourcePort, int timeout) throw(SocketException); - - /** - * Set the multicast TTL - * @param multicastTTL multicast TTL - * @exception SocketException thrown if unable to set TTL - */ - void setMulticastTTL(unsigned char multicastTTL) throw(SocketException); - - /** - * Join the specified multicast group - * @param multicastGroup multicast group address to join - * @exception SocketException thrown if unable to join group - */ - void joinGroup(const string &multicastGroup) throw(SocketException); - - /** - * Leave the specified multicast group - * @param multicastGroup multicast group address to leave - * @exception SocketException thrown if unable to leave group - */ - void leaveGroup(const string &multicastGroup) throw(SocketException); - -private: - void setBroadcast(); -}; - -#endif diff --git a/src/anytun.cpp b/src/anytun.cpp index 553c451..05d2164 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -31,13 +31,7 @@ #include #include -#include -#include -#include -#include -#include -#include -#include + #include #ifndef NOCRYPT @@ -57,14 +51,18 @@ #include "cipherFactory.h" #include "authAlgoFactory.h" #include "keyDerivationFactory.h" +#ifndef NOSIGNALCONTROLLER #include "signalController.h" +#endif #include "packetSource.h" #include "tunDevice.h" #include "options.h" #include "seqWindow.h" #include "connectionList.h" +#ifndef NOROUTING #include "routingTable.h" #include "networkAddress.h" +#endif #include "syncQueue.h" #include "syncCommand.h" @@ -96,9 +94,11 @@ void createConnection(const PacketSourceEndpoint & remote_end, ConnectionList & ConnectionParam connparam ( (*kd), (*seq), seq_nr_, remote_end); cl.addConnection(connparam,mux); +#ifndef NOROUTING NetworkAddress addr(ipv4,gOpt.getIfconfigParamRemoteNetmask().c_str()); NetworkPrefix prefix(addr,32); gRoutingTable.addRoute(prefix,mux); +#endif SyncCommand sc (cl,mux); queue.push(sc); SyncCommand sc2 (prefix); @@ -158,9 +158,14 @@ void sender(void* p) if(param->cl.empty()) continue; //std::cout << "got Packet for plain "< "<cl.getConnection(mux); +#else + ConnectionMap::iterator cit = param->cl.getBegin(); +#endif + if(cit==param->cl.getEnd()) continue; ConnectionParam & conn = cit->second; @@ -416,9 +421,11 @@ int main(int argc, char* argv[]) pidFile << pid; pidFile.close(); } - + +#ifndef NOSIGNALCONTROLLER SignalController sig; sig.init(); +#endif ThreadParam p(dev, *src, cl, queue,*(new OptionConnectTo())); diff --git a/src/anytun.sln b/src/anytun.sln new file mode 100644 index 0000000..256ef8c --- /dev/null +++ b/src/anytun.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual C++ Express 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "anytun", "anytun.vcproj", "{12460D00-D78A-4C68-BDE2-9E3B2F9CD0F3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {12460D00-D78A-4C68-BDE2-9E3B2F9CD0F3}.Debug|Win32.ActiveCfg = Debug|Win32 + {12460D00-D78A-4C68-BDE2-9E3B2F9CD0F3}.Debug|Win32.Build.0 = Debug|Win32 + {12460D00-D78A-4C68-BDE2-9E3B2F9CD0F3}.Release|Win32.ActiveCfg = Release|Win32 + {12460D00-D78A-4C68-BDE2-9E3B2F9CD0F3}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/anytun.vcproj b/src/anytun.vcproj new file mode 100644 index 0000000..4e092db --- /dev/null +++ b/src/anytun.vcproj @@ -0,0 +1,440 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/cipher.h b/src/cipher.h index d402bce..c49b3fc 100644 --- a/src/cipher.h +++ b/src/cipher.h @@ -37,8 +37,9 @@ #include "encryptedPacket.h" #include "plainPacket.h" +#ifndef NOCRYPT #include - +#endif class Cipher { diff --git a/src/connectionList.cpp b/src/connectionList.cpp index 5292529..d272a03 100644 --- a/src/connectionList.cpp +++ b/src/connectionList.cpp @@ -81,6 +81,7 @@ void ConnectionList::addConnection(ConnectionParam &conn, u_int16_t mux ) const ConnectionMap::iterator ConnectionList::getEnd() { + Lock lock(mutex_); return connections_.end(); } @@ -89,6 +90,13 @@ ConnectionMap::iterator ConnectionList::getBeginUnlocked() return connections_.begin(); } +const ConnectionMap::iterator ConnectionList::getBegin() +{ + Lock lock(mutex_); + return connections_.begin(); +} + + ConnectionMap::iterator ConnectionList::getEndUnlocked() { return connections_.end(); diff --git a/src/connectionList.h b/src/connectionList.h index 95e9052..cdf5268 100644 --- a/src/connectionList.h +++ b/src/connectionList.h @@ -51,7 +51,8 @@ public: const ConnectionMap::iterator getConnection(u_int16_t mux); const ConnectionMap::iterator getEnd(); ConnectionMap::iterator getEndUnlocked(); - ConnectionMap::iterator getBeginUnlocked(); + ConnectionMap::iterator getBeginUnlocked(); + const ConnectionMap::iterator getBegin(); ConnectionParam & getOrNewConnectionUnlocked(u_int16_t mux); bool empty(); void clear(); diff --git a/src/daemon.hpp b/src/daemon.hpp index be5c710..9f1715d 100644 --- a/src/daemon.hpp +++ b/src/daemon.hpp @@ -2,6 +2,14 @@ #define _DAEMON_HPP #ifndef NODAEMON +#include +#include +#include +#include +#include +#include +#include + void chrootAndDrop(std::string const& chrootdir, std::string const& username) { if (getuid() != 0) diff --git a/src/encryptedPacket.cpp b/src/encryptedPacket.cpp index 1562f71..483fb50 100644 --- a/src/encryptedPacket.cpp +++ b/src/encryptedPacket.cpp @@ -31,7 +31,7 @@ #include #include -#include +//#include #include // for std::memcpy #include "encryptedPacket.h" diff --git a/src/keyDerivation.cpp b/src/keyDerivation.cpp index 946943e..ed29fca 100644 --- a/src/keyDerivation.cpp +++ b/src/keyDerivation.cpp @@ -33,6 +33,7 @@ #include "log.h" #include "keyDerivation.h" #include "threadUtils.hpp" +#include "datatypes.h" #include #include @@ -43,7 +44,7 @@ #include "mpi.h" #endif -void KeyDerivation::setLogKDRate(const uint8_t log_rate) +void KeyDerivation::setLogKDRate(const u_int8_t log_rate) { Lock lock(mutex_); if( log_rate < 49 ) diff --git a/src/log.cpp b/src/log.cpp index 23922cc..72c3d3c 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -31,7 +31,6 @@ #include #include -#include #include "log.h" @@ -54,7 +53,9 @@ LogStringBuilder::LogStringBuilder(Log& l, int p) : log(l), prio(p) LogStringBuilder::~LogStringBuilder() { Lock lock(log.mutex); +#ifndef NOSYSLOG syslog(prio | log.getFacility(), stream.str().c_str()); +#endif } Log& Log::instance() @@ -69,19 +70,23 @@ Log& Log::instance() Log::Log() { - facility = LOG_DAEMON; + facility = FAC_DAEMON; logName = "anytun"; open(); } Log::~Log() { +#ifndef NOSYSLOG closelog(); +#endif } void Log::open() { +#ifndef NOSYSLOG openlog(logName.c_str(), LOG_PID, facility); +#endif } Log& Log::setLogName(std::string newLogName) diff --git a/src/log.h b/src/log.h index ac87bbd..5c12661 100644 --- a/src/log.h +++ b/src/log.h @@ -34,7 +34,9 @@ #include #include +#ifndef NOSYSLOG #include +#endif #include "threadUtils.hpp" @@ -59,6 +61,7 @@ private: class Log : public std::ostringstream { public: +#ifndef NOSYSLOG static const int FAC_USER = LOG_USER; static const int FAC_MAIL = LOG_MAIL; static const int FAC_DAEMON = LOG_DAEMON; @@ -87,6 +90,36 @@ public: static const int PRIO_NOTICE = LOG_NOTICE; static const int PRIO_INFO = LOG_INFO; static const int PRIO_DEBUG = LOG_DEBUG; +#else + static const int FAC_USER = 0; + static const int FAC_MAIL = 0; + static const int FAC_DAEMON = 0; + static const int FAC_AUTH = 0; + static const int FAC_SYSLOG = 0; + static const int FAC_LPR = 0; + static const int FAC_NEWS = 0; + static const int FAC_UUCP = 0; + static const int FAC_CRON = 0; + static const int FAC_AUTHPRIV = 0; + static const int FAC_FTP = 0; + static const int FAC_LOCAL0 = 0; + static const int FAC_LOCAL1 = 0; + static const int FAC_LOCAL2 = 0; + static const int FAC_LOCAL3 = 0; + static const int FAC_LOCAL4 = 0; + static const int FAC_LOCAL5 = 0; + static const int FAC_LOCAL6 = 0; + static const int FAC_LOCAL7 = 0; + + static const int PRIO_EMERG = 0; + static const int PRIO_ALERT = 0; + static const int PRIO_CRIT = 0; + static const int PRIO_ERR = 0; + static const int PRIO_WARNING = 0; + static const int PRIO_NOTICE = 0; + static const int PRIO_INFO = 0; + static const int PRIO_DEBUG = 0; +#endif static Log& instance(); diff --git a/src/plainPacket.h b/src/plainPacket.h index 667c723..dddc3f1 100644 --- a/src/plainPacket.h +++ b/src/plainPacket.h @@ -35,7 +35,7 @@ #include "datatypes.h" #include "buffer.h" -#include "networkAddress.h" +//#include "networkAddress.h" class Cipher; /** @@ -93,8 +93,8 @@ public: */ u_int8_t* getPayload(); - NetworkAddress getSrcAddr() const; - NetworkAddress getDstAddr() const; +// NetworkAddress getSrcAddr() const; +// NetworkAddress getDstAddr() const; private: PlainPacket(); -- cgit v1.2.3