From 1dd3a5cac42fe9fea3de05b9bcc2857b5e283774 Mon Sep 17 00:00:00 2001 From: Greg V Date: Sun, 30 Sep 2018 15:56:50 +0300 Subject: [PATCH] Allow creating sockets from raw file descriptors This is useful for socket activation, sandboxes, etc. --- src/socket.cr | 2 +- src/socket/tcp_server.cr | 5 +++++ src/socket/tcp_socket.cr | 5 +++++ src/socket/unix_server.cr | 5 +++++ src/socket/unix_socket.cr | 3 ++- 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/socket.cr b/src/socket.cr index 34e28e3b822b..9f3e7bfe0c4e 100644 --- a/src/socket.cr +++ b/src/socket.cr @@ -79,7 +79,7 @@ class Socket < IO end end - protected def initialize(@fd : Int32, @family, @type, @protocol = Protocol::IP, blocking = false) + def initialize(@fd : Int32, @family, @type, @protocol = Protocol::IP, blocking = false) @closed = false init_close_on_exec(@fd) diff --git a/src/socket/tcp_server.cr b/src/socket/tcp_server.cr index badb99b0f1cc..6809b833a6ba 100644 --- a/src/socket/tcp_server.cr +++ b/src/socket/tcp_server.cr @@ -48,6 +48,11 @@ class TCPServer < TCPSocket end end + # Creates a TCPServer from an already configured raw file descriptor + def initialize(*, fd : Int32, family : Family = Family::INET) + super(fd, family) + end + # Creates a new TCP server, listening on all local interfaces (`::`). def self.new(port : Int, backlog = SOMAXCONN, reuse_port = false) new("::", port, backlog, reuse_port: reuse_port) diff --git a/src/socket/tcp_socket.cr b/src/socket/tcp_socket.cr index 56b080ccb360..7a5bdbcd2d0f 100644 --- a/src/socket/tcp_socket.cr +++ b/src/socket/tcp_socket.cr @@ -42,6 +42,11 @@ class TCPSocket < IPSocket super fd, family, type, protocol end + # Creates a TCPSocket from an already configured raw file descriptor + def initialize(*, fd : Int32, family : Family = Family::INET) + super fd, family, Type::STREAM, Protocol::TCP + end + # Opens a TCP socket to a remote TCP server, yields it to the block, then # eventually closes the socket when the block returns. # diff --git a/src/socket/unix_server.cr b/src/socket/unix_server.cr index 1562674129d6..50c760d986e4 100644 --- a/src/socket/unix_server.cr +++ b/src/socket/unix_server.cr @@ -45,6 +45,11 @@ class UNIXServer < UNIXSocket end end + # Creates a UNIXServer from an already configured raw file descriptor + def initialize(*, fd : Int32, type : Type = Type::STREAM, @path : String? = nil) + super(fd, type, path) + end + # Creates a new UNIX server and yields it to the block. Eventually closes the # server socket when the block returns. # diff --git a/src/socket/unix_socket.cr b/src/socket/unix_socket.cr index 9956306a929b..bd676831dc21 100644 --- a/src/socket/unix_socket.cr +++ b/src/socket/unix_socket.cr @@ -28,7 +28,8 @@ class UNIXSocket < Socket super family, type, Protocol::IP end - protected def initialize(fd : Int32, type : Type, @path : String? = nil) + # Creates a UNIXSocket from an already configured raw file descriptor + def initialize(*, fd : Int32, type : Type = Type::STREAM, @path : String? = nil) super fd, Family::UNIX, type, Protocol::IP end