Skip to content

Commit

Permalink
sockets: splitting connection socket out of listen_socket_impl
Browse files Browse the repository at this point in the history
Signed-off-by: Alyssa Wilk <[email protected]>
  • Loading branch information
alyssawilk committed Sep 14, 2023
1 parent 8e7beb3 commit 55d67be
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 95 deletions.
15 changes: 13 additions & 2 deletions source/common/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ envoy_cc_library(
srcs = ["connection_impl_base.cc"],
hdrs = ["connection_impl_base.h"],
deps = [
":connection_socket_lib",
":filter_manager_lib",
":listen_socket_lib",
"//envoy/common:scope_tracker_interface",
"//envoy/event:dispatcher_interface",
"//source/common/common:assert_lib",
Expand Down Expand Up @@ -117,7 +117,6 @@ envoy_cc_library(
"//source/common/common:enum_to_int",
"//source/common/common:minimal_logger_lib",
"//source/common/event:libevent_lib",
"//source/common/network:listen_socket_lib",
"//source/common/network:socket_option_factory_lib",
"//source/common/runtime:runtime_features_lib",
"//source/common/stream_info:stream_info_lib",
Expand Down Expand Up @@ -281,11 +280,23 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "connection_socket_lib",
hdrs = ["connection_socket_impl.h"],
deps = [
":socket_lib",
":utility_lib",
"//envoy/network:exception_interface",
"//source/common/common:assert_lib",
],
)

envoy_cc_library(
name = "listen_socket_lib",
srcs = ["listen_socket_impl.cc"],
hdrs = ["listen_socket_impl.h"],
deps = [
":connection_socket_lib",
":socket_lib",
":utility_lib",
"//envoy/network:exception_interface",
Expand Down
2 changes: 1 addition & 1 deletion source/common/network/connection_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "source/common/common/enum_to_int.h"
#include "source/common/common/scope_tracker.h"
#include "source/common/network/address_impl.h"
#include "source/common/network/listen_socket_impl.h"
#include "source/common/network/connection_socket_impl.h"
#include "source/common/network/raw_buffer_socket.h"
#include "source/common/network/socket_option_factory.h"
#include "source/common/network/socket_option_impl.h"
Expand Down
110 changes: 110 additions & 0 deletions source/common/network/connection_socket_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#pragma once

#include <memory>
#include <string>
#include <vector>

#include "envoy/common/platform.h"
#include "envoy/network/connection.h"
#include "envoy/network/listen_socket.h"
#include "envoy/network/socket.h"
#include "envoy/network/socket_interface.h"

#include "source/common/common/assert.h"
#include "source/common/common/dump_state_utils.h"
#include "source/common/network/socket_impl.h"
#include "source/common/network/socket_interface.h"

namespace Envoy {
namespace Network {

/**
* Wraps a unix socket.
*/
template <Socket::Type T> struct NetworkSocketTrait {};

template <> struct NetworkSocketTrait<Socket::Type::Stream> {
static constexpr Socket::Type type = Socket::Type::Stream;
};

template <> struct NetworkSocketTrait<Socket::Type::Datagram> {
static constexpr Socket::Type type = Socket::Type::Datagram;
};

class ConnectionSocketImpl : public SocketImpl, public ConnectionSocket {
public:
ConnectionSocketImpl(IoHandlePtr&& io_handle,
const Address::InstanceConstSharedPtr& local_address,
const Address::InstanceConstSharedPtr& remote_address)
: SocketImpl(std::move(io_handle), local_address, remote_address) {}

ConnectionSocketImpl(Socket::Type type, const Address::InstanceConstSharedPtr& local_address,
const Address::InstanceConstSharedPtr& remote_address,
const SocketCreationOptions& options)
: SocketImpl(type, local_address, remote_address, options) {
connection_info_provider_->setLocalAddress(local_address);
}

// Network::ConnectionSocket
void setDetectedTransportProtocol(absl::string_view protocol) override {
transport_protocol_ = std::string(protocol);
}
absl::string_view detectedTransportProtocol() const override { return transport_protocol_; }

void setRequestedApplicationProtocols(const std::vector<absl::string_view>& protocols) override {
application_protocols_.clear();
for (const auto& protocol : protocols) {
application_protocols_.emplace_back(protocol);
}
}
const std::vector<std::string>& requestedApplicationProtocols() const override {
return application_protocols_;
}

void setRequestedServerName(absl::string_view server_name) override {
// Always keep the server_name_ as lower case.
connectionInfoProvider().setRequestedServerName(absl::AsciiStrToLower(server_name));
}
absl::string_view requestedServerName() const override {
return connectionInfoProvider().requestedServerName();
}

void setJA3Hash(absl::string_view ja3_hash) override {
connectionInfoProvider().setJA3Hash(ja3_hash);
}
absl::string_view ja3Hash() const override { return connectionInfoProvider().ja3Hash(); }

absl::optional<std::chrono::milliseconds> lastRoundTripTime() override {
return ioHandle().lastRoundTripTime();
}

absl::optional<uint64_t> congestionWindowInBytes() const override {
return ioHandle().congestionWindowInBytes();
}

void dumpState(std::ostream& os, int indent_level) const override {
const char* spaces = spacesForLevel(indent_level);
os << spaces << "ListenSocketImpl " << this << DUMP_MEMBER(transport_protocol_) << "\n";
DUMP_DETAILS(connection_info_provider_);
}

protected:
std::string transport_protocol_;
std::vector<std::string> application_protocols_;
};

// ConnectionSocket used with client connections.
class ClientSocketImpl : public ConnectionSocketImpl {
public:
ClientSocketImpl(const Address::InstanceConstSharedPtr& remote_address,
const OptionsSharedPtr& options)
: ConnectionSocketImpl(Network::ioHandleForAddr(Socket::Type::Stream, remote_address, {}),
nullptr, remote_address) {
if (options) {
addOptions(options);
}
}
};

} // namespace Network
} // namespace Envoy
89 changes: 1 addition & 88 deletions source/common/network/listen_socket_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "source/common/common/assert.h"
#include "source/common/common/dump_state_utils.h"
#include "source/common/network/connection_socket_impl.h"
#include "source/common/network/socket_impl.h"
#include "source/common/network/socket_interface.h"

Expand Down Expand Up @@ -42,19 +43,6 @@ class ListenSocketImpl : public SocketImpl {
bool isOpen() const override { return io_handle_ != nullptr && io_handle_->isOpen(); }
};

/**
* Wraps a unix socket.
*/
template <Socket::Type T> struct NetworkSocketTrait {};

template <> struct NetworkSocketTrait<Socket::Type::Stream> {
static constexpr Socket::Type type = Socket::Type::Stream;
};

template <> struct NetworkSocketTrait<Socket::Type::Datagram> {
static constexpr Socket::Type type = Socket::Type::Datagram;
};

template <typename T> class NetworkListenSocket : public ListenSocketImpl {
public:
NetworkListenSocket(const Address::InstanceConstSharedPtr& address,
Expand Down Expand Up @@ -178,68 +166,6 @@ class InternalListenSocket : public ListenSocketImpl {
}
};

class ConnectionSocketImpl : public SocketImpl, public ConnectionSocket {
public:
ConnectionSocketImpl(IoHandlePtr&& io_handle,
const Address::InstanceConstSharedPtr& local_address,
const Address::InstanceConstSharedPtr& remote_address)
: SocketImpl(std::move(io_handle), local_address, remote_address) {}

ConnectionSocketImpl(Socket::Type type, const Address::InstanceConstSharedPtr& local_address,
const Address::InstanceConstSharedPtr& remote_address,
const SocketCreationOptions& options)
: SocketImpl(type, local_address, remote_address, options) {
connection_info_provider_->setLocalAddress(local_address);
}

// Network::ConnectionSocket
void setDetectedTransportProtocol(absl::string_view protocol) override {
transport_protocol_ = std::string(protocol);
}
absl::string_view detectedTransportProtocol() const override { return transport_protocol_; }

void setRequestedApplicationProtocols(const std::vector<absl::string_view>& protocols) override {
application_protocols_.clear();
for (const auto& protocol : protocols) {
application_protocols_.emplace_back(protocol);
}
}
const std::vector<std::string>& requestedApplicationProtocols() const override {
return application_protocols_;
}

void setRequestedServerName(absl::string_view server_name) override {
// Always keep the server_name_ as lower case.
connectionInfoProvider().setRequestedServerName(absl::AsciiStrToLower(server_name));
}
absl::string_view requestedServerName() const override {
return connectionInfoProvider().requestedServerName();
}

void setJA3Hash(absl::string_view ja3_hash) override {
connectionInfoProvider().setJA3Hash(ja3_hash);
}
absl::string_view ja3Hash() const override { return connectionInfoProvider().ja3Hash(); }

absl::optional<std::chrono::milliseconds> lastRoundTripTime() override {
return ioHandle().lastRoundTripTime();
}

absl::optional<uint64_t> congestionWindowInBytes() const override {
return ioHandle().congestionWindowInBytes();
}

void dumpState(std::ostream& os, int indent_level) const override {
const char* spaces = spacesForLevel(indent_level);
os << spaces << "ListenSocketImpl " << this << DUMP_MEMBER(transport_protocol_) << "\n";
DUMP_DETAILS(connection_info_provider_);
}

protected:
std::string transport_protocol_;
std::vector<std::string> application_protocols_;
};

// ConnectionSocket used with server connections.
class AcceptedSocketImpl : public ConnectionSocketImpl {
public:
Expand All @@ -262,18 +188,5 @@ class AcceptedSocketImpl : public ConnectionSocketImpl {
static std::atomic<uint64_t> global_accepted_socket_count_;
};

// ConnectionSocket used with client connections.
class ClientSocketImpl : public ConnectionSocketImpl {
public:
ClientSocketImpl(const Address::InstanceConstSharedPtr& remote_address,
const OptionsSharedPtr& options)
: ConnectionSocketImpl(Network::ioHandleForAddr(Socket::Type::Stream, remote_address, {}),
nullptr, remote_address) {
if (options) {
addOptions(options);
}
}
};

} // namespace Network
} // namespace Envoy
4 changes: 2 additions & 2 deletions source/common/quic/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ envoy_cc_library(
tags = ["nofips"],
deps = [
"//envoy/network:connection_interface",
"//source/common/network:listen_socket_lib",
],
)

Expand All @@ -349,6 +348,7 @@ envoy_cc_library(
":quic_io_handle_wrapper_lib",
":quic_network_connection_lib",
"//source/common/network:generic_listener_filter_impl_base_lib",
"//source/common/network:listen_socket_lib",
"//source/common/quic:envoy_quic_utils_lib",
"@com_github_google_quiche//:quic_core_connection_lib",
],
Expand Down Expand Up @@ -434,7 +434,7 @@ envoy_cc_library(
"//source/common/http:header_map_lib",
"//source/common/http:header_utility_lib",
"//source/common/network:address_lib",
"//source/common/network:listen_socket_lib",
"//source/common/network:connection_socket_lib",
"//source/common/network:socket_option_factory_lib",
"//source/common/quic:quic_io_handle_wrapper_lib",
"@com_github_google_quiche//:quic_core_config_lib",
Expand Down
1 change: 0 additions & 1 deletion source/common/quic/envoy_quic_client_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "envoy/config/core/v3/base.pb.h"

#include "source/common/network/listen_socket_impl.h"
#include "source/common/network/socket_option_factory.h"
#include "source/common/network/udp_packet_writer_handler_impl.h"
#include "source/common/quic/envoy_quic_utils.h"
Expand Down
2 changes: 1 addition & 1 deletion source/common/quic/envoy_quic_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "source/common/http/header_map_impl.h"
#include "source/common/http/header_utility.h"
#include "source/common/network/address_impl.h"
#include "source/common/network/listen_socket_impl.h"
#include "source/common/network/connection_socket_impl.h"
#include "source/common/quic/quic_io_handle_wrapper.h"

#include "openssl/ssl.h"
Expand Down

0 comments on commit 55d67be

Please sign in to comment.