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 vnsw_agent_dns_handler_hpp 7 : #define vnsw_agent_dns_handler_hpp 8 : 9 : #include <mutex> 10 : 11 : #include "pkt/proto_handler.h" 12 : #include "vnc_cfg_types.h" 13 : #include "bind/bind_util.h" 14 : #include "bind/bind_resolver.h" 15 : 16 : #define DEFAULT_DNS_TTL 120 17 : 18 : typedef boost::asio::ip::udp boost_udp; 19 : 20 : class AgentDnsXmppChannel; 21 : class VmInterface; 22 : class Timer; 23 : 24 : class DnsHandler : public ProtoHandler { 25 : public: 26 : typedef boost::function<void(const boost::system::error_code&, 27 : boost_udp::resolver::iterator)> ResolveHandler; 28 : typedef std::vector<boost_udp::resolver *> ResolvList; 29 : static const uint32_t max_items_per_xmpp_msg = 20; 30 : 31 : struct QueryKey { 32 0 : QueryKey(const Interface *i, uint16_t x) : itf(i), xid(x) {} 33 0 : bool operator<(const QueryKey &rhs) const { 34 0 : if (itf != rhs.itf) 35 0 : return itf < rhs.itf; 36 0 : return xid < rhs.xid; 37 : } 38 : 39 : const Interface *itf; 40 : uint16_t xid; 41 : }; 42 : 43 : enum Action { 44 : NONE, 45 : DNS_QUERY, 46 : DNS_UPDATE 47 : }; 48 : 49 : DnsHandler(Agent *agent, boost::shared_ptr<PktInfo> info, 50 : boost::asio::io_context &io); 51 : virtual ~DnsHandler(); 52 : bool Run(); 53 : bool TimerExpiry(uint16_t xid); 54 : void DefaultDnsResolveHandler(const boost::system::error_code &error, 55 : boost_udp::resolver::iterator it, 56 : DnsItems::iterator item); 57 : 58 : private: 59 : friend class DnsTest; 60 : 61 : bool HandleRequest(); 62 : bool HandleDefaultDnsRequest(const VmInterface *vmitf); 63 : void DefaultDnsSendResponse(); 64 : bool HandleVirtualDnsRequest(const VmInterface *vmitf); 65 : bool ResolveLinkLocalRequest(DnsItems::iterator &item, 66 : DnsItems *linklocal_items) const; 67 : bool ResolveAllLinkLocalRequests(); 68 : void BuildDnsResolvers(); 69 : void BuildDefaultDnsResolvers(); 70 : bool HandleMessage(); 71 : bool HandleDefaultDnsResponse(); 72 : bool HandleBindResponse(); 73 : bool HandleUpdateResponse(); 74 : bool HandleRetryExpiry(); 75 : bool HandleUpdate(); 76 : bool HandleModifyVdns(); 77 : bool UpdateAll(); 78 : void SendXmppUpdate(AgentDnsXmppChannel *channel, DnsUpdateData *xmpp_data); 79 : void ParseQuery(); 80 : void Resolve(dns_flags flags, const DnsItems &ques, DnsItems &ans, 81 : DnsItems &auth, DnsItems &add); 82 : void SendDnsResponse(); 83 : void UpdateQueryNames(); 84 : void UpdateOffsets(DnsItem &item, bool name_update_required); 85 : void UpdateGWAddress(DnsItem &item); 86 : void Update(InterTaskMsg *msg); 87 : void DelUpdate(InterTaskMsg *msg); 88 : void UpdateStats(); 89 : void GetDomainName(const VmInterface *vm_itf, std::string *domain_name) const; 90 : bool GetDomainNameFromDhcp(std::vector<autogen::DhcpOptionType> &options, 91 : std::string *domain_name) const; 92 : void GetBaseName(const std::string &name, std::string *base) const; 93 : std::string DnsItemsToString(DnsItems &items) const; 94 : void HandleInvalidBindResponse(DnsHandler *handler, dns_flags flags, 95 : const DnsItems &ques, DnsItems &ans, 96 : DnsItems &auth, DnsItems &add, uint16_t xid); 97 : bool NeedRetryForNextServer(uint16_t code); 98 : bool SendToDefaultServer(); 99 0 : bool DefaultMethodInUse() { return default_method_; } 100 0 : uint8_t curr_index() { return curr_index_; } 101 0 : uint8_t last_index() { return def_dns_resolvers_.size() - 1; } 102 0 : void IncrCurrIndex() { curr_index_++; } 103 : 104 : dnshdr *dns_; 105 : uint8_t *resp_ptr_; 106 : uint16_t dns_resp_size_; 107 : uint16_t xid_; 108 : Action action_; 109 : QueryKey *rkey_; 110 : struct DnsResolverInfo { 111 : boost::asio::ip::udp::endpoint ep_; 112 : uint32_t retries_; 113 : Timer *timer_; 114 : }; 115 : std::vector<DnsResolverInfo *> dns_resolvers_; 116 : std::vector<DnsResolverInfo *> def_dns_resolvers_; 117 : std::string ipam_name_; 118 : std::string domain_name_; 119 : autogen::IpamType ipam_type_; 120 : autogen::VirtualDnsType vdns_type_; 121 : DnsItems items_; 122 : DnsItems linklocal_items_; 123 : DnsNameEncoder name_encoder_; 124 : bool query_name_update_; 125 : uint16_t query_name_update_len_; // num bytes added in the query section 126 : uint16_t pend_req_; 127 : ResolvList resolv_list_; 128 : std::mutex mutex_; 129 : bool default_method_; 130 : uint8_t curr_index_; 131 : bool SendDnsQuery(DnsResolverInfo *resolver, uint16_t xid); 132 : 133 : DISALLOW_COPY_AND_ASSIGN(DnsHandler); 134 : }; 135 : 136 : #endif // vnsw_agent_dns_handler_hpp