Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2015 Regents of the University of California |
| 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" |
| 23 | #include "rib/nrd.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/global-io.hpp" |
| 26 | #include "core/config-file.hpp" |
| 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 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 30 | #include <stdlib.h> |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 31 | #include <boost/property_tree/info_parser.hpp> |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 32 | #include <boost/thread.hpp> |
| 33 | #include <mutex> |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 34 | |
| 35 | NFD_LOG_INIT("NfdWrapper"); |
| 36 | |
| 37 | namespace nfd { |
| 38 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 39 | |
| 40 | // A little bit of cheating to make sure NFD can be properly restarted |
| 41 | |
| 42 | namespace scheduler { |
| 43 | // defined in scheduler.cpp |
| 44 | void |
| 45 | resetGlobalScheduler(); |
| 46 | } // namespace scheduler |
| 47 | |
| 48 | void |
| 49 | resetGlobalIoService(); |
| 50 | |
| 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 | { |
| 58 | std::string initialConfig = |
| 59 | "general\n" |
| 60 | "{\n" |
| 61 | "}\n" |
| 62 | "\n" |
| 63 | "log\n" |
| 64 | "{\n" |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 65 | " default_level ALL\n" |
| 66 | " NameTree INFO\n" |
| 67 | " BestRouteStrategy2 INFO\n" |
| 68 | " InternalFace INFO\n" |
| 69 | " Forwarder INFO\n" |
| 70 | " ContentStore INFO\n" |
| 71 | " DeadNonceList INFO\n" |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 72 | "}\n" |
| 73 | "tables\n" |
| 74 | "{\n" |
| 75 | " cs_max_packets 100\n" |
| 76 | "\n" |
| 77 | " strategy_choice\n" |
| 78 | " {\n" |
| 79 | " / /localhost/nfd/strategy/best-route\n" |
| 80 | " /localhost /localhost/nfd/strategy/broadcast\n" |
| 81 | " /localhost/nfd /localhost/nfd/strategy/best-route\n" |
| 82 | " /ndn/broadcast /localhost/nfd/strategy/broadcast\n" |
| 83 | " }\n" |
| 84 | "}\n" |
| 85 | "\n" |
| 86 | "face_system\n" |
| 87 | "{\n" |
| 88 | " tcp\n" |
| 89 | " {\n" |
| 90 | " port 6363\n" |
| 91 | " }\n" |
| 92 | "\n" |
| 93 | " udp\n" |
| 94 | " {\n" |
| 95 | " port 6363\n" |
| 96 | " idle_timeout 600\n" |
| 97 | " keep_alive_interval 25\n" |
| 98 | " mcast no\n" |
| 99 | " }\n" |
| 100 | "}\n" |
| 101 | "\n" |
| 102 | "authorizations\n" |
| 103 | "{\n" |
| 104 | " authorize\n" |
| 105 | " {\n" |
| 106 | " certfile any\n" |
| 107 | " privileges\n" |
| 108 | " {\n" |
| 109 | " faces\n" |
| 110 | " fib\n" |
| 111 | " strategy-choice\n" |
| 112 | " }\n" |
| 113 | " }\n" |
| 114 | "}\n" |
| 115 | "\n" |
| 116 | "rib\n" |
| 117 | "{\n" |
| 118 | " localhost_security\n" |
| 119 | " {\n" |
| 120 | " trust-anchor\n" |
| 121 | " {\n" |
| 122 | " type any\n" |
| 123 | " }\n" |
| 124 | " }\n" |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 125 | "\n" |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 126 | " remote_register\n" |
| 127 | " {\n" |
| 128 | " cost 15\n" |
| 129 | " timeout 10000\n" |
| 130 | " retry 0\n" |
| 131 | " refresh_interval 300\n" |
| 132 | " }\n" |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 133 | "}\n" |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 134 | "\n"; |
| 135 | |
| 136 | std::istringstream input(initialConfig); |
| 137 | boost::property_tree::read_info(input, m_config); |
| 138 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 139 | std::unique_lock<std::mutex> lock(m_pointerMutex); |
| 140 | m_nfd.reset(new Nfd(m_config, m_keyChain)); |
| 141 | m_nrd.reset(new rib::Nrd(m_config, m_keyChain)); |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 142 | |
| 143 | m_nfd->initialize(); |
| 144 | m_nrd->initialize(); |
| 145 | } |
| 146 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 147 | ~Runner() |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 148 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 149 | stop(); |
| 150 | m_io->reset(); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | start() |
| 155 | { |
| 156 | { |
| 157 | std::unique_lock<std::mutex> lock(m_pointerMutex); |
| 158 | m_io = &getGlobalIoService(); |
| 159 | } |
| 160 | m_io->run(); |
| 161 | m_io->reset(); |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void |
| 165 | stop() |
| 166 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 167 | std::unique_lock<std::mutex> lock(m_pointerMutex); |
| 168 | |
| 169 | m_io->post([this] { |
| 170 | m_io->stop(); |
| 171 | this->m_nrd.reset(); |
| 172 | this->m_nfd.reset(); |
| 173 | }); |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private: |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 177 | std::mutex m_pointerMutex; |
| 178 | boost::asio::io_service* m_io; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 179 | ndn::KeyChain m_keyChain; |
| 180 | unique_ptr<Nfd> m_nfd; // will use globalIoService |
| 181 | unique_ptr<rib::Nrd> m_nrd; // will use globalIoService |
| 182 | |
| 183 | nfd::ConfigSection m_config; |
| 184 | }; |
| 185 | |
| 186 | static unique_ptr<Runner> g_runner; |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 187 | static boost::thread g_thread; |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 188 | |
| 189 | } // namespace nfd |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 190 | |
| 191 | JNIEXPORT void JNICALL |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 192 | Java_net_named_1data_nfd_wrappers_NfdWrapper_startNfd(JNIEnv* env, jclass, jstring homePathJ) |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 193 | { |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 194 | if (nfd::g_runner.get() == nullptr) { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 195 | // set/update HOME environment variable |
| 196 | const char* homePath = env->GetStringUTFChars(homePathJ, nullptr); |
| 197 | ::setenv("HOME", homePath, true); |
| 198 | env->ReleaseStringUTFChars(homePathJ, homePath); |
| 199 | NFD_LOG_INFO("Use [" << homePath << "] as a security storage"); |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 201 | nfd::g_thread = boost::thread([] { |
| 202 | NFD_LOG_INFO("Starting NFD..."); |
| 203 | try { |
| 204 | nfd::g_runner.reset(new nfd::Runner()); |
| 205 | nfd::g_runner->start(); |
| 206 | } |
| 207 | catch (const std::exception& e) { |
| 208 | NFD_LOG_FATAL(e.what()); |
| 209 | } |
| 210 | catch (const nfd::PrivilegeHelper::Error& e) { |
| 211 | NFD_LOG_FATAL("PrivilegeHelper: " << e.what()); |
| 212 | } |
| 213 | catch (...) { |
| 214 | NFD_LOG_FATAL("Unknown fatal error"); |
| 215 | } |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 216 | |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 217 | nfd::g_runner.reset(); |
| 218 | nfd::scheduler::resetGlobalScheduler(); |
| 219 | nfd::resetGlobalIoService(); |
| 220 | NFD_LOG_INFO("NFD stopped"); |
| 221 | }); |
| 222 | } |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | JNIEXPORT void JNICALL |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 226 | Java_net_named_1data_nfd_wrappers_NfdWrapper_stopNfd(JNIEnv*, jclass) |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 227 | { |
Alexander Afanasyev | c134b6f | 2015-02-12 17:01:44 -0800 | [diff] [blame^] | 228 | if (nfd::g_runner.get() != nullptr) { |
| 229 | NFD_LOG_INFO("Stopping NFD..."); |
| 230 | nfd::g_runner->stop(); |
| 231 | // do not block anything |
Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 232 | } |
Ivan Yeo | db0052d | 2015-02-08 17:27:04 -0800 | [diff] [blame] | 233 | } |