From b7e412af9d3816e4d7800821fab81e6ee5bea7f3 Mon Sep 17 00:00:00 2001 From: David Gao Date: Tue, 3 Dec 2024 11:41:33 +0800 Subject: [PATCH] add socket --- CMakeLists.txt | 1 + src/unix/core.cpp | 10 +++++--- src/unix/pipe.cpp | 6 ++--- src/unix/socket.cpp | 43 +++++++++++++++++++++++++++++++++++ src/unix/unix_base_engine.cpp | 2 +- src/unix/unix_platform.h | 12 ++++++---- 6 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 src/unix/socket.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ed703a2..30c57a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ if(UNIX) ${SOURCE_FILES} src/unix/core.cpp src/unix/pipe.cpp + src/unix/socket.cpp src/unix/unix_base_engine.cpp src/unix/select_engine.cpp) endif() diff --git a/src/unix/core.cpp b/src/unix/core.cpp index 0528bfa..c4bad75 100644 --- a/src/unix/core.cpp +++ b/src/unix/core.cpp @@ -1,8 +1,10 @@ #include #include #include + + #include "unix_platform.h" -int mark_cloexec(int fd,int set) { +int unix_cloexec(int fd,int set) { int r = 0; int flags = 0; do @@ -37,7 +39,7 @@ int mark_cloexec(int fd,int set) { return 0; } -int mark_nonblock_ioctl(int fd, int set) +int unix_nonblock_ioctl(int fd, int set) { int r; @@ -55,7 +57,7 @@ int mark_nonblock_ioctl(int fd, int set) } -int mark_nonblock_fcntl(int fd, int set) +int unix_nonblock_fcntl(int fd, int set) { int flags; int r; @@ -97,3 +99,5 @@ int mark_nonblock_fcntl(int fd, int set) return 0; } + + diff --git a/src/unix/pipe.cpp b/src/unix/pipe.cpp index bd11145..4239cd0 100644 --- a/src/unix/pipe.cpp +++ b/src/unix/pipe.cpp @@ -9,7 +9,7 @@ #include "../chez_socket.h" #include "unix_platform.h" -int make_pipe(fd_t fds[2], int read_flags, int write_flags) { +int unix_make_pipe(fd_t fds[2], int read_flags, int write_flags) { fd_t temp[2]; int err; #if defined(__linux__) || \ @@ -42,11 +42,11 @@ defined(__NetBSD__) #endif if (read_flags & O_NONBLOCK) - if ((err = mark_nonblock(temp[0], 1))) + if ((err = unix_nonblock(temp[0], 1))) goto fail; if (write_flags & O_NONBLOCK) - if ((err = mark_nonblock(temp[1], 1))) + if ((err = unix_nonblock(temp[1], 1))) goto fail; fds[0] = temp[0]; diff --git a/src/unix/socket.cpp b/src/unix/socket.cpp new file mode 100644 index 0000000..5084bbb --- /dev/null +++ b/src/unix/socket.cpp @@ -0,0 +1,43 @@ +// +// Created by david on 12/3/24. +// +#include +#include +#include +#include "unix_platform.h" + +int unix_make_socket(int domain, int type, int protocol) { + int sockfd; + int err; + +#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) + sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol); + if (sockfd != -1) + return sockfd; + + if (errno != EINVAL) + return errno; +#endif + + sockfd = socket(domain, type, protocol); + if (sockfd == -1) + return errno; + + err = unix_nonblock(sockfd, 1); + if (err == 0) + err = unix_cloexec(sockfd, 1); + + if (err) { + close(sockfd); + return err; + } + +#if defined(SO_NOSIGPIPE) + { + int on = 1; + setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on)); + } +#endif + + return sockfd; +} diff --git a/src/unix/unix_base_engine.cpp b/src/unix/unix_base_engine.cpp index 4bede0d..e8ff535 100644 --- a/src/unix/unix_base_engine.cpp +++ b/src/unix/unix_base_engine.cpp @@ -15,7 +15,7 @@ unix_base_engine::unix_base_engine() { - if (0 != make_pipe(_wakeup_fd,O_NONBLOCK,O_NONBLOCK)) + if (0 != unix_make_pipe(_wakeup_fd,O_NONBLOCK,O_NONBLOCK)) { throw std::runtime_error("Failed to create pipe for unix_base_engine"); } diff --git a/src/unix/unix_platform.h b/src/unix/unix_platform.h index e52ed33..7392333 100644 --- a/src/unix/unix_platform.h +++ b/src/unix/unix_platform.h @@ -4,9 +4,11 @@ #include "../chez_socket.h" -int mark_nonblock_ioctl(int fd, int set); -int mark_nonblock_fcntl(int fd, int set); -int make_pipe(fd_t fds[2], int read_flags, int write_flags); +int unix_cloexec(int fd,int set); +int unix_nonblock_ioctl(int fd, int set); +int unix_nonblock_fcntl(int fd, int set); +int unix_make_pipe(fd_t fds[2], int read_flags, int write_flags); +int unix_make_socket(int domain, int type, int protocol); #ifndef UNIX_PLATFORM_H #define UNIX_PLATFORM_H @@ -16,8 +18,8 @@ int make_pipe(fd_t fds[2], int read_flags, int write_flags); defined(__linux__) || \ defined(__OpenBSD__) || \ defined(__NetBSD__) -#define mark_nonblock mark_nonblock_ioctl +#define unix_nonblock unix_nonblock_ioctl #else -#define mark_nonblock mark_nonblock_fcntl +#define unix_nonblock unix_nonblock_fcntl #endif #endif //UNIX_PLATFORM_H