diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8a1aa375..8154e1fa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,7 +36,7 @@ endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h) set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc - mboxscan.cc read_tcp.cc scroll.cc specials.cc tailhead.cc + mboxscan.cc read_tcpip.cc scroll.cc specials.cc tailhead.cc temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.cc proc.cc user.cc) diff --git a/src/core.cc b/src/core.cc index a88c6821..2dd3763e 100644 --- a/src/core.cc +++ b/src/core.cc @@ -64,7 +64,7 @@ #ifdef BUILD_NVIDIA #include "nvidia.h" #endif -#include "read_tcp.h" +#include "read_tcpip.h" #include "scroll.h" #include "specials.h" #include "temphelper.h" @@ -209,13 +209,13 @@ struct text_object *construct_text_object(char *s, const char *arg, long } obj->callbacks.print = &print_freq_g; END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)") - parse_read_tcp_arg(obj, arg, free_at_crash); + parse_read_tcpip_arg(obj, arg, free_at_crash); obj->callbacks.print = &print_read_tcp; - obj->callbacks.free = &free_read_tcp; + obj->callbacks.free = &free_read_tcpip; END OBJ_ARG(read_udp, 0, "read_udp: Needs \"(host) port\" as argument(s)") - parse_read_tcp_arg(obj, arg, free_at_crash); + parse_read_tcpip_arg(obj, arg, free_at_crash); obj->callbacks.print = &print_read_udp; - obj->callbacks.free = &free_read_tcp; + obj->callbacks.free = &free_read_tcpip; #if defined(__linux__) END OBJ(voltage_mv, 0) get_cpu_count(); diff --git a/src/read_tcp.cc b/src/read_tcpip.cc similarity index 60% rename from src/read_tcp.cc rename to src/read_tcpip.cc index c492035a..39258f13 100644 --- a/src/read_tcp.cc +++ b/src/read_tcpip.cc @@ -37,17 +37,17 @@ #include #include -struct read_tcp_data { +struct read_tcpip_data { char *host; unsigned int port; }; -void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash) +void parse_read_tcpip_arg(struct text_object *obj, const char *arg, void *free_at_crash) { - struct read_tcp_data *rtd; + struct read_tcpip_data *rtd; - rtd = (struct read_tcp_data *) malloc(sizeof(struct read_tcp_data)); - memset(rtd, 0, sizeof(struct read_tcp_data)); + rtd = (struct read_tcpip_data *) malloc(sizeof(struct read_tcpip_data)); + memset(rtd, 0, sizeof(struct read_tcpip_data)); rtd->host = (char *) malloc(text_buffer_size); sscanf(arg, "%s", rtd->host); @@ -63,24 +63,24 @@ void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_ obj->data.opaque = rtd; } -void print_read_tcp(struct text_object *obj, char *p, int p_max_size) +void print_read_tcpip(struct text_object *obj, char *p, int p_max_size, int protocol) { int sock, received; struct sockaddr_in addr; struct hostent* he; fd_set readfds; struct timeval tv; - struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque; + struct read_tcpip_data *rtd = (struct read_tcpip_data *) obj->data.opaque; if (!rtd) return; if (!(he = gethostbyname(rtd->host))) { - NORM_ERR("read_tcp: Problem with resolving the hostname"); + NORM_ERR("%s: Problem with resolving the hostname", protocol == IPPROTO_TCP ? "read_tcp" : "read_udp"); return; } - if ((sock = socket(he->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) == -1) { - NORM_ERR("read_tcp: Couldn't create a socket"); + if ((sock = socket(he->h_addrtype, protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM, protocol)) == -1) { + NORM_ERR("%s: Couldn't create a socket", protocol == IPPROTO_TCP ? "read_tcp" : "read_udp"); return; } memset(&addr, 0, sizeof(addr)); @@ -88,49 +88,15 @@ void print_read_tcp(struct text_object *obj, char *p, int p_max_size) addr.sin_port = rtd->port; memcpy(&addr.sin_addr, he->h_addr, he->h_length); if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) != 0) { - NORM_ERR("read_tcp: Couldn't create a connection"); + 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; } - FD_ZERO(&readfds); - FD_SET(sock, &readfds); - tv.tv_sec = 1; - tv.tv_usec = 0; - if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){ - received = recv(sock, p, p_max_size, 0); - p[received] = 0; - } - close(sock); -} - -void print_read_udp(struct text_object *obj, char *p, int p_max_size) -{ - int sock, received; - struct sockaddr_in addr; - struct hostent* he; - fd_set readfds; - struct timeval tv; - struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque; - - if (!rtd) - return; - - if (!(he = gethostbyname(rtd->host))) { - NORM_ERR("read_udp: Problem with resolving the hostname"); - return; - } - if ((sock = socket(he->h_addrtype, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - NORM_ERR("read_udp: Couldn't create a socket"); - return; - } - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = rtd->port; - memcpy(&addr.sin_addr, he->h_addr, he->h_length); - if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) != 0) { - NORM_ERR("read_udp: Couldn't create a connection"); - return; - } - write(sock, NULL, 0); //when using UDP i have to start writing to the socket before i can read from it, even if i don't actually write anything + if(protocol == IPPROTO_UDP) + write(sock, NULL, 0); //when using udp send a zero-length packet to let the other end know of our existence FD_ZERO(&readfds); FD_SET(sock, &readfds); tv.tv_sec = 1; @@ -142,9 +108,19 @@ void print_read_udp(struct text_object *obj, char *p, int p_max_size) close(sock); } -void free_read_tcp(struct text_object *obj) +void print_read_tcp(struct text_object *obj, char *p, int p_max_size) { - struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque; + print_read_tcpip(obj, p, p_max_size, IPPROTO_TCP); +} + +void print_read_udp(struct text_object *obj, char *p, int p_max_size) +{ + print_read_tcpip(obj, p, p_max_size, IPPROTO_UDP); +} + +void free_read_tcpip(struct text_object *obj) +{ + struct read_tcpip_data *rtd = (struct read_tcpip_data *) obj->data.opaque; if (!rtd) return; diff --git a/src/read_tcp.h b/src/read_tcpip.h similarity index 92% rename from src/read_tcp.h rename to src/read_tcpip.h index f06ed752..44bcf932 100644 --- a/src/read_tcp.h +++ b/src/read_tcpip.h @@ -31,9 +31,9 @@ #ifndef _READ_TCP_H #define _READ_TCP_H -void parse_read_tcp_arg(struct text_object *, const char *, void *); +void parse_read_tcpip_arg(struct text_object *, const char *, void *); void print_read_tcp(struct text_object *, char *, int); void print_read_udp(struct text_object *, char *, int); -void free_read_tcp(struct text_object *); +void free_read_tcpip(struct text_object *); #endif /* _READ_TCP_H */