1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
34 void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash)
36 obj->data.read_tcp.host = malloc(text_buffer_size);
37 sscanf(arg, "%s", obj->data.read_tcp.host);
38 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
39 if(obj->data.read_tcp.port == 0) {
40 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
41 strcpy(obj->data.read_tcp.host,"localhost");
43 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535)
44 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
46 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
49 void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
52 struct sockaddr_in addr;
57 if (!(he = gethostbyname(obj->data.read_tcp.host))) {
58 NORM_ERR("read_tcp: Problem with resolving the hostname");
61 if ((sock = socket(he->h_addrtype, SOCK_STREAM, 0)) == -1) {
62 NORM_ERR("read_tcp: Couldn't create a socket");
65 memset(&addr, 0, sizeof(addr));
66 addr.sin_family = AF_INET;
67 addr.sin_port = obj->data.read_tcp.port;
68 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
69 if (!connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))) {
70 NORM_ERR("read_tcp: Couldn't create a connection");
74 FD_SET(sock, &readfds);
77 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
78 received = recv(sock, p, p_max_size, 0);
84 void free_read_tcp(struct text_object *obj)
86 if (obj->data.read_tcp.host)
87 free(obj->data.read_tcp.host);