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