dns-srv.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, 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 "dns-srv.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 
37 #include <iostream>
38 
39 namespace ndn {
40 namespace tools {
41 namespace autoconfig {
42 
43 union QueryAnswer
44 {
45  HEADER header;
46  uint8_t buf[NS_PACKETSZ];
47 };
48 
53 static std::string
54 parseSrvRr(const QueryAnswer& queryAnswer, int answerSize)
55 {
56  // The references of the next classes are:
57  // http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm
58 
59  struct rechdr
60  {
61  uint16_t type;
62  uint16_t iclass;
63  uint32_t ttl;
64  uint16_t length;
65  };
66 
67  struct srv_t
68  {
69  uint16_t priority;
70  uint16_t weight;
71  uint16_t port;
72  uint8_t* target;
73  };
74 
75  if (ntohs(queryAnswer.header.ancount) == 0) {
76  BOOST_THROW_EXCEPTION(DnsSrvError("SRV record cannot be parsed"));
77  }
78 
79  const uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
80 
81  blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
82 
83  char srvName[NS_MAXDNAME];
84  int serverNameSize = dn_expand(queryAnswer.buf, // message pointer
85  queryAnswer.buf + answerSize, // end of message
86  blob, // compressed server name
87  srvName, // expanded server name
88  NS_MAXDNAME);
89  if (serverNameSize <= 0) {
90  BOOST_THROW_EXCEPTION(DnsSrvError("SRV record cannot be parsed (error decoding domain name)"));
91  }
92 
93  const srv_t* server = reinterpret_cast<const srv_t*>(&blob[sizeof(rechdr)]);
94  uint16_t convertedPort = be16toh(server->port);
95 
96  blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ;
97 
98  char hostName[NS_MAXDNAME];
99  int hostNameSize = dn_expand(queryAnswer.buf, // message pointer
100  queryAnswer.buf + answerSize, // end of message
101  blob, // compressed host name
102  hostName, // expanded host name
103  NS_MAXDNAME);
104  if (hostNameSize <= 0) {
105  BOOST_THROW_EXCEPTION(DnsSrvError("SRV record cannot be parsed (error decoding host name)"));
106  }
107 
108  std::string uri = "udp://";
109  uri.append(hostName);
110  uri.append(":");
111  uri.append(to_string(convertedPort));
112 
113  return uri;
114 }
115 
116 std::string
117 querySrvRr(const std::string& fqdn)
118 {
119  std::string srvDomain = "_ndn._udp." + fqdn;
120  std::cerr << "Sending DNS query for SRV record for " << srvDomain << std::endl;
121 
122  res_init();
123 
124  _res.retrans = 1;
125  _res.retry = 2;
126  _res.ndots = 10;
127 
128  QueryAnswer queryAnswer;
129  int answerSize = res_query(srvDomain.data(),
130  ns_c_in,
131  ns_t_srv,
132  queryAnswer.buf,
133  sizeof(queryAnswer));
134  if (answerSize == 0) {
135  BOOST_THROW_EXCEPTION(DnsSrvError("No DNS SRV records found for " + srvDomain));
136  }
137  return parseSrvRr(queryAnswer, answerSize);
138 }
139 
143 std::string
145 {
146  std::cerr << "Sending DNS query for SRV record for _ndn._udp" << std::endl;
147 
148  QueryAnswer queryAnswer;
149 
150  res_init();
151 
152  _res.retrans = 1;
153  _res.retry = 2;
154  _res.ndots = 10;
155 
156  int answerSize = res_search("_ndn._udp",
157  ns_c_in,
158  ns_t_srv,
159  queryAnswer.buf,
160  sizeof(queryAnswer));
161 
162  if (answerSize == 0) {
163  BOOST_THROW_EXCEPTION(DnsSrvError("No DNS SRV records found for _ndn._udp"));
164  }
165 
166  return parseSrvRr(queryAnswer, answerSize);
167 }
168 
169 } // namespace autoconfig
170 } // namespace tools
171 } // namespace ndn
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
static std::string parseSrvRr(const QueryAnswer &queryAnswer, int answerSize)
Parse SRV record.
Definition: dns-srv.cpp:54
provide synchronous DNS SRV record querying
std::string querySrvRr(const std::string &fqdn)
Send DNS SRV request for fqdn.
Definition: dns-srv.cpp:117
std::string querySrvRrSearch()
Send DNS SRV request using search domain list.
Definition: dns-srv.cpp:144