blob: b0c46c9c7f978e21056e0fda91533464f3735c2d [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shidda0b462014-06-30 19:42:29 -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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shidda0b462014-06-30 19:42:29 -070024 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070025
Alexander Afanasyev4a771362014-04-24 21:29:33 -070026#include <ndn-cxx/face.hpp>
27#include <ndn-cxx/name.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070028
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029#include <ndn-cxx/management/nfd-controller.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070030
31#include <boost/program_options/options_description.hpp>
32#include <boost/program_options/variables_map.hpp>
33#include <boost/program_options/parsers.hpp>
34
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070035#include "version.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070036#include "core/face-uri.hpp"
Junxiao Shi15b12e72014-08-09 19:56:24 -070037#include "core/face-monitor.hpp"
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070038#include "network.hpp"
39
40namespace po = boost::program_options;
41
42namespace nfd {
43
44using namespace ndn::nfd;
45using ndn::Face;
46
Junxiao Shidda0b462014-06-30 19:42:29 -070047class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070048{
49public:
50 AutoregServer()
51 : m_controller(m_face)
Junxiao Shi15b12e72014-08-09 19:56:24 -070052 , m_faceMonitor(m_face)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070053 , m_cost(255)
54 {
55 }
56
57 void
Junxiao Shidda0b462014-06-30 19:42:29 -070058 onRegisterCommandSuccess(const FaceEventNotification& notification, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070059 {
Junxiao Shidda0b462014-06-30 19:42:29 -070060 std::cerr << "SUCCEED: register " << prefix << " on face "
61 << notification.getFaceId() << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070062 }
63
64 void
Junxiao Shidda0b462014-06-30 19:42:29 -070065 onRegisterCommandFailure(const FaceEventNotification& notification, const Name& prefix,
66 uint32_t code, const std::string& reason)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070067 {
Junxiao Shidda0b462014-06-30 19:42:29 -070068 std::cerr << "FAILED: register " << prefix << " on face " << notification.getFaceId()
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +030069 << " (code: " << code << ", reason: " << reason << ")" << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070070 }
71
Junxiao Shidda0b462014-06-30 19:42:29 -070072 /**
73 * \return true if auto-register should not be performed for uri
74 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070075 bool
76 isFiltered(const FaceUri& uri)
77 {
78 const std::string& scheme = uri.getScheme();
79 if (!(scheme == "udp4" || scheme == "tcp4" ||
80 scheme == "udp6" || scheme == "tcp6"))
81 return true;
82
83 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost());
84
85 for (std::vector<Network>::const_iterator network = m_blackList.begin();
86 network != m_blackList.end();
87 ++network)
88 {
89 if (network->doesContain(address))
90 return true;
91 }
92
93 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
94 network != m_whiteList.end();
95 ++network)
96 {
97 if (network->doesContain(address))
98 return false;
99 }
100
101 return true;
102 }
103
104 void
105 processCreateFace(const FaceEventNotification& notification)
106 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700107 FaceUri uri(notification.getRemoteUri());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700108
109 if (isFiltered(uri))
110 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700111 std::cerr << "FILTERED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700112 return;
113 }
114
Junxiao Shidda0b462014-06-30 19:42:29 -0700115 std::cerr << "PROCESSING: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700116 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
117 prefix != m_autoregPrefixes.end();
118 ++prefix)
119 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700120 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700121 ControlParameters()
122 .setName(*prefix)
123 .setFaceId(notification.getFaceId())
Junxiao Shidda0b462014-06-30 19:42:29 -0700124 .setOrigin(ROUTE_ORIGIN_AUTOREG)
125 .setCost(m_cost)
126 .setExpirationPeriod(time::milliseconds::max()),
127 bind(&AutoregServer::onRegisterCommandSuccess, this, notification, *prefix),
128 bind(&AutoregServer::onRegisterCommandFailure, this, notification, *prefix, _1, _2));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700129 }
130 }
131
132 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700133 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700134 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700135 if (notification.getKind() == FACE_EVENT_CREATED &&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700136 !notification.isLocal() &&
137 notification.isOnDemand())
138 {
139 processCreateFace(notification);
140 }
141 else
142 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700143 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700144 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700145 }
146
147 void
148 signalHandler()
149 {
150 m_face.shutdown();
151 }
152
153
154
155 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700156 usage(std::ostream& os,
157 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700158 const char* programName)
159 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700160 os << "Usage:\n"
161 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
162 << "\n";
163 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700164 }
165
166 void
167 startProcessing()
168 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700169 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700170 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
171 prefix != m_autoregPrefixes.end();
172 ++prefix)
173 {
174 std::cout << " " << *prefix << std::endl;
175 }
176
177 if (!m_blackList.empty())
178 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700179 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700180 for (std::vector<Network>::const_iterator network = m_blackList.begin();
181 network != m_blackList.end();
182 ++network)
183 {
184 std::cout << " " << *network << std::endl;
185 }
186 }
187
Junxiao Shidda0b462014-06-30 19:42:29 -0700188 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700189 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
190 network != m_whiteList.end();
191 ++network)
192 {
193 std::cout << " " << *network << std::endl;
194 }
195
Junxiao Shi15b12e72014-08-09 19:56:24 -0700196 m_faceMonitor.onNotification += bind(&AutoregServer::onNotification, this, _1);
197 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700198
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700199 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700200 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700201
202 m_face.processEvents();
203 }
204
205 int
206 main(int argc, char* argv[])
207 {
208 po::options_description optionDesciption;
209 optionDesciption.add_options()
210 ("help,h", "produce help message")
211 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
212 "prefix that should be automatically registered when new remote face is established")
213 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
214 "FIB cost which should be assigned to autoreg nexthops")
215 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
216 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
217 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
218 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700219 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700220 ;
221
222 po::variables_map options;
223 try
224 {
225 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
226 po::notify(options);
227 }
228 catch (std::exception& e)
229 {
230 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700231 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700232 return 1;
233 }
234
235 if (options.count("help"))
236 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700237 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700238 return 0;
239 }
240
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700241 if (options.count("version"))
242 {
243 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
244 return 0;
245 }
246
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700247 if (m_autoregPrefixes.empty())
248 {
249 std::cerr << "ERROR: at least one --prefix must be specified" << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700250 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700251 return 2;
252 }
253
254 if (m_whiteList.empty())
255 {
256 // Allow everything
257 m_whiteList.push_back(Network::getMaxRangeV4());
258 m_whiteList.push_back(Network::getMaxRangeV6());
259 }
260
261 try
262 {
263 startProcessing();
264 }
265 catch (std::exception& e)
266 {
267 std::cerr << "ERROR: " << e.what() << std::endl;
268 return 2;
269 }
270
271 return 0;
272 }
273
274private:
275 Face m_face;
276 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700277 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700278 std::vector<ndn::Name> m_autoregPrefixes;
279 uint64_t m_cost;
280 std::vector<Network> m_whiteList;
281 std::vector<Network> m_blackList;
282};
283
284} // namespace nfd
285
286int
287main(int argc, char* argv[])
288{
289 nfd::AutoregServer server;
290 return server.main(argc, argv);
291}