blob: 99770833417cf342dd73fc1e39f9af0eedd1e948 [file] [log] [blame]
Ivan Yeodb0052d2015-02-08 17:27:04 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev63e81f92019-02-05 13:30:26 -05003 * Copyright (c) 2015-2019 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/config-file.hpp"
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040026#include "core/global-io.hpp"
Alexander Afanasyev216df012015-02-10 17:35:46 -080027#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
30#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080031#include <boost/thread.hpp>
32#include <mutex>
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040033#include <ndn-cxx/util/logging.hpp>
34#include <stdlib.h>
Alexander Afanasyev216df012015-02-10 17:35:46 -080035
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040036NFD_LOG_INIT(NfdWrapper);
Alexander Afanasyev216df012015-02-10 17:35:46 -080037
38namespace nfd {
39
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080040
41// A little bit of cheating to make sure NFD can be properly restarted
42
43namespace scheduler {
44// defined in scheduler.cpp
45void
46resetGlobalScheduler();
47} // namespace scheduler
48
49void
50resetGlobalIoService();
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 {
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040058 std::string initialConfig = R"CONF(
59 log
60 {
61 default_level ALL
62 }
63 tables
64 {
65 cs_max_packets 100
66 strategy_choice
67 {
68 / /localhost/nfd/strategy/best-route
69 /localhost /localhost/nfd/strategy/multicast
70 /localhost/nfd /localhost/nfd/strategy/best-route
71 /ndn/broadcast /localhost/nfd/strategy/multicast
72 /ndn/multicast /localhost/nfd/strategy/multicast
Alexander Afanasyev63e81f92019-02-05 13:30:26 -050073 /sl /localhost/nfd/strategy/self-learning
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040074 }
75 }
76 face_system
77 {
78 tcp
79 udp
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040080 websocket
81 }
82 authorizations
83 {
84 authorize
85 {
86 certfile any
87 privileges
88 {
89 faces
90 fib
91 cs
92 strategy-choice
93 }
94 }
95 }
96 rib
97 {
98 localhost_security
99 {
100 trust-anchor
101 {
102 type any
103 }
104 }
Alexander Afanasyev449a4312019-02-14 10:27:37 -0500105 localhop_security
106 {
107 trust-anchor
108 {
109 type any
110 }
111 }
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -0400112 auto_prefix_propagate
113 {
114 refresh_interval 300
115 }
116 }
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500117 )CONF";
Alexander Afanasyev216df012015-02-10 17:35:46 -0800118
119 std::istringstream input(initialConfig);
120 boost::property_tree::read_info(input, m_config);
121
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800122 m_nfd.reset(new Nfd(m_config, m_keyChain));
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500123 m_ribService.reset(new rib::Service(m_config, m_keyChain));
Alexander Afanasyev216df012015-02-10 17:35:46 -0800124
125 m_nfd->initialize();
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800126 }
127
128 void
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500129 run()
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800130 {
131 {
132 std::unique_lock<std::mutex> lock(m_pointerMutex);
133 m_io = &getGlobalIoService();
134 }
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700135
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500136 setMainIoService(m_io);
137 setRibIoService(m_io);
138
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800139 m_io->run();
140 m_io->reset();
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500141
142 m_ribService.reset();
143 m_nfd.reset();
144
145 m_io = nullptr;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800146 }
147
148 void
149 stop()
150 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800151 std::unique_lock<std::mutex> lock(m_pointerMutex);
152
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500153 if (m_io != nullptr) {
154 m_io->stop();
155 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800156 }
157
158private:
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800159 std::mutex m_pointerMutex;
160 boost::asio::io_service* m_io;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800161 ndn::KeyChain m_keyChain;
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500162 unique_ptr<Nfd> m_nfd;
163 unique_ptr<rib::Service> m_ribService;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800164
165 nfd::ConfigSection m_config;
166};
167
168static unique_ptr<Runner> g_runner;
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800169static boost::thread g_thread;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700170static std::map<std::string, std::string> g_params;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800171
172} // namespace nfd
Ivan Yeodb0052d2015-02-08 17:27:04 -0800173
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700174
175std::map<std::string, std::string>
176getParams(JNIEnv* env, jobject jParams)
177{
178 std::map<std::string, std::string> params;
179
180 jclass jcMap = env->GetObjectClass(jParams);
181 jclass jcSet = env->FindClass("java/util/Set");
182 jclass jcIterator = env->FindClass("java/util/Iterator");
183 jclass jcMapEntry = env->FindClass("java/util/Map$Entry");
184
185 jmethodID jcMapEntrySet = env->GetMethodID(jcMap, "entrySet", "()Ljava/util/Set;");
186 jmethodID jcSetIterator = env->GetMethodID(jcSet, "iterator", "()Ljava/util/Iterator;");
187 jmethodID jcIteratorHasNext = env->GetMethodID(jcIterator, "hasNext", "()Z");
188 jmethodID jcIteratorNext = env->GetMethodID(jcIterator, "next", "()Ljava/lang/Object;");
189 jmethodID jcMapEntryGetKey = env->GetMethodID(jcMapEntry, "getKey", "()Ljava/lang/Object;");
190 jmethodID jcMapEntryGetValue = env->GetMethodID(jcMapEntry, "getValue", "()Ljava/lang/Object;");
191
192 jobject jParamsEntrySet = env->CallObjectMethod(jParams, jcMapEntrySet);
193 jobject jParamsIterator = env->CallObjectMethod(jParamsEntrySet, jcSetIterator);
194 jboolean bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
195 while (bHasNext) {
196 jobject entry = env->CallObjectMethod(jParamsIterator, jcIteratorNext);
197
198 jstring jKey = (jstring)env->CallObjectMethod(entry, jcMapEntryGetKey);
199 jstring jValue = (jstring)env->CallObjectMethod(entry, jcMapEntryGetValue);
200
201 const char* cKey = env->GetStringUTFChars(jKey, nullptr);
202 const char* cValue = env->GetStringUTFChars(jValue, nullptr);
203
204 params.insert(std::make_pair(cKey, cValue));
205
206 env->ReleaseStringUTFChars(jKey, cKey);
207 env->ReleaseStringUTFChars(jValue, cValue);
208
209 bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
210 }
211
212 return params;
213}
214
215
Ivan Yeodb0052d2015-02-08 17:27:04 -0800216JNIEXPORT void JNICALL
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700217Java_net_named_1data_nfd_service_NfdService_startNfd(JNIEnv* env, jclass, jobject jParams)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800218{
Alexander Afanasyev216df012015-02-10 17:35:46 -0800219 if (nfd::g_runner.get() == nullptr) {
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700220 nfd::g_params = getParams(env, jParams);
221
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800222 // set/update HOME environment variable
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700223 ::setenv("HOME", nfd::g_params["homePath"].c_str(), true);
224 NFD_LOG_INFO("Use [" << nfd::g_params["homePath"] << "] as a security storage");
Ivan Yeodb0052d2015-02-08 17:27:04 -0800225
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800226 nfd::g_thread = boost::thread([] {
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700227 nfd::scheduler::resetGlobalScheduler();
228 nfd::resetGlobalIoService();
229
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800230 NFD_LOG_INFO("Starting NFD...");
231 try {
232 nfd::g_runner.reset(new nfd::Runner());
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500233 nfd::g_runner->run();
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800234 }
235 catch (const std::exception& e) {
236 NFD_LOG_FATAL(e.what());
237 }
238 catch (const nfd::PrivilegeHelper::Error& e) {
239 NFD_LOG_FATAL("PrivilegeHelper: " << e.what());
240 }
241 catch (...) {
242 NFD_LOG_FATAL("Unknown fatal error");
243 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800244
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800245 nfd::g_runner.reset();
246 nfd::scheduler::resetGlobalScheduler();
247 nfd::resetGlobalIoService();
248 NFD_LOG_INFO("NFD stopped");
249 });
250 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800251}
252
253JNIEXPORT void JNICALL
Ivan Yeo6296dce2015-02-10 23:29:43 -0800254Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800255{
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800256 if (nfd::g_runner.get() != nullptr) {
257 NFD_LOG_INFO("Stopping NFD...");
258 nfd::g_runner->stop();
259 // do not block anything
Alexander Afanasyev216df012015-02-10 17:35:46 -0800260 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800261}
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700262
Haitao Zhang0c79a9c2016-11-21 00:38:51 -0800263JNIEXPORT jboolean JNICALL
264Java_net_named_1data_nfd_service_NfdService_isNfdRunning(JNIEnv*, jclass)
265{
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500266 return nfd::g_runner.get() != nullptr;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700267}