From 8045e26efacd8b1b1f7467002070eaf440a1c2bb Mon Sep 17 00:00:00 2001 From: zhenwei pi Date: Wed, 27 Jul 2022 09:38:25 +0800 Subject: [PATCH] Move 'connGetSocketError' to 'anetGetError' getsockopt is part of TCP, rename 'connGetSocketError' to 'anetGetError', and move it into anet.c. Signed-off-by: zhenwei pi --- src/anet.c | 9 +++++++++ src/anet.h | 1 + src/connection.h | 2 -- src/socket.c | 12 +----------- src/tls.c | 2 +- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/anet.c b/src/anet.c index 753f2fe42..d4411382c 100644 --- a/src/anet.c +++ b/src/anet.c @@ -62,6 +62,15 @@ static void anetSetError(char *err, const char *fmt, ...) va_end(ap); } +int anetGetError(int fd) { + int sockerr = 0; + socklen_t errlen = sizeof(sockerr); + + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1) + sockerr = errno; + return sockerr; +} + int anetSetBlock(char *err, int fd, int non_block) { int flags; diff --git a/src/anet.h b/src/anet.h index ff86e2029..b1a3847de 100644 --- a/src/anet.h +++ b/src/anet.h @@ -74,5 +74,6 @@ int anetFormatAddr(char *fmt, size_t fmt_len, char *ip, int port); int anetFormatFdAddr(int fd, char *buf, size_t buf_len, int fd_to_str_type); int anetPipe(int fds[2], int read_flags, int write_flags); int anetSetSockMarkId(char *err, int fd, uint32_t id); +int anetGetError(int fd); #endif diff --git a/src/connection.h b/src/connection.h index 6e07f5c28..efcc8d7f6 100644 --- a/src/connection.h +++ b/src/connection.h @@ -264,8 +264,6 @@ static inline const char *connGetInfo(connection *conn, char *buf, size_t buf_le return buf; } -int connGetSocketError(connection *conn); - /* anet-style wrappers to conns */ int connBlock(connection *conn); int connNonBlock(connection *conn); diff --git a/src/socket.c b/src/socket.c index 5372510d0..25b909475 100644 --- a/src/socket.c +++ b/src/socket.c @@ -255,7 +255,7 @@ static void connSocketEventHandler(struct aeEventLoop *el, int fd, void *clientD if (conn->state == CONN_STATE_CONNECTING && (mask & AE_WRITABLE) && conn->conn_handler) { - int conn_error = connGetSocketError(conn); + int conn_error = anetGetError(conn->fd); if (conn_error) { conn->last_errno = conn_error; conn->state = CONN_STATE_ERROR; @@ -358,16 +358,6 @@ ConnectionType CT_Socket = { .get_type = connSocketGetType }; - -int connGetSocketError(connection *conn) { - int sockerr = 0; - socklen_t errlen = sizeof(sockerr); - - if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1) - sockerr = errno; - return sockerr; -} - int connPeerToString(connection *conn, char *ip, size_t ip_len, int *port) { if (anetFdToString(conn ? conn->fd : -1, ip, ip_len, port, FD_TO_PEER_NAME) == -1) { if (conn) conn->last_errno = errno; diff --git a/src/tls.c b/src/tls.c index e8d8a4f64..52b22d455 100644 --- a/src/tls.c +++ b/src/tls.c @@ -595,7 +595,7 @@ static void tlsHandleEvent(tls_connection *conn, int mask) { switch (conn->c.state) { case CONN_STATE_CONNECTING: - conn_error = connGetSocketError((connection *) conn); + conn_error = anetGetError(conn->c.fd); if (conn_error) { conn->c.last_errno = conn_error; conn->c.state = CONN_STATE_ERROR;