1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-16 18:15:17 +00:00
conky/src/read_tcp.cc

155 lines
4.4 KiB
C++
Raw Normal View History

2010-02-09 01:02:00 +00:00
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
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
2010-01-01 23:46:17 +00:00
* Copyright (c) 2005-2010 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/>.
*
*/
#include "conky.h"
2009-10-17 13:47:06 +00:00
#include "logging.h"
#include "text_object.h"
#include <netdb.h>
#include <stdlib.h>
2010-02-09 01:02:00 +00:00
#include <string>
#include <unistd.h>
#include <netinet/in.h>
struct read_tcp_data {
char *host;
unsigned int port;
};
2009-10-17 13:47:06 +00:00
void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash)
{
struct read_tcp_data *rtd;
2010-02-09 01:02:00 +00:00
rtd = (struct read_tcp_data *) malloc(sizeof(struct read_tcp_data));
memset(rtd, 0, sizeof(struct read_tcp_data));
2010-02-09 01:02:00 +00:00
rtd->host = (char *) malloc(text_buffer_size);
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");
2009-10-17 13:47:06 +00:00
}
if(rtd->port < 1 || rtd->port > 65535)
2010-04-16 16:53:48 +00:00
CRIT_ERR(obj, free_at_crash, "read_tcp and read_udp need a port from 1 to 65535 as argument");
2009-10-17 13:47:06 +00:00
rtd->port = htons(rtd->port);
obj->data.opaque = rtd;
2009-10-17 13:47:06 +00:00
}
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;
2010-02-09 01:02:00 +00:00
struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque;
if (!rtd)
return;
2009-10-17 13:47:06 +00:00
if (!(he = gethostbyname(rtd->host))) {
2009-10-17 13:47:06 +00:00
NORM_ERR("read_tcp: Problem with resolving the hostname");
return;
}
2010-04-16 16:53:48 +00:00
if ((sock = socket(he->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) == -1) {
2009-10-17 13:47:06 +00:00
NORM_ERR("read_tcp: Couldn't create a socket");
return;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = rtd->port;
2009-10-17 13:47:06 +00:00
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) != 0) {
2009-10-17 13:47:06 +00:00
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);
}
2010-04-16 16:53:48 +00:00
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
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);
if(received != -1) p[received] = 0; else p[0] = 0;
}
close(sock);
}
2009-10-17 13:47:06 +00:00
void free_read_tcp(struct text_object *obj)
{
2010-02-09 01:02:00 +00:00
struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque;
if (!rtd)
return;
free_and_zero(rtd->host);
free_and_zero(obj->data.opaque);
2009-10-17 13:47:06 +00:00
}