5 #include <boost/bind.hpp>
13 const std::vector<DnsServer> &dns_servers,
14 uint16_t client_port,
Callback cb, uint8_t dscp) {
27 const std::vector<DnsServer> &dns_servers,
30 : pkt_buf_(NULL), cb_(cb), sock_(io), dscp_value_(dscp) {
32 boost::system::error_code ec;
36 for (
unsigned int i = 0; i < dns_servers.size(); ++i) {
37 boost::asio::ip::address dns_address(
39 boost::asio::ip::udp::endpoint *ep =
40 new boost::asio::ip::udp::endpoint(dns_address, dns_servers[i].port_);
41 assert (ec.value() == 0);
45 boost::asio::ip::udp::endpoint local_ep(boost::asio::ip::address::
46 from_string(
"0.0.0.0", ec),
48 sock_.open(boost::asio::ip::udp::v4(), ec);
49 assert(ec.value() == 0);
50 sock_.bind(local_ep, ec);
51 if (ec.value() != 0) {
53 sock_.bind(local_ep, ec);
54 assert(ec.value() == 0);
68 int retval = setsockopt(
sock_.native_handle(), IPPROTO_IP, IP_TOS,
69 reinterpret_cast<const char *
>(&value),
sizeof(value));
71 DNS_BIND_TRACE(DnsBindError,
"Setting DSCP bits on socket failed for "
72 <<
dscp_value_ <<
" with errno " << strerror(errno));
83 unsigned int optlen =
sizeof(dscp);
84 int retval = getsockopt(
sock_.native_handle(), IPPROTO_IP, IP_TOS,
85 reinterpret_cast<char *
>(&dscp),
86 reinterpret_cast<socklen_t *>(&optlen));
89 "with errno " << strerror(errno));
95 boost::system::error_code ec;
98 for (
unsigned int i = 0; i <
dns_ep_.size(); ++i) {
108 DNS_BIND_TRACE(DnsBindError,
"BindResolver doesnt support more than " <<
119 boost::system::error_code ec;
120 boost::asio::ip::udp::endpoint *ep =
new boost::asio::ip::udp::endpoint(
122 assert (ec.value() == 0);
128 if (dns_srv_index <
dns_ep_.size() &&
dns_ep_[dns_srv_index] && len > 0) {
130 boost::asio::buffer(pkt, len), *
dns_ep_[dns_srv_index],
132 boost::asio::placeholders::error,
133 boost::asio::placeholders::bytes_transferred, pkt));
136 DNS_BIND_TRACE(DnsBindError,
"Invalid server index: " << dns_srv_index
147 boost::asio::buffer(pkt, len),ep,
149 boost::asio::placeholders::error,
150 boost::asio::placeholders::bytes_transferred, pkt));
161 std::size_t length, uint8_t *pkt) {
163 DNS_BIND_TRACE(DnsBindError,
"Error sending packet to DNS server : " <<
164 boost::system::system_error(error).what() <<
";");
172 boost::asio::placeholders::error,
173 boost::asio::placeholders::bytes_transferred));
177 std::size_t length) {
186 boost::system::system_error(error).what() <<
";");
187 if (error.value() == boost::asio::error::operation_aborted) {
#define DNS_BIND_TRACE(obj, arg)
boost::asio::ip::udp::socket sock_
void SetDscpSocketOption()
static const int max_pkt_size
BindResolver(boost::asio::io_context &io, const std::vector< DnsServer > &dns_servers, uint16_t client_port, Callback cb, uint8_t dscp)
void SetDscpValue(uint8_t val)
static const uint8_t max_dns_servers
static BindResolver * resolver_
bool DnsSend(uint8_t *pkt, unsigned int dns_srv_index, std::size_t len)
void DnsSendHandler(const boost::system::error_code &error, std::size_t length, uint8_t *pkt)
void SetupResolver(const DnsServer &server, uint8_t idx)
std::vector< boost::asio::ip::udp::endpoint * > dns_ep_
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
void DnsRcvHandler(const boost::system::error_code &error, std::size_t length)
static void Init(boost::asio::io_context &io, const std::vector< DnsServer > &dns_servers, uint16_t client_port, Callback cb, uint8_t dscp)
boost::function< void(uint8_t *, std::size_t)> Callback