1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-12 11:04:10 +00:00

read_tcp: outsource code

This commit is contained in:
Phil Sutter 2009-10-17 15:47:06 +02:00
parent 7a168c377b
commit 24d9e8bf24
5 changed files with 134 additions and 46 deletions

View File

@ -52,9 +52,9 @@ endif # BUILD_CONFIG_OUTPUT
mandatory_sources = colours.c colours.h common.c common.h conky.c conky.h \ mandatory_sources = colours.c colours.h common.c common.h conky.c conky.h \
core.c core.h diskio.c diskio.h exec.c exec.h fs.c fs.h logging.h \ core.c core.h diskio.c diskio.h exec.c exec.h fs.c fs.h logging.h \
mail.c mail.h mixer.c mixer.h template.c template.h timed_thread.c \ mail.c mail.h mixer.c mixer.h template.c template.h timed_thread.c \
timed_thread.h mboxscan.c mboxscan.h specials.c specials.h tailhead.c \ timed_thread.h mboxscan.c mboxscan.h read_tcp.c read_tcp.h specials.c \
tailhead.h temphelper.c temphelper.h text_object.c text_object.h \ specials.h tailhead.c tailhead.h temphelper.c temphelper.h \
timeinfo.c timeinfo.h algebra.c algebra.h text_object.c text_object.h timeinfo.c timeinfo.h algebra.c algebra.h
# source files only needed when the apropriate option is enabled # source files only needed when the apropriate option is enabled
audacious = audacious.c audacious.h audacious = audacious.c audacious.h

View File

@ -88,6 +88,7 @@
#include "mixer.h" #include "mixer.h"
#include "mail.h" #include "mail.h"
#include "mboxscan.h" #include "mboxscan.h"
#include "read_tcp.h"
#include "specials.h" #include "specials.h"
#include "temphelper.h" #include "temphelper.h"
#include "template.h" #include "template.h"
@ -882,37 +883,7 @@ static void generate_text_internal(char *p, int p_max_size,
default: default:
NORM_ERR("not implemented obj type %d", obj->type); NORM_ERR("not implemented obj type %d", obj->type);
OBJ(read_tcp) { OBJ(read_tcp) {
int sock, received; print_read_tcp(obj, p, p_max_size);
struct sockaddr_in addr;
struct hostent* he;
fd_set readfds;
struct timeval tv;
if (!(he = gethostbyname(obj->data.read_tcp.host))) {
NORM_ERR("read_tcp: Problem with resolving the hostname");
break;
}
if ((sock = socket(he->h_addrtype, SOCK_STREAM, 0)) == -1) {
NORM_ERR("read_tcp: Couldn't create a socket");
break;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = obj->data.read_tcp.port;
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (!connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))) {
NORM_ERR("read_tcp: Couldn't create a connection");
break;
}
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);
} }
#ifndef __OpenBSD__ #ifndef __OpenBSD__
OBJ(acpitemp) { OBJ(acpitemp) {

View File

@ -47,6 +47,7 @@
#include "mixer.h" #include "mixer.h"
#include "mail.h" #include "mail.h"
#include "mboxscan.h" #include "mboxscan.h"
#include "read_tcp.h"
#include "specials.h" #include "specials.h"
#include "temphelper.h" #include "temphelper.h"
#include "template.h" #include "template.h"
@ -176,17 +177,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
} }
obj->a = 1; obj->a = 1;
END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)") END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)")
obj->data.read_tcp.host = malloc(text_buffer_size); parse_read_tcp_arg(obj, arg, free_at_crash);
sscanf(arg, "%s", obj->data.read_tcp.host);
sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
if(obj->data.read_tcp.port == 0) {
obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
strcpy(obj->data.read_tcp.host,"localhost");
}
obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
}
#if defined(__linux__) #if defined(__linux__)
END OBJ(voltage_mv, 0) END OBJ(voltage_mv, 0)
get_cpu_count(); get_cpu_count();
@ -1803,7 +1794,7 @@ void free_text_objects(struct text_object *root, int internal)
break; break;
#endif /* __linux__ */ #endif /* __linux__ */
case OBJ_read_tcp: case OBJ_read_tcp:
free(data.read_tcp.host); free_read_tcp(obj);
break; break;
case OBJ_time: case OBJ_time:
case OBJ_utime: case OBJ_utime:

88
src/read_tcp.c Normal file
View File

@ -0,0 +1,88 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c
*
* 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
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
* (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/>.
*
*/
#include "logging.h"
#include "text_object.h"
void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash)
{
obj->data.read_tcp.host = malloc(text_buffer_size);
sscanf(arg, "%s", obj->data.read_tcp.host);
sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
if(obj->data.read_tcp.port == 0) {
obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
strcpy(obj->data.read_tcp.host,"localhost");
}
if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535)
CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
}
void print_read_tcp(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;
if (!(he = gethostbyname(obj->data.read_tcp.host))) {
NORM_ERR("read_tcp: Problem with resolving the hostname");
return;
}
if ((sock = socket(he->h_addrtype, SOCK_STREAM, 0)) == -1) {
NORM_ERR("read_tcp: Couldn't create a socket");
return;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = obj->data.read_tcp.port;
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (!connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))) {
NORM_ERR("read_tcp: Couldn't create a connection");
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 free_read_tcp(struct text_object *obj)
{
if (obj->data.read_tcp.host)
free(obj->data.read_tcp.host);
}

38
src/read_tcp.h Normal file
View File

@ -0,0 +1,38 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c
*
* 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
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
* (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/>.
*
*/
#ifndef _READ_TCP_H
#define _READ_TCP_H
void parse_read_tcp_arg(struct text_object *, const char *, void *);
void print_read_tcp(struct text_object *, char *, int);
void free_read_tcp(struct text_object *);
#endif /* _READ_TCP_H */