Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
| 24 | |
| 25 | #include "general-config-section.hpp" |
| 26 | |
| 27 | #include "common.hpp" |
| 28 | #include "core/logger.hpp" |
| 29 | #include "core/privilege-helper.hpp" |
| 30 | #include "core/config-file.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | namespace general { |
| 35 | |
| 36 | NFD_LOG_INIT("GeneralConfigSection"); |
| 37 | |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 38 | const ndn::Name |
| 39 | RouterName::getName() const |
| 40 | { |
| 41 | ndn::Name routerName; |
| 42 | |
| 43 | if (network.empty() || site.empty() || router.empty()) |
| 44 | { |
| 45 | return routerName; |
| 46 | } |
| 47 | |
| 48 | routerName = network; |
| 49 | routerName.append(site); |
| 50 | routerName.append(ROUTER_MARKER); |
| 51 | routerName.append(router); |
| 52 | |
| 53 | return routerName; |
| 54 | } |
| 55 | |
| 56 | const ndn::PartialName RouterName::ROUTER_MARKER("%C1.Router"); |
| 57 | |
| 58 | static RouterName& |
| 59 | getRouterNameInstance() |
| 60 | { |
| 61 | static RouterName routerName; |
| 62 | return routerName; |
| 63 | } |
| 64 | |
| 65 | ndn::PartialName |
| 66 | loadPartialNameFromSection(const ConfigSection& section, const std::string& key) |
| 67 | { |
| 68 | ndn::PartialName value; |
| 69 | |
| 70 | try |
| 71 | { |
| 72 | value = section.get<ndn::PartialName>(key); |
| 73 | |
| 74 | if (value.empty()) |
| 75 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 76 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"router_name." + key + "\"" |
| 77 | " in \"general\" section")); |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | catch (const boost::property_tree::ptree_error& error) |
| 81 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 82 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"router_name." + key + "\"" |
| 83 | " in \"general\" section")); |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return value; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | processSectionRouterName(const ConfigSection& section, bool isDryRun) |
| 91 | { |
| 92 | RouterName& routerName = getRouterNameInstance(); |
| 93 | |
| 94 | routerName.network = loadPartialNameFromSection(section, "network"); |
| 95 | routerName.site = loadPartialNameFromSection(section, "site"); |
| 96 | routerName.router = loadPartialNameFromSection(section, "router"); |
| 97 | } |
| 98 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 99 | static void |
| 100 | onConfig(const ConfigSection& configSection, |
| 101 | bool isDryRun, |
| 102 | const std::string& filename) |
| 103 | { |
| 104 | // general |
| 105 | // { |
| 106 | // ; user "ndn-user" |
| 107 | // ; group "ndn-user" |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 108 | // |
| 109 | // ; router_name |
| 110 | // ; { |
| 111 | // ; network ndn |
| 112 | // ; site edu/site |
| 113 | // ; router router/name |
| 114 | // ; } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 115 | // } |
| 116 | |
| 117 | std::string user; |
| 118 | std::string group; |
| 119 | |
| 120 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 121 | i != configSection.end(); |
| 122 | ++i) |
| 123 | { |
| 124 | if (i->first == "user") |
| 125 | { |
| 126 | try |
| 127 | { |
| 128 | user = i->second.get_value<std::string>("user"); |
| 129 | |
| 130 | if (user.empty()) |
| 131 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 132 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\"" |
| 133 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | catch (const boost::property_tree::ptree_error& error) |
| 137 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 138 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\"" |
| 139 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | else if (i->first == "group") |
| 143 | { |
| 144 | try |
| 145 | { |
| 146 | group = i->second.get_value<std::string>("group"); |
| 147 | |
| 148 | if (group.empty()) |
| 149 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 150 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\"" |
| 151 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | catch (const boost::property_tree::ptree_error& error) |
| 155 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 156 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\"" |
| 157 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | NFD_LOG_TRACE("using user \"" << user << "\" group \"" << group << "\""); |
| 162 | |
| 163 | PrivilegeHelper::initialize(user, group); |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 164 | |
| 165 | boost::optional<const ConfigSection&> routerNameSection = |
| 166 | configSection.get_child_optional("router_name"); |
| 167 | |
| 168 | if (routerNameSection) |
| 169 | { |
| 170 | processSectionRouterName(*routerNameSection, isDryRun); |
| 171 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void |
| 175 | setConfigFile(ConfigFile& configFile) |
| 176 | { |
| 177 | configFile.addSectionHandler("general", &onConfig); |
| 178 | } |
| 179 | |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 180 | const RouterName& |
| 181 | getRouterName() |
| 182 | { |
| 183 | return getRouterNameInstance(); |
| 184 | } |
| 185 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 186 | } // namespace general |
| 187 | |
| 188 | } // namespace nfd |