blob: 23e87826b3e50ed7dcd6dcb08054b81dd7f0bffd [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
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
26#include "base-dns.hpp"
27
28#include <sys/types.h>
29#include <netinet/in.h>
30#include <resolv.h>
31#include <arpa/nameser.h>
32
33#ifdef __APPLE__
34#include <arpa/nameser_compat.h>
35#endif
36
37namespace ndn {
38namespace tools {
39namespace autoconfig {
40
41union BaseDns::QueryAnswer
42{
43 HEADER header;
44 uint8_t buf[NS_PACKETSZ];
45};
46
47BaseDns::BaseDns(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure)
48 : Base(face, keyChain, nextStageOnFailure)
49{
50}
51
52std::string
53BaseDns::querySrvRr(const std::string& fqdn)
54{
55 std::string srvDomain = "_ndn._udp." + fqdn;
56 std::cerr << "Sending DNS query for SRV record for " << srvDomain << std::endl;
57
58 QueryAnswer queryAnswer;
59 int answerSize = res_query(srvDomain.c_str(),
60 ns_c_in,
61 ns_t_srv,
62 queryAnswer.buf,
63 sizeof(queryAnswer));
64 if (answerSize == 0) {
65 throw Error("No DNS SRV records found for " + srvDomain);
66 }
67 return parseSrvRr(queryAnswer, answerSize);
68}
69
70/**
71 * @brief Send DNS SRV request using search domain list
72 */
73std::string
74BaseDns::querySrvRrSearch()
75{
76 std::cerr << "Sending DNS query for SRV record for _ndn._udp" << std::endl;
77
78 QueryAnswer queryAnswer;
79
80 _res.retry = 2;
81 _res.ndots = 10;
82
83 int answerSize = res_search("_ndn._udp",
84 ns_c_in,
85 ns_t_srv,
86 queryAnswer.buf,
87 sizeof(queryAnswer));
88
89 if (answerSize == 0) {
90 throw Error("No DNS SRV records found for _ndn._udp");
91 }
92
93 return parseSrvRr(queryAnswer, answerSize);
94}
95
96std::string
97BaseDns::parseSrvRr(const QueryAnswer& queryAnswer, int answerSize)
98{
99 // The references of the next classes are:
100 // http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm
101
102 struct rechdr
103 {
104 uint16_t type;
105 uint16_t iclass;
106 uint32_t ttl;
107 uint16_t length;
108 };
109
110 struct srv_t
111 {
112 uint16_t priority;
113 uint16_t weight;
114 uint16_t port;
115 uint8_t* target;
116 };
117
118 if (ntohs(queryAnswer.header.ancount) == 0) {
119 throw Error("SRV record cannot be parsed");
120 }
121
122 const uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
123
124 blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
125
126 char srvName[NS_MAXDNAME];
127 int serverNameSize = dn_expand(queryAnswer.buf, // message pointer
128 queryAnswer.buf + answerSize, // end of message
129 blob, // compressed server name
130 srvName, // expanded server name
131 NS_MAXDNAME);
132 if (serverNameSize <= 0) {
133 throw Error("SRV record cannot be parsed (error decoding domain name)");
134 }
135
136 const srv_t* server = reinterpret_cast<const srv_t*>(&blob[sizeof(rechdr)]);
137 uint16_t convertedPort = be16toh(server->port);
138
139 blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ;
140
141 char hostName[NS_MAXDNAME];
142 int hostNameSize = dn_expand(queryAnswer.buf, // message pointer
143 queryAnswer.buf + answerSize, // end of message
144 blob, // compressed host name
145 hostName, // expanded host name
146 NS_MAXDNAME);
147 if (hostNameSize <= 0) {
148 throw Error("SRV record cannot be parsed (error decoding host name)");
149 }
150
151 std::string uri = "udp://";
152 uri.append(hostName);
153 uri.append(":");
154 uri.append(std::to_string(convertedPort));
155
156 return uri;
157}
158
159} // namespace autoconfig
160} // namespace tools
161} // namespace ndn