2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-10-17 13:47:06 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2018-05-12 16:03:00 +00:00
|
|
|
* Copyright (c) 2005-2018 Brenden Matthews, Philip Kovacs, et. al.
|
2009-10-17 13:47:06 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-05-23 17:03:01 +00:00
|
|
|
#include <fcntl.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <netdb.h>
|
2009-11-14 03:19:51 +00:00
|
|
|
#include <netinet/in.h>
|
2010-04-23 23:40:24 +00:00
|
|
|
#include <sys/time.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <unistd.h>
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <cerrno>
|
2010-04-27 16:11:38 +00:00
|
|
|
#include <cinttypes>
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <cstdlib>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <string>
|
|
|
|
#include "conky.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "text_object.h"
|
2009-10-06 22:27:04 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
#ifndef SOCK_CLOEXEC
|
|
|
|
# define SOCK_CLOEXEC O_CLOEXEC
|
|
|
|
#endif /* SOCK_CLOEXEC */
|
|
|
|
|
2010-04-17 00:31:37 +00:00
|
|
|
struct read_tcpip_data {
|
2018-05-12 16:03:00 +00:00
|
|
|
char *host;
|
|
|
|
unsigned int port;
|
2009-10-06 22:27:04 +00:00
|
|
|
};
|
2009-10-17 13:47:06 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_read_tcpip_arg(struct text_object *obj, const char *arg,
|
|
|
|
void *free_at_crash) {
|
|
|
|
struct read_tcpip_data *rtd;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
rtd = static_cast<struct read_tcpip_data *>(
|
|
|
|
malloc(sizeof(struct read_tcpip_data)));
|
2018-05-12 16:03:00 +00:00
|
|
|
memset(rtd, 0, sizeof(struct read_tcpip_data));
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
rtd->host = static_cast<char *>(malloc(text_buffer_size.get(*state)));
|
2018-05-12 16:03:00 +00:00
|
|
|
sscanf(arg, "%s", rtd->host);
|
|
|
|
sscanf(arg + strlen(rtd->host), "%u", &(rtd->port));
|
|
|
|
if (rtd->port == 0) {
|
|
|
|
rtd->port = atoi(rtd->host);
|
|
|
|
strcpy(rtd->host, "localhost");
|
|
|
|
}
|
|
|
|
if (rtd->port < 1 || rtd->port > 65535)
|
|
|
|
CRIT_ERR(obj, free_at_crash,
|
|
|
|
"read_tcp and read_udp need a port from 1 to 65535 as argument");
|
|
|
|
|
|
|
|
obj->data.opaque = rtd;
|
2009-10-17 13:47:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_tcp_ping_arg(struct text_object *obj, const char *arg,
|
|
|
|
void *free_at_crash) {
|
2010-04-24 10:33:40 +00:00
|
|
|
#define DEFAULT_TCP_PING_PORT 80
|
2018-05-12 16:03:00 +00:00
|
|
|
struct sockaddr_in *addr;
|
|
|
|
char *hostname;
|
|
|
|
struct hostent *he;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
addr = static_cast<struct sockaddr_in *>(malloc(sizeof(struct sockaddr_in)));
|
2018-05-12 16:03:00 +00:00
|
|
|
obj->data.opaque = addr;
|
|
|
|
memset(addr, 0, sizeof(struct sockaddr_in));
|
2018-05-12 23:26:31 +00:00
|
|
|
hostname = static_cast<char *>(malloc(strlen(arg) + 1));
|
2018-05-12 16:03:00 +00:00
|
|
|
switch (sscanf(arg, "%s %" SCNu16, hostname, &(addr->sin_port))) {
|
|
|
|
case 1:
|
|
|
|
addr->sin_port = DEFAULT_TCP_PING_PORT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default: // this point should never be reached
|
|
|
|
free(hostname);
|
|
|
|
CRIT_ERR(obj, free_at_crash, "tcp_ping: Reading arguments failed");
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((he = gethostbyname(hostname)) == nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("tcp_ping: Problem with resolving '%s', using 'localhost' instead",
|
|
|
|
hostname);
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((he = gethostbyname("localhost")) == nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
free(hostname);
|
|
|
|
CRIT_ERR(obj, free_at_crash,
|
|
|
|
"tcp_ping: Resolving 'localhost' also failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(hostname);
|
|
|
|
addr->sin_port = htons(addr->sin_port);
|
|
|
|
addr->sin_family = he->h_addrtype;
|
|
|
|
memcpy(&(addr->sin_addr), he->h_addr, he->h_length);
|
2010-04-23 23:40:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void print_tcp_ping(struct text_object *obj, char *p, int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
struct timeval tv1 {
|
|
|
|
}, tv2{}, timeout{};
|
|
|
|
auto *addr = static_cast<struct sockaddr_in *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
int addrlen = sizeof(struct sockaddr);
|
2018-05-12 23:26:31 +00:00
|
|
|
int sock = socket(addr->sin_family, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
|
2018-05-12 16:03:00 +00:00
|
|
|
unsigned long long usecdiff;
|
|
|
|
fd_set writefds;
|
2010-04-23 23:40:24 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (sock != -1) {
|
|
|
|
fcntl(sock, F_SETFL, O_NONBLOCK | fcntl(sock, F_GETFL));
|
2010-05-23 17:03:01 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_SET(sock, &writefds);
|
2010-04-23 23:40:24 +00:00
|
|
|
#define TCP_PING_TIMEOUT 10
|
2018-05-12 23:26:31 +00:00
|
|
|
timeout.tv_sec = TCP_PING_TIMEOUT;
|
2018-05-12 16:03:00 +00:00
|
|
|
timeout.tv_usec = (TCP_PING_TIMEOUT - timeout.tv_sec) * 1000000;
|
2018-05-12 23:26:31 +00:00
|
|
|
connect(sock, reinterpret_cast<struct sockaddr *>(addr),
|
2018-05-12 16:03:00 +00:00
|
|
|
addrlen); // this will "fail" because sock is non-blocking
|
|
|
|
if (errno == EINPROGRESS) { // but EINPROGRESS is only a "false fail"
|
2018-05-12 23:26:31 +00:00
|
|
|
gettimeofday(&tv1, nullptr);
|
|
|
|
if (select(sock + 1, nullptr, &writefds, nullptr, &timeout) != -1) {
|
|
|
|
gettimeofday(&tv2, nullptr);
|
2018-05-12 16:03:00 +00:00
|
|
|
usecdiff =
|
|
|
|
((tv2.tv_sec - tv1.tv_sec) * 1000000) + tv2.tv_usec - tv1.tv_usec;
|
|
|
|
if (usecdiff <= TCP_PING_TIMEOUT * 1000000) {
|
|
|
|
snprintf(p, p_max_size, "%llu", usecdiff);
|
|
|
|
} else {
|
2010-04-23 23:40:24 +00:00
|
|
|
#define TCP_PING_FAILED "down"
|
2018-05-12 16:03:00 +00:00
|
|
|
snprintf(p, p_max_size, TCP_PING_FAILED);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NORM_ERR("tcp_ping: Couldn't wait on the 'pong'");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NORM_ERR("tcp_ping: Couldn't start connection");
|
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
} else {
|
|
|
|
NORM_ERR("tcp_ping: Couldn't create socket");
|
|
|
|
}
|
2010-04-23 23:40:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void print_read_tcpip(struct text_object *obj, char *p, int p_max_size,
|
|
|
|
int protocol) {
|
|
|
|
int sock, received;
|
|
|
|
fd_set readfds;
|
2018-05-12 23:26:31 +00:00
|
|
|
struct timeval tv {};
|
|
|
|
auto *rtd = static_cast<struct read_tcpip_data *>(obj->data.opaque);
|
|
|
|
struct addrinfo hints {};
|
2018-05-12 16:03:00 +00:00
|
|
|
struct addrinfo *airesult, *rp;
|
|
|
|
char portbuf[8];
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (rtd == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM;
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
hints.ai_protocol = protocol;
|
|
|
|
snprintf(portbuf, 8, "%d", rtd->port);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (getaddrinfo(rtd->host, portbuf, &hints, &airesult) != 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("%s: Problem with resolving the hostname",
|
|
|
|
protocol == IPPROTO_TCP ? "read_tcp" : "read_udp");
|
|
|
|
return;
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
for (rp = airesult; rp != nullptr; rp = rp->ai_next) {
|
2018-05-12 16:03:00 +00:00
|
|
|
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if (sock == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
freeaddrinfo(airesult);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (rp == nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
if (protocol == IPPROTO_TCP) {
|
|
|
|
NORM_ERR("read_tcp: Couldn't create a connection");
|
|
|
|
} else {
|
|
|
|
NORM_ERR("read_udp: Couldn't listen"); // other error because udp is
|
|
|
|
// connectionless
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (protocol == IPPROTO_UDP) {
|
|
|
|
// when using udp send a zero-length packet to let the other end know of our
|
|
|
|
// existence
|
2018-05-12 23:26:31 +00:00
|
|
|
if (write(sock, nullptr, 0) < 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("read_udp: Couldn't create a empty package");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(sock, &readfds);
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
if (select(sock + 1, &readfds, nullptr, nullptr, &tv) > 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
received = recv(sock, p, p_max_size, 0);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (received != -1) {
|
2018-05-12 16:03:00 +00:00
|
|
|
p[received] = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
p[0] = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
close(sock);
|
2009-10-17 13:47:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void print_read_tcp(struct text_object *obj, char *p, int p_max_size) {
|
|
|
|
print_read_tcpip(obj, p, p_max_size, IPPROTO_TCP);
|
2010-04-17 00:31:37 +00:00
|
|
|
}
|
2010-04-16 16:53:48 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void print_read_udp(struct text_object *obj, char *p, int p_max_size) {
|
|
|
|
print_read_tcpip(obj, p, p_max_size, IPPROTO_UDP);
|
2010-04-16 16:53:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_read_tcpip(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *rtd = static_cast<struct read_tcpip_data *>(obj->data.opaque);
|
2009-10-06 22:27:04 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (rtd == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-06 22:27:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(rtd->host);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-10-17 13:47:06 +00:00
|
|
|
}
|
2010-04-23 23:40:24 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_tcp_ping(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *addr = static_cast<struct sockaddr_in *>(obj->data.opaque);
|
2010-04-23 23:40:24 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (addr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2010-04-23 23:40:24 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(obj->data.opaque);
|
2010-04-23 23:40:24 +00:00
|
|
|
}
|