blob: 3273b50b143da6726e553abfaa462fdfcf0a699a [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070024
25#include <ndn-cpp-dev/face.hpp>
26#include <ndn-cpp-dev/name.hpp>
27
28#include <ndn-cpp-dev/management/nfd-face-event-notification.hpp>
29#include <ndn-cpp-dev/management/nfd-controller.hpp>
30
31#include <boost/program_options/options_description.hpp>
32#include <boost/program_options/variables_map.hpp>
33#include <boost/program_options/parsers.hpp>
34
35#include "daemon/core/face-uri.hpp"
36#include "network.hpp"
37
38namespace po = boost::program_options;
39
40namespace nfd {
41
42using namespace ndn::nfd;
43using ndn::Face;
44
45class AutoregServer
46{
47public:
48 AutoregServer()
49 : m_controller(m_face)
50 // , m_lastNotification(<undefined>)
51 , m_cost(255)
52 {
53 }
54
55 void
56 onNfdCommandSuccess(const FaceEventNotification& notification)
57 {
58 std::cerr << "SUCCEED: " << notification << std::endl;
59 }
60
61 void
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070062 onNfdCommandFailure(const FaceEventNotification& notification,
63 uint32_t code, const std::string& reason)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070064 {
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070065 std::cerr << "FAILED: " << notification << " (code: " << code << ")" << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070066 }
67
68 bool
69 isFiltered(const FaceUri& uri)
70 {
71 const std::string& scheme = uri.getScheme();
72 if (!(scheme == "udp4" || scheme == "tcp4" ||
73 scheme == "udp6" || scheme == "tcp6"))
74 return true;
75
76 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost());
77
78 for (std::vector<Network>::const_iterator network = m_blackList.begin();
79 network != m_blackList.end();
80 ++network)
81 {
82 if (network->doesContain(address))
83 return true;
84 }
85
86 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
87 network != m_whiteList.end();
88 ++network)
89 {
90 if (network->doesContain(address))
91 return false;
92 }
93
94 return true;
95 }
96
97 void
98 processCreateFace(const FaceEventNotification& notification)
99 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700100 FaceUri uri(notification.getRemoteUri());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700101
102 if (isFiltered(uri))
103 {
104 std::cerr << "Not processing (filtered): " << notification << std::endl;
105 return;
106 }
107
108 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
109 prefix != m_autoregPrefixes.end();
110 ++prefix)
111 {
112 std::cout << "Try auto-register: " << *prefix << std::endl;
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700113
114 m_controller.start<FibAddNextHopCommand>(
115 ControlParameters()
116 .setName(*prefix)
117 .setFaceId(notification.getFaceId())
118 .setCost(m_cost),
119 bind(&AutoregServer::onNfdCommandSuccess, this, notification),
120 bind(&AutoregServer::onNfdCommandFailure, this, notification, _1, _2));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700121 }
122 }
123
124 void
125 onNotification(const Data& data)
126 {
127 m_lastNotification = data.getName().get(-1).toSegment();
128
129 // process
130 FaceEventNotification notification(data.getContent().blockFromValue());
131
Junxiao Shi6e694322014-04-03 10:27:13 -0700132 if (notification.getKind() == FACE_EVENT_CREATED &&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700133 !notification.isLocal() &&
134 notification.isOnDemand())
135 {
136 processCreateFace(notification);
137 }
138 else
139 {
140 std::cout << "IGNORED: " << notification << std::endl;
141 }
142
143 Name nextNotification("/localhost/nfd/faces/events");
144 nextNotification
145 .appendSegment(m_lastNotification + 1);
146
147 // no need to set freshness or child selectors
148 m_face.expressInterest(nextNotification,
149 bind(&AutoregServer::onNotification, this, _2),
150 bind(&AutoregServer::onTimeout, this, _1));
151 }
152
153 void
154 onTimeout(const Interest& timedOutInterest)
155 {
156 // re-express the timed out interest, but reset Nonce, since it has to change
157
158 // To be robust against missing notification, use ChildSelector and MustBeFresh
159 Interest interest("/localhost/nfd/faces/events");
160 interest
161 .setMustBeFresh(true)
162 .setChildSelector(1)
163 .setInterestLifetime(time::seconds(60))
164 ;
165
166 m_face.expressInterest(interest,
167 bind(&AutoregServer::onNotification, this, _2),
168 bind(&AutoregServer::onTimeout, this, _1));
169 }
170
171 void
172 signalHandler()
173 {
174 m_face.shutdown();
175 }
176
177
178
179 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700180 usage(std::ostream& os,
181 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700182 const char* programName)
183 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700184 os << "Usage:\n"
185 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
186 << "\n";
187 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700188 }
189
190 void
191 startProcessing()
192 {
193 std::cout << "AUTOREG prefixes: " << std::endl;
194 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
195 prefix != m_autoregPrefixes.end();
196 ++prefix)
197 {
198 std::cout << " " << *prefix << std::endl;
199 }
200
201 if (!m_blackList.empty())
202 {
203 std::cout << "Blacklisted networks: " << std::endl;
204 for (std::vector<Network>::const_iterator network = m_blackList.begin();
205 network != m_blackList.end();
206 ++network)
207 {
208 std::cout << " " << *network << std::endl;
209 }
210 }
211
212 std::cout << "Whitelisted networks: " << std::endl;
213 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
214 network != m_whiteList.end();
215 ++network)
216 {
217 std::cout << " " << *network << std::endl;
218 }
219
220 Interest interest("/localhost/nfd/faces/events");
221 interest
222 .setMustBeFresh(true)
223 .setChildSelector(1)
224 .setInterestLifetime(time::seconds(60))
225 ;
226
227 m_face.expressInterest(interest,
228 bind(&AutoregServer::onNotification, this, _2),
229 bind(&AutoregServer::onTimeout, this, _1));
230
231 boost::asio::signal_set signalSet(*m_face.ioService(), SIGINT, SIGTERM);
232 signalSet.async_wait(boost::bind(&AutoregServer::signalHandler, this));
233
234 m_face.processEvents();
235 }
236
237 int
238 main(int argc, char* argv[])
239 {
240 po::options_description optionDesciption;
241 optionDesciption.add_options()
242 ("help,h", "produce help message")
243 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
244 "prefix that should be automatically registered when new remote face is established")
245 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
246 "FIB cost which should be assigned to autoreg nexthops")
247 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
248 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
249 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
250 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
251 ;
252
253 po::variables_map options;
254 try
255 {
256 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
257 po::notify(options);
258 }
259 catch (std::exception& e)
260 {
261 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700262 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700263 return 1;
264 }
265
266 if (options.count("help"))
267 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700268 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700269 return 0;
270 }
271
272 if (m_autoregPrefixes.empty())
273 {
274 std::cerr << "ERROR: at least one --prefix must be specified" << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700275 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700276 return 2;
277 }
278
279 if (m_whiteList.empty())
280 {
281 // Allow everything
282 m_whiteList.push_back(Network::getMaxRangeV4());
283 m_whiteList.push_back(Network::getMaxRangeV6());
284 }
285
286 try
287 {
288 startProcessing();
289 }
290 catch (std::exception& e)
291 {
292 std::cerr << "ERROR: " << e.what() << std::endl;
293 return 2;
294 }
295
296 return 0;
297 }
298
299private:
300 Face m_face;
301 Controller m_controller;
302 uint64_t m_lastNotification;
303 std::vector<ndn::Name> m_autoregPrefixes;
304 uint64_t m_cost;
305 std::vector<Network> m_whiteList;
306 std::vector<Network> m_blackList;
307};
308
309} // namespace nfd
310
311int
312main(int argc, char* argv[])
313{
314 nfd::AutoregServer server;
315 return server.main(argc, argv);
316}