From 8218a098e8fc4d7bded7e38218b74345ee60d3f3 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 19 May 2020 08:17:44 +0900 Subject: [PATCH] server: Replace `net2` crate with `socket2` --- actix-server/CHANGES.md | 6 ++++++ actix-server/Cargo.toml | 2 +- actix-server/src/builder.rs | 16 +++++++++------- actix-server/tests/test_server.rs | 10 +++++----- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/actix-server/CHANGES.md b/actix-server/CHANGES.md index 127e7262..7ec1c1b3 100644 --- a/actix-server/CHANGES.md +++ b/actix-server/CHANGES.md @@ -1,5 +1,11 @@ # Changes +## [Unreleased] + +### Changed + +* Replace deprecated `net2` crate with `socket2` + ## [1.0.2] - 2020-02-26 ### Fixed diff --git a/actix-server/Cargo.toml b/actix-server/Cargo.toml index bf9ee344..f6dd8b61 100644 --- a/actix-server/Cargo.toml +++ b/actix-server/Cargo.toml @@ -29,7 +29,7 @@ actix-utils = "1.0.4" log = "0.4" num_cpus = "1.11" mio = "0.6.19" -net2 = "0.2" +socket2 = "0.3" futures-channel = { version = "0.3.4", default-features = false } futures-util = { version = "0.3.4", default-features = false, features = ["sink"] } slab = "0.4" diff --git a/actix-server/src/builder.rs b/actix-server/src/builder.rs index 27475cfc..d43657f2 100644 --- a/actix-server/src/builder.rs +++ b/actix-server/src/builder.rs @@ -12,7 +12,7 @@ use futures_util::future::ready; use futures_util::stream::FuturesUnordered; use futures_util::{ready, future::Future, FutureExt, stream::Stream, StreamExt}; use log::{error, info}; -use net2::TcpBuilder; +use socket2::{Domain, Protocol, Socket, Type}; use crate::accept::{AcceptLoop, AcceptNotify, Command}; use crate::config::{ConfiguredService, ServiceConfig}; @@ -487,11 +487,13 @@ pub(super) fn bind_addr( } fn create_tcp_listener(addr: net::SocketAddr, backlog: i32) -> io::Result { - let builder = match addr { - net::SocketAddr::V4(_) => TcpBuilder::new_v4()?, - net::SocketAddr::V6(_) => TcpBuilder::new_v6()?, + let domain = match addr { + net::SocketAddr::V4(_) => Domain::ipv4(), + net::SocketAddr::V6(_) => Domain::ipv6(), }; - builder.reuse_address(true)?; - builder.bind(addr)?; - Ok(builder.listen(backlog)?) + let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?; + socket.set_reuse_address(true)?; + socket.bind(&addr.into())?; + socket.listen(backlog)?; + Ok(socket.into_tcp_listener()) } diff --git a/actix-server/tests/test_server.rs b/actix-server/tests/test_server.rs index e150f56b..0e9f7b6d 100644 --- a/actix-server/tests/test_server.rs +++ b/actix-server/tests/test_server.rs @@ -5,14 +5,14 @@ use std::{net, thread, time}; use actix_server::Server; use actix_service::fn_service; use futures_util::future::{lazy, ok}; -use net2::TcpBuilder; +use socket2::{Domain, Protocol, Socket, Type}; fn unused_addr() -> net::SocketAddr { let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap(); - let socket = TcpBuilder::new_v4().unwrap(); - socket.bind(&addr).unwrap(); - socket.reuse_address(true).unwrap(); - let tcp = socket.to_tcp_listener().unwrap(); + let socket = Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp())).unwrap(); + socket.bind(&addr.into()).unwrap(); + socket.set_reuse_address(true).unwrap(); + let tcp = socket.into_tcp_listener(); tcp.local_addr().unwrap() }