From 53247ae748174ddb639bb5fd9a19c8a6ad121717 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:04:43 +0100 Subject: netutils.c: clang-format + do NOT print here --- plugins/netutils.c | 79 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/plugins/netutils.c b/plugins/netutils.c index ee81912a..f5df7f3d 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -43,10 +43,11 @@ int address_family = AF_INET; /* handles socket timeouts */ void socket_timeout_alarm_handler(int sig) { - if (sig == SIGALRM) + if (sig == SIGALRM) { printf(_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); - else + } else { printf(_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); + } exit(socket_timeout_state); } @@ -65,12 +66,13 @@ int process_tcp_request2(const char *server_address, int server_port, const char int recv_length = 0; result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); - if (result != STATE_OK) + if (result != STATE_OK) { return STATE_CRITICAL; + } send_result = send(sd, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { - printf("%s\n", _("Send failed")); + // printf("%s\n", _("Send failed")); result = STATE_WARNING; } @@ -87,7 +89,7 @@ int process_tcp_request2(const char *server_address, int server_port, const char if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ if (!recv_length) { strcpy(recv_buffer, ""); - printf("%s\n", _("No data was received from host!")); + // printf("%s\n", _("No data was received from host!")); result = STATE_WARNING; } else { /* this one failed, but previous ones worked */ recv_buffer[recv_length] = 0; @@ -130,8 +132,9 @@ int process_request(const char *server_address, int server_port, int proto, cons result = STATE_OK; result = np_net_connect(server_address, server_port, &sd, proto); - if (result != STATE_OK) + if (result != STATE_OK) { return STATE_CRITICAL; + } result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); @@ -169,15 +172,16 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { host_name++; len -= 2; } - if (len >= sizeof(host)) + if (len >= sizeof(host)) { return STATE_UNKNOWN; + } memcpy(host, host_name, len); host[len] = '\0'; snprintf(port_str, sizeof(port_str), "%d", port); result = getaddrinfo(host, port_str, &hints, &res); if (result != 0) { - printf("%s\n", gai_strerror(result)); + // printf("%s\n", gai_strerror(result)); return STATE_UNKNOWN; } @@ -187,7 +191,7 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { *sd = socket(r->ai_family, socktype, r->ai_protocol); if (*sd < 0) { - printf("%s\n", _("Socket creation failed")); + // printf("%s\n", _("Socket creation failed")); freeaddrinfo(r); return STATE_UNKNOWN; } @@ -226,21 +230,23 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { die(STATE_UNKNOWN, _("Socket creation failed")); } result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); - if (result < 0 && errno == ECONNREFUSED) + if (result < 0 && errno == ECONNREFUSED) { was_refused = true; + } } - if (result == 0) + if (result == 0) { return STATE_OK; - else if (was_refused) { + } else if (was_refused) { switch (econn_refuse_state) { /* a user-defined expected outcome */ case STATE_OK: case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */ - if (is_socket) - printf("connect to file socket %s: %s\n", host_name, strerror(errno)); - else - printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + if (is_socket) { + // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); + } else { + // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + } return STATE_CRITICAL; break; default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ @@ -248,10 +254,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { break; } } else { - if (is_socket) - printf("connect to file socket %s: %s\n", host_name, strerror(errno)); - else - printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + if (is_socket) { + // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); + } else { + // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + } return STATE_CRITICAL; } } @@ -265,7 +272,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, send_result = send(sd, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { - printf("%s\n", _("Send failed")); + // printf("%s\n", _("Send failed")); result = STATE_WARNING; } @@ -280,7 +287,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, /* make sure some data has arrived */ if (!FD_ISSET(sd, &readfds)) { strcpy(recv_buffer, ""); - printf("%s\n", _("No data was received from host!")); + // printf("%s\n", _("No data was received from host!")); result = STATE_WARNING; } @@ -288,11 +295,13 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); if (recv_result == -1) { strcpy(recv_buffer, ""); - if (proto != IPPROTO_TCP) - printf("%s\n", _("Receive failed")); + if (proto != IPPROTO_TCP) { + // printf("%s\n", _("Receive failed")); + } result = STATE_WARNING; - } else + } else { recv_buffer[recv_result] = 0; + } /* die returned string */ recv_buffer[recv_size - 1] = 0; @@ -301,26 +310,30 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, } bool is_host(const char *address) { - if (is_addr(address) || is_hostname(address)) + if (is_addr(address) || is_hostname(address)) { return (true); + } return (false); } void host_or_die(const char *str) { - if (!str || (!is_addr(str) && !is_hostname(str))) + if (!str || (!is_addr(str) && !is_hostname(str))) { usage_va(_("Invalid hostname/address - %s"), str); + } } bool is_addr(const char *address) { #ifdef USE_IPV6 - if (address_family == AF_INET && is_inet_addr(address)) + if (address_family == AF_INET && is_inet_addr(address)) { return true; - else if (address_family == AF_INET6 && is_inet6_addr(address)) + } else if (address_family == AF_INET6 && is_inet6_addr(address)) { return true; + } #else - if (is_inet_addr(address)) + if (is_inet_addr(address)) { return (true); + } #endif return (false); @@ -335,11 +348,13 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { hints.ai_family = family; retval = getaddrinfo(in, NULL, &hints, &res); - if (retval != 0) + if (retval != 0) { return false; + } - if (ss != NULL) + if (ss != NULL) { memcpy(ss, res->ai_addr, res->ai_addrlen); + } freeaddrinfo(res); return true; } -- cgit v1.2.3-74-g34f1