Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2015-2019 Regents of the University of California |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 4 | * |
| 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 Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 22 | #include "daemon/nfd.hpp" |
Alexander Afanasyev | a8d404b | 2016-11-05 10:07:08 -0600 | [diff] [blame] | 23 | #include "rib/service.hpp" |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 25 | #include "core/config-file.hpp" |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 26 | #include "core/global-io.hpp" |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 28 | #include "core/privilege-helper.hpp" |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 29 | |
| 30 | #include <boost/property_tree/info_parser.hpp> |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 31 | #include <boost/thread.hpp> |
| 32 | #include <mutex> |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 33 | #include <ndn-cxx/util/logging.hpp> |
| 34 | #include <stdlib.h> |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 36 | NFD_LOG_INIT(NfdWrapper); |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 37 | |
| 38 | namespace nfd { |
| 39 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 40 | |
| 41 | // A little bit of cheating to make sure NFD can be properly restarted |
| 42 | |
| 43 | namespace scheduler { |
| 44 | // defined in scheduler.cpp |
| 45 | void |
| 46 | resetGlobalScheduler(); |
| 47 | } // namespace scheduler |
| 48 | |
| 49 | void |
| 50 | resetGlobalIoService(); |
| 51 | |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 52 | class Runner |
| 53 | { |
| 54 | public: |
| 55 | Runner() |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 56 | : m_io(nullptr) |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 57 | { |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 58 | 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 Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 73 | /sl /localhost/nfd/strategy/self-learning |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 74 | } |
| 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 Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 108 | ; localhop_security |
| 109 | ; { |
| 110 | ; trust-anchor |
| 111 | ; { |
| 112 | ; type any |
| 113 | ; } |
| 114 | ; } |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 115 | auto_prefix_propagate |
| 116 | { |
| 117 | refresh_interval 300 |
| 118 | } |
| 119 | } |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 120 | )CONF"; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 121 | |
| 122 | std::istringstream input(initialConfig); |
| 123 | boost::property_tree::read_info(input, m_config); |
| 124 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 125 | m_nfd.reset(new Nfd(m_config, m_keyChain)); |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 126 | m_ribService.reset(new rib::Service(m_config, m_keyChain)); |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 127 | |
| 128 | m_nfd->initialize(); |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 132 | run() |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 133 | { |
| 134 | { |
| 135 | std::unique_lock<std::mutex> lock(m_pointerMutex); |
| 136 | m_io = &getGlobalIoService(); |
| 137 | } |
Alexander Afanasyev | f7b6236 | 2015-09-10 23:29:47 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 139 | setMainIoService(m_io); |
| 140 | setRibIoService(m_io); |
| 141 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 142 | m_io->run(); |
| 143 | m_io->reset(); |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 144 | |
| 145 | m_ribService.reset(); |
| 146 | m_nfd.reset(); |
| 147 | |
| 148 | m_io = nullptr; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void |
| 152 | stop() |
| 153 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 154 | std::unique_lock<std::mutex> lock(m_pointerMutex); |
| 155 | |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 156 | if (m_io != nullptr) { |
| 157 | m_io->stop(); |
| 158 | } |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | private: |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 162 | std::mutex m_pointerMutex; |
| 163 | boost::asio::io_service* m_io; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 164 | ndn::KeyChain m_keyChain; |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 165 | unique_ptr<Nfd> m_nfd; |
| 166 | unique_ptr<rib::Service> m_ribService; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 167 | |
| 168 | nfd::ConfigSection m_config; |
| 169 | }; |
| 170 | |
| 171 | static unique_ptr<Runner> g_runner; |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 172 | static boost::thread g_thread; |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 173 | static std::map<std::string, std::string> g_params; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 174 | |
| 175 | } // namespace nfd |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 177 | |
| 178 | std::map<std::string, std::string> |
| 179 | getParams(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 Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 219 | JNIEXPORT void JNICALL |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 220 | Java_net_named_1data_nfd_service_NfdService_startNfd(JNIEnv* env, jclass, jobject jParams) |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 221 | { |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 222 | if (nfd::g_runner.get() == nullptr) { |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 223 | nfd::g_params = getParams(env, jParams); |
| 224 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 225 | // set/update HOME environment variable |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 226 | ::setenv("HOME", nfd::g_params["homePath"].c_str(), true); |
| 227 | NFD_LOG_INFO("Use [" << nfd::g_params["homePath"] << "] as a security storage"); |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 229 | nfd::g_thread = boost::thread([] { |
Alexander Afanasyev | f7b6236 | 2015-09-10 23:29:47 -0700 | [diff] [blame] | 230 | nfd::scheduler::resetGlobalScheduler(); |
| 231 | nfd::resetGlobalIoService(); |
| 232 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 233 | NFD_LOG_INFO("Starting NFD..."); |
| 234 | try { |
| 235 | nfd::g_runner.reset(new nfd::Runner()); |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 236 | nfd::g_runner->run(); |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 237 | } |
| 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 Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 247 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 248 | nfd::g_runner.reset(); |
| 249 | nfd::scheduler::resetGlobalScheduler(); |
| 250 | nfd::resetGlobalIoService(); |
| 251 | NFD_LOG_INFO("NFD stopped"); |
| 252 | }); |
| 253 | } |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | JNIEXPORT void JNICALL |
Ivan Yeo | 6296dce | 2015-02-10 23:29:43 -0800 | [diff] [blame] | 257 | Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass) |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 258 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 259 | if (nfd::g_runner.get() != nullptr) { |
| 260 | NFD_LOG_INFO("Stopping NFD..."); |
| 261 | nfd::g_runner->stop(); |
| 262 | // do not block anything |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 263 | } |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 264 | } |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 265 | |
Haitao Zhang | 0c79a9c | 2016-11-21 00:38:51 -0800 | [diff] [blame] | 266 | JNIEXPORT jboolean JNICALL |
| 267 | Java_net_named_1data_nfd_service_NfdService_isNfdRunning(JNIEnv*, jclass) |
| 268 | { |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame^] | 269 | return nfd::g_runner.get() != nullptr; |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 270 | } |