6 #include <boost/bind/bind.hpp>
11 using namespace boost::placeholders;
16 const std::vector<DnsServer> &dns_servers,
17 uint16_t client_port,
Callback cb, uint8_t dscp) {
18 assert(resolver_ == NULL);
19 resolver_ =
new BindResolver(
io, dns_servers, client_port, cb, dscp);
30 const std::vector<DnsServer> &dns_servers,
33 : pkt_buf_(NULL), cb_(cb), sock_(
io), dscp_value_(dscp) {
35 boost::system::error_code ec;
39 for (
unsigned int i = 0; i < dns_servers.size(); ++i) {
40 boost::asio::ip::address dns_address(
42 boost::asio::ip::udp::endpoint *ep =
43 new boost::asio::ip::udp::endpoint(dns_address, dns_servers[i].port_);
44 assert (ec.value() == 0);
48 boost::asio::ip::udp::endpoint local_ep(boost::asio::ip::address::
49 from_string(
"0.0.0.0", ec),
51 sock_.open(boost::asio::ip::udp::v4(), ec);
52 assert(ec.value() == 0);
53 sock_.bind(local_ep, ec);
54 if (ec.value() != 0) {
56 sock_.bind(local_ep, ec);
57 assert(ec.value() == 0);
71 int retval = setsockopt(
sock_.native_handle(), IPPROTO_IP, IP_TOS,
72 reinterpret_cast<const char *
>(&value),
sizeof(value));
74 DNS_BIND_TRACE(DnsBindError,
"Setting DSCP bits on socket failed for "
75 <<
dscp_value_ <<
" with errno " << strerror(errno));
86 unsigned int optlen =
sizeof(dscp);
87 int retval = getsockopt(
sock_.native_handle(), IPPROTO_IP, IP_TOS,
88 reinterpret_cast<char *
>(&dscp),
89 reinterpret_cast<socklen_t *
>(&optlen));
92 "with errno " << strerror(errno));
98 boost::system::error_code ec;
101 for (
unsigned int i = 0; i <
dns_ep_.size(); ++i) {
111 DNS_BIND_TRACE(DnsBindError,
"BindResolver doesnt support more than " <<
122 boost::system::error_code ec;
123 boost::asio::ip::udp::endpoint *ep =
new boost::asio::ip::udp::endpoint(
125 assert (ec.value() == 0);
131 if (dns_srv_index <
dns_ep_.size() &&
dns_ep_[dns_srv_index] && len > 0) {
133 boost::asio::buffer(pkt, len), *
dns_ep_[dns_srv_index],
135 boost::asio::placeholders::error,
136 boost::asio::placeholders::bytes_transferred, pkt));
139 DNS_BIND_TRACE(DnsBindError,
"Invalid server index: " << dns_srv_index
150 boost::asio::buffer(pkt, len),ep,
152 boost::asio::placeholders::error,
153 boost::asio::placeholders::bytes_transferred, pkt));
164 std::size_t length, uint8_t *pkt) {
166 DNS_BIND_TRACE(DnsBindError,
"Error sending packet to DNS server : " <<
167 boost::system::system_error(error).what() <<
";");
175 boost::asio::placeholders::error,
176 boost::asio::placeholders::bytes_transferred));
180 std::size_t length) {
189 boost::system::system_error(error).what() <<
";");
190 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_