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-2024, 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 <filesystem>
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/nfd.sock ; on Linux
49  // path /var/run/nfd/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/nfd.sock";
63 #else
64  std::string path = "/var/run/nfd/nfd.sock";
65 #endif // __linux__
66 
67  for (const auto& [key, value] : *configSection) {
68  if (key == "path") {
69  path = value.get_value<std::string>();
70  }
71  else {
72  NDN_THROW(ConfigFile::Error("Unrecognized option face_system.unix." + key));
73  }
74  }
75 
76  if (context.isDryRun) {
77  return;
78  }
79 
80  auto channel = this->createChannel(path);
81  if (!channel->isListening()) {
82  channel->listen(this->addFace, nullptr);
83  }
84 }
85 
86 shared_ptr<UnixStreamChannel>
87 UnixStreamFactory::createChannel(const std::string& socketPath)
88 {
89  auto normalizedPath = std::filesystem::weakly_canonical(std::filesystem::absolute(socketPath));
90  unix_stream::Endpoint endpoint(normalizedPath);
91 
92  auto it = m_channels.find(endpoint);
93  if (it != m_channels.end())
94  return it->second;
95 
96  auto channel = make_shared<UnixStreamChannel>(endpoint, m_wantCongestionMarking);
97  m_channels[endpoint] = channel;
98  return channel;
99 }
100 
101 std::vector<shared_ptr<const Channel>>
102 UnixStreamFactory::doGetChannels() const
103 {
104  return getChannelsFromMap(m_channels);
105 }
106 
107 } // namespace nfd::face
Context for processing a config section in ProtocolFactory.
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.
static const std::string & getId() noexcept
shared_ptr< UnixStreamChannel > createChannel(const std::string &socketPath)
Create stream-oriented Unix channel using specified socket path.
#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:46
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
Registers a protocol factory.