mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-11-16 18:15:17 +00:00
read_tcp: convert to generic object payload
This commit is contained in:
parent
b74e0494de
commit
bf9263bc5e
@ -30,20 +30,32 @@
|
|||||||
|
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "text_object.h"
|
#include "text_object.h"
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
|
struct read_tcp_data {
|
||||||
|
char *host;
|
||||||
|
unsigned int port;
|
||||||
|
};
|
||||||
|
|
||||||
void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash)
|
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);
|
struct read_tcp_data *rtd;
|
||||||
sscanf(arg, "%s", obj->data.read_tcp.host);
|
|
||||||
sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
|
rtd = malloc(sizeof(struct read_tcp_data));
|
||||||
if(obj->data.read_tcp.port == 0) {
|
memset(rtd, 0, sizeof(struct read_tcp_data));
|
||||||
obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
|
|
||||||
strcpy(obj->data.read_tcp.host,"localhost");
|
rtd->host = 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");
|
||||||
}
|
}
|
||||||
if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535)
|
if(rtd->port < 1 || rtd->port > 65535)
|
||||||
CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
|
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);
|
rtd->port = htons(rtd->port);
|
||||||
|
obj->data.opaque = rtd;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
|
void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
|
||||||
@ -53,8 +65,12 @@ void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
|
|||||||
struct hostent* he;
|
struct hostent* he;
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
struct read_tcp_data *rtd = obj->data.opaque;
|
||||||
|
|
||||||
if (!(he = gethostbyname(obj->data.read_tcp.host))) {
|
if (!rtd)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!(he = gethostbyname(rtd->host))) {
|
||||||
NORM_ERR("read_tcp: Problem with resolving the hostname");
|
NORM_ERR("read_tcp: Problem with resolving the hostname");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -64,7 +80,7 @@ void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
|
|||||||
}
|
}
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = obj->data.read_tcp.port;
|
addr.sin_port = rtd->port;
|
||||||
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
|
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
|
||||||
if (!connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))) {
|
if (!connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))) {
|
||||||
NORM_ERR("read_tcp: Couldn't create a connection");
|
NORM_ERR("read_tcp: Couldn't create a connection");
|
||||||
@ -83,6 +99,13 @@ void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
|
|||||||
|
|
||||||
void free_read_tcp(struct text_object *obj)
|
void free_read_tcp(struct text_object *obj)
|
||||||
{
|
{
|
||||||
if (obj->data.read_tcp.host)
|
struct read_tcp_data *rtd = obj->data.opaque;
|
||||||
free(obj->data.read_tcp.host);
|
|
||||||
|
if (!rtd)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (rtd->host)
|
||||||
|
free(rtd->host);
|
||||||
|
free(obj->data.opaque);
|
||||||
|
obj->data.opaque = NULL;
|
||||||
}
|
}
|
||||||
|
@ -474,11 +474,6 @@ struct text_object {
|
|||||||
char *right;
|
char *right;
|
||||||
} combine;
|
} combine;
|
||||||
|
|
||||||
struct {
|
|
||||||
char *host;
|
|
||||||
unsigned int port;
|
|
||||||
} read_tcp;
|
|
||||||
|
|
||||||
struct local_mail_s local_mail;
|
struct local_mail_s local_mail;
|
||||||
} data;
|
} data;
|
||||||
int type;
|
int type;
|
||||||
|
Loading…
Reference in New Issue
Block a user