sammler/network.c

107 lines
2.3 KiB
C
Raw Normal View History

2006-08-03 19:49:07 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2006-09-30 20:27:56 +02:00
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
2006-08-03 19:49:07 +02:00
#include "configfile.h"
2006-10-08 16:33:07 +02:00
#include "helper.h"
2006-09-30 20:27:56 +02:00
#include "list.h"
2006-08-03 19:49:07 +02:00
#include "logging.h"
2006-09-30 20:27:56 +02:00
#include "network.h"
#include "plugins.h"
2006-09-30 20:27:56 +02:00
#include "rrdtool.h"
2006-08-03 19:49:07 +02:00
2006-09-30 20:27:56 +02:00
#define BUFSIZE 1024
2006-08-03 19:49:07 +02:00
2006-10-08 16:33:07 +02:00
static struct sockaddr_in fwd_sa;
static int fwd_sock;
2006-09-30 20:27:56 +02:00
// todo: never freed..
static char *tx_buf, *rx_buf;
2006-10-07 20:48:20 +02:00
int net_submit(char *hostname, char *plugin, char *filename, int ds_id, char *data)
2006-09-30 20:27:56 +02:00
{
int size = snprintf(tx_buf, BUFSIZE, "%s:%s:%s:%d %s", hostname, plugin, filename, ds_id, data);
if (size < 0 || size >= BUFSIZE) {
log_print(LOG_ERROR, "net_submit(): arguments too long");
2006-10-07 20:48:20 +02:00
return -1;
2006-09-30 20:27:56 +02:00
}
2006-10-08 16:33:07 +02:00
sendto(fwd_sock, tx_buf, size +1, 0, (struct sockaddr *)&fwd_sa, sizeof(fwd_sa));
2006-10-07 20:48:20 +02:00
return 0;
2006-09-30 20:27:56 +02:00
}
int net_receive(int socket)
{
2006-10-08 16:33:07 +02:00
recv(socket, rx_buf, BUFSIZE, 0);
2006-09-30 20:27:56 +02:00
2006-10-08 16:33:07 +02:00
char *data[2], *part[4];
int ret = strsplit(rx_buf, " ", data, 2);
if (ret != 2)
2006-09-30 20:27:56 +02:00
return -1;
2006-10-08 16:33:07 +02:00
ret = strsplit(data[0], ":", part, 4);
if (ret != 4)
2006-09-30 20:27:56 +02:00
return -1;
2006-10-08 16:33:07 +02:00
rrd_submit(part[0], part[1], part[2], atoi(part[3]), data[1]);
2006-09-30 20:27:56 +02:00
return 0;
}
2006-10-08 16:33:07 +02:00
int net_init_cli()
2006-08-03 19:49:07 +02:00
{
2006-10-08 16:33:07 +02:00
int ret = config_get_saddr("global", "forward", &fwd_sa);
if (ret < 0)
2006-09-30 20:27:56 +02:00
return -1;
2006-08-03 19:49:07 +02:00
2006-10-07 20:48:20 +02:00
tx_buf = malloc(BUFSIZE);
if (tx_buf == NULL) {
log_print(LOG_ERROR, "net_init_cli(): out of memory");
return -1;
}
2006-09-30 20:27:56 +02:00
2006-10-08 16:33:07 +02:00
fwd_sock = socket(PF_INET, SOCK_DGRAM, 0);
if (fwd_sock < 0) {
log_print(LOG_ERROR, "net_init_cli(): socket()");
2006-10-07 20:48:20 +02:00
return -1;
2006-09-30 20:27:56 +02:00
}
2006-10-08 16:33:07 +02:00
log_print(LOG_INFO, "forwarding to %s:%d", inet_ntoa(fwd_sa.sin_addr), ntohs(fwd_sa.sin_port));
return 0;
2006-08-03 19:49:07 +02:00
}
2006-09-30 20:27:56 +02:00
int net_init_srv()
2006-08-03 19:49:07 +02:00
{
2006-09-30 20:27:56 +02:00
struct sockaddr_in sa_srv;
2006-10-08 16:33:07 +02:00
int ret = config_get_saddr("global", "listen", &sa_srv);
if (ret < 0)
2006-09-30 20:27:56 +02:00
return -1;
2006-10-08 16:33:07 +02:00
int srv_sock = socket(PF_INET, SOCK_DGRAM, 0);
if (srv_sock < 0) {
2006-09-30 20:27:56 +02:00
log_print(LOG_ERROR, "net_init_src(): socket()");
return -1;
}
if (bind(srv_sock, (struct sockaddr *)&sa_srv, sizeof(sa_srv)) < 0) {
log_print(LOG_ERROR, "net_init_src(): bind()");
close(srv_sock);
return -1;
}
rx_buf = malloc(BUFSIZE);
if (rx_buf == NULL) {
log_print(LOG_ERROR, "net_init_srv(): out of memory");
2006-10-07 20:48:20 +02:00
close(srv_sock);
return -1;
2006-09-30 20:27:56 +02:00
}
2006-08-03 19:49:07 +02:00
2006-09-30 20:27:56 +02:00
log_print(LOG_INFO, "listen on %s:%d", inet_ntoa(sa_srv.sin_addr), ntohs(sa_srv.sin_port));
return srv_sock;
2006-08-03 19:49:07 +02:00
}