Repository: envoyproxy/envoy
File: source/common/listener_manager/listener_manager_impl.cc (C++)

Code before:

  // Check if this address specifies a custom socket interface.
  if (&socket_interface != &default_interface) {
    ENVOY_LOG(debug, "creating socket using custom interface for address: {}",
              address->logicalName());
    auto io_handle = socket_interface.socket(socket_type, address, creation_options);
    if (!io_handle) {
      return absl::InvalidArgumentError("failed to create socket using custom interface");
    }
    return std::make_shared<Network::TcpListenSocket>(std::move(io_handle), address, options,
                                                      absl::nullopt, bind_type != BindType::NoBind);
  }

  // Continue with standard socket creation for addresses using the default interface.
  // First we try to get the socket from our parent if applicable in each case below.
  if (address->type() == Network::Address::Type::Pipe) {
    if (socket_type != Network::Socket::Type::Stream) {
      // This could be implemented in the future, since Unix domain sockets
      // support SOCK_DGRAM, but there would need to be a way to specify it in
      // envoy.api.v2.core.Pipe.
      return absl::InvalidArgumentError(
          fmt::format("socket type {} not supported for pipes", toString(socket_type)));
    }
    const std::string addr = fmt::format("unix://{}", address->asString());
    const int fd =
        server_.hotRestart().duplicateParentListenSocket(addr, worker_index, std::string());
    Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);
    if (io_handle->isOpen()) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      return std::make_shared<Network::UdsListenSocket>(std::move(io_handle), address);
    }
    return std::make_shared<Network::UdsListenSocket>(address);
  } else if (address->type() == Network::Address::Type::EnvoyInternal) {
    // Listener manager should have validated that envoy internal address doesn't work with udp
    // listener yet.
    ASSERT(socket_type == Network::Socket::Type::Stream);
    return std::make_shared<Network::InternalListenSocket>(address);
  }

  const std::string scheme = (socket_type == Network::Socket::Type::Stream)
                                 ? std::string(Network::Utility::TCP_SCHEME)
                                 : std::string(Network::Utility::UDP_SCHEME);
  const std::string addr = absl::StrCat(scheme, address->asString());

  if (bind_type != BindType::NoBind) {
    const std::string network_namespace = address->networkNamespace().value_or("");
    const int fd =
        server_.hotRestart().duplicateParentListenSocket(addr, worker_index, network_namespace);
    if (fd != -1) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);

Review comment (refactor):
```suggestion
        server_.hotRestart().duplicateParentListenSocket(addr, worker_index, "");
```

Code after:

  // Check if this address specifies a custom socket interface.
  if (&socket_interface != &default_interface) {
    ENVOY_LOG(debug, "creating socket using custom interface for address: {}",
              address->logicalName());
    auto io_handle = socket_interface.socket(socket_type, address, creation_options);
    if (!io_handle) {
      return absl::InvalidArgumentError("failed to create socket using custom interface");
    }
    return std::make_shared<Network::TcpListenSocket>(std::move(io_handle), address, options,
                                                      absl::nullopt, bind_type != BindType::NoBind);
  }

  // Continue with standard socket creation for addresses using the default interface.
  // First we try to get the socket from our parent if applicable in each case below.
  if (address->type() == Network::Address::Type::Pipe) {
    if (socket_type != Network::Socket::Type::Stream) {
      // This could be implemented in the future, since Unix domain sockets
      // support SOCK_DGRAM, but there would need to be a way to specify it in
      // envoy.api.v2.core.Pipe.
      return absl::InvalidArgumentError(
          fmt::format("socket type {} not supported for pipes", toString(socket_type)));
    }
    const std::string addr = fmt::format("unix://{}", address->asString());
    const int fd = server_.hotRestart().duplicateParentListenSocket(addr, worker_index, "");
    Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);
    if (io_handle->isOpen()) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      return std::make_shared<Network::UdsListenSocket>(std::move(io_handle), address);
    }
    return std::make_shared<Network::UdsListenSocket>(address);
  } else if (address->type() == Network::Address::Type::EnvoyInternal) {
    // Listener manager should have validated that envoy internal address doesn't work with udp
    // listener yet.
    ASSERT(socket_type == Network::Socket::Type::Stream);
    return std::make_shared<Network::InternalListenSocket>(address);
  }

  const std::string scheme = (socket_type == Network::Socket::Type::Stream)
                                 ? std::string(Network::Utility::TCP_SCHEME)
                                 : std::string(Network::Utility::UDP_SCHEME);
  const std::string addr = absl::StrCat(scheme, address->asString());

  if (bind_type != BindType::NoBind) {
    const int fd = server_.hotRestart().duplicateParentListenSocket(
        addr, worker_index, address->networkNamespace().value_or(""));
    if (fd != -1) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);
      if (socket_type == Network::Socket::Type::Stream) {
        return std::make_shared<Network::TcpListenSocket>(std::move(io_handle), address, options);