Fix building with --disable-x11 --disable-lua --disable-ncurses
[monky] / src / read_tcp.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #include "conky.h"
32 #include "logging.h"
33 #include "text_object.h"
34 #include <netdb.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 struct read_tcp_data {
40         char *host;
41         unsigned int port;
42 };
43
44 void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash)
45 {
46         struct read_tcp_data *rtd;
47
48         rtd = malloc(sizeof(struct read_tcp_data));
49         memset(rtd, 0, sizeof(struct read_tcp_data));
50
51         rtd->host = malloc(text_buffer_size);
52         sscanf(arg, "%s", rtd->host);
53         sscanf(arg+strlen(rtd->host), "%u", &(rtd->port));
54         if(rtd->port == 0) {
55                 rtd->port = atoi(rtd->host);
56                 strcpy(rtd->host,"localhost");
57         }
58         if(rtd->port < 1 || rtd->port > 65535)
59                 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
60
61         rtd->port = htons(rtd->port);
62         obj->data.opaque = rtd;
63 }
64
65 void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
66 {
67         int sock, received;
68         struct sockaddr_in addr;
69         struct hostent* he;
70         fd_set readfds;
71         struct timeval tv;
72         struct read_tcp_data *rtd = obj->data.opaque;
73
74         if (!rtd)
75                 return;
76
77         if (!(he = gethostbyname(rtd->host))) {
78                 NORM_ERR("read_tcp: Problem with resolving the hostname");
79                 return;
80         }
81         if ((sock = socket(he->h_addrtype, SOCK_STREAM, 0)) == -1) {
82                 NORM_ERR("read_tcp: Couldn't create a socket");
83                 return;
84         }
85         memset(&addr, 0, sizeof(addr));
86         addr.sin_family = AF_INET;
87         addr.sin_port = rtd->port;
88         memcpy(&addr.sin_addr, he->h_addr, he->h_length);
89         if (!connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))) {
90                 NORM_ERR("read_tcp: Couldn't create a connection");
91                 return;
92         }
93         FD_ZERO(&readfds);
94         FD_SET(sock, &readfds);
95         tv.tv_sec = 1;
96         tv.tv_usec = 0;
97         if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
98                 received = recv(sock, p, p_max_size, 0);
99                 p[received] = 0;
100         }
101         close(sock);
102 }
103
104 void free_read_tcp(struct text_object *obj)
105 {
106         struct read_tcp_data *rtd = obj->data.opaque;
107
108         if (!rtd)
109                 return;
110
111         if (rtd->host)
112                 free(rtd->host);
113         free(obj->data.opaque);
114         obj->data.opaque = NULL;
115 }