5 #include <boost/bind/bind.hpp>
10 using namespace boost::placeholders;
15 const std::vector<DnsServer> &dns_servers,
16 uint16_t client_port,
Callback cb, uint8_t dscp) {
17 assert(resolver_ == NULL);
18 resolver_ =
new BindResolver(
io, dns_servers, client_port, cb, dscp);
29 const std::vector<DnsServer> &dns_servers,
32 : pkt_buf_(NULL), cb_(cb), sock_(
io), dscp_value_(dscp) {
34 boost::system::error_code ec;
38 for (
unsigned int i = 0; i < dns_servers.size(); ++i) {
39 boost::asio::ip::address dns_address(
41 boost::asio::ip::udp::endpoint *ep =
42 new boost::asio::ip::udp::endpoint(dns_address, dns_servers[i].port_);
43 assert (ec.value() == 0);
47 boost::asio::ip::udp::endpoint local_ep(boost::asio::ip::address::
48 from_string(
"0.0.0.0", ec),
50 sock_.open(boost::asio::ip::udp::v4(), ec);
51 assert(ec.value() == 0);
52 sock_.bind(local_ep, ec);
53 if (ec.value() != 0) {
55 sock_.bind(local_ep, ec);
56 assert(ec.value() == 0);
70 int retval = setsockopt(
sock_.native_handle(), IPPROTO_IP, IP_TOS,
71 reinterpret_cast<const char *
>(&value),
sizeof(value));
73 DNS_BIND_TRACE(DnsBindError,
"Setting DSCP bits on socket failed for "
74 <<
dscp_value_ <<
" with errno " << strerror(errno));
85 unsigned int optlen =
sizeof(dscp);
86 int retval = getsockopt(
sock_.native_handle(), IPPROTO_IP, IP_TOS,
87 reinterpret_cast<char *
>(&dscp),
88 reinterpret_cast<socklen_t *
>(&optlen));
91 "with errno " << strerror(errno));
97 boost::system::error_code ec;
100 for (
unsigned int i = 0; i <
dns_ep_.size(); ++i) {
110 DNS_BIND_TRACE(DnsBindError,
"BindResolver doesnt support more than " <<
121 boost::system::error_code ec;
122 boost::asio::ip::udp::endpoint *ep =
new boost::asio::ip::udp::endpoint(
124 assert (ec.value() == 0);
130 if (dns_srv_index <
dns_ep_.size() &&
dns_ep_[dns_srv_index] && len > 0) {
132 boost::asio::buffer(pkt, len), *
dns_ep_[dns_srv_index],
134 boost::asio::placeholders::error,
135 boost::asio::placeholders::bytes_transferred, pkt));
138 DNS_BIND_TRACE(DnsBindError,
"Invalid server index: " << dns_srv_index
149 boost::asio::buffer(pkt, len),ep,
151 boost::asio::placeholders::error,
152 boost::asio::placeholders::bytes_transferred, pkt));
163 std::size_t length, uint8_t *pkt) {
165 DNS_BIND_TRACE(DnsBindError,
"Error sending packet to DNS server : " <<
166 boost::system::system_error(error).what() <<
";");
174 boost::asio::placeholders::error,
175 boost::asio::placeholders::bytes_transferred));
179 std::size_t length) {
188 boost::system::system_error(error).what() <<
";");
189 if (error.value() == boost::asio::error::operation_aborted) {
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
#define DNS_BIND_TRACE(obj, arg)
void DnsRcvHandler(const boost::system::error_code &error, std::size_t length)
void SetupResolver(const DnsServer &server, uint8_t idx)
BindResolver(boost::asio::io_context &io, const std::vector< DnsServer > &dns_servers, uint16_t client_port, Callback cb, uint8_t dscp)
bool DnsSend(uint8_t *pkt, unsigned int dns_srv_index, std::size_t len)
static const int max_pkt_size
boost::asio::ip::udp::socket sock_
void DnsSendHandler(const boost::system::error_code &error, std::size_t length, uint8_t *pkt)
boost::function< void(uint8_t *, std::size_t)> Callback
static void Init(boost::asio::io_context &io, const std::vector< DnsServer > &dns_servers, uint16_t client_port, Callback cb, uint8_t dscp)
static const uint8_t max_dns_servers
void SetDscpValue(uint8_t val)
static BindResolver * resolver_
void SetDscpSocketOption()
std::vector< boost::asio::ip::udp::endpoint * > dns_ep_