blob: 7b7243001db46f8fcb7d20f63fe7bc56f8330b2f [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 Afanasyevb5d5deb2019-05-02 17:55:26 -040022#include <daemon/common/config-file.hpp>
23#include <daemon/common/global.hpp>
24#include <daemon/common/logger.hpp>
25#include <daemon/common/privilege-helper.hpp>
26#include <daemon/nfd.hpp>
27#include <daemon/rib/service.hpp>
Ivan Yeodb0052d2015-02-08 17:27:04 -080028
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -040029#include <ndn-cxx/util/exception.hpp>
30#include <ndn-cxx/util/logging.hpp>
Alexander Afanasyev216df012015-02-10 17:35:46 -080031
32#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -040033
34#include <thread>
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080035#include <mutex>
Alexander Afanasyev216df012015-02-10 17:35:46 -080036
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040037NFD_LOG_INIT(NfdWrapper);
Alexander Afanasyev216df012015-02-10 17:35:46 -080038
39namespace nfd {
40
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080041
Alexander Afanasyev216df012015-02-10 17:35:46 -080042class Runner
43{
44public:
45 Runner()
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080046 : m_io(nullptr)
Alexander Afanasyev216df012015-02-10 17:35:46 -080047 {
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040048 std::string initialConfig = R"CONF(
49 log
50 {
51 default_level ALL
52 }
53 tables
54 {
55 cs_max_packets 100
56 strategy_choice
57 {
58 / /localhost/nfd/strategy/best-route
59 /localhost /localhost/nfd/strategy/multicast
60 /localhost/nfd /localhost/nfd/strategy/best-route
61 /ndn/broadcast /localhost/nfd/strategy/multicast
62 /ndn/multicast /localhost/nfd/strategy/multicast
Alexander Afanasyev63e81f92019-02-05 13:30:26 -050063 /sl /localhost/nfd/strategy/self-learning
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040064 }
65 }
66 face_system
67 {
68 tcp
69 udp
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040070 websocket
71 }
72 authorizations
73 {
74 authorize
75 {
76 certfile any
77 privileges
78 {
79 faces
80 fib
81 cs
82 strategy-choice
83 }
84 }
85 }
86 rib
87 {
88 localhost_security
89 {
90 trust-anchor
91 {
92 type any
93 }
94 }
Alexander Afanasyev449a4312019-02-14 10:27:37 -050095 localhop_security
96 {
97 trust-anchor
98 {
99 type any
100 }
101 }
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -0400102 auto_prefix_propagate
103 {
104 refresh_interval 300
105 }
106 }
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500107 )CONF";
Alexander Afanasyev216df012015-02-10 17:35:46 -0800108
109 std::istringstream input(initialConfig);
110 boost::property_tree::read_info(input, m_config);
111
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -0400112 // now, the start procedure can update config if needed
113 }
114
115 void
116 finishInit()
117 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800118 m_nfd.reset(new Nfd(m_config, m_keyChain));
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500119 m_ribService.reset(new rib::Service(m_config, m_keyChain));
Alexander Afanasyev216df012015-02-10 17:35:46 -0800120
121 m_nfd->initialize();
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800122 }
123
124 void
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500125 run()
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800126 {
127 {
128 std::unique_lock<std::mutex> lock(m_pointerMutex);
129 m_io = &getGlobalIoService();
130 }
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700131
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500132 setMainIoService(m_io);
133 setRibIoService(m_io);
134
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800135 m_io->run();
136 m_io->reset();
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500137
138 m_ribService.reset();
139 m_nfd.reset();
140
141 m_io = nullptr;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800142 }
143
144 void
145 stop()
146 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800147 std::unique_lock<std::mutex> lock(m_pointerMutex);
148
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500149 if (m_io != nullptr) {
150 m_io->stop();
151 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800152 }
153
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -0400154 nfd::ConfigSection&
155 getConfig()
156 {
157 return m_config;
158 }
159
Alexander Afanasyev216df012015-02-10 17:35:46 -0800160private:
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800161 std::mutex m_pointerMutex;
162 boost::asio::io_service* m_io;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800163 ndn::KeyChain m_keyChain;
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500164 unique_ptr<Nfd> m_nfd;
165 unique_ptr<rib::Service> m_ribService;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800166
167 nfd::ConfigSection m_config;
168};
169
170static unique_ptr<Runner> g_runner;
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -0400171static std::thread g_thread;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700172static std::map<std::string, std::string> g_params;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800173
174} // namespace nfd
Ivan Yeodb0052d2015-02-08 17:27:04 -0800175
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700176
177std::map<std::string, std::string>
178getParams(JNIEnv* env, jobject jParams)
179{
180 std::map<std::string, std::string> params;
181
182 jclass jcMap = env->GetObjectClass(jParams);
183 jclass jcSet = env->FindClass("java/util/Set");
184 jclass jcIterator = env->FindClass("java/util/Iterator");
185 jclass jcMapEntry = env->FindClass("java/util/Map$Entry");
186
187 jmethodID jcMapEntrySet = env->GetMethodID(jcMap, "entrySet", "()Ljava/util/Set;");
188 jmethodID jcSetIterator = env->GetMethodID(jcSet, "iterator", "()Ljava/util/Iterator;");
189 jmethodID jcIteratorHasNext = env->GetMethodID(jcIterator, "hasNext", "()Z");
190 jmethodID jcIteratorNext = env->GetMethodID(jcIterator, "next", "()Ljava/lang/Object;");
191 jmethodID jcMapEntryGetKey = env->GetMethodID(jcMapEntry, "getKey", "()Ljava/lang/Object;");
192 jmethodID jcMapEntryGetValue = env->GetMethodID(jcMapEntry, "getValue", "()Ljava/lang/Object;");
193
194 jobject jParamsEntrySet = env->CallObjectMethod(jParams, jcMapEntrySet);
195 jobject jParamsIterator = env->CallObjectMethod(jParamsEntrySet, jcSetIterator);
196 jboolean bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
197 while (bHasNext) {
198 jobject entry = env->CallObjectMethod(jParamsIterator, jcIteratorNext);
199
200 jstring jKey = (jstring)env->CallObjectMethod(entry, jcMapEntryGetKey);
201 jstring jValue = (jstring)env->CallObjectMethod(entry, jcMapEntryGetValue);
202
203 const char* cKey = env->GetStringUTFChars(jKey, nullptr);
204 const char* cValue = env->GetStringUTFChars(jValue, nullptr);
205
206 params.insert(std::make_pair(cKey, cValue));
207
208 env->ReleaseStringUTFChars(jKey, cKey);
209 env->ReleaseStringUTFChars(jValue, cValue);
210
211 bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
212 }
213
214 return params;
215}
216
217
Ivan Yeodb0052d2015-02-08 17:27:04 -0800218JNIEXPORT void JNICALL
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700219Java_net_named_1data_nfd_service_NfdService_startNfd(JNIEnv* env, jclass, jobject jParams)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800220{
Alexander Afanasyev216df012015-02-10 17:35:46 -0800221 if (nfd::g_runner.get() == nullptr) {
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700222 nfd::g_params = getParams(env, jParams);
223
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800224 // set/update HOME environment variable
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700225 ::setenv("HOME", nfd::g_params["homePath"].c_str(), true);
226 NFD_LOG_INFO("Use [" << nfd::g_params["homePath"] << "] as a security storage");
Ivan Yeodb0052d2015-02-08 17:27:04 -0800227
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -0400228 nfd::g_thread = std::thread([] {
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700229 nfd::resetGlobalIoService();
230
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800231 NFD_LOG_INFO("Starting NFD...");
232 try {
233 nfd::g_runner.reset(new nfd::Runner());
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -0400234 // update config
235 for (const auto& pair : nfd::g_params) {
236 if (pair.first == "homePath")
237 continue;
238
239 nfd::g_runner->getConfig().put(pair.first, pair.second);
240 }
241 nfd::g_runner->finishInit();
242
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500243 nfd::g_runner->run();
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800244 }
245 catch (const std::exception& e) {
Alexander Afanasyevb5d5deb2019-05-02 17:55:26 -0400246 NFD_LOG_FATAL(boost::diagnostic_information(e));
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800247 }
248 catch (const nfd::PrivilegeHelper::Error& e) {
249 NFD_LOG_FATAL("PrivilegeHelper: " << e.what());
250 }
251 catch (...) {
252 NFD_LOG_FATAL("Unknown fatal error");
253 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800254
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800255 nfd::g_runner.reset();
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800256 nfd::resetGlobalIoService();
257 NFD_LOG_INFO("NFD stopped");
258 });
259 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800260}
261
262JNIEXPORT void JNICALL
Ivan Yeo6296dce2015-02-10 23:29:43 -0800263Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800264{
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800265 if (nfd::g_runner.get() != nullptr) {
266 NFD_LOG_INFO("Stopping NFD...");
267 nfd::g_runner->stop();
268 // do not block anything
Alexander Afanasyev216df012015-02-10 17:35:46 -0800269 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800270}
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700271
Haitao Zhang0c79a9c2016-11-21 00:38:51 -0800272JNIEXPORT jboolean JNICALL
273Java_net_named_1data_nfd_service_NfdService_isNfdRunning(JNIEnv*, jclass)
274{
Alexander Afanasyev63e81f92019-02-05 13:30:26 -0500275 return nfd::g_runner.get() != nullptr;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700276}