Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 26 | #include "dns-srv.hpp" |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 27 | |
| 28 | #include <sys/types.h> |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 29 | #include <arpa/nameser.h> |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 30 | #include <netinet/in.h> |
| 31 | #include <resolv.h> |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 32 | |
| 33 | #ifdef __APPLE__ |
| 34 | #include <arpa/nameser_compat.h> |
| 35 | #endif |
| 36 | |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 37 | #include <iostream> |
| 38 | |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 39 | #include <boost/endian/conversion.hpp> |
| 40 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 41 | namespace ndn { |
| 42 | namespace tools { |
| 43 | namespace autoconfig { |
| 44 | |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 45 | using namespace std::string_literals; |
| 46 | |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 47 | union QueryAnswer |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 48 | { |
| 49 | HEADER header; |
| 50 | uint8_t buf[NS_PACKETSZ]; |
| 51 | }; |
| 52 | |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 53 | /** \brief Parse SRV record |
| 54 | * \return FaceUri of the hub from the SRV record |
| 55 | * \throw DnsSrvError if SRV record cannot be parsed |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 56 | */ |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 57 | static std::string |
| 58 | parseSrvRr(const QueryAnswer& queryAnswer, int answerSize) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 59 | { |
| 60 | // The references of the next classes are: |
| 61 | // http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm |
| 62 | |
| 63 | struct rechdr |
| 64 | { |
| 65 | uint16_t type; |
| 66 | uint16_t iclass; |
| 67 | uint32_t ttl; |
| 68 | uint16_t length; |
| 69 | }; |
| 70 | |
| 71 | struct srv_t |
| 72 | { |
| 73 | uint16_t priority; |
| 74 | uint16_t weight; |
| 75 | uint16_t port; |
| 76 | uint8_t* target; |
| 77 | }; |
| 78 | |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 79 | uint16_t ancount = queryAnswer.header.ancount; |
| 80 | if (boost::endian::big_to_native(ancount) == 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 81 | NDN_THROW(DnsSrvError("SRV record cannot be parsed")); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | const uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 85 | blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ; |
| 86 | |
| 87 | char srvName[NS_MAXDNAME]; |
| 88 | int serverNameSize = dn_expand(queryAnswer.buf, // message pointer |
| 89 | queryAnswer.buf + answerSize, // end of message |
| 90 | blob, // compressed server name |
| 91 | srvName, // expanded server name |
| 92 | NS_MAXDNAME); |
| 93 | if (serverNameSize <= 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 94 | NDN_THROW(DnsSrvError("SRV record cannot be parsed (error decoding domain name)")); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | const srv_t* server = reinterpret_cast<const srv_t*>(&blob[sizeof(rechdr)]); |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 98 | uint16_t port = boost::endian::big_to_native(server->port); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 99 | |
| 100 | blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ; |
| 101 | |
| 102 | char hostName[NS_MAXDNAME]; |
| 103 | int hostNameSize = dn_expand(queryAnswer.buf, // message pointer |
| 104 | queryAnswer.buf + answerSize, // end of message |
| 105 | blob, // compressed host name |
| 106 | hostName, // expanded host name |
| 107 | NS_MAXDNAME); |
| 108 | if (hostNameSize <= 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 109 | NDN_THROW(DnsSrvError("SRV record cannot be parsed (error decoding host name)")); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame] | 112 | return "udp://"s + hostName + ":" + to_string(port); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 115 | std::string |
| 116 | querySrvRr(const std::string& fqdn) |
| 117 | { |
| 118 | std::string srvDomain = "_ndn._udp." + fqdn; |
| 119 | std::cerr << "Sending DNS query for SRV record for " << srvDomain << std::endl; |
| 120 | |
| 121 | res_init(); |
| 122 | |
| 123 | _res.retrans = 1; |
| 124 | _res.retry = 2; |
| 125 | _res.ndots = 10; |
| 126 | |
| 127 | QueryAnswer queryAnswer; |
| 128 | int answerSize = res_query(srvDomain.data(), |
| 129 | ns_c_in, |
| 130 | ns_t_srv, |
| 131 | queryAnswer.buf, |
| 132 | sizeof(queryAnswer)); |
| 133 | if (answerSize == 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 134 | NDN_THROW(DnsSrvError("No DNS SRV records found for " + srvDomain)); |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 135 | } |
| 136 | return parseSrvRr(queryAnswer, answerSize); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @brief Send DNS SRV request using search domain list |
| 141 | */ |
| 142 | std::string |
| 143 | querySrvRrSearch() |
| 144 | { |
| 145 | std::cerr << "Sending DNS query for SRV record for _ndn._udp" << std::endl; |
| 146 | |
| 147 | QueryAnswer queryAnswer; |
| 148 | |
| 149 | res_init(); |
| 150 | |
| 151 | _res.retrans = 1; |
| 152 | _res.retry = 2; |
| 153 | _res.ndots = 10; |
| 154 | |
| 155 | int answerSize = res_search("_ndn._udp", |
| 156 | ns_c_in, |
| 157 | ns_t_srv, |
| 158 | queryAnswer.buf, |
| 159 | sizeof(queryAnswer)); |
| 160 | |
| 161 | if (answerSize == 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 162 | NDN_THROW(DnsSrvError("No DNS SRV records found for _ndn._udp")); |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return parseSrvRr(queryAnswer, answerSize); |
| 166 | } |
| 167 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 168 | } // namespace autoconfig |
| 169 | } // namespace tools |
| 170 | } // namespace ndn |