blob: 64c16f146127f73e92e583aaa70b1b731eb53dee [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
80 {
81 mcast no
82 }
83 websocket
84 }
85 authorizations
86 {
87 authorize
88 {
89 certfile any
90 privileges
91 {
92 faces
93 fib
94 cs
95 strategy-choice
96 }
97 }
98 }
99 rib
100 {
101 localhost_security
102 {
103 trust-anchor
104 {
105 type any
106 }
107 }
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500108 ; localhop_security
109 ; {
110 ; trust-anchor
111 ; {
112 ; type any
113 ; }
114 ; }
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -0400115 auto_prefix_propagate
116 {
117 refresh_interval 300
118 }
119 }
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500120 )CONF";
Alexander Afanasyev216df012015-02-10 17:35:46 -0800121
122 std::istringstream input(initialConfig);
123 boost::property_tree::read_info(input, m_config);
124
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800125 m_nfd.reset(new Nfd(m_config, m_keyChain));
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500126 m_ribService.reset(new rib::Service(m_config, m_keyChain));
Alexander Afanasyev216df012015-02-10 17:35:46 -0800127
128 m_nfd->initialize();
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800129 }
130
131 void
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500132 run()
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800133 {
134 {
135 std::unique_lock<std::mutex> lock(m_pointerMutex);
136 m_io = &getGlobalIoService();
137 }
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700138
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500139 setMainIoService(m_io);
140 setRibIoService(m_io);
141
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800142 m_io->run();
143 m_io->reset();
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500144
145 m_ribService.reset();
146 m_nfd.reset();
147
148 m_io = nullptr;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800149 }
150
151 void
152 stop()
153 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800154 std::unique_lock<std::mutex> lock(m_pointerMutex);
155
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500156 if (m_io != nullptr) {
157 m_io->stop();
158 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800159 }
160
161private:
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800162 std::mutex m_pointerMutex;
163 boost::asio::io_service* m_io;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800164 ndn::KeyChain m_keyChain;
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500165 unique_ptr<Nfd> m_nfd;
166 unique_ptr<rib::Service> m_ribService;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800167
168 nfd::ConfigSection m_config;
169};
170
171static unique_ptr<Runner> g_runner;
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800172static boost::thread g_thread;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700173static std::map<std::string, std::string> g_params;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800174
175} // namespace nfd
Ivan Yeodb0052d2015-02-08 17:27:04 -0800176
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700177
178std::map<std::string, std::string>
179getParams(JNIEnv* env, jobject jParams)
180{
181 std::map<std::string, std::string> params;
182
183 jclass jcMap = env->GetObjectClass(jParams);
184 jclass jcSet = env->FindClass("java/util/Set");
185 jclass jcIterator = env->FindClass("java/util/Iterator");
186 jclass jcMapEntry = env->FindClass("java/util/Map$Entry");
187
188 jmethodID jcMapEntrySet = env->GetMethodID(jcMap, "entrySet", "()Ljava/util/Set;");
189 jmethodID jcSetIterator = env->GetMethodID(jcSet, "iterator", "()Ljava/util/Iterator;");
190 jmethodID jcIteratorHasNext = env->GetMethodID(jcIterator, "hasNext", "()Z");
191 jmethodID jcIteratorNext = env->GetMethodID(jcIterator, "next", "()Ljava/lang/Object;");
192 jmethodID jcMapEntryGetKey = env->GetMethodID(jcMapEntry, "getKey", "()Ljava/lang/Object;");
193 jmethodID jcMapEntryGetValue = env->GetMethodID(jcMapEntry, "getValue", "()Ljava/lang/Object;");
194
195 jobject jParamsEntrySet = env->CallObjectMethod(jParams, jcMapEntrySet);
196 jobject jParamsIterator = env->CallObjectMethod(jParamsEntrySet, jcSetIterator);
197 jboolean bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
198 while (bHasNext) {
199 jobject entry = env->CallObjectMethod(jParamsIterator, jcIteratorNext);
200
201 jstring jKey = (jstring)env->CallObjectMethod(entry, jcMapEntryGetKey);
202 jstring jValue = (jstring)env->CallObjectMethod(entry, jcMapEntryGetValue);
203
204 const char* cKey = env->GetStringUTFChars(jKey, nullptr);
205 const char* cValue = env->GetStringUTFChars(jValue, nullptr);
206
207 params.insert(std::make_pair(cKey, cValue));
208
209 env->ReleaseStringUTFChars(jKey, cKey);
210 env->ReleaseStringUTFChars(jValue, cValue);
211
212 bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
213 }
214
215 return params;
216}
217
218
Ivan Yeodb0052d2015-02-08 17:27:04 -0800219JNIEXPORT void JNICALL
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700220Java_net_named_1data_nfd_service_NfdService_startNfd(JNIEnv* env, jclass, jobject jParams)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800221{
Alexander Afanasyev216df012015-02-10 17:35:46 -0800222 if (nfd::g_runner.get() == nullptr) {
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700223 nfd::g_params = getParams(env, jParams);
224
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800225 // set/update HOME environment variable
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700226 ::setenv("HOME", nfd::g_params["homePath"].c_str(), true);
227 NFD_LOG_INFO("Use [" << nfd::g_params["homePath"] << "] as a security storage");
Ivan Yeodb0052d2015-02-08 17:27:04 -0800228
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800229 nfd::g_thread = boost::thread([] {
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700230 nfd::scheduler::resetGlobalScheduler();
231 nfd::resetGlobalIoService();
232
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800233 NFD_LOG_INFO("Starting NFD...");
234 try {
235 nfd::g_runner.reset(new nfd::Runner());
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500236 nfd::g_runner->run();
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800237 }
238 catch (const std::exception& e) {
239 NFD_LOG_FATAL(e.what());
240 }
241 catch (const nfd::PrivilegeHelper::Error& e) {
242 NFD_LOG_FATAL("PrivilegeHelper: " << e.what());
243 }
244 catch (...) {
245 NFD_LOG_FATAL("Unknown fatal error");
246 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800247
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800248 nfd::g_runner.reset();
249 nfd::scheduler::resetGlobalScheduler();
250 nfd::resetGlobalIoService();
251 NFD_LOG_INFO("NFD stopped");
252 });
253 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800254}
255
256JNIEXPORT void JNICALL
Ivan Yeo6296dce2015-02-10 23:29:43 -0800257Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800258{
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800259 if (nfd::g_runner.get() != nullptr) {
260 NFD_LOG_INFO("Stopping NFD...");
261 nfd::g_runner->stop();
262 // do not block anything
Alexander Afanasyev216df012015-02-10 17:35:46 -0800263 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800264}
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700265
Haitao Zhang0c79a9c2016-11-21 00:38:51 -0800266JNIEXPORT jboolean JNICALL
267Java_net_named_1data_nfd_service_NfdService_isNfdRunning(JNIEnv*, jclass)
268{
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500269 return nfd::g_runner.get() != nullptr;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700270}