face: handle error in TCP socket remote_endpoint

refs #5158

Change-Id: I67fa134cd01a80a4f91122bbdcfc64e089d8145e
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index 6cf50c4..3382a91 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2020,  Regents of the University of California,
+ * Copyright (c) 2014-2022,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -98,10 +98,19 @@
 void
 TcpChannel::createFace(ip::tcp::socket&& socket,
                        const FaceParams& params,
-                       const FaceCreatedCallback& onFaceCreated)
+                       const FaceCreatedCallback& onFaceCreated,
+                       const FaceCreationFailedCallback& onFaceCreationFailed)
 {
   shared_ptr<Face> face;
-  tcp::Endpoint remoteEndpoint = socket.remote_endpoint();
+  boost::system::error_code ec;
+  tcp::Endpoint remoteEndpoint = socket.remote_endpoint(ec);
+  if (ec) {
+    NFD_LOG_CHAN_DEBUG("Retrieve socket remote endpoint failed: " << ec.message());
+    if (onFaceCreationFailed) {
+      onFaceCreationFailed(500, "Retrieve socket remote endpoint failed: " + ec.message());
+    }
+    return;
+  }
 
   auto it = m_channelFaces.find(remoteEndpoint);
   if (it == m_channelFaces.end()) {
@@ -174,7 +183,7 @@
 
   FaceParams params;
   params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
-  createFace(std::move(m_socket), params, onFaceCreated);
+  createFace(std::move(m_socket), params, onFaceCreated, onAcceptFailed);
 
   // prepare accepting the next connection
   accept(onFaceCreated, onAcceptFailed);
@@ -201,7 +210,7 @@
   }
 
   NFD_LOG_CHAN_TRACE("Connected to " << socket->remote_endpoint());
-  createFace(std::move(*socket), params, onFaceCreated);
+  createFace(std::move(*socket), params, onFaceCreated, onConnectFailed);
 }
 
 void
diff --git a/daemon/face/tcp-channel.hpp b/daemon/face/tcp-channel.hpp
index 788fdc1..bc6fba4 100644
--- a/daemon/face/tcp-channel.hpp
+++ b/daemon/face/tcp-channel.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2021,  Regents of the University of California,
+ * Copyright (c) 2014-2022,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -98,7 +98,8 @@
   void
   createFace(boost::asio::ip::tcp::socket&& socket,
              const FaceParams& params,
-             const FaceCreatedCallback& onFaceCreated);
+             const FaceCreatedCallback& onFaceCreated,
+             const FaceCreationFailedCallback& onFaceCreationFailed);
 
   void
   accept(const FaceCreatedCallback& onFaceCreated,