NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
unix-stream-channel.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "unix-stream-channel.hpp"
27 #include "unix-stream-face.hpp"
28 #include "core/global-io.hpp"
29 
30 #include <boost/filesystem.hpp>
31 #include <sys/stat.h> // for chmod()
32 
33 namespace nfd {
34 
35 NFD_LOG_INIT("UnixStreamChannel");
36 
38  : m_endpoint(endpoint)
39  , m_acceptor(getGlobalIoService())
40  , m_socket(getGlobalIoService())
41 {
42  setUri(FaceUri(m_endpoint));
43 }
44 
46 {
47  if (isListening()) {
48  // use the non-throwing variants during destruction
49  // and ignore any errors
50  boost::system::error_code error;
51  m_acceptor.close(error);
52  NFD_LOG_DEBUG("[" << m_endpoint << "] Removing socket file");
53  boost::filesystem::remove(m_endpoint.path(), error);
54  }
55 }
56 
57 void
59  const ConnectFailedCallback& onAcceptFailed,
60  int backlog/* = acceptor::max_connections*/)
61 {
62  if (isListening()) {
63  NFD_LOG_WARN("[" << m_endpoint << "] Already listening");
64  return;
65  }
66 
67  namespace fs = boost::filesystem;
68 
69  fs::path socketPath(m_endpoint.path());
70  fs::file_type type = fs::symlink_status(socketPath).type();
71 
72  if (type == fs::socket_file) {
73  boost::system::error_code error;
74  boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
75  socket.connect(m_endpoint, error);
76  NFD_LOG_TRACE("[" << m_endpoint << "] connect() on existing socket file returned: "
77  + error.message());
78  if (!error) {
79  // someone answered, leave the socket alone
80  BOOST_THROW_EXCEPTION(Error("Socket file at " + m_endpoint.path()
81  + " belongs to another NFD process"));
82  }
83  else if (error == boost::asio::error::connection_refused ||
84  error == boost::asio::error::timed_out) {
85  // no one is listening on the remote side,
86  // we can safely remove the stale socket
87  NFD_LOG_DEBUG("[" << m_endpoint << "] Removing stale socket file");
88  fs::remove(socketPath);
89  }
90  }
91  else if (type != fs::file_not_found) {
92  BOOST_THROW_EXCEPTION(Error(m_endpoint.path() + " already exists and is not a socket file"));
93  }
94 
95  m_acceptor.open();
96  m_acceptor.bind(m_endpoint);
97  m_acceptor.listen(backlog);
98 
99  if (::chmod(m_endpoint.path().c_str(), 0666) < 0) {
100  BOOST_THROW_EXCEPTION(Error("chmod(" + m_endpoint.path() + ") failed: " +
101  std::strerror(errno)));
102  }
103 
104  // start accepting connections
105  accept(onFaceCreated, onAcceptFailed);
106 }
107 
108 void
109 UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
110  const ConnectFailedCallback& onAcceptFailed)
111 {
112  m_acceptor.async_accept(m_socket, bind(&UnixStreamChannel::handleAccept, this,
113  boost::asio::placeholders::error,
114  onFaceCreated, onAcceptFailed));
115 }
116 
117 void
118 UnixStreamChannel::handleAccept(const boost::system::error_code& error,
119  const FaceCreatedCallback& onFaceCreated,
120  const ConnectFailedCallback& onAcceptFailed)
121 {
122  if (error) {
123  if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
124  return;
125 
126  NFD_LOG_DEBUG("[" << m_endpoint << "] Accept failed: " << error.message());
127  if (onAcceptFailed)
128  onAcceptFailed(error.message());
129  return;
130  }
131 
132  NFD_LOG_DEBUG("[" << m_endpoint << "] Incoming connection");
133 
134  auto remoteUri = FaceUri::fromFd(m_socket.native_handle());
135  auto localUri = FaceUri(m_socket.local_endpoint());
136  auto face = make_shared<UnixStreamFace>(remoteUri, localUri, std::move(m_socket));
137  onFaceCreated(face);
138 
139  // prepare accepting the next connection
140  accept(onFaceCreated, onAcceptFailed);
141 }
142 
143 } // namespace nfd
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:36
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
UnixStreamChannel-related error.
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:39
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
UnixStreamChannel(const unix_stream::Endpoint &endpoint)
Create UnixStream channel for the specified endpoint.
void setUri(const FaceUri &uri)
Definition: channel.cpp:34
function< void(const std::string &reason)> ConnectFailedCallback
Prototype for the callback that is called when face is failed to get created.
Definition: channel.hpp:46
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:35
boost::asio::local::stream_protocol::endpoint Endpoint
boost::asio::io_service & getGlobalIoService()
Definition: global-io.hpp:35
void listen(const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &onAcceptFailed, int backlog=boost::asio::local::stream_protocol::acceptor::max_connections)
Enable listening on the local endpoint, accept connections, and create faces when a connection is mad...
static FaceUri fromFd(int fd)
create fd FaceUri from file descriptor
Definition: face-uri.cpp:144
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...
Definition: channel.hpp:41