diff options
author | Christian Pointner <equinox@anytun.org> | 2009-03-05 01:21:31 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anytun.org> | 2009-03-05 01:21:31 +0000 |
commit | e808ee8668e19337773a5e2b2e35f5aabb84baf7 (patch) | |
tree | 7b99757ac1083565e8a426652471c54e564a3f07 /src | |
parent | moved sync thread creation before priv.drop to allow bind on privileged ports (diff) |
added resolvAddrType to gResolver
Diffstat (limited to 'src')
-rw-r--r-- | src/anytun.cpp | 11 | ||||
-rw-r--r-- | src/datatypes.h | 2 | ||||
-rw-r--r-- | src/options.h | 2 | ||||
-rw-r--r-- | src/resolver.cpp | 50 | ||||
-rw-r--r-- | src/resolver.h | 8 |
5 files changed, 52 insertions, 21 deletions
diff --git a/src/anytun.cpp b/src/anytun.cpp index 088b7ad..335ce43 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -447,22 +447,19 @@ int main(int argc, char* argv[]) else src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort()); - HostList connect_to = gOpt.getRemoteSyncHosts(); - if(gOpt.getRemoteAddr() != "") - gResolver.resolveUdp(gOpt.getRemoteAddr(), gOpt.getRemotePort(), boost::bind(createConnection, _1, gOpt.getSeqWindowSize(), gOpt.getMux())); + gResolver.resolveUdp(gOpt.getRemoteAddr(), gOpt.getRemotePort(), boost::bind(createConnection, _1, gOpt.getSeqWindowSize(), gOpt.getMux()), gOpt.getResolvAddrType()); + HostList connect_to = gOpt.getRemoteSyncHosts(); #ifndef NO_ROUTING NetworkList routes = gOpt.getRoutes(); NetworkList::const_iterator rit; - for(rit = routes.begin(); rit != routes.end(); ++rit) - { + for(rit = routes.begin(); rit != routes.end(); ++rit) { NetworkAddress addr( rit->net_addr ); NetworkPrefix prefix( addr, static_cast<u_int8_t>(rit->prefix_length)); gRoutingTable.addRoute( prefix, gOpt.getMux() ); } - if (connect_to.begin() == connect_to.end() || gOpt.getDevType()!="tun") - { + if (connect_to.begin() == connect_to.end() || gOpt.getDevType()!="tun") { cLog.msg(Log::PRIO_NOTICE) << "No sync/control host defined or not a tun device. Disabling multi connection support (routing)"; disableRouting=true; } diff --git a/src/datatypes.h b/src/datatypes.h index 27fac4b..23bdde6 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -52,6 +52,8 @@ typedef u_int16_t sender_id_t; typedef u_int16_t payload_type_t; typedef u_int16_t mux_t; +typedef enum { ANY, IPV4_ONLY, IPV6_ONLY } ResolvAddrType; + #ifndef _MSC_VER #define ATTR_PACKED __attribute__((__packed__)) typedef int system_error_t; diff --git a/src/options.h b/src/options.h index dce8eeb..122f3f0 100644 --- a/src/options.h +++ b/src/options.h @@ -77,8 +77,6 @@ std::istream& operator>>(std::istream& stream, OptionNetwork& network); typedef std::list<std::string> StringList; -typedef enum { ANY, IPV4_ONLY, IPV6_ONLY } ResolvAddrType; - class Options { public: diff --git a/src/resolver.cpp b/src/resolver.cpp index 7439f3d..e0d6fa8 100644 --- a/src/resolver.cpp +++ b/src/resolver.cpp @@ -35,7 +35,7 @@ #include "resolver.h" #include "log.h" -template<class Proto> ResolveHandler<Proto>::ResolveHandler(const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<Proto>)> const& onResolve) : addr_(addr), port_(port), callback_(onResolve) +template<class Proto> ResolveHandler<Proto>::ResolveHandler(const std::string& addr, const std::string& port, boost::function<void(boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ResolvAddrType r) : addr_(addr), port_(port), callback_(onResolve), resolv_addr_type_(r) { } @@ -90,20 +90,52 @@ void Resolver::run() } } -void Resolver::resolveUdp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::udp::endpoint)> const& onResolve) +using ::boost::asio::ip::udp; + +void Resolver::resolveUdp(const std::string& addr, const std::string& port, boost::function<void (udp::endpoint)> const& onResolve, ResolvAddrType r) { cLog.msg(Log::PRIO_DEBUG) << "trying to resolv UDP: " << addr << " " << port; - boost::asio::ip::udp::resolver::query query(addr, port); - UdpResolveHandler handler(addr, port, onResolve); - udp_resolver_.async_resolve(query, handler); + std::auto_ptr<udp::resolver::query> query; + if(addr != "") { + switch(r) { + case IPV4_ONLY: query = std::auto_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), addr, port)); break; + case IPV6_ONLY: query = std::auto_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), addr, port)); break; + default: query = std::auto_ptr<udp::resolver::query>(new udp::resolver::query(addr, port)); break; + } + } + else { + switch(r) { + case IPV4_ONLY: query = std::auto_ptr<udp::resolver::query>(new udp::resolver::query(udp::v4(), port)); break; + case IPV6_ONLY: query = std::auto_ptr<udp::resolver::query>(new udp::resolver::query(udp::v6(), port)); break; + default: query = std::auto_ptr<udp::resolver::query>(new udp::resolver::query(port)); break; + } + } + UdpResolveHandler handler(addr, port, onResolve, r); + udp_resolver_.async_resolve(*query, handler); } -void Resolver::resolveTcp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::tcp::endpoint)> const& onResolve) +using ::boost::asio::ip::tcp; + +void Resolver::resolveTcp(const std::string& addr, const std::string& port, boost::function<void (tcp::endpoint)> const& onResolve, ResolvAddrType r) { cLog.msg(Log::PRIO_DEBUG) << "trying to resolv TCP: " << addr << " " << port; - boost::asio::ip::tcp::resolver::query query(addr, port); - TcpResolveHandler handler(addr, port, onResolve); - tcp_resolver_.async_resolve(query, handler); + std::auto_ptr<tcp::resolver::query> query; + if(addr != "") { + switch(r) { + case IPV4_ONLY: query = std::auto_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), addr, port)); break; + case IPV6_ONLY: query = std::auto_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), addr, port)); break; + default: query = std::auto_ptr<tcp::resolver::query>(new tcp::resolver::query(addr, port)); break; + } + } + else { + switch(r) { + case IPV4_ONLY: query = std::auto_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v4(), port)); break; + case IPV6_ONLY: query = std::auto_ptr<tcp::resolver::query>(new tcp::resolver::query(tcp::v6(), port)); break; + default: query = std::auto_ptr<tcp::resolver::query>(new tcp::resolver::query(port)); break; + } + } + TcpResolveHandler handler(addr, port, onResolve, r); + tcp_resolver_.async_resolve(*query, handler); } diff --git a/src/resolver.h b/src/resolver.h index 6bf969c..8a64738 100644 --- a/src/resolver.h +++ b/src/resolver.h @@ -36,19 +36,21 @@ #include <boost/asio.hpp> #include <boost/function.hpp> +#include "datatypes.h" #include "threadUtils.hpp" template<class Proto> class ResolveHandler { public: - ResolveHandler(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::basic_endpoint<Proto>)> const& onResolve); + ResolveHandler(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::basic_endpoint<Proto>)> const& onResolve, ResolvAddrType r = ANY); void operator()(const boost::system::error_code& e, const boost::asio::ip::basic_resolver_iterator<Proto>); private: std::string addr_; std::string port_; boost::function<void (boost::asio::ip::basic_endpoint<Proto>)> callback_; + ResolvAddrType resolv_addr_type_; }; typedef ResolveHandler<boost::asio::ip::udp> UdpResolveHandler; @@ -62,8 +64,8 @@ public: void init(); void run(); - void resolveUdp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::udp::endpoint)> const& onResolve); - void resolveTcp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::tcp::endpoint)> const& onResolve); + void resolveUdp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::udp::endpoint)> const& onResolve, ResolvAddrType r = ANY); + void resolveTcp(const std::string& addr, const std::string& port, boost::function<void (boost::asio::ip::tcp::endpoint)> const& onResolve, ResolvAddrType r = ANY); private: Resolver(); |