Use getaddrinfo instead of gethostbyname
[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-2010 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 #include <netinet/in.h>
39
40 struct read_tcp_data {
41         char *host;
42         unsigned int port;
43 };
44
45 void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash)
46 {
47         struct read_tcp_data *rtd;
48
49         rtd = malloc(sizeof(struct read_tcp_data));
50         memset(rtd, 0, sizeof(struct read_tcp_data));
51
52         rtd->host = malloc(text_buffer_size);
53         sscanf(arg, "%s", rtd->host);
54         sscanf(arg+strlen(rtd->host), "%u", &(rtd->port));
55         if(rtd->port == 0) {
56                 rtd->port = atoi(rtd->host);
57                 strcpy(rtd->host,"localhost");
58         }
59         if(rtd->port < 1 || rtd->port > 65535)
60                 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
61
62         rtd->port = htons(rtd->port);
63         obj->data.opaque = rtd;
64 }
65
66 void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
67 {
68         int sock, received;
69         fd_set readfds;
70         struct timeval tv;
71         struct read_tcp_data *rtd = obj->data.opaque;
72         struct addrinfo hints;
73         struct addrinfo* airesult, *rp;
74         char portbuf[8];
75
76         if (!rtd)
77                 return;
78
79         memset(&hints, 0, sizeof(struct addrinfo));
80         hints.ai_family = AF_UNSPEC;
81         hints.ai_socktype = SOCK_STREAM;
82         hints.ai_flags = 0;
83         hints.ai_protocol = 0;
84         snprintf(portbuf, 8, "%d", rtd->port);
85         if (getaddrinfo(rtd->host, portbuf, &hints, &airesult)) {
86                 NORM_ERR("read_tcp: Problem with resolving the hostname");
87                 return;
88         }
89         for (rp = airesult; rp != NULL; rp = rp->ai_next) {
90                 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
91                 if (sock == -1) {
92                         continue;
93                 }
94                 if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) {
95                         break;
96                 }
97                 close(sock);
98         }
99         freeaddrinfo(airesult);
100         if (rp == NULL) {
101                 NORM_ERR("read_tcp: Couldn't create a connection");
102                 return;
103         }
104         FD_ZERO(&readfds);
105         FD_SET(sock, &readfds);
106         tv.tv_sec = 1;
107         tv.tv_usec = 0;
108         if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
109                 received = recv(sock, p, p_max_size, 0);
110                 p[received] = 0;
111         }
112         close(sock);
113 }
114
115 void free_read_tcp(struct text_object *obj)
116 {
117         struct read_tcp_data *rtd = obj->data.opaque;
118
119         if (!rtd)
120                 return;
121
122         if (rtd->host)
123                 free(rtd->host);
124         free(obj->data.opaque);
125         obj->data.opaque = NULL;
126 }