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 | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 22 | #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 Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/util/exception.hpp> |
| 30 | #include <ndn-cxx/util/logging.hpp> |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 31 | |
| 32 | #include <boost/property_tree/info_parser.hpp> |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 33 | |
| 34 | #include <thread> |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 35 | #include <mutex> |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 37 | NFD_LOG_INIT(NfdWrapper); |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 38 | |
| 39 | namespace nfd { |
| 40 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 42 | class Runner |
| 43 | { |
| 44 | public: |
| 45 | Runner() |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 46 | : m_io(nullptr) |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 47 | { |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 48 | 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 Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 63 | /sl /localhost/nfd/strategy/self-learning |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | face_system |
| 67 | { |
| 68 | tcp |
| 69 | udp |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 70 | 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 Afanasyev | 449a431 | 2019-02-14 10:27:37 -0500 | [diff] [blame] | 95 | localhop_security |
| 96 | { |
| 97 | trust-anchor |
| 98 | { |
| 99 | type any |
| 100 | } |
| 101 | } |
Alexander Afanasyev | 14fd3e2 | 2018-07-04 14:20:53 -0400 | [diff] [blame] | 102 | auto_prefix_propagate |
| 103 | { |
| 104 | refresh_interval 300 |
| 105 | } |
| 106 | } |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 107 | )CONF"; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 108 | |
| 109 | std::istringstream input(initialConfig); |
| 110 | boost::property_tree::read_info(input, m_config); |
| 111 | |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 112 | // now, the start procedure can update config if needed |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | finishInit() |
| 117 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 118 | m_nfd.reset(new Nfd(m_config, m_keyChain)); |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 119 | m_ribService.reset(new rib::Service(m_config, m_keyChain)); |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 120 | |
| 121 | m_nfd->initialize(); |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 125 | run() |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 126 | { |
| 127 | { |
| 128 | std::unique_lock<std::mutex> lock(m_pointerMutex); |
| 129 | m_io = &getGlobalIoService(); |
| 130 | } |
Alexander Afanasyev | f7b6236 | 2015-09-10 23:29:47 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 132 | setMainIoService(m_io); |
| 133 | setRibIoService(m_io); |
| 134 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 135 | m_io->run(); |
| 136 | m_io->reset(); |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 137 | |
| 138 | m_ribService.reset(); |
| 139 | m_nfd.reset(); |
| 140 | |
| 141 | m_io = nullptr; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | stop() |
| 146 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 147 | std::unique_lock<std::mutex> lock(m_pointerMutex); |
| 148 | |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 149 | if (m_io != nullptr) { |
| 150 | m_io->stop(); |
| 151 | } |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 154 | nfd::ConfigSection& |
| 155 | getConfig() |
| 156 | { |
| 157 | return m_config; |
| 158 | } |
| 159 | |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 160 | private: |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 161 | std::mutex m_pointerMutex; |
| 162 | boost::asio::io_service* m_io; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 163 | ndn::KeyChain m_keyChain; |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 164 | unique_ptr<Nfd> m_nfd; |
| 165 | unique_ptr<rib::Service> m_ribService; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 166 | |
| 167 | nfd::ConfigSection m_config; |
| 168 | }; |
| 169 | |
| 170 | static unique_ptr<Runner> g_runner; |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 171 | static std::thread g_thread; |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 172 | static std::map<std::string, std::string> g_params; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 173 | |
| 174 | } // namespace nfd |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 176 | |
| 177 | std::map<std::string, std::string> |
| 178 | getParams(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 Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 218 | JNIEXPORT void JNICALL |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 219 | 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] | 220 | { |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 221 | if (nfd::g_runner.get() == nullptr) { |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 222 | nfd::g_params = getParams(env, jParams); |
| 223 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 224 | // set/update HOME environment variable |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 225 | ::setenv("HOME", nfd::g_params["homePath"].c_str(), true); |
| 226 | NFD_LOG_INFO("Use [" << nfd::g_params["homePath"] << "] as a security storage"); |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 227 | |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 228 | nfd::g_thread = std::thread([] { |
Alexander Afanasyev | f7b6236 | 2015-09-10 23:29:47 -0700 | [diff] [blame] | 229 | nfd::resetGlobalIoService(); |
| 230 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 231 | NFD_LOG_INFO("Starting NFD..."); |
| 232 | try { |
| 233 | nfd::g_runner.reset(new nfd::Runner()); |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 234 | // 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 Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 243 | nfd::g_runner->run(); |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 244 | } |
| 245 | catch (const std::exception& e) { |
Alexander Afanasyev | b5d5deb | 2019-05-02 17:55:26 -0400 | [diff] [blame] | 246 | NFD_LOG_FATAL(boost::diagnostic_information(e)); |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 247 | } |
| 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 Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 254 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 255 | nfd::g_runner.reset(); |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 256 | nfd::resetGlobalIoService(); |
| 257 | NFD_LOG_INFO("NFD stopped"); |
| 258 | }); |
| 259 | } |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | JNIEXPORT void JNICALL |
Ivan Yeo | 6296dce | 2015-02-10 23:29:43 -0800 | [diff] [blame] | 263 | Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass) |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 264 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame] | 265 | if (nfd::g_runner.get() != nullptr) { |
| 266 | NFD_LOG_INFO("Stopping NFD..."); |
| 267 | nfd::g_runner->stop(); |
| 268 | // do not block anything |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 269 | } |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 270 | } |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 271 | |
Haitao Zhang | 0c79a9c | 2016-11-21 00:38:51 -0800 | [diff] [blame] | 272 | JNIEXPORT jboolean JNICALL |
| 273 | Java_net_named_1data_nfd_service_NfdService_isNfdRunning(JNIEnv*, jclass) |
| 274 | { |
Alexander Afanasyev | 63e81f9 | 2019-02-05 13:30:26 -0500 | [diff] [blame] | 275 | return nfd::g_runner.get() != nullptr; |
Alexander Afanasyev | 45b7ad6 | 2015-04-19 22:00:45 -0700 | [diff] [blame] | 276 | } |