diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-11-25 17:33:56 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-11-25 17:34:10 +0000 |
commit | 83ba6762cc43d9db581b979bb5e3445669e46cc2 (patch) | |
tree | 2e69833b43f791ed253a7a20318b767ebe56cdb8 /src/libnetdata/socket/socket.c | |
parent | Releasing debian version 1.47.5-1. (diff) | |
download | netdata-83ba6762cc43d9db581b979bb5e3445669e46cc2.tar.xz netdata-83ba6762cc43d9db581b979bb5e3445669e46cc2.zip |
Merging upstream version 2.0.3+dfsg (Closes: #923993, #1042533, #1045145).
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/libnetdata/socket/socket.c')
-rw-r--r-- | src/libnetdata/socket/socket.c | 104 |
1 files changed, 50 insertions, 54 deletions
diff --git a/src/libnetdata/socket/socket.c b/src/libnetdata/socket/socket.c index f907fefeb..3b0d1f824 100644 --- a/src/libnetdata/socket/socket.c +++ b/src/libnetdata/socket/socket.c @@ -119,22 +119,17 @@ bool fd_is_socket(int fd) { return true; } -bool sock_has_output_error(int fd) { - if(fd < 0) { - //internal_error(true, "invalid socket %d", fd); - return false; - } +#ifdef POLLRDHUP +bool is_socket_closed(int fd) { + if(fd < 0) + return true; // if(!fd_is_socket(fd)) { // //internal_error(true, "fd %d is not a socket", fd); // return false; // } - short int errors = POLLERR | POLLHUP | POLLNVAL; - -#ifdef POLLRDHUP - errors |= POLLRDHUP; -#endif + short int errors = POLLERR | POLLHUP | POLLNVAL | POLLRDHUP; struct pollfd pfd = { .fd = fd, @@ -149,6 +144,31 @@ bool sock_has_output_error(int fd) { return ((pfd.revents & errors) || !(pfd.revents & POLLOUT)); } +#else +bool is_socket_closed(int fd) { + if(fd < 0) + return true; + + char buffer; + ssize_t result = recv(fd, &buffer, 1, MSG_PEEK | MSG_DONTWAIT); + if (result == 0) { + // Connection closed + return true; + } + else if (result < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + // No data available, but socket is still open + return false; + } else { + // An error occurred + return true; + } + } + + // Data is available, socket is open + return false; +} +#endif int sock_setnonblock(int fd) { int flags; @@ -515,7 +535,6 @@ HTTP_ACL socket_ssl_acl(char *acl) { //Due the format of the SSL command it is always the last command, //we finish it here to avoid problems with the ACLs *ssl = '\0'; -#ifdef ENABLE_HTTPS ssl++; if (!strncmp("SSL=",ssl,4)) { ssl += 4; @@ -526,7 +545,6 @@ HTTP_ACL socket_ssl_acl(char *acl) { return HTTP_ACL_SSL_FORCE; } } -#endif } return HTTP_ACL_NONE; @@ -558,7 +576,7 @@ static inline int bind_to_this(LISTEN_SOCKETS *sockets, const char *definition, char buffer2[10 + 1]; snprintfz(buffer2, 10, "%d", default_port); - char *ip = buffer, *port = buffer2, *interface = "", *portconfig; + char *ip = buffer, *port = buffer2, *iface = "", *portconfig; int protocol = IPPROTO_TCP, socktype = SOCK_STREAM; const char *protocol_str = "tcp"; @@ -613,7 +631,7 @@ static inline int bind_to_this(LISTEN_SOCKETS *sockets, const char *definition, if(*e == '%') { *e = '\0'; e++; - interface = e; + iface = e; while(*e && *e != ':' && *e != '=') e++; } @@ -650,13 +668,13 @@ static inline int bind_to_this(LISTEN_SOCKETS *sockets, const char *definition, } uint32_t scope_id = 0; - if(*interface) { - scope_id = if_nametoindex(interface); + if(*iface) { + scope_id = if_nametoindex(iface); if(!scope_id) nd_log(NDLS_DAEMON, NDLP_ERR, "LISTENER: Cannot find a network interface named '%s'. " "Continuing with limiting the network interface", - interface); + iface); } if(!*ip || *ip == '*' || !strcmp(ip, "any") || !strcmp(ip, "all")) @@ -750,9 +768,9 @@ int listen_sockets_setup(LISTEN_SOCKETS *sockets) { } else sockets->default_port = (uint16_t)new_port; - char *s = appconfig_get(sockets->config, sockets->config_section, "bind to", sockets->default_bind_to); + const char *s = appconfig_get(sockets->config, sockets->config_section, "bind to", sockets->default_bind_to); while(*s) { - char *e = s; + const char *e = s; // skip separators, moving both s(tart) and e(nd) while(isspace((uint8_t)*e) || *e == ',') s = ++e; @@ -935,12 +953,10 @@ int connect_to_this_ip46( hostBfr, servBfr); // Convert 'struct timeval' to milliseconds for poll(): - int timeout_ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + int timeout_ms = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : 1000; switch(wait_on_socket_or_cancel_with_timeout( -#ifdef ENABLE_HTTPS - NULL, -#endif + NULL, fd, timeout_ms, POLLOUT, NULL)) { case 0: // proceed nd_log(NDLS_DAEMON, NDLP_DEBUG, @@ -1019,7 +1035,7 @@ int connect_to_this(const char *definition, int default_port, struct timeval *ti char default_service[10 + 1]; snprintfz(default_service, 10, "%d", default_port); - char *host = buffer, *service = default_service, *interface = ""; + char *host = buffer, *service = default_service, *iface = ""; int protocol = IPPROTO_TCP, socktype = SOCK_STREAM; uint32_t scope_id = 0; @@ -1058,7 +1074,7 @@ int connect_to_this(const char *definition, int default_port, struct timeval *ti if(*e == '%') { *e = '\0'; e++; - interface = e; + iface = e; while(*e && *e != ':') e++; } @@ -1076,12 +1092,12 @@ int connect_to_this(const char *definition, int default_port, struct timeval *ti return -1; } - if(*interface) { - scope_id = if_nametoindex(interface); + if(*iface) { + scope_id = if_nametoindex(iface); if(!scope_id) nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot find a network interface named '%s'. Continuing with limiting the network interface", - interface); + iface); } if(!*service) @@ -1186,9 +1202,7 @@ int connect_to_one_of_urls(const char *destination, int default_port, struct tim // returns: -1 = thread cancelled, 0 = proceed to read/write, 1 = time exceeded, 2 = error on fd // timeout parameter can be zero to wait forever inline int wait_on_socket_or_cancel_with_timeout( -#ifdef ENABLE_HTTPS NETDATA_SSL *ssl, -#endif int fd, int timeout_ms, short int poll_events, short int *revents) { struct pollfd pfd = { .fd = fd, @@ -1204,10 +1218,8 @@ inline int wait_on_socket_or_cancel_with_timeout( return -1; } -#ifdef ENABLE_HTTPS if(poll_events == POLLIN && ssl && SSL_connection(ssl) && netdata_ssl_has_pending(ssl)) return 0; -#endif const int wait_ms = (timeout_ms >= ND_CHECK_CANCELLABILITY_WHILE_WAITING_EVERY_MS || forever) ? ND_CHECK_CANCELLABILITY_WHILE_WAITING_EVERY_MS : timeout_ms; @@ -1247,16 +1259,10 @@ inline int wait_on_socket_or_cancel_with_timeout( return 1; } -ssize_t recv_timeout( -#ifdef ENABLE_HTTPS - NETDATA_SSL *ssl, -#endif - int sockfd, void *buf, size_t len, int flags, int timeout) { +ssize_t recv_timeout(NETDATA_SSL *ssl, int sockfd, void *buf, size_t len, int flags, int timeout) { switch(wait_on_socket_or_cancel_with_timeout( -#ifdef ENABLE_HTTPS - ssl, -#endif + ssl, sockfd, timeout * 1000, POLLIN, NULL)) { case 0: // data are waiting break; @@ -1270,25 +1276,16 @@ ssize_t recv_timeout( return -1; } -#ifdef ENABLE_HTTPS - if (SSL_connection(ssl)) { + if (SSL_connection(ssl)) return netdata_ssl_read(ssl, buf, len); - } -#endif return recv(sockfd, buf, len, flags); } -ssize_t send_timeout( -#ifdef ENABLE_HTTPS - NETDATA_SSL *ssl, -#endif - int sockfd, void *buf, size_t len, int flags, int timeout) { +ssize_t send_timeout(NETDATA_SSL *ssl, int sockfd, void *buf, size_t len, int flags, int timeout) { switch(wait_on_socket_or_cancel_with_timeout( -#ifdef ENABLE_HTTPS - ssl, -#endif + ssl, sockfd, timeout * 1000, POLLOUT, NULL)) { case 0: // data are waiting break; @@ -1302,7 +1299,6 @@ ssize_t send_timeout( return -1; } -#ifdef ENABLE_HTTPS if(ssl->conn) { if (SSL_connection(ssl)) { return netdata_ssl_write(ssl, buf, len); @@ -1314,7 +1310,7 @@ ssize_t send_timeout( return -1; } } -#endif + return send(sockfd, buf, len, flags); } |