blob: ce5890923893fee433e61b35980937e57da773e3 [file] [log] [blame]
Steve DiBenedetto24b9a642014-04-07 15:45:39 -06001/* -*- 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
32namespace nfd {
33
34namespace general {
35
36NFD_LOG_INIT("GeneralConfigSection");
37
Vince Lehmanee7c8442015-07-16 16:42:19 -050038const ndn::Name
39RouterName::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
56const ndn::PartialName RouterName::ROUTER_MARKER("%C1.Router");
57
58static RouterName&
59getRouterNameInstance()
60{
61 static RouterName routerName;
62 return routerName;
63}
64
65ndn::PartialName
66loadPartialNameFromSection(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 Mastorakis149e02c2015-07-27 13:22:22 -070076 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"router_name." + key + "\""
77 " in \"general\" section"));
Vince Lehmanee7c8442015-07-16 16:42:19 -050078 }
79 }
80 catch (const boost::property_tree::ptree_error& error)
81 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070082 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"router_name." + key + "\""
83 " in \"general\" section"));
Vince Lehmanee7c8442015-07-16 16:42:19 -050084 }
85
86 return value;
87}
88
89void
90processSectionRouterName(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 DiBenedetto24b9a642014-04-07 15:45:39 -060099static void
100onConfig(const ConfigSection& configSection,
101 bool isDryRun,
102 const std::string& filename)
103{
104 // general
105 // {
106 // ; user "ndn-user"
107 // ; group "ndn-user"
Vince Lehmanee7c8442015-07-16 16:42:19 -0500108 //
109 // ; router_name
110 // ; {
111 // ; network ndn
112 // ; site edu/site
113 // ; router router/name
114 // ; }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600115 // }
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 Mastorakis149e02c2015-07-27 13:22:22 -0700132 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\""
133 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600134 }
135 }
136 catch (const boost::property_tree::ptree_error& error)
137 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700138 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\""
139 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600140 }
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 Mastorakis149e02c2015-07-27 13:22:22 -0700150 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\""
151 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600152 }
153 }
154 catch (const boost::property_tree::ptree_error& error)
155 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700156 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\""
157 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600158 }
159 }
160 }
161 NFD_LOG_TRACE("using user \"" << user << "\" group \"" << group << "\"");
162
163 PrivilegeHelper::initialize(user, group);
Vince Lehmanee7c8442015-07-16 16:42:19 -0500164
165 boost::optional<const ConfigSection&> routerNameSection =
166 configSection.get_child_optional("router_name");
167
168 if (routerNameSection)
169 {
170 processSectionRouterName(*routerNameSection, isDryRun);
171 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600172}
173
174void
175setConfigFile(ConfigFile& configFile)
176{
177 configFile.addSectionHandler("general", &onConfig);
178}
179
Vince Lehmanee7c8442015-07-16 16:42:19 -0500180const RouterName&
181getRouterName()
182{
183 return getRouterNameInstance();
184}
185
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600186} // namespace general
187
188} // namespace nfd