From 7c505f917a54e321bef8425c7fef803e798fc907 Mon Sep 17 00:00:00 2001 From: Sukchan Lee Date: Thu, 16 Nov 2017 01:51:05 +0000 Subject: [PATCH] minor update --- lib/core/include/core_sock.h | 82 +++++++++++++++++++----------------- lib/core/include/core_tcp.h | 2 +- lib/core/include/core_udp.h | 2 +- lib/core/src/unix/sock.c | 39 +++++++++-------- lib/core/src/unix/tcp.c | 4 +- lib/core/src/unix/udp.c | 4 +- lib/core/test/testsock.c | 18 ++++---- 7 files changed, 77 insertions(+), 74 deletions(-) diff --git a/lib/core/include/core_sock.h b/lib/core/include/core_sock.h index 996fc9332..3d5777d4f 100644 --- a/lib/core/include/core_sock.h +++ b/lib/core/include/core_sock.h @@ -12,9 +12,6 @@ extern "C" { #endif /* __cplusplus */ -#define SOCK_F_BIND (1 << 0) -#define SOCK_F_CONNECT (1 << 1) - #define SOCK_NTOP(__aDDR, __bUF) \ ((struct sockaddr_in *)__aDDR)->sin_family == AF_INET ? \ inet_ntop(AF_INET, &(((struct sockaddr_in *)__aDDR)->sin_addr), \ @@ -24,52 +21,59 @@ extern "C" { __bUF, INET6_ADDRSTRLEN) : "Unknown Family" /** - * @defgroup core_sockopt Socket option definitions + * @defgroup sock_flags Socket flags definitions * @{ */ -#define CORE_SO_LINGER 1 /**< Linger */ -#define CORE_SO_KEEPALIVE 2 /**< Keepalive */ -#define CORE_SO_DEBUG 4 /**< Debug */ -#define CORE_SO_NONBLOCK 8 /**< Non-blocking IO */ -#define CORE_SO_REUSEADDR 16 /**< Reuse addresses */ -#define CORE_SO_SNDBUF 64 /**< Send buffer */ -#define CORE_SO_RCVBUF 128 /**< Receive buffer */ -#define CORE_SO_DISCONNECTED 256 /**< Disconnected */ -#define CORE_TCP_NODELAY 512 /**< For SCTP sockets, this is mapped +#define SOCK_F_BIND (1 << 0) +#define SOCK_F_CONNECT (1 << 1) + +/** + * @defgroup sock_option Socket option definitions + * @{ + */ +#define SOCK_O_LINGER (1 << 0) /**< Linger */ +#define SOCK_O_KEEPALIVE (1 << 1) /**< Keepalive */ +#define SOCK_O_DEBUG (1 << 2) /**< Debug */ +#define SOCK_O_NONBLOCK (1 << 3) /**< Non-blocking IO */ +#define SOCK_O_REUSEADDR (1 << 4) /**< Reuse addresses */ +#define SOCK_O_SNDBUF (1 << 5) /**< Send buffer */ +#define SOCK_O_RCVBUF (1 << 6) /**< Receive buffer */ +#define SOCK_O_DISCONNECTED (1 << 7) /**< Disconnected */ +#define SOCK_O_TCP_NODELAY (1 << 8) /**< For SCTP sockets, this is mapped * to STCP_NODELAY internally. */ -#define CORE_TCP_NOPUSH 1024 /**< No push */ -#define CORE_RESET_NODELAY 2048 /**< This flag is ONLY set internally - * when we set CORE_TCP_NOPUSH with - * CORE_TCP_NODELAY set to tell us that - * CORE_TCP_NODELAY should be turned on +#define SOCK_O_TCP_NOPUSH (1 << 9) /**< No push */ +#define SOCK_O_RESET_NODELAY (1 << 10) /**< This flag is ONLY set internally + * when we set SOCK_O_TCP_NOPUSH with + * SOCK_O_TCP_NODELAY set to tell us that + * SOCK_O_TCP_NODELAY should be turned on * again when NOPUSH is turned off */ -#define CORE_INCOMPLETE_READ 4096 /**< Set on non-blocking sockets - * (timeout != 0) on which the - * previous read() did not fill a buffer - * completely. the next apr_socket_recv() +#define SOCK_O_INCOMPLETE_READ (1 << 11) /**< Set on non-blocking sockets + * (timeout != 0) on which the + * previous read() did not fill a buffer + * completely. the next sock_recv() * will first call select()/poll() rather than - * going straight into read(). (Can also - * be set by an application to force a - * select()/poll() call before the next - * read, in cases where the app expects - * that an immediate read would fail.) - */ -#define CORE_INCOMPLETE_WRITE 8192 /**< like CORE_INCOMPLETE_READ, but for write - * @see CORE_INCOMPLETE_READ + * going straight into read(). (Can also + * be set by an application to force a + * select()/poll() call before the next + * read, in cases where the app expects + * that an immediate read would fail.) */ -#define CORE_IPV6_V6ONLY 16384 /**< Don't accept IPv4 connections on an - * IPv6 listening socket. +#define SOCK_O_INCOMPLETE_WRITE (1 << 12) /**< like SOCK_O_INCOMPLETE_READ, but + * for write + * @see SOCK_O_INCOMPLETE_READ */ -#define CORE_TCP_DEFER_ACCEPT 32768 /**< Delay accepting of new connections - * until data is available. +#define SOCK_O_IPV6_V6ONLY (1 << 13) /**< Don't accept IPv4 connections + * on a IPv6 listening socket. + */ +#define SOCK_O_TCP_DEFER_ACCEPT (1 << 14) /**< Delay accepting of new + * connections until data is available. * @see apr_socket_accept_filter */ -#define CORE_SO_BROADCAST 65536 /**< Allow broadcast - */ -#define CORE_SO_FREEBIND 131072 /**< Allow binding to addresses not owned - * by any interface +#define SOCK_O_BROADCAST (1 << 15) /**< Allow broadcast */ +#define SOCK_O_FREEBIND (1 << 16) /**< Allow binding to addresses not + * owned by any interface */ typedef c_uintptr_t sock_id; @@ -82,7 +86,7 @@ CORE_DECLARE(status_t) sock_create( sock_id *id, int family, int type, int protocol, int flags); CORE_DECLARE(status_t) sock_delete(sock_id id); -CORE_DECLARE(status_t) sock_opt_set(sock_id id, c_int32_t opt, c_int32_t on); +CORE_DECLARE(status_t) sock_setsockopt(sock_id id, c_int32_t opt, c_int32_t on); CORE_DECLARE(status_t) sock_bind(sock_id id, const char *host, c_uint16_t port); diff --git a/lib/core/include/core_tcp.h b/lib/core/include/core_tcp.h index 7716b9bd4..3767db7e0 100644 --- a/lib/core/include/core_tcp.h +++ b/lib/core/include/core_tcp.h @@ -7,7 +7,7 @@ extern "C" { #endif /* __cplusplus */ -CORE_DECLARE(status_t) tcp_create(sock_id *new, +CORE_DECLARE(status_t) tcp_open(sock_id *new, int family, const char *local_host, c_uint16_t local_port, const char *remote_host, c_uint16_t remote_port, diff --git a/lib/core/include/core_udp.h b/lib/core/include/core_udp.h index baee0c936..ea2a18d79 100644 --- a/lib/core/include/core_udp.h +++ b/lib/core/include/core_udp.h @@ -7,7 +7,7 @@ extern "C" { #endif /* __cplusplus */ -CORE_DECLARE(status_t) udp_create(sock_id *new, +CORE_DECLARE(status_t) udp_open(sock_id *new, int family, const char *local_host, c_uint16_t local_port, const char *remote_host, c_uint16_t remote_port, diff --git a/lib/core/src/unix/sock.c b/lib/core/src/unix/sock.c index 32369bcb8..bf8d1ffd5 100644 --- a/lib/core/src/unix/sock.c +++ b/lib/core/src/unix/sock.c @@ -96,7 +96,7 @@ status_t sock_delete(sock_id id) return CORE_OK; } -status_t sock_opt_set(sock_id id, c_int32_t opt, c_int32_t on) +status_t sock_setsockopt(sock_id id, c_int32_t opt, c_int32_t on) { sock_t *sock = (sock_t *)id; int one; @@ -111,19 +111,19 @@ status_t sock_opt_set(sock_id id, c_int32_t opt, c_int32_t on) switch(opt) { - case CORE_SO_REUSEADDR: - if (on != sock_is_option_set(sock, CORE_SO_REUSEADDR)) + case SOCK_O_REUSEADDR: + if (on != sock_is_option_set(sock, SOCK_O_REUSEADDR)) { if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(int)) == -1) { return errno; } - sock_set_option(sock, CORE_SO_REUSEADDR, on); + sock_set_option(sock, SOCK_O_REUSEADDR, on); } break; - case CORE_SO_NONBLOCK: - if (sock_is_option_set(sock, CORE_SO_NONBLOCK) != on) + case SOCK_O_NONBLOCK: + if (sock_is_option_set(sock, SOCK_O_NONBLOCK) != on) { if (on) { @@ -135,7 +135,7 @@ status_t sock_opt_set(sock_id id, c_int32_t opt, c_int32_t on) if ((rv = soblock(sock->fd)) != CORE_OK) return rv; } - sock_set_option(sock, CORE_SO_NONBLOCK, on); + sock_set_option(sock, SOCK_O_NONBLOCK, on); } break; default: @@ -165,7 +165,7 @@ status_t sock_bind(sock_id id, const char *host, c_uint16_t port) if (sock->fd < 0) continue; - if (sock_opt_set(id, CORE_SO_REUSEADDR, 1) == CORE_ERROR) + if (sock_setsockopt(id, SOCK_O_REUSEADDR, 1) == CORE_ERROR) { d_error("setsockopt(%s:%d) failed(%d:%s)", host, port, errno, strerror(errno)); @@ -277,7 +277,6 @@ status_t sock_accept(sock_id *new, sock_id id) d_assert(rv == CORE_OK && (*new), return CORE_ERROR,); remote_sock = (sock_t *)(*new); remote_sock->fd = remote_fd; - remote_sock->flags = SOCK_F_CONNECT; return CORE_OK; } @@ -290,16 +289,16 @@ ssize_t sock_send(sock_id id, const void *buf, size_t len, int flags, d_assert(id, return -1, ); - if (sock->flags & SOCK_F_CONNECT) - { - size = send(sock->fd, buf, len, flags); - } - else + if (sock->type == SOCK_DGRAM && !(sock->flags & SOCK_F_CONNECT)) { d_assert(dest_addr, return -1,); d_assert(addrlen, return -1,); size = sendto(sock->fd, buf, len, flags, dest_addr, addrlen); } + else + { + size = send(sock->fd, buf, len, flags); + } if (size < 0) { @@ -317,15 +316,15 @@ ssize_t sock_recv(sock_id id, void *buf, size_t len, int flags, d_assert(id, return -1,); - if (sock->flags & SOCK_F_CONNECT) - { - size = recv(sock->fd, buf, len, flags); - } - else + if (sock->type == SOCK_DGRAM && !(sock->flags & SOCK_F_CONNECT)) { *addrlen = sizeof(struct sockaddr); size = recvfrom(sock->fd, buf, len, flags, src_addr, addrlen); } + else + { + size = recv(sock->fd, buf, len, flags); + } if (size < 0) { @@ -347,7 +346,7 @@ status_t sock_register(sock_id id, sock_handler handler, void *data) return CORE_ERROR; } - if (sock_opt_set(id, CORE_SO_NONBLOCK, 1) == CORE_ERROR) + if (sock_setsockopt(id, SOCK_O_NONBLOCK, 1) == CORE_ERROR) { d_error("cannot set socket to non-block"); return CORE_ERROR; diff --git a/lib/core/src/unix/tcp.c b/lib/core/src/unix/tcp.c index 2e92a0b56..87f36c816 100644 --- a/lib/core/src/unix/tcp.c +++ b/lib/core/src/unix/tcp.c @@ -1,9 +1,9 @@ #define TRACE_MODULE _tcp #include "core_debug.h" -#include "core_udp.h" +#include "core_sock.h" -status_t tcp_create(sock_id *new, +status_t tcp_open(sock_id *new, int family, const char *local_host, c_uint16_t local_port, const char *remote_host, c_uint16_t remote_port, diff --git a/lib/core/src/unix/udp.c b/lib/core/src/unix/udp.c index b0b602a00..5c2d8a413 100644 --- a/lib/core/src/unix/udp.c +++ b/lib/core/src/unix/udp.c @@ -1,9 +1,9 @@ #define TRACE_MODULE _udp #include "core_debug.h" -#include "core_udp.h" +#include "core_sock.h" -status_t udp_create(sock_id *new, +status_t udp_open(sock_id *new, int family, const char *local_host, c_uint16_t local_port, const char *remote_host, c_uint16_t remote_port, diff --git a/lib/core/test/testsock.c b/lib/core/test/testsock.c index 2bf531150..239e9a16d 100644 --- a/lib/core/test/testsock.c +++ b/lib/core/test/testsock.c @@ -16,20 +16,20 @@ static void sock_test1(abts_case *tc, void *data) sock_id udp; status_t rv; - rv = udp_create(&udp, AF_UNSPEC, 0, SRV_PORT, NULL, 0, SOCK_F_BIND); + rv = udp_open(&udp, AF_UNSPEC, 0, SRV_PORT, NULL, 0, SOCK_F_BIND); ABTS_INT_EQUAL(tc, CORE_OK, rv); rv = sock_delete(udp); ABTS_INT_EQUAL(tc, CORE_OK, rv); - rv = udp_create(&udp, AF_UNSPEC, + rv = udp_open(&udp, AF_UNSPEC, "127.0.0.1", SRV_PORT, NULL, 0, SOCK_F_BIND); ABTS_INT_EQUAL(tc, CORE_OK, rv); rv = sock_delete(udp); ABTS_INT_EQUAL(tc, CORE_OK, rv); - rv = udp_create(&udp, AF_UNSPEC, "::1", SRV_PORT, NULL, 0, SOCK_F_BIND); + rv = udp_open(&udp, AF_UNSPEC, "::1", SRV_PORT, NULL, 0, SOCK_F_BIND); ABTS_INT_EQUAL(tc, CORE_OK, rv); rv = sock_delete(udp); @@ -45,7 +45,7 @@ static void *THREAD_FUNC test2_main(thread_id id, void *data) char str[STRLEN]; ssize_t size; - rv = tcp_create(&tcp, AF_UNSPEC, NULL, 0, "::1", SRV_PORT, SOCK_F_CONNECT); + rv = tcp_open(&tcp, AF_UNSPEC, NULL, 0, "::1", SRV_PORT, SOCK_F_CONNECT); ABTS_INT_EQUAL(tc, CORE_OK, rv); size = sock_recv(tcp, str, STRLEN, 0, NULL, NULL); @@ -64,7 +64,7 @@ static void sock_test2(abts_case *tc, void *data) status_t rv; ssize_t size; - rv = tcp_create(&tcp, AF_INET6, NULL, SRV_PORT, NULL, 0, SOCK_F_BIND); + rv = tcp_open(&tcp, AF_INET6, NULL, SRV_PORT, NULL, 0, SOCK_F_BIND); ABTS_INT_EQUAL(tc, CORE_OK, rv); rv = thread_create(&test2_thread, NULL, test2_main, tc); @@ -98,7 +98,7 @@ static void *THREAD_FUNC test3_main(thread_id id, void *data) ssize_t size; int rc; - rv = udp_create(&udp, AF_INET, NULL, 0, NULL, 0, 0); + rv = udp_open(&udp, AF_INET, NULL, 0, NULL, 0, 0); ABTS_INT_EQUAL(tc, CORE_OK, rv); memset(&dst_addr, 0, sizeof(dst_addr)); @@ -128,7 +128,7 @@ static void sock_test3(abts_case *tc, void *data) char str[STRLEN]; char buf[INET6_ADDRSTRLEN]; - rv = udp_create(&udp, AF_INET, NULL, SRV_PORT, NULL, 0, SOCK_F_BIND); + rv = udp_open(&udp, AF_INET, NULL, SRV_PORT, NULL, 0, SOCK_F_BIND); ABTS_INT_EQUAL(tc, CORE_OK, rv); rv = thread_create(&test3_thread, NULL, test3_main, tc); @@ -155,7 +155,7 @@ static void *THREAD_FUNC test4_main(thread_id id, void *data) char str[STRLEN]; ssize_t size; - rv = udp_create(&udp, AF_UNSPEC, + rv = udp_open(&udp, AF_UNSPEC, NULL, 0, "127.0.0.1", SRV_PORT, SOCK_F_CONNECT); ABTS_INT_EQUAL(tc, CORE_OK, rv); @@ -181,7 +181,7 @@ static void sock_test4(abts_case *tc, void *data) socklen_t addrlen; char str[STRLEN]; - rv = udp_create(&udp, AF_INET, NULL, SRV_PORT, NULL, 0, SOCK_F_BIND); + rv = udp_open(&udp, AF_INET, NULL, SRV_PORT, NULL, 0, SOCK_F_BIND); ABTS_INT_EQUAL(tc, CORE_OK, rv); rv = thread_create(&test4_thread, NULL, test4_main, tc);