diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-04-21 13:46:18 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-04-21 13:46:18 +0100 |
commit | a9f2d1158d616aaec93478440902c09866a84194 (patch) | |
tree | c04b547a3977ba3fb068457fe6de910b1897faad | |
parent | 5f25b69c03972b0adff6bc649e1922111d0ebb2b (diff) | |
download | rspamd-a9f2d1158d616aaec93478440902c09866a84194.tar.gz rspamd-a9f2d1158d616aaec93478440902c09866a84194.zip |
Implement unified accept function.
-rw-r--r-- | src/util.c | 70 | ||||
-rw-r--r-- | src/util.h | 12 |
2 files changed, 43 insertions, 39 deletions
diff --git a/src/util.c b/src/util.c index d5a4a19df..704200374 100644 --- a/src/util.c +++ b/src/util.c @@ -195,41 +195,6 @@ make_udp_socket (struct addrinfo *addr, gboolean is_server, gboolean async) } gint -accept_from_socket (gint listen_sock, struct sockaddr *addr, socklen_t * len) -{ - gint nfd; - gint serrno; - - if ((nfd = accept (listen_sock, addr, len)) == -1) { - if (errno == EAGAIN) { - return 0; - } - msg_warn ("accept failed: %d, '%s'", errno, strerror (errno)); - return -1; - } - if (make_socket_nonblocking (nfd) < 0) { - goto out; - } - - /* Set close on exec */ - if (fcntl (nfd, F_SETFD, FD_CLOEXEC) == -1) { - msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno)); - goto out; - } - - - - return (nfd); - - out: - serrno = errno; - close (nfd); - errno = serrno; - return (-1); - -} - -gint make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboolean is_server, gboolean async) { gint fd = -1, s_error, r, optlen, serrno, on = 1; @@ -2218,6 +2183,41 @@ rspamd_ucl_emit_gstring (ucl_object_t *obj, enum ucl_emitter emit_type, GString ucl_object_emit_full (obj, emit_type, &func); } +gint +rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t *addr) +{ + gint nfd, serrno; + socklen_t len = sizeof (addr->addr.ss); + + if ((nfd = accept (sock, &addr->addr.sa, &len)) == -1) { + if (errno == EAGAIN) { + return 0; + } + return -1; + } + + addr->slen = len; + addr->af = addr->addr.sa.sa_family; + + if (make_socket_nonblocking (nfd) < 0) { + goto out; + } + + /* Set close on exec */ + if (fcntl (nfd, F_SETFD, FD_CLOEXEC) == -1) { + msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno)); + goto out; + } + + return (nfd); + + out: + serrno = errno; + close (nfd); + errno = serrno; + return (-1); + +} /* * vi:ts=4 */ diff --git a/src/util.h b/src/util.h index 972730d04..32f0dcd85 100644 --- a/src/util.h +++ b/src/util.h @@ -41,10 +41,7 @@ gint make_tcp_socket (struct addrinfo *, gboolean is_server, gboolean async); * Create socket and bind or connect it to specified address and port */ gint make_udp_socket (struct addrinfo *, gboolean is_server, gboolean async); -/* - * Accept from socket - */ -gint accept_from_socket (gint listen_sock, struct sockaddr *addr, socklen_t *len); + /* * Create and bind or connect unix socket */ @@ -461,5 +458,12 @@ gboolean rspamd_ip_is_valid (void *ptr, int af); */ void rspamd_ucl_emit_gstring (ucl_object_t *obj, enum ucl_emitter emit_type, GString *target); +/** + * Accept from listening socket filling addr structure + * @param sock listening socket + * @param addr + * @return + */ +gint rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t *addr); #endif |