Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef __bind_resolver_h__ 6 : #define __bind_resolver_h__ 7 : 8 : #include <iostream> 9 : #include <stdint.h> 10 : #include <vector> 11 : #include <boost/asio.hpp> 12 : #include <boost/function.hpp> 13 : 14 : class BindResolver { 15 : public: 16 : typedef boost::function<void(uint8_t *, std::size_t)> Callback; 17 : static const int max_pkt_size = 1024; 18 : static const uint8_t max_dns_servers = 2; 19 : 20 : struct DnsServer { 21 16 : DnsServer (const std::string &ip, uint16_t port) 22 16 : : ip_(ip), port_(port) {} 23 : 24 : std::string ip_; 25 : uint16_t port_; 26 : }; 27 : 28 : BindResolver(boost::asio::io_context &io, 29 : const std::vector<DnsServer> &dns_servers, 30 : uint16_t client_port, Callback cb, uint8_t dscp); 31 : virtual ~BindResolver(); 32 : void SetupResolver(const DnsServer &server, uint8_t idx); 33 : bool DnsSend(uint8_t *pkt, unsigned int dns_srv_index, std::size_t len); 34 : bool DnsSend(uint8_t *pkt, boost::asio::ip::udp::endpoint ep, 35 : std::size_t len); 36 : void SetDscpValue(uint8_t val); 37 : uint8_t GetDscpValue(); 38 : 39 : static void Init(boost::asio::io_context &io, 40 : const std::vector<DnsServer> &dns_servers, 41 : uint16_t client_port, Callback cb, uint8_t dscp); 42 : static void Shutdown(); 43 0 : static BindResolver *Resolver() { return resolver_; } 44 : 45 : private: 46 : void SetDscpSocketOption(); 47 : void AsyncRead(); 48 : void DnsSendHandler(const boost::system::error_code &error, 49 : std::size_t length, uint8_t *pkt); 50 : void DnsRcvHandler(const boost::system::error_code &error, 51 : std::size_t length); 52 : 53 : uint8_t *pkt_buf_; 54 : Callback cb_; 55 : boost::asio::ip::udp::socket sock_; 56 : std::vector<boost::asio::ip::udp::endpoint *> dns_ep_; 57 : uint8_t dscp_value_; 58 : static BindResolver *resolver_; 59 : 60 : DISALLOW_COPY_AND_ASSIGN(BindResolver); 61 : }; 62 : 63 : #endif // __bind_resolver_h__