From c7bb6ebe786c706dec3ad48d395297b60899420f Mon Sep 17 00:00:00 2001 From: equinox Date: Sun, 19 Oct 2008 19:34:42 +0000 Subject: [PATCH] switched from PracticalSocket to libasio git-svn-id: https://anytun.org/svn/anytun@572 2edecd69-f0ce-4815-94af-351a89d40aaa --- src/Makefile | 2 -- src/anytun.cpp | 40 ++++++++++++++-------------------------- src/packetSource.cpp | 43 +++++++++++++++++++++++++++++++++++-------- src/packetSource.h | 10 +++++++--- 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/Makefile b/src/Makefile index 39dd864..5f38371 100644 --- a/src/Makefile +++ b/src/Makefile @@ -78,7 +78,6 @@ OBJS = tunDevice.o \ connectionParam.o \ networkAddress.o \ networkPrefix.o \ - PracticalSocket.o \ router.o \ routingTable.o \ signalController.o \ @@ -119,7 +118,6 @@ ANYCONFOBJS = log.o \ rtpSession.o \ anyrtpproxy/callIdQueue.o \ syncRtpCommand.o \ - PracticalSocket.o \ anyConfOptions.o \ router.o \ routingTable.o \ diff --git a/src/anytun.cpp b/src/anytun.cpp index 15b2cff..cb4fb48 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -186,17 +186,17 @@ void sender(void* p) { param->src.send(encrypted_packet.getBuf(), encrypted_packet.getLength(), conn.remote_host_, conn.remote_port_); } - catch (std::exception e) + catch (std::exception& e) { // ignoring icmp port unreachable :) and other socket errors :( } } } - catch(std::runtime_error e) + catch(std::runtime_error& e) { cLog.msg(Log::PRIO_ERR) << "sender thread died due to an uncaught runtime_error: " << e.what(); } - catch(std::exception e) + catch(std::exception& e) { cLog.msg(Log::PRIO_ERR) << "sender thread died due to an uncaught exception: " << e.what(); } @@ -255,7 +255,7 @@ void receiver(void* p) while(1) { - string remote_host; + std::string remote_host; u_int16_t remote_port; plain_packet.setLength(MAX_PACKET_LENGTH); @@ -326,11 +326,11 @@ void receiver(void* p) param->dev.write(plain_packet.getPayload(), plain_packet.getLength()); } } - catch(std::runtime_error e) + catch(std::runtime_error& e) { cLog.msg(Log::PRIO_ERR) << "sender thread died due to an uncaught runtime_error: " << e.what(); } - catch(std::exception e) + catch(std::exception& e) { cLog.msg(Log::PRIO_ERR) << "receiver thread died due to an uncaught exception: " << e.what(); } @@ -433,7 +433,7 @@ void daemonize() umask(027); } -int execScript(string const& script, string const& ifname) +int execScript(std::string const& script, std::string const& ifname) { pid_t pid; pid = fork(); @@ -484,26 +484,14 @@ int main(int argc, char* argv[]) int postup_ret = execScript(gOpt.getPostUpScript(), dev.getActualName()); cLog.msg(Log::PRIO_NOTICE) << "post up script '" << gOpt.getPostUpScript() << "' returned " << postup_ret; } - - -// Buffer buff(u_int32_t(1600)); -// int len; -// while(1) -// { -// len = dev.read(buff.getBuf(), buff.getLength()); -// std::cout << "read " << len << " bytes from interface " << dev.getActualName() << std::endl; -// dev.write(buff.getBuf(), len); -// } - -// return 0; - - - + if(gOpt.getChroot()) chrootAndDrop(gOpt.getChrootDir(), gOpt.getUsername()); if(gOpt.getDaemonize()) + { daemonize(); - daemonized = true; + daemonized = true; + } if(pidFile.is_open()) { pid_t pid = getpid(); @@ -519,7 +507,7 @@ int main(int argc, char* argv[]) src = new UDPPacketSource(gOpt.getLocalPort()); else src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort()); - + ConnectionList cl; ConnectToList connect_to = gOpt.getConnectTo(); SyncQueue queue; @@ -576,14 +564,14 @@ int main(int argc, char* argv[]) return ret; */ } - catch(std::runtime_error e) + catch(std::runtime_error& e) { if(daemonized) cLog.msg(Log::PRIO_ERR) << "uncaught runtime error, exiting: " << e.what(); else std::cout << "uncaught runtime error, exiting: " << e.what() << std::endl; } - catch(std::exception e) + catch(std::exception& e) { if(daemonized) cLog.msg(Log::PRIO_ERR) << "uncaught exception, exiting: " << e.what(); diff --git a/src/packetSource.cpp b/src/packetSource.cpp index b6f0250..7d06814 100644 --- a/src/packetSource.cpp +++ b/src/packetSource.cpp @@ -29,30 +29,57 @@ * along with anytun. If not, see . */ +#include +#include + #include "datatypes.h" #include "packetSource.h" #include "buffer.h" -#include "PracticalSocket.h" -UDPPacketSource::UDPPacketSource() +UDPPacketSource::UDPPacketSource(u_int16_t port) : sock_(io_service_) { -} + std::stringstream ps; + ps << port; -UDPPacketSource::UDPPacketSource(u_int16_t port) : UDPSocket(port) -{ + asio::ip::udp::resolver resolver(io_service_); + asio::ip::udp::resolver::query query(ps.str()); + asio::ip::udp::endpoint e = *resolver.resolve(query); + sock_.open(e.protocol()); + sock_.bind(e); } -UDPPacketSource::UDPPacketSource(std::string localaddr, u_int16_t port) : UDPSocket(localaddr, port) +UDPPacketSource::UDPPacketSource(std::string localaddr, u_int16_t port) : sock_(io_service_) { + std::stringstream ps; + ps << port; + + asio::ip::udp::resolver resolver(io_service_); + asio::ip::udp::resolver::query query(localaddr, ps.str()); + asio::ip::udp::endpoint e = *resolver.resolve(query); + sock_.open(e.protocol()); + sock_.bind(e); } u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, std::string& addr, u_int16_t &port) { - return recvFrom(buf, len, addr, port); + asio::ip::udp::endpoint e; + u_int32_t rtn = sock_.receive_from(asio::buffer(buf, len), e); + + addr = e.address().to_string(); + port = e.port(); + + return rtn; } void UDPPacketSource::send(u_int8_t* buf, u_int32_t len, std::string addr, u_int16_t port) { - sendTo(buf, len, addr, port); + std::stringstream ps; + ps << port; + + asio::ip::udp::resolver resolver(io_service_); + asio::ip::udp::resolver::query query(addr, ps.str()); + asio::ip::udp::endpoint e = *resolver.resolve(query); + + sock_.send_to(asio::buffer(buf, len), e); } diff --git a/src/packetSource.h b/src/packetSource.h index 7bcc52a..4fe6125 100644 --- a/src/packetSource.h +++ b/src/packetSource.h @@ -29,9 +29,10 @@ * along with anytun. If not, see . */ +#include + #include "datatypes.h" #include "buffer.h" -#include "PracticalSocket.h" class PacketSource { @@ -42,14 +43,17 @@ public: virtual void send(u_int8_t* buf, u_int32_t len, std::string addr, u_int16_t port) = 0; }; -class UDPPacketSource : public PacketSource, public UDPSocket +class UDPPacketSource : public PacketSource { public: - UDPPacketSource(); UDPPacketSource(u_int16_t port); UDPPacketSource(std::string localaddr, u_int16_t port); u_int32_t recv(u_int8_t* buf, u_int32_t len, std::string& addr, u_int16_t &port); void send(u_int8_t* buf, u_int32_t len, std::string addr, u_int16_t port); + +private: + asio::io_service io_service_; + asio::ip::udp::socket sock_; }; -- 2.11.4.GIT