From 09445efffdc6ed1aa52919d6e23db8a22eb2faac Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 19 Oct 2008 19:34:42 +0000 Subject: switched from PracticalSocket to libasio --- src/packetSource.cpp | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'src/packetSource.cpp') 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); } -- cgit v1.2.3