diff --git a/net/stack.cc b/net/stack.cc index c30f861c07..8a9f922dcc 100644 --- a/net/stack.cc +++ b/net/stack.cc @@ -14,26 +14,15 @@ namespace net { -class native_network_stack; - template class native_server_socket_impl; template class native_connected_socket_impl; -template -class native_connected_socket_impl : public connected_socket_impl { - typename Protocol::connection _conn; - class native_data_source_impl; - class native_data_sink_impl; -public: - explicit native_connected_socket_impl(typename Protocol::connection conn) - : _conn(std::move(conn)) {} - virtual input_stream input() override; - virtual output_stream output() override; -}; +class native_network_stack; +// native_server_socket_impl template class native_server_socket_impl : public server_socket_impl { typename Protocol::listener _listener; @@ -42,39 +31,6 @@ public: virtual future accept() override; }; - - -class native_network_stack : public network_stack { - static std::unique_ptr _s; - interface _netif; - ipv4 _inet; - udp_v4 _udp; - using tcp4 = tcp; -public: - explicit native_network_stack(boost::program_options::variables_map opts); - virtual server_socket listen(socket_address sa, listen_options opt) override; - virtual udp_channel make_udp_channel(ipv4_addr addr) override; - static std::unique_ptr create(boost::program_options::variables_map opts) { - return std::make_unique(opts); - } - friend class native_server_socket_impl; -}; - -udp_channel -native_network_stack::make_udp_channel(ipv4_addr addr) { - return _udp.make_channel(addr); -} - -native_network_stack::native_network_stack(boost::program_options::variables_map opts) - : _netif(smp::main_thread() ? create_virtio_net_device(opts["tap-device"].as(), opts) : create_proxy_net_device(opts)) - , _inet(&_netif) - , _udp(_inet) { - _inet.set_host_address(ipv4_address(opts["host-ipv4-addr"].as())); - _inet.set_gw_address(ipv4_address(opts["gw-ipv4-addr"].as())); - _inet.set_netmask_address(ipv4_address(opts["netmask-ipv4-addr"].as())); - _udp.set_queue_size(opts["udpv4-queue-size"].as()); -} - template native_server_socket_impl::native_server_socket_impl(Protocol& proto, uint16_t port, listen_options opt) : _listener(proto.listen(port)) { @@ -90,12 +46,18 @@ native_server_socket_impl::accept() { }); } -server_socket -native_network_stack::listen(socket_address sa, listen_options opts) { - assert(sa.as_posix_sockaddr().sa_family == AF_INET); - return server_socket(std::make_unique>( - _inet.get_tcp(), ntohs(sa.as_posix_sockaddr_in().sin_port), opts)); -} +// native_connected_socket_impl +template +class native_connected_socket_impl : public connected_socket_impl { + typename Protocol::connection _conn; + class native_data_source_impl; + class native_data_sink_impl; +public: + explicit native_connected_socket_impl(typename Protocol::connection conn) + : _conn(std::move(conn)) {} + virtual input_stream input() override; + virtual output_stream output() override; +}; template class native_connected_socket_impl::native_data_source_impl final @@ -160,6 +122,44 @@ native_connected_socket_impl::output() { return output_stream(std::move(ds), 8192); } +// native_network_stack +class native_network_stack : public network_stack { + static std::unique_ptr _s; + interface _netif; + ipv4 _inet; + udp_v4 _udp; + using tcp4 = tcp; +public: + explicit native_network_stack(boost::program_options::variables_map opts); + virtual server_socket listen(socket_address sa, listen_options opt) override; + virtual udp_channel make_udp_channel(ipv4_addr addr) override; + static std::unique_ptr create(boost::program_options::variables_map opts) { + return std::make_unique(opts); + } + friend class native_server_socket_impl; +}; + +udp_channel +native_network_stack::make_udp_channel(ipv4_addr addr) { + return _udp.make_channel(addr); +} + +native_network_stack::native_network_stack(boost::program_options::variables_map opts) + : _netif(smp::main_thread() ? create_virtio_net_device(opts["tap-device"].as(), opts) : create_proxy_net_device(opts)) + , _inet(&_netif) + , _udp(_inet) { + _inet.set_host_address(ipv4_address(opts["host-ipv4-addr"].as())); + _inet.set_gw_address(ipv4_address(opts["gw-ipv4-addr"].as())); + _inet.set_netmask_address(ipv4_address(opts["netmask-ipv4-addr"].as())); + _udp.set_queue_size(opts["udpv4-queue-size"].as()); +} + +server_socket +native_network_stack::listen(socket_address sa, listen_options opts) { + assert(sa.as_posix_sockaddr().sa_family == AF_INET); + return server_socket(std::make_unique>( + _inet.get_tcp(), ntohs(sa.as_posix_sockaddr_in().sin_port), opts)); +} std::unique_ptr native_network_stack::_s;