unix-stream-factory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "unix-stream-factory.hpp"
27 
28 #include <boost/filesystem.hpp>
29 
30 namespace nfd::face {
31 
32 NFD_LOG_INIT(UnixStreamFactory);
34 
35 const std::string&
37 {
38  static std::string id("unix");
39  return id;
40 }
41 
42 void
43 UnixStreamFactory::doProcessConfig(OptionalConfigSection configSection,
45 {
46  // unix
47  // {
48  // path /run/nfd.sock ; on Linux
49  // path /var/run/nfd.sock ; on other platforms
50  // }
51 
52  m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
53 
54  if (!configSection) {
55  if (!context.isDryRun && !m_channels.empty()) {
56  NFD_LOG_WARN("Cannot disable Unix channel after initialization");
57  }
58  return;
59  }
60 
61 #ifdef __linux__
62  std::string path = "/run/nfd.sock";
63 #else
64  std::string path = "/var/run/nfd.sock";
65 #endif // __linux__
66 
67  for (const auto& pair : *configSection) {
68  const std::string& key = pair.first;
69  const ConfigSection& value = pair.second;
70 
71  if (key == "path") {
72  path = value.get_value<std::string>();
73  }
74  else {
75  NDN_THROW(ConfigFile::Error("Unrecognized option face_system.unix." + key));
76  }
77  }
78 
79  if (context.isDryRun) {
80  return;
81  }
82 
83  auto channel = this->createChannel(path);
84  if (!channel->isListening()) {
85  channel->listen(this->addFace, nullptr);
86  }
87 }
88 
89 shared_ptr<UnixStreamChannel>
90 UnixStreamFactory::createChannel(const std::string& unixSocketPath)
91 {
92  boost::filesystem::path p(unixSocketPath);
93  p = boost::filesystem::canonical(p.parent_path()) / p.filename();
94  unix_stream::Endpoint endpoint(p.string());
95 
96  auto it = m_channels.find(endpoint);
97  if (it != m_channels.end())
98  return it->second;
99 
100  auto channel = make_shared<UnixStreamChannel>(endpoint, m_wantCongestionMarking);
101  m_channels[endpoint] = channel;
102  return channel;
103 }
104 
105 std::vector<shared_ptr<const Channel>>
106 UnixStreamFactory::doGetChannels() const
107 {
108  return getChannelsFromMap(m_channels);
109 }
110 
111 } // namespace nfd::face
Context for processing a config section in ProtocolFactory.
Definition: face-system.hpp:99
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
FaceCreatedCallback addFace
callback when a new face is created
Protocol factory for stream-oriented Unix sockets.
shared_ptr< UnixStreamChannel > createChannel(const std::string &unixSocketPath)
Create stream-oriented Unix channel using specified socket path.
static const std::string & getId() noexcept
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_WARN
Definition: logger.hpp:40
boost::asio::local::stream_protocol::endpoint Endpoint
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:43
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:38
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
Registers a protocol factory.