2014-12-05 12:30:48 +01:00
|
|
|
|
/** Various socket related functions
|
|
|
|
|
*
|
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2015-06-02 21:53:04 +02:00
|
|
|
|
*********************************************************************************/
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/socket.h>
|
2015-08-22 17:42:02 +02:00
|
|
|
|
#include <netinet/ip.h>
|
2017-03-03 20:21:33 -04:00
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
|
#include <arpa/inet.h>
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-10-30 16:58:45 -04:00
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <byteswap.h>
|
|
|
|
|
#elif defined(__PPC__) /* Xilinx toolchain */
|
|
|
|
|
#include <xil_io.h>
|
|
|
|
|
#define bswap_16(x) Xil_EndianSwap16(x)
|
|
|
|
|
#define bswap_32(x) Xil_EndianSwap32(x)
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-14 01:17:58 +02:00
|
|
|
|
#include "nodes/socket.h"
|
2016-06-08 22:39:43 +02:00
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "utils.h"
|
2016-06-14 01:17:58 +02:00
|
|
|
|
|
|
|
|
|
#include "kernel/if.h"
|
|
|
|
|
#include "kernel/nl.h"
|
|
|
|
|
#include "kernel/tc.h"
|
2016-06-08 22:39:43 +02:00
|
|
|
|
#include "msg.h"
|
|
|
|
|
#include "sample.h"
|
|
|
|
|
#include "queue.h"
|
2017-02-12 14:35:05 -03:00
|
|
|
|
#include "plugin.h"
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2015-11-23 16:44:01 +01:00
|
|
|
|
/* Forward declartions */
|
2017-03-12 17:13:37 -03:00
|
|
|
|
static struct plugin p;
|
2015-05-06 11:48:30 +02:00
|
|
|
|
|
2015-12-13 00:42:59 +01:00
|
|
|
|
/* Private static storage */
|
|
|
|
|
struct list interfaces;
|
|
|
|
|
|
2017-03-13 00:04:56 -03:00
|
|
|
|
int socket_init(int argc, char *argv[], config_setting_t *cfg)
|
2015-12-11 17:56:14 +01:00
|
|
|
|
{
|
2017-03-29 04:25:30 +02:00
|
|
|
|
int ret;
|
|
|
|
|
|
2016-03-18 12:38:28 +01:00
|
|
|
|
if (getuid() != 0)
|
2017-03-13 00:04:56 -03:00
|
|
|
|
error("The 'socket' node-type requires super-user privileges!");
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-09-14 18:05:03 +02:00
|
|
|
|
nl_init(); /* Fill link cache */
|
2016-04-16 19:54:26 +02:00
|
|
|
|
list_init(&interfaces);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
|
/* Gather list of used network interfaces */
|
2017-03-25 21:23:31 +01:00
|
|
|
|
for (size_t i = 0; i < list_length(&p.node.instances); i++) {
|
|
|
|
|
struct node *n = list_at(&p.node.instances, i);
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2015-08-22 17:42:02 +02:00
|
|
|
|
struct rtnl_link *link;
|
2015-05-06 11:48:30 +02:00
|
|
|
|
|
|
|
|
|
/* Determine outgoing interface */
|
2017-03-29 04:25:30 +02:00
|
|
|
|
ret = if_get_egress((struct sockaddr *) &s->remote, &link);
|
|
|
|
|
if (ret) {
|
2015-09-22 12:58:37 +02:00
|
|
|
|
char *buf = socket_print_addr((struct sockaddr *) &s->remote);
|
2015-05-06 11:48:30 +02:00
|
|
|
|
error("Failed to get interface for socket address '%s'", buf);
|
2015-09-22 12:58:37 +02:00
|
|
|
|
free(buf);
|
2015-05-06 11:48:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 00:42:59 +01:00
|
|
|
|
/* Search of existing interface with correct ifindex */
|
|
|
|
|
struct interface *i;
|
2017-03-25 21:23:31 +01:00
|
|
|
|
|
|
|
|
|
for (size_t k = 0; k < list_length(&interfaces); k++) {
|
|
|
|
|
i = list_at(&interfaces, k);
|
|
|
|
|
|
2015-12-13 00:42:59 +01:00
|
|
|
|
if (rtnl_link_get_ifindex(i->nl_link) == rtnl_link_get_ifindex(link))
|
|
|
|
|
goto found;
|
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-12-13 00:42:59 +01:00
|
|
|
|
/* If not found, create a new interface */
|
2017-03-12 17:08:04 -03:00
|
|
|
|
struct interface j;
|
|
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
|
ret = if_init(&j, link);
|
|
|
|
|
if (ret)
|
|
|
|
|
continue;
|
2017-03-12 17:08:04 -03:00
|
|
|
|
|
|
|
|
|
list_push(&interfaces, memdup(&j, sizeof(j)));
|
|
|
|
|
i = &j;
|
2015-05-06 11:48:30 +02:00
|
|
|
|
|
2015-12-13 00:42:59 +01:00
|
|
|
|
found: list_push(&i->sockets, s);
|
2015-05-06 11:48:30 +02:00
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
|
for (size_t j = 0; j < list_length(&interfaces); j++) {
|
2017-03-25 21:23:31 +01:00
|
|
|
|
struct interface *i = list_at(&interfaces, j);
|
|
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
|
if_start(i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int socket_deinit()
|
2015-12-11 17:56:14 +01:00
|
|
|
|
{
|
2017-03-25 21:23:31 +01:00
|
|
|
|
for (size_t j = 0; list_length(&interfaces); j++) {
|
|
|
|
|
struct interface *i = list_at(&interfaces, j);
|
|
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
|
if_stop(i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-04-16 19:54:26 +02:00
|
|
|
|
list_destroy(&interfaces, (dtor_cb_t) if_destroy, false);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
|
char * socket_print(struct node *n)
|
2014-12-05 12:30:48 +01:00
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2016-10-30 17:10:20 -04:00
|
|
|
|
char *layer = NULL, *header = NULL, *endian = NULL, *buf;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
switch (s->layer) {
|
2016-09-10 22:16:23 -04:00
|
|
|
|
case SOCKET_LAYER_UDP: layer = "udp"; break;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
case SOCKET_LAYER_IP: layer = "ip"; break;
|
|
|
|
|
case SOCKET_LAYER_ETH: layer = "eth"; break;
|
2015-08-22 17:42:02 +02:00
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
|
|
|
|
switch (s->header) {
|
2016-10-20 09:06:29 -04:00
|
|
|
|
case SOCKET_HEADER_NONE: header = "none"; break;
|
|
|
|
|
case SOCKET_HEADER_FAKE: header = "fake"; break;
|
|
|
|
|
case SOCKET_HEADER_DEFAULT: header = "default"; break;
|
2015-08-22 17:42:02 +02:00
|
|
|
|
}
|
2016-10-30 17:10:20 -04:00
|
|
|
|
|
|
|
|
|
if (s->header == SOCKET_HEADER_DEFAULT)
|
|
|
|
|
endian = "auto";
|
2017-03-12 17:13:37 -03:00
|
|
|
|
else {
|
2016-10-30 17:10:20 -04:00
|
|
|
|
switch (s->endian) {
|
|
|
|
|
case MSG_ENDIAN_LITTLE: endian = "little"; break;
|
|
|
|
|
case MSG_ENDIAN_BIG: endian = "big"; break;
|
|
|
|
|
}
|
2017-03-12 17:13:37 -03:00
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
|
char *local = socket_print_addr((struct sockaddr *) &s->local);
|
|
|
|
|
char *remote = socket_print_addr((struct sockaddr *) &s->remote);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-10-30 17:10:20 -04:00
|
|
|
|
buf = strf("layer=%s, header=%s, endian=%s, local=%s, remote=%s", layer, header, endian, local, remote);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
|
free(local);
|
|
|
|
|
free(remote);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
|
return buf;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 23:30:24 -03:00
|
|
|
|
int socket_start(struct node *n)
|
2014-12-05 12:30:48 +01:00
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2014-12-09 17:19:07 +00:00
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *) &s->local;
|
|
|
|
|
struct sockaddr_ll *sll = (struct sockaddr_ll *) &s->local;
|
|
|
|
|
int ret;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
|
/* Create socket */
|
2015-08-07 01:24:19 +02:00
|
|
|
|
switch (s->layer) {
|
2016-09-10 20:40:37 -04:00
|
|
|
|
case SOCKET_LAYER_UDP: s->sd = socket(sin->sin_family, SOCK_DGRAM, IPPROTO_UDP); break;
|
|
|
|
|
case SOCKET_LAYER_IP: s->sd = socket(sin->sin_family, SOCK_RAW, ntohs(sin->sin_port)); break;
|
|
|
|
|
case SOCKET_LAYER_ETH: s->sd = socket(sll->sll_family, SOCK_DGRAM, sll->sll_protocol); break;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
default:
|
|
|
|
|
error("Invalid socket type!");
|
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
|
if (s->sd < 0)
|
2014-12-09 15:39:17 +00:00
|
|
|
|
serror("Failed to create socket");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
|
/* Bind socket for receiving */
|
2014-12-09 17:19:07 +00:00
|
|
|
|
ret = bind(s->sd, (struct sockaddr *) &s->local, sizeof(s->local));
|
|
|
|
|
if (ret < 0)
|
2014-12-09 15:39:17 +00:00
|
|
|
|
serror("Failed to bind socket");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
|
/* Set fwmark for outgoing packets if netem is enabled for this node */
|
|
|
|
|
if (s->mark) {
|
|
|
|
|
ret = setsockopt(s->sd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark));
|
|
|
|
|
if (ret)
|
|
|
|
|
serror("Failed to set FW mark for outgoing packets");
|
|
|
|
|
else
|
2017-02-12 14:12:35 -03:00
|
|
|
|
debug(LOG_SOCKET | 4, "Set FW mark for socket (sd=%u) to %u", s->sd, s->mark);
|
2016-06-08 22:39:43 +02:00
|
|
|
|
}
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
|
|
/* Set socket priority, QoS or TOS IP options */
|
|
|
|
|
int prio;
|
2015-08-07 01:24:19 +02:00
|
|
|
|
switch (s->layer) {
|
2016-09-10 20:40:37 -04:00
|
|
|
|
case SOCKET_LAYER_UDP:
|
|
|
|
|
case SOCKET_LAYER_IP:
|
2014-12-05 12:30:48 +01:00
|
|
|
|
prio = IPTOS_LOWDELAY;
|
|
|
|
|
if (setsockopt(s->sd, IPPROTO_IP, IP_TOS, &prio, sizeof(prio)))
|
2014-12-09 15:39:17 +00:00
|
|
|
|
serror("Failed to set type of service (QoS)");
|
2014-12-05 12:30:48 +01:00
|
|
|
|
else
|
2017-02-12 14:12:35 -03:00
|
|
|
|
debug(LOG_SOCKET | 4, "Set QoS/TOS IP option for node %s to %#x", node_name(n), prio);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
prio = SOCKET_PRIO;
|
|
|
|
|
if (setsockopt(s->sd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
|
2014-12-09 15:39:17 +00:00
|
|
|
|
serror("Failed to set socket priority");
|
2014-12-05 12:30:48 +01:00
|
|
|
|
else
|
2017-02-12 14:12:35 -03:00
|
|
|
|
debug(LOG_SOCKET | 4, "Set socket priority for node %s to %d", node_name(n), prio);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
|
int socket_reverse(struct node *n)
|
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2016-06-08 22:39:43 +02:00
|
|
|
|
union sockaddr_union tmp;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
|
tmp = s->local;
|
|
|
|
|
s->local = s->remote;
|
|
|
|
|
s->remote = tmp;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 23:30:24 -03:00
|
|
|
|
int socket_stop(struct node *n)
|
2014-12-05 12:30:48 +01:00
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2014-12-09 17:48:33 +00:00
|
|
|
|
if (s->sd >= 0)
|
2014-12-09 17:19:07 +00:00
|
|
|
|
close(s->sd);
|
2015-08-07 01:24:19 +02:00
|
|
|
|
|
2014-12-09 17:19:07 +00:00
|
|
|
|
return 0;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
|
int socket_destroy(struct node *n)
|
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
|
rtnl_qdisc_put(s->tc_qdisc);
|
|
|
|
|
rtnl_cls_put(s->tc_classifier);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
|
int socket_read(struct node *n, struct sample *smps[], unsigned cnt)
|
2014-12-05 12:30:48 +01:00
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-09-10 22:16:23 -04:00
|
|
|
|
int samples, ret, received, length;
|
2016-06-08 22:39:43 +02:00
|
|
|
|
ssize_t bytes;
|
|
|
|
|
|
2016-10-20 09:06:29 -04:00
|
|
|
|
if (s->header == SOCKET_HEADER_NONE || s->header == SOCKET_HEADER_FAKE) {
|
2016-09-10 20:58:46 -04:00
|
|
|
|
if (cnt < 1)
|
|
|
|
|
return 0;
|
2016-09-13 21:20:58 -04:00
|
|
|
|
|
2016-09-10 20:58:46 -04:00
|
|
|
|
/* The GTNETv2-SKT protocol send every sample in a single packet.
|
|
|
|
|
* socket_read() receives a single packet. */
|
2016-10-20 09:06:29 -04:00
|
|
|
|
int iov_len = s->header == SOCKET_HEADER_FAKE ? 2 : 1;
|
|
|
|
|
struct iovec iov[iov_len];
|
2016-09-10 20:58:46 -04:00
|
|
|
|
struct sample *smp = smps[0];
|
2016-10-20 09:06:29 -04:00
|
|
|
|
|
2016-09-13 21:20:58 -04:00
|
|
|
|
uint32_t header[3];
|
2016-10-20 09:06:29 -04:00
|
|
|
|
if (s->header == SOCKET_HEADER_FAKE) {
|
|
|
|
|
iov[0].iov_base = header;
|
|
|
|
|
iov[0].iov_len = sizeof(header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Remaining values are payload */
|
|
|
|
|
iov[iov_len-1].iov_base = &smp->data;
|
|
|
|
|
iov[iov_len-1].iov_len = SAMPLE_DATA_LEN(smp->capacity);
|
2016-09-13 21:20:58 -04:00
|
|
|
|
|
|
|
|
|
struct msghdr mhdr = {
|
|
|
|
|
.msg_iov = iov,
|
2016-10-20 09:06:29 -04:00
|
|
|
|
.msg_iovlen = iov_len,
|
2016-09-13 21:20:58 -04:00
|
|
|
|
.msg_name = (struct sockaddr *) &s->remote,
|
|
|
|
|
.msg_namelen = sizeof(s->remote)
|
|
|
|
|
};
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-09-10 20:58:46 -04:00
|
|
|
|
/* Receive next sample */
|
2016-09-13 21:20:58 -04:00
|
|
|
|
bytes = recvmsg(s->sd, &mhdr, MSG_TRUNC);
|
2016-07-17 01:01:43 +02:00
|
|
|
|
if (bytes == 0)
|
2016-09-10 20:58:46 -04:00
|
|
|
|
error("Remote node %s closed the connection", node_name(n)); /** @todo Should we really hard fail here? */
|
2016-07-17 01:01:43 +02:00
|
|
|
|
else if (bytes < 0)
|
|
|
|
|
serror("Failed recv from node %s", node_name(n));
|
2016-09-10 20:58:46 -04:00
|
|
|
|
else if (bytes % 4 != 0) {
|
2016-09-10 22:16:23 -04:00
|
|
|
|
warn("Packet size is invalid: %zd Must be multiple of 4 bytes.", bytes);
|
2016-09-10 20:58:46 -04:00
|
|
|
|
recv(s->sd, NULL, 0, 0); /* empty receive buffer */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2016-10-30 16:58:45 -04:00
|
|
|
|
|
|
|
|
|
/* Convert message to host endianess */
|
|
|
|
|
if (s->endian != MSG_ENDIAN_HOST) {
|
|
|
|
|
for (int i = 0; i < ARRAY_LEN(header); i++)
|
|
|
|
|
header[i] = bswap_32(header[i]);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes / SAMPLE_DATA_LEN(1); i++)
|
|
|
|
|
smp->data[i].i = bswap_32(smp->data[i].i);
|
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-10-30 16:58:45 -04:00
|
|
|
|
if (s->header == SOCKET_HEADER_FAKE)
|
|
|
|
|
length = (bytes - sizeof(header)) / SAMPLE_DATA_LEN(1);
|
|
|
|
|
else
|
|
|
|
|
length = bytes / SAMPLE_DATA_LEN(1);
|
|
|
|
|
|
2016-09-10 22:16:23 -04:00
|
|
|
|
if (length > smp->capacity) {
|
|
|
|
|
warn("Node %s received more values than supported. Dropping %u values", node_name(n), length - smp->capacity);
|
|
|
|
|
length = smp->capacity;
|
2016-07-17 01:01:43 +02:00
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-10-20 09:06:29 -04:00
|
|
|
|
if (s->header == SOCKET_HEADER_FAKE) {
|
|
|
|
|
smp->sequence = header[0];
|
|
|
|
|
smp->ts.origin.tv_sec = header[1];
|
|
|
|
|
smp->ts.origin.tv_nsec = header[2];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
smp->sequence = n->sequence++; /* Fake sequence no generated by VILLASnode */
|
|
|
|
|
smp->ts.origin.tv_sec = -1;
|
|
|
|
|
smp->ts.origin.tv_nsec = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 21:20:58 -04:00
|
|
|
|
smp->ts.received.tv_sec = -1;
|
|
|
|
|
smp->ts.received.tv_nsec = -1;
|
|
|
|
|
|
2016-09-10 22:16:23 -04:00
|
|
|
|
smp->length = length;
|
|
|
|
|
|
|
|
|
|
received = 1; /* GTNET-SKT sends every sample in a single packet */
|
2015-05-06 11:48:30 +02:00
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
else {
|
2016-09-10 20:58:46 -04:00
|
|
|
|
struct msg msgs[cnt];
|
|
|
|
|
struct msg hdr;
|
|
|
|
|
struct iovec iov[2*cnt];
|
|
|
|
|
struct msghdr mhdr = {
|
|
|
|
|
.msg_iov = iov
|
|
|
|
|
};
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
/* Peak into message header of the first sample and to get total packet size. */
|
|
|
|
|
bytes = recv(s->sd, &hdr, sizeof(struct msg), MSG_PEEK | MSG_TRUNC);
|
|
|
|
|
if (bytes < sizeof(struct msg) || bytes % 4 != 0) {
|
2016-09-10 22:16:23 -04:00
|
|
|
|
warn("Packet size is invalid: %zd Must be multiple of 4 bytes.", bytes);
|
|
|
|
|
recv(s->sd, NULL, 0, 0); /* empty receive buffer */
|
2016-07-17 01:01:43 +02:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
ret = msg_verify(&hdr);
|
|
|
|
|
if (ret) {
|
|
|
|
|
warn("Invalid message received: reason=%d, bytes=%zd", ret, bytes);
|
2016-09-10 20:58:46 -04:00
|
|
|
|
recv(s->sd, NULL, 0, 0); /* empty receive buffer */
|
2016-07-17 01:01:43 +02:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2015-09-25 03:17:14 +02:00
|
|
|
|
|
2015-10-08 10:51:48 +02:00
|
|
|
|
/* Convert message to host endianess */
|
2016-07-17 01:01:43 +02:00
|
|
|
|
if (hdr.endian != MSG_ENDIAN_HOST)
|
2016-10-30 17:11:12 -04:00
|
|
|
|
msg_hdr_swap(&hdr);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-09-10 22:52:46 -04:00
|
|
|
|
samples = bytes / MSG_LEN(hdr.length);
|
2016-07-17 01:01:43 +02:00
|
|
|
|
if (samples > cnt) {
|
2016-09-10 20:40:37 -04:00
|
|
|
|
warn("Node %s received more samples than supported. Dropping %u samples", node_name(n), samples - cnt);
|
2016-07-17 01:01:43 +02:00
|
|
|
|
samples = cnt;
|
|
|
|
|
}
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
/* We expect that all received samples have the same amount of values! */
|
|
|
|
|
for (int i = 0; i < samples; i++) {
|
|
|
|
|
iov[2*i+0].iov_base = &msgs[i];
|
|
|
|
|
iov[2*i+0].iov_len = MSG_LEN(0);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
iov[2*i+1].iov_base = SAMPLE_DATA_OFFSET(smps[i]);
|
2016-09-10 22:52:46 -04:00
|
|
|
|
iov[2*i+1].iov_len = SAMPLE_DATA_LEN(hdr.length);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
mhdr.msg_iovlen += 2;
|
2016-09-10 22:16:23 -04:00
|
|
|
|
|
2016-09-10 22:52:46 -04:00
|
|
|
|
if (hdr.length > smps[i]->capacity)
|
|
|
|
|
error("Node %s received more values than supported. Dropping %d values.", node_name(n), hdr.length - smps[i]->capacity);
|
2016-07-17 01:01:43 +02:00
|
|
|
|
}
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
/* Receive message from socket */
|
|
|
|
|
bytes = recvmsg(s->sd, &mhdr, 0); //--? samples - cnt samples dropped
|
|
|
|
|
if (bytes == 0)
|
|
|
|
|
error("Remote node %s closed the connection", node_name(n));
|
|
|
|
|
else if (bytes < 0)
|
|
|
|
|
serror("Failed recv from node %s", node_name(n));
|
2015-09-25 03:17:14 +02:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
for (received = 0; received < samples; received++) {
|
|
|
|
|
struct msg *m = &msgs[received];
|
|
|
|
|
struct sample *smp = smps[received];
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
ret = msg_verify(m);
|
|
|
|
|
if (ret)
|
|
|
|
|
break;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-09-10 22:52:46 -04:00
|
|
|
|
if (m->length != hdr.length)
|
2016-07-17 01:01:43 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Convert message to host endianess */
|
2016-10-30 17:11:12 -04:00
|
|
|
|
if (m->endian != MSG_ENDIAN_HOST) {
|
|
|
|
|
msg_hdr_swap(m);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < m->length; i++)
|
|
|
|
|
smp->data[i].i = bswap_32(smp->data[i].i);
|
|
|
|
|
}
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-09-10 22:52:46 -04:00
|
|
|
|
smp->length = m->length;
|
2016-07-17 01:01:43 +02:00
|
|
|
|
smp->sequence = m->sequence;
|
|
|
|
|
smp->ts.origin = MSG_TS(m);
|
2016-09-13 21:20:58 -04:00
|
|
|
|
smp->ts.received.tv_sec = -1;
|
|
|
|
|
smp->ts.received.tv_nsec = -1;
|
2016-07-17 01:01:43 +02:00
|
|
|
|
}
|
2015-05-06 11:48:30 +02:00
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2017-02-12 14:12:35 -03:00
|
|
|
|
debug(LOG_SOCKET | 17, "Received message of %zd bytes: %u samples", bytes, received);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
|
return received;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
|
int socket_write(struct node *n, struct sample *smps[], unsigned cnt)
|
2014-12-05 12:30:48 +01:00
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2016-06-08 22:39:43 +02:00
|
|
|
|
ssize_t bytes;
|
2016-09-10 21:10:00 -04:00
|
|
|
|
int sent = 0;
|
2015-08-07 01:24:19 +02:00
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
|
/* Construct iovecs */
|
2016-10-20 09:06:29 -04:00
|
|
|
|
if (s->header == SOCKET_HEADER_NONE || s->header == SOCKET_HEADER_FAKE) {
|
2016-09-10 21:10:00 -04:00
|
|
|
|
if (cnt < 1)
|
|
|
|
|
return 0;
|
2014-12-09 17:19:07 +00:00
|
|
|
|
|
2016-10-30 16:58:45 -04:00
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
|
int off = s->header == SOCKET_HEADER_FAKE ? 3 : 0;
|
|
|
|
|
int len = smps[i]->length + off;
|
|
|
|
|
uint32_t data[len];
|
|
|
|
|
|
2016-10-20 09:06:29 -04:00
|
|
|
|
/* First three values are sequence, seconds and nano-seconds timestamps */
|
|
|
|
|
if (s->header == SOCKET_HEADER_FAKE) {
|
2016-10-30 16:58:45 -04:00
|
|
|
|
data[0] = smps[i]->sequence;
|
|
|
|
|
data[1] = smps[i]->ts.origin.tv_sec;
|
|
|
|
|
data[2] = smps[i]->ts.origin.tv_nsec;
|
2016-10-20 09:06:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-30 16:58:45 -04:00
|
|
|
|
for (int j = 0; j < smps[i]->length; j++) {
|
|
|
|
|
if (s->endian == MSG_ENDIAN_HOST)
|
|
|
|
|
data[off + j] = smps[i]->data[j].i;
|
|
|
|
|
else
|
|
|
|
|
data[off + j] = bswap_32(smps[i]->data[j].i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytes = sendto(s->sd, data, len * sizeof(data[0]), 0,
|
|
|
|
|
(struct sockaddr *) &s->remote, sizeof(s->remote));
|
2016-09-10 21:10:00 -04:00
|
|
|
|
if (bytes < 0)
|
|
|
|
|
serror("Failed send to node %s", node_name(n));
|
|
|
|
|
|
|
|
|
|
sent++;
|
|
|
|
|
|
2017-02-12 14:12:35 -03:00
|
|
|
|
debug(LOG_SOCKET | 17, "Sent packet of %zd bytes with 1 sample", bytes);
|
2016-07-17 01:01:43 +02:00
|
|
|
|
}
|
2015-03-31 18:29:07 +02:00
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
else {
|
2016-09-10 21:10:00 -04:00
|
|
|
|
struct msg msgs[cnt];
|
|
|
|
|
struct iovec iov[2*cnt];
|
|
|
|
|
struct msghdr mhdr = {
|
|
|
|
|
.msg_iov = iov,
|
|
|
|
|
.msg_iovlen = ARRAY_LEN(iov),
|
|
|
|
|
.msg_name = (struct sockaddr *) &s->remote,
|
|
|
|
|
.msg_namelen = sizeof(s->remote)
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-17 01:01:43 +02:00
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-09-10 20:40:37 -04:00
|
|
|
|
msgs[i] = MSG_INIT(smps[i]->length, smps[i]->sequence);
|
2015-08-09 23:51:48 +02:00
|
|
|
|
|
2016-09-10 20:40:37 -04:00
|
|
|
|
msgs[i].ts.sec = smps[i]->ts.origin.tv_sec;
|
|
|
|
|
msgs[i].ts.nsec = smps[i]->ts.origin.tv_nsec;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-09-10 21:10:00 -04:00
|
|
|
|
iov[i*2+0].iov_base = &msgs[i];
|
|
|
|
|
iov[i*2+0].iov_len = MSG_LEN(0);
|
2016-07-17 01:01:43 +02:00
|
|
|
|
|
2016-09-10 21:10:00 -04:00
|
|
|
|
iov[i*2+1].iov_base = SAMPLE_DATA_OFFSET(smps[i]);
|
|
|
|
|
iov[i*2+1].iov_len = SAMPLE_DATA_LEN(smps[i]->length);
|
2016-07-17 01:01:43 +02:00
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2016-09-10 21:10:00 -04:00
|
|
|
|
/* Send message */
|
|
|
|
|
bytes = sendmsg(s->sd, &mhdr, 0);
|
|
|
|
|
if (bytes < 0)
|
|
|
|
|
serror("Failed send to node %s", node_name(n));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-09-10 21:10:00 -04:00
|
|
|
|
sent = cnt; /** @todo Find better way to determine how many values we actually sent */
|
2015-08-09 23:51:48 +02:00
|
|
|
|
|
2017-02-12 14:12:35 -03:00
|
|
|
|
debug(LOG_SOCKET | 17, "Sent packet of %zd bytes with %u samples", bytes, cnt);
|
2016-09-10 21:10:00 -04:00
|
|
|
|
}
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-09-10 21:10:00 -04:00
|
|
|
|
return sent;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
|
int socket_parse(struct node *n, config_setting_t *cfg)
|
2015-03-31 13:48:41 +02:00
|
|
|
|
{
|
2016-10-30 16:58:45 -04:00
|
|
|
|
const char *local, *remote, *layer, *hdr, *endian;
|
2015-03-31 13:48:41 +02:00
|
|
|
|
int ret;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2015-11-29 22:47:57 +01:00
|
|
|
|
struct socket *s = n->_vd;
|
2015-03-31 13:48:41 +02:00
|
|
|
|
|
2016-09-10 20:40:37 -04:00
|
|
|
|
/* IP layer */
|
2015-08-07 01:24:19 +02:00
|
|
|
|
if (!config_setting_lookup_string(cfg, "layer", &layer))
|
2016-09-10 20:40:37 -04:00
|
|
|
|
s->layer = SOCKET_LAYER_UDP;
|
|
|
|
|
else {
|
|
|
|
|
if (!strcmp(layer, "eth"))
|
|
|
|
|
s->layer = SOCKET_LAYER_ETH;
|
|
|
|
|
else if (!strcmp(layer, "ip"))
|
|
|
|
|
s->layer = SOCKET_LAYER_IP;
|
|
|
|
|
else if (!strcmp(layer, "udp"))
|
|
|
|
|
s->layer = SOCKET_LAYER_UDP;
|
|
|
|
|
else
|
|
|
|
|
cerror(cfg, "Invalid layer '%s' for node %s", layer, node_name(n));
|
|
|
|
|
}
|
2015-08-07 01:24:19 +02:00
|
|
|
|
|
2016-09-10 20:40:37 -04:00
|
|
|
|
/* Application header */
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "header", &hdr))
|
|
|
|
|
s->header = SOCKET_HEADER_DEFAULT;
|
|
|
|
|
else {
|
2016-10-20 09:06:29 -04:00
|
|
|
|
if (!strcmp(hdr, "gtnet-skt") || (!strcmp(hdr, "none")))
|
|
|
|
|
s->header = SOCKET_HEADER_NONE;
|
|
|
|
|
else if (!strcmp(hdr, "gtnet-skt:fake") || (!strcmp(hdr, "fake")))
|
|
|
|
|
s->header = SOCKET_HEADER_FAKE;
|
|
|
|
|
else if (!strcmp(hdr, "villas") || !strcmp(hdr, "default"))
|
2016-09-10 20:40:37 -04:00
|
|
|
|
s->header = SOCKET_HEADER_DEFAULT;
|
|
|
|
|
else
|
|
|
|
|
cerror(cfg, "Invalid application header type '%s' for node %s", hdr, node_name(n));
|
|
|
|
|
}
|
2016-10-30 16:58:45 -04:00
|
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "endian", &endian))
|
|
|
|
|
s->endian = MSG_ENDIAN_BIG;
|
|
|
|
|
else {
|
|
|
|
|
if (!strcmp(endian, "big") || !strcmp(endian, "network"))
|
|
|
|
|
s->endian = MSG_ENDIAN_BIG;
|
|
|
|
|
else if (!strcmp(endian, "little"))
|
|
|
|
|
s->endian = MSG_ENDIAN_LITTLE;
|
|
|
|
|
else
|
|
|
|
|
cerror(cfg, "Invalid endianness type '%s' for node %s", endian, node_name(n));
|
|
|
|
|
}
|
2015-08-07 01:24:19 +02:00
|
|
|
|
|
2015-03-31 13:48:41 +02:00
|
|
|
|
if (!config_setting_lookup_string(cfg, "remote", &remote))
|
2015-11-29 22:45:46 +01:00
|
|
|
|
cerror(cfg, "Missing remote address for node %s", node_name(n));
|
2015-03-31 13:48:41 +02:00
|
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "local", &local))
|
2015-11-29 22:45:46 +01:00
|
|
|
|
cerror(cfg, "Missing local address for node %s", node_name(n));
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-08-07 01:24:19 +02:00
|
|
|
|
ret = socket_parse_addr(local, (struct sockaddr *) &s->local, s->layer, AI_PASSIVE);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
if (ret) {
|
2015-11-29 22:45:46 +01:00
|
|
|
|
cerror(cfg, "Failed to resolve local address '%s' of node %s: %s",
|
|
|
|
|
local, node_name(n), gai_strerror(ret));
|
2015-08-22 17:42:02 +02:00
|
|
|
|
}
|
2015-03-31 13:48:41 +02:00
|
|
|
|
|
2015-08-07 01:24:19 +02:00
|
|
|
|
ret = socket_parse_addr(remote, (struct sockaddr *) &s->remote, s->layer, 0);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
if (ret) {
|
2015-11-29 22:45:46 +01:00
|
|
|
|
cerror(cfg, "Failed to resolve remote address '%s' of node %s: %s",
|
|
|
|
|
remote, node_name(n), gai_strerror(ret));
|
2015-08-22 17:42:02 +02:00
|
|
|
|
}
|
2015-03-31 13:48:41 +02:00
|
|
|
|
|
|
|
|
|
config_setting_t *cfg_netem = config_setting_get_member(cfg, "netem");
|
|
|
|
|
if (cfg_netem) {
|
2015-08-07 01:52:13 +02:00
|
|
|
|
int enabled = 1;
|
2015-09-14 18:05:03 +02:00
|
|
|
|
if (!config_setting_lookup_bool(cfg_netem, "enabled", &enabled) || enabled)
|
|
|
|
|
tc_parse(cfg_netem, &s->tc_qdisc);
|
2015-03-31 13:48:41 +02:00
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2015-03-31 13:48:41 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
|
char * socket_print_addr(struct sockaddr *saddr)
|
2014-12-05 12:30:48 +01:00
|
|
|
|
{
|
2015-08-22 17:42:02 +02:00
|
|
|
|
union sockaddr_union *sa = (union sockaddr_union *) saddr;
|
2015-09-22 12:58:37 +02:00
|
|
|
|
char *buf = alloc(64);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
/* Address */
|
|
|
|
|
switch (sa->sa.sa_family) {
|
|
|
|
|
case AF_INET6:
|
2015-09-22 12:58:37 +02:00
|
|
|
|
inet_ntop(AF_INET6, &sa->sin6.sin6_addr, buf, 64);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
break;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
case AF_INET:
|
2015-09-22 12:58:37 +02:00
|
|
|
|
inet_ntop(AF_INET, &sa->sin.sin_addr, buf, 64);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
break;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
case AF_PACKET:
|
2015-09-22 12:58:37 +02:00
|
|
|
|
strcatf(&buf, "%02x", sa->sll.sll_addr[0]);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
for (int i = 1; i < sa->sll.sll_halen; i++)
|
2015-09-22 12:58:37 +02:00
|
|
|
|
strcatf(&buf, ":%02x", sa->sll.sll_addr[i]);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
break;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
|
|
default:
|
2015-08-22 17:42:02 +02:00
|
|
|
|
error("Unknown address family: '%u'", sa->sa.sa_family);
|
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
/* Port / Interface */
|
|
|
|
|
switch (sa->sa.sa_family) {
|
|
|
|
|
case AF_INET6:
|
|
|
|
|
case AF_INET:
|
2015-09-22 12:58:37 +02:00
|
|
|
|
strcatf(&buf, ":%hu", ntohs(sa->sin.sin_port));
|
2015-08-22 17:42:02 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AF_PACKET: {
|
2015-09-14 18:05:03 +02:00
|
|
|
|
struct nl_cache *cache = nl_cache_mngt_require("route/link");
|
|
|
|
|
struct rtnl_link *link = rtnl_link_get(cache, sa->sll.sll_ifindex);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
if (!link)
|
|
|
|
|
error("Failed to get interface for index: %u", sa->sll.sll_ifindex);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
|
strcatf(&buf, "%%%s", rtnl_link_get_name(link));
|
|
|
|
|
strcatf(&buf, ":%hu", ntohs(sa->sll.sll_protocol));
|
2015-08-22 17:42:02 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2014-12-05 12:30:48 +01:00
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
|
return buf;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
int socket_parse_addr(const char *addr, struct sockaddr *saddr, enum socket_layer layer, int flags)
|
2014-12-05 12:30:48 +01:00
|
|
|
|
{
|
|
|
|
|
/** @todo: Add support for IPv6 */
|
2015-08-22 17:42:02 +02:00
|
|
|
|
union sockaddr_union *sa = (union sockaddr_union *) saddr;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
|
|
char *copy = strdup(addr);
|
|
|
|
|
int ret;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2016-09-10 20:40:37 -04:00
|
|
|
|
if (layer == SOCKET_LAYER_ETH) { /* Format: "ab:cd:ef:12:34:56%ifname:protocol" */
|
2014-12-05 12:30:48 +01:00
|
|
|
|
/* Split string */
|
|
|
|
|
char *node = strtok(copy, "%");
|
2014-12-09 17:19:07 +00:00
|
|
|
|
char *ifname = strtok(NULL, ":");
|
|
|
|
|
char *proto = strtok(NULL, "\0");
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
|
|
/* Parse link layer (MAC) address */
|
|
|
|
|
struct ether_addr *mac = ether_aton(node);
|
|
|
|
|
if (!mac)
|
2015-08-22 17:42:02 +02:00
|
|
|
|
error("Failed to parse MAC address: %s", node);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
memcpy(&sa->sll.sll_addr, &mac->ether_addr_octet, 6);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
|
/* Get interface index from name */
|
2015-09-14 18:05:03 +02:00
|
|
|
|
struct nl_cache *cache = nl_cache_mngt_require("route/link");
|
|
|
|
|
struct rtnl_link *link = rtnl_link_get_by_name(cache, ifname);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
if (!link)
|
|
|
|
|
error("Failed to get network interface: '%s'", ifname);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
2016-06-08 23:21:42 +02:00
|
|
|
|
sa->sll.sll_protocol = htons((proto) ? strtol(proto, NULL, 0) : ETH_P_VILLAS);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
sa->sll.sll_halen = 6;
|
|
|
|
|
sa->sll.sll_family = AF_PACKET;
|
|
|
|
|
sa->sll.sll_ifindex = rtnl_link_get_ifindex(link);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
2014-12-09 17:19:07 +00:00
|
|
|
|
else { /* Format: "192.168.0.10:12001" */
|
2014-12-05 12:30:48 +01:00
|
|
|
|
struct addrinfo hint = {
|
|
|
|
|
.ai_flags = flags,
|
2015-08-22 17:42:02 +02:00
|
|
|
|
.ai_family = AF_UNSPEC
|
2014-12-05 12:30:48 +01:00
|
|
|
|
};
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
|
/* Split string */
|
|
|
|
|
char *node = strtok(copy, ":");
|
|
|
|
|
char *service = strtok(NULL, "\0");
|
|
|
|
|
|
|
|
|
|
if (node && !strcmp(node, "*"))
|
|
|
|
|
node = NULL;
|
|
|
|
|
|
|
|
|
|
if (service && !strcmp(service, "*"))
|
|
|
|
|
service = NULL;
|
|
|
|
|
|
2015-08-07 01:24:19 +02:00
|
|
|
|
switch (layer) {
|
2016-09-10 20:40:37 -04:00
|
|
|
|
case SOCKET_LAYER_IP:
|
2014-12-09 17:19:07 +00:00
|
|
|
|
hint.ai_socktype = SOCK_RAW;
|
2016-06-08 23:21:42 +02:00
|
|
|
|
hint.ai_protocol = (service) ? strtol(service, NULL, 0) : IPPROTO_VILLAS;
|
2014-12-09 17:19:07 +00:00
|
|
|
|
hint.ai_flags |= AI_NUMERICSERV;
|
2014-12-05 12:30:48 +01:00
|
|
|
|
break;
|
|
|
|
|
|
2016-09-10 20:40:37 -04:00
|
|
|
|
case SOCKET_LAYER_UDP:
|
2014-12-05 12:30:48 +01:00
|
|
|
|
hint.ai_socktype = SOCK_DGRAM;
|
|
|
|
|
hint.ai_protocol = IPPROTO_UDP;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
error("Invalid address type");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Lookup address */
|
|
|
|
|
struct addrinfo *result;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
ret = getaddrinfo(node, (layer == SOCKET_LAYER_IP) ? NULL : service, &hint, &result);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
if (!ret) {
|
2016-09-10 20:40:37 -04:00
|
|
|
|
if (layer == SOCKET_LAYER_IP) {
|
2014-12-09 17:19:07 +00:00
|
|
|
|
/* We mis-use the sin_port field to store the IP protocol number on RAW sockets */
|
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *) result->ai_addr;
|
|
|
|
|
sin->sin_port = htons(result->ai_protocol);
|
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
|
|
|
|
memcpy(sa, result->ai_addr, result->ai_addrlen);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
freeaddrinfo(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
|
free(copy);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2015-09-19 15:26:30 +02:00
|
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
|
static struct plugin p = {
|
2015-11-23 16:44:01 +01:00
|
|
|
|
.name = "socket",
|
2016-06-08 22:39:43 +02:00
|
|
|
|
.description = "BSD network sockets",
|
2017-02-12 14:35:05 -03:00
|
|
|
|
.type = PLUGIN_TYPE_NODE,
|
|
|
|
|
.node = {
|
|
|
|
|
.vectorize = 0,
|
|
|
|
|
.size = sizeof(struct socket),
|
|
|
|
|
.destroy = socket_destroy,
|
|
|
|
|
.reverse = socket_reverse,
|
|
|
|
|
.parse = socket_parse,
|
|
|
|
|
.print = socket_print,
|
2017-03-11 23:30:24 -03:00
|
|
|
|
.start = socket_start,
|
|
|
|
|
.stop = socket_stop,
|
2017-02-12 14:35:05 -03:00
|
|
|
|
.read = socket_read,
|
|
|
|
|
.write = socket_write,
|
|
|
|
|
.init = socket_init,
|
2017-03-12 17:08:52 -03:00
|
|
|
|
.deinit = socket_deinit,
|
|
|
|
|
.instances = LIST_INIT()
|
2017-02-12 14:35:05 -03:00
|
|
|
|
}
|
2015-11-23 16:44:01 +01:00
|
|
|
|
};
|
|
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
|
REGISTER_PLUGIN(&p)
|