blob: 90e19ed60edfab14bbf84041cbc8b129f432d300 [file] [log] [blame]
Ivan Yeodb0052d2015-02-08 17:27:04 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -06003 * Copyright (c) 2015-2016 Regents of the University of California
Ivan Yeodb0052d2015-02-08 17:27:04 -08004 *
5 * This file is part of NFD (Named Data Networking Forwarding Daemon) Android.
6 * See AUTHORS.md for complete list of NFD Android authors and contributors.
7 *
8 * NFD Android is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Android is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NFD Android, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nfd-wrapper.hpp"
Ivan Yeodb0052d2015-02-08 17:27:04 -080021
Alexander Afanasyev216df012015-02-10 17:35:46 -080022#include "daemon/nfd.hpp"
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -060023#include "rib/service.hpp"
Ivan Yeodb0052d2015-02-08 17:27:04 -080024
Alexander Afanasyev216df012015-02-10 17:35:46 -080025#include "core/global-io.hpp"
26#include "core/config-file.hpp"
27#include "core/logger.hpp"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080028#include "core/privilege-helper.hpp"
Alexander Afanasyev216df012015-02-10 17:35:46 -080029
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080030#include <stdlib.h>
Alexander Afanasyev216df012015-02-10 17:35:46 -080031#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080032#include <boost/thread.hpp>
33#include <mutex>
Alexander Afanasyev216df012015-02-10 17:35:46 -080034
35NFD_LOG_INIT("NfdWrapper");
36
37namespace nfd {
38
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080039
40// A little bit of cheating to make sure NFD can be properly restarted
41
42namespace scheduler {
43// defined in scheduler.cpp
44void
45resetGlobalScheduler();
46} // namespace scheduler
47
48void
49resetGlobalIoService();
50
51
Alexander Afanasyev216df012015-02-10 17:35:46 -080052class Runner
53{
54public:
55 Runner()
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080056 : m_io(nullptr)
Alexander Afanasyev216df012015-02-10 17:35:46 -080057 {
58 std::string initialConfig =
59 "general\n"
60 "{\n"
61 "}\n"
62 "\n"
63 "log\n"
64 "{\n"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080065 " default_level ALL\n"
66 " NameTree INFO\n"
67 " BestRouteStrategy2 INFO\n"
68 " InternalFace INFO\n"
69 " Forwarder INFO\n"
70 " ContentStore INFO\n"
71 " DeadNonceList INFO\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -080072 "}\n"
73 "tables\n"
74 "{\n"
75 " cs_max_packets 100\n"
76 "\n"
77 " strategy_choice\n"
78 " {\n"
79 " / /localhost/nfd/strategy/best-route\n"
Alexander Afanasyevf7b62362015-09-10 23:29:47 -070080 " /localhost /localhost/nfd/strategy/multicast\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -080081 " /localhost/nfd /localhost/nfd/strategy/best-route\n"
Alexander Afanasyevf7b62362015-09-10 23:29:47 -070082 " /ndn/broadcast /localhost/nfd/strategy/multicast\n"
83 " /ndn/multicast /localhost/nfd/strategy/multicast\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -080084 " }\n"
85 "}\n"
86 "\n"
87 "face_system\n"
88 "{\n"
89 " tcp\n"
90 " {\n"
Alexander Afanasyev77a30092016-01-21 19:46:58 -080091 " listen yes\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -080092 " port 6363\n"
Alexander Afanasyev77a30092016-01-21 19:46:58 -080093 " enable_v4 yes\n"
94 " enable_v6 yes\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -080095 " }\n"
96 "\n"
97 " udp\n"
98 " {\n"
99 " port 6363\n"
Alexander Afanasyev77a30092016-01-21 19:46:58 -0800100 " enable_v4 yes\n"
101 " enable_v6 yes\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800102 " idle_timeout 600\n"
103 " keep_alive_interval 25\n"
104 " mcast no\n"
105 " }\n"
Alexander Afanasyevedf1e2b2015-04-19 19:31:17 -0700106 " websocket\n"
107 " {\n"
108 " listen yes\n"
109 " port 9696\n"
110 " enable_v4 yes\n"
111 " enable_v6 yes\n"
112 " }\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800113 "}\n"
114 "\n"
115 "authorizations\n"
116 "{\n"
117 " authorize\n"
118 " {\n"
119 " certfile any\n"
120 " privileges\n"
121 " {\n"
122 " faces\n"
123 " fib\n"
124 " strategy-choice\n"
125 " }\n"
126 " }\n"
127 "}\n"
128 "\n"
129 "rib\n"
130 "{\n"
131 " localhost_security\n"
132 " {\n"
133 " trust-anchor\n"
134 " {\n"
135 " type any\n"
136 " }\n"
137 " }\n"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800138 "\n"
Alexander Afanasyev77a30092016-01-21 19:46:58 -0800139 " auto_prefix_propagate\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800140 " {\n"
141 " cost 15\n"
142 " timeout 10000\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800143 " refresh_interval 300\n"
Alexander Afanasyev77a30092016-01-21 19:46:58 -0800144 " base_retry_wait 50\n"
145 " max_retry_wait 3600\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800146 " }\n"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800147 "}\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800148 "\n";
149
150 std::istringstream input(initialConfig);
151 boost::property_tree::read_info(input, m_config);
152
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800153 std::unique_lock<std::mutex> lock(m_pointerMutex);
154 m_nfd.reset(new Nfd(m_config, m_keyChain));
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -0600155 m_nrd.reset(new rib::Service(m_config, m_keyChain));
Alexander Afanasyev216df012015-02-10 17:35:46 -0800156
157 m_nfd->initialize();
158 m_nrd->initialize();
159 }
160
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800161 ~Runner()
Alexander Afanasyev216df012015-02-10 17:35:46 -0800162 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800163 stop();
164 m_io->reset();
165 }
166
167 void
168 start()
169 {
170 {
171 std::unique_lock<std::mutex> lock(m_pointerMutex);
172 m_io = &getGlobalIoService();
173 }
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700174
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800175 m_io->run();
176 m_io->reset();
Alexander Afanasyev216df012015-02-10 17:35:46 -0800177 }
178
179 void
180 stop()
181 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800182 std::unique_lock<std::mutex> lock(m_pointerMutex);
183
184 m_io->post([this] {
185 m_io->stop();
186 this->m_nrd.reset();
187 this->m_nfd.reset();
188 });
Alexander Afanasyev216df012015-02-10 17:35:46 -0800189 }
190
191private:
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800192 std::mutex m_pointerMutex;
193 boost::asio::io_service* m_io;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800194 ndn::KeyChain m_keyChain;
195 unique_ptr<Nfd> m_nfd; // will use globalIoService
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -0600196 unique_ptr<rib::Service> m_nrd; // will use globalIoService
Alexander Afanasyev216df012015-02-10 17:35:46 -0800197
198 nfd::ConfigSection m_config;
199};
200
201static unique_ptr<Runner> g_runner;
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800202static boost::thread g_thread;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700203static std::map<std::string, std::string> g_params;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800204
205} // namespace nfd
Ivan Yeodb0052d2015-02-08 17:27:04 -0800206
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700207
208std::map<std::string, std::string>
209getParams(JNIEnv* env, jobject jParams)
210{
211 std::map<std::string, std::string> params;
212
213 jclass jcMap = env->GetObjectClass(jParams);
214 jclass jcSet = env->FindClass("java/util/Set");
215 jclass jcIterator = env->FindClass("java/util/Iterator");
216 jclass jcMapEntry = env->FindClass("java/util/Map$Entry");
217
218 jmethodID jcMapEntrySet = env->GetMethodID(jcMap, "entrySet", "()Ljava/util/Set;");
219 jmethodID jcSetIterator = env->GetMethodID(jcSet, "iterator", "()Ljava/util/Iterator;");
220 jmethodID jcIteratorHasNext = env->GetMethodID(jcIterator, "hasNext", "()Z");
221 jmethodID jcIteratorNext = env->GetMethodID(jcIterator, "next", "()Ljava/lang/Object;");
222 jmethodID jcMapEntryGetKey = env->GetMethodID(jcMapEntry, "getKey", "()Ljava/lang/Object;");
223 jmethodID jcMapEntryGetValue = env->GetMethodID(jcMapEntry, "getValue", "()Ljava/lang/Object;");
224
225 jobject jParamsEntrySet = env->CallObjectMethod(jParams, jcMapEntrySet);
226 jobject jParamsIterator = env->CallObjectMethod(jParamsEntrySet, jcSetIterator);
227 jboolean bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
228 while (bHasNext) {
229 jobject entry = env->CallObjectMethod(jParamsIterator, jcIteratorNext);
230
231 jstring jKey = (jstring)env->CallObjectMethod(entry, jcMapEntryGetKey);
232 jstring jValue = (jstring)env->CallObjectMethod(entry, jcMapEntryGetValue);
233
234 const char* cKey = env->GetStringUTFChars(jKey, nullptr);
235 const char* cValue = env->GetStringUTFChars(jValue, nullptr);
236
237 params.insert(std::make_pair(cKey, cValue));
238
239 env->ReleaseStringUTFChars(jKey, cKey);
240 env->ReleaseStringUTFChars(jValue, cValue);
241
242 bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
243 }
244
245 return params;
246}
247
248
Ivan Yeodb0052d2015-02-08 17:27:04 -0800249JNIEXPORT void JNICALL
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700250Java_net_named_1data_nfd_service_NfdService_startNfd(JNIEnv* env, jclass, jobject jParams)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800251{
Alexander Afanasyev216df012015-02-10 17:35:46 -0800252 if (nfd::g_runner.get() == nullptr) {
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700253 nfd::g_params = getParams(env, jParams);
254
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800255 // set/update HOME environment variable
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700256 ::setenv("HOME", nfd::g_params["homePath"].c_str(), true);
257 NFD_LOG_INFO("Use [" << nfd::g_params["homePath"] << "] as a security storage");
Ivan Yeodb0052d2015-02-08 17:27:04 -0800258
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800259 nfd::g_thread = boost::thread([] {
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700260 nfd::scheduler::resetGlobalScheduler();
261 nfd::resetGlobalIoService();
262
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800263 NFD_LOG_INFO("Starting NFD...");
264 try {
265 nfd::g_runner.reset(new nfd::Runner());
266 nfd::g_runner->start();
267 }
268 catch (const std::exception& e) {
269 NFD_LOG_FATAL(e.what());
270 }
271 catch (const nfd::PrivilegeHelper::Error& e) {
272 NFD_LOG_FATAL("PrivilegeHelper: " << e.what());
273 }
274 catch (...) {
275 NFD_LOG_FATAL("Unknown fatal error");
276 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800277
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800278 nfd::g_runner.reset();
279 nfd::scheduler::resetGlobalScheduler();
280 nfd::resetGlobalIoService();
281 NFD_LOG_INFO("NFD stopped");
282 });
283 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800284}
285
286JNIEXPORT void JNICALL
Ivan Yeo6296dce2015-02-10 23:29:43 -0800287Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800288{
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800289 if (nfd::g_runner.get() != nullptr) {
290 NFD_LOG_INFO("Stopping NFD...");
291 nfd::g_runner->stop();
292 // do not block anything
Alexander Afanasyev216df012015-02-10 17:35:46 -0800293 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800294}
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700295
296JNIEXPORT jobject JNICALL
297Java_net_named_1data_nfd_service_NfdService_getNfdLogModules(JNIEnv* env, jclass)
298{
299 jclass jcLinkedList = env->FindClass("java/util/LinkedList");
300 jmethodID jcLinkedListConstructor = env->GetMethodID(jcLinkedList, "<init>", "()V");
301 jmethodID jcLinkedListAdd = env->GetMethodID(jcLinkedList, "add", "(Ljava/lang/Object;)Z");
302
303 jobject jModules = env->NewObject(jcLinkedList, jcLinkedListConstructor);
304
305 for (const auto& module : nfd::LoggerFactory::getInstance().getModules()) {
306 jstring jModule = env->NewStringUTF(module.c_str());
307 env->CallBooleanMethod(jModules, jcLinkedListAdd, jModule);
308 }
309
310 return jModules;
311}