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
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
2018-07-03 18:25:15 +02:00
|
|
|
#include <sys/types.h>
|
2015-08-22 17:42:02 +02:00
|
|
|
#include <netinet/ip.h>
|
2017-03-03 20:21:33 -04:00
|
|
|
#include <arpa/inet.h>
|
2017-08-30 22:22:05 +02:00
|
|
|
#include <errno.h>
|
2017-07-24 19:21:57 +02:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#endif
|
2016-10-30 16:58:45 -04:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/nodes/socket.h>
|
|
|
|
#include <villas/config.h>
|
|
|
|
#include <villas/utils.h>
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/format_type.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/sample.h>
|
|
|
|
#include <villas/queue.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/compat.h>
|
2016-06-14 01:17:58 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
#ifdef WITH_NETEM
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/kernel/if.h>
|
|
|
|
#include <villas/kernel/nl.h>
|
|
|
|
#include <villas/kernel/tc_netem.h>
|
2017-08-14 14:42:07 +02:00
|
|
|
#endif /* WITH_NETEM */
|
2017-07-24 19:21:57 +02: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 */
|
2017-04-07 17:44:20 +02:00
|
|
|
struct list interfaces = { .state = STATE_DESTROYED };
|
2015-12-13 00:42:59 +01:00
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
int socket_type_start(struct super_node *sn)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef WITH_NETEM
|
2017-03-29 04:25:30 +02:00
|
|
|
int ret;
|
|
|
|
|
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++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct node *n = (struct node *) list_at(&p.node.instances, i);
|
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2015-08-22 17:42:02 +02:00
|
|
|
struct rtnl_link *link;
|
2015-05-06 11:48:30 +02:00
|
|
|
|
2017-12-14 13:23:19 +01:00
|
|
|
if (s->layer != SOCKET_LAYER_ETH &&
|
|
|
|
s->layer != SOCKET_LAYER_IP &&
|
2017-10-26 18:32:51 +02:00
|
|
|
s->layer != SOCKET_LAYER_UDP)
|
|
|
|
continue;
|
|
|
|
|
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-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t k = 0; k < list_length(&interfaces); k++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
i = (struct interface *) list_at(&interfaces, k);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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-07-09 14:36:09 +02:00
|
|
|
i = alloc(sizeof(struct interface));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
ret = if_init(i, link);
|
2017-03-29 04:25:30 +02:00
|
|
|
if (ret)
|
|
|
|
continue;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-05-05 19:09:57 +00:00
|
|
|
list_push(&interfaces, i);
|
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-10-18 15:39:53 +02:00
|
|
|
struct interface *i = (struct interface *) list_at(&interfaces, j);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
if_start(i);
|
2017-03-25 21:23:31 +01:00
|
|
|
}
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* WITH_NETEM */
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
int socket_type_stop()
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef WITH_NETEM
|
2017-04-07 17:44:20 +02:00
|
|
|
for (size_t j = 0; j < list_length(&interfaces); j++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct interface *i = (struct interface *) list_at(&interfaces, j);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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);
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* WITH_NETEM */
|
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
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2017-08-14 14:42:07 +02:00
|
|
|
char *layer = 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;
|
2017-10-26 18:32:51 +02:00
|
|
|
case SOCKET_LAYER_UNIX: layer = "unix"; break;
|
2015-08-22 17:42:02 +02:00
|
|
|
}
|
2016-09-10 20:40:37 -04: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
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
buf = strf("layer=%s, format=%s, local=%s, remote=%s", layer, plugin_name(s->format), local, remote);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-06-28 10:39:41 +02:00
|
|
|
if (s->multicast.enabled) {
|
|
|
|
char group[INET_ADDRSTRLEN];
|
|
|
|
char interface[INET_ADDRSTRLEN];
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-06-28 10:39:41 +02:00
|
|
|
inet_ntop(AF_INET, &s->multicast.mreq.imr_multiaddr, group, sizeof(group));
|
|
|
|
inet_ntop(AF_INET, &s->multicast.mreq.imr_interface, interface, sizeof(interface));
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-06-28 10:39:41 +02:00
|
|
|
strcatf(&buf, ", multicast.enabled=%s", s->multicast.enabled ? "yes" : "no");
|
|
|
|
strcatf(&buf, ", multicast.loop=%s", s->multicast.loop ? "yes" : "no");
|
|
|
|
strcatf(&buf, ", multicast.group=%s", group);
|
|
|
|
strcatf(&buf, ", multicast.interface=%s", s->multicast.mreq.imr_interface.s_addr == INADDR_ANY ? "any" : interface);
|
|
|
|
strcatf(&buf, ", multicast.ttl=%u", s->multicast.ttl);
|
|
|
|
}
|
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
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2014-12-09 17:19:07 +00:00
|
|
|
int ret;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-05-12 15:25:29 +02:00
|
|
|
// TODO: Move to socket_check() ?
|
2017-05-28 18:38:20 +02:00
|
|
|
/* Some checks on the addresses */
|
2017-10-26 18:32:51 +02:00
|
|
|
if (s->layer != SOCKET_LAYER_UNIX) {
|
|
|
|
if (s->local.sa.sa_family != s->remote.sa.sa_family)
|
|
|
|
error("Address families of local and remote must match!");
|
2017-06-28 10:39:41 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-28 18:38:20 +02:00
|
|
|
if (s->layer == SOCKET_LAYER_IP) {
|
|
|
|
if (ntohs(s->local.sin.sin_port) != ntohs(s->remote.sin.sin_port))
|
|
|
|
error("IP protocol numbers of local and remote must match!");
|
|
|
|
}
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
2017-06-28 10:39:41 +02:00
|
|
|
else if (s->layer == SOCKET_LAYER_ETH) {
|
2017-05-28 18:38:20 +02:00
|
|
|
if (ntohs(s->local.sll.sll_protocol) != ntohs(s->remote.sll.sll_protocol))
|
|
|
|
error("Ethertypes of local and remote must match!");
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-28 18:38:20 +02:00
|
|
|
if (ntohs(s->local.sll.sll_protocol) <= 0x5DC)
|
|
|
|
error("Ethertype must be large than %d or it is interpreted as an IEEE802.3 length field!", 0x5DC);
|
|
|
|
}
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* __linux__ */
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-10-26 18:32:51 +02:00
|
|
|
if (s->multicast.enabled) {
|
|
|
|
if (s->local.sa.sa_family != AF_INET)
|
|
|
|
error("Multicast is only supported by IPv4 for node %s", node_name(n));
|
|
|
|
|
|
|
|
uint32_t addr = ntohl(s->multicast.mreq.imr_multiaddr.s_addr);
|
|
|
|
if ((addr >> 28) != 14)
|
|
|
|
error("Multicast group address of node %s must be within 224.0.0.0/4", node_name(n));
|
|
|
|
}
|
|
|
|
|
2018-05-12 15:25:29 +02:00
|
|
|
/* Initialize IO */
|
|
|
|
ret = io_init(&s->io, s->format, n, SAMPLE_HAS_ALL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
/* Create socket */
|
2015-08-07 01:24:19 +02:00
|
|
|
switch (s->layer) {
|
2017-05-28 18:40:22 +02:00
|
|
|
case SOCKET_LAYER_UDP: s->sd = socket(s->local.sa.sa_family, SOCK_DGRAM, IPPROTO_UDP); break;
|
|
|
|
case SOCKET_LAYER_IP: s->sd = socket(s->local.sa.sa_family, SOCK_RAW, ntohs(s->local.sin.sin_port)); break;
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
2017-05-28 18:40:22 +02:00
|
|
|
case SOCKET_LAYER_ETH: s->sd = socket(s->local.sa.sa_family, SOCK_DGRAM, s->local.sll.sll_protocol); break;
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* __linux__ */
|
2017-10-26 18:32:51 +02:00
|
|
|
case SOCKET_LAYER_UNIX: s->sd = socket(s->local.sa.sa_family, SOCK_DGRAM, 0); 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
|
|
|
|
2018-07-16 21:59:23 +02:00
|
|
|
/* Delete Unix domain socket if already existing */
|
2017-10-26 18:32:51 +02:00
|
|
|
if (s->layer == SOCKET_LAYER_UNIX) {
|
|
|
|
ret = unlink(s->local.sun.sun_path);
|
2017-10-27 21:46:40 +02:00
|
|
|
if (ret && errno != ENOENT)
|
2017-10-26 18:32:51 +02:00
|
|
|
return ret;
|
2018-07-16 21:59:23 +02:00
|
|
|
}
|
2017-10-26 18:32:51 +02:00
|
|
|
|
2018-07-16 21:59:23 +02:00
|
|
|
/* Bind socket for receiving */
|
|
|
|
socklen_t addrlen = 0;
|
|
|
|
switch(s->local.ss.ss_family) {
|
|
|
|
case AF_INET: addrlen = sizeof(struct sockaddr_in); break;
|
|
|
|
case AF_INET6: addrlen = sizeof(struct sockaddr_in6); break;
|
|
|
|
case AF_UNIX: addrlen = SUN_LEN(&s->local.sun); break;
|
|
|
|
#ifdef __linux__
|
|
|
|
case AF_PACKET: addrlen = sizeof(struct sockaddr_ll); break;
|
|
|
|
#endif
|
|
|
|
default: addrlen = sizeof(s->local); break;
|
2017-10-26 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = bind(s->sd, (struct sockaddr *) &s->local, addrlen);
|
2014-12-09 17:19:07 +00:00
|
|
|
if (ret < 0)
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed to bind socket");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
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
|
|
|
}
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2017-06-28 10:39:41 +02:00
|
|
|
if (s->multicast.enabled) {
|
|
|
|
ret = setsockopt(s->sd, IPPROTO_IP, IP_MULTICAST_LOOP, &s->multicast.loop, sizeof(s->multicast.loop));
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to set multicast loop option");
|
|
|
|
|
2017-07-24 19:21:57 +02:00
|
|
|
ret = setsockopt(s->sd, IPPROTO_IP, IP_MULTICAST_TTL, &s->multicast.ttl, sizeof(s->multicast.ttl));
|
2017-06-28 10:39:41 +02:00
|
|
|
if (ret)
|
|
|
|
serror("Failed to set multicast ttl option");
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-06-28 10:39:41 +02:00
|
|
|
ret = setsockopt(s->sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &s->multicast.mreq, sizeof(s->multicast.mreq));
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to join multicast group");
|
|
|
|
}
|
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:
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
2014-12-05 12:30:48 +01:00
|
|
|
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;
|
2017-07-24 19:21:57 +02:00
|
|
|
#else
|
|
|
|
{ }
|
|
|
|
#endif /* __linux__ */
|
2014-12-05 12:30:48 +01:00
|
|
|
}
|
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)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) 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
|
|
|
{
|
2017-06-28 10:39:41 +02:00
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-06-28 10:39:41 +02:00
|
|
|
if (s->multicast.enabled) {
|
|
|
|
ret = setsockopt(s->sd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &s->multicast.mreq, sizeof(s->multicast.mreq));
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to leave multicast group");
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-10-26 18:32:51 +02:00
|
|
|
if (s->sd >= 0) {
|
|
|
|
ret = close(s->sd);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2018-05-12 15:25:29 +02:00
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2016-09-10 20:40:37 -04:00
|
|
|
|
2018-05-12 15:25:29 +02:00
|
|
|
ret = io_destroy(&s->io);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
#ifdef WITH_NETEM
|
2015-11-23 16:42:43 +01:00
|
|
|
rtnl_qdisc_put(s->tc_qdisc);
|
|
|
|
rtnl_cls_put(s->tc_classifier);
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* WITH_NETEM */
|
2016-09-10 20:40:37 -04:00
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
int socket_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-04-23 21:54:24 +02:00
|
|
|
{
|
2017-05-23 08:59:42 +02:00
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2017-04-23 21:54:24 +02:00
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
char *buf, *ptr;
|
|
|
|
ssize_t buflen;
|
2017-04-23 21:54:24 +02:00
|
|
|
ssize_t bytes;
|
2017-08-14 14:42:07 +02:00
|
|
|
size_t rbytes;
|
2016-09-10 22:16:23 -04:00
|
|
|
|
2017-05-28 18:49:49 +02:00
|
|
|
union sockaddr_union src;
|
2017-05-24 15:19:23 +00:00
|
|
|
socklen_t srclen = sizeof(src);
|
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
/* Get size of next packet */
|
|
|
|
buflen = recvfrom(s->sd, NULL, 0, MSG_TRUNC | MSG_PEEK, &src.sa, &srclen);
|
|
|
|
if (buflen < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
buf = alloc(buflen);
|
|
|
|
if (!buf)
|
|
|
|
return -1;
|
|
|
|
|
2017-05-28 18:49:49 +02:00
|
|
|
/* Receive next sample */
|
2017-10-27 19:15:23 +02:00
|
|
|
bytes = recvfrom(s->sd, buf, buflen, 0, &src.sa, &srclen);
|
2017-08-23 15:48:05 +02:00
|
|
|
if (bytes < 0)
|
2017-05-28 18:49:49 +02:00
|
|
|
serror("Failed recv from node %s", node_name(n));
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
ptr = buf;
|
|
|
|
|
2017-05-28 18:49:49 +02:00
|
|
|
/* Strip IP header from packet */
|
|
|
|
if (s->layer == SOCKET_LAYER_IP) {
|
2017-10-27 19:15:23 +02:00
|
|
|
struct ip *iphdr = (struct ip *) ptr;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
bytes -= iphdr->ip_hl * 4;
|
|
|
|
ptr += iphdr->ip_hl * 4;
|
2017-05-28 18:49:49 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-28 18:49:49 +02:00
|
|
|
/* SOCK_RAW IP sockets to not provide the IP protocol number via recvmsg()
|
|
|
|
* So we simply set it ourself. */
|
|
|
|
if (s->layer == SOCKET_LAYER_IP) {
|
|
|
|
switch (src.sa.sa_family) {
|
2017-10-27 19:15:23 +02:00
|
|
|
case AF_INET: src.sin.sin_port = s->remote.sin.sin_port; break;
|
2017-05-28 18:49:49 +02:00
|
|
|
case AF_INET6: src.sin6.sin6_port = s->remote.sin6.sin6_port; break;
|
|
|
|
}
|
2015-05-06 11:48:30 +02:00
|
|
|
}
|
2017-05-24 15:19:23 +00:00
|
|
|
|
2017-05-28 18:49:49 +02:00
|
|
|
if (s->verify_source && socket_compare_addr(&src.sa, &s->remote.sa) != 0) {
|
2017-05-24 15:19:23 +00:00
|
|
|
char *buf = socket_print_addr((struct sockaddr *) &src);
|
|
|
|
warn("Received packet from unauthorized source: %s", buf);
|
|
|
|
free(buf);
|
2017-05-28 18:49:49 +02:00
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
ret = 0;
|
|
|
|
goto out;
|
2017-05-24 15:19:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
ret = io_sscan(&s->io, ptr, bytes, &rbytes, smps, cnt);
|
2017-10-27 19:15:23 +02:00
|
|
|
|
|
|
|
if (ret < 0 || bytes != rbytes)
|
|
|
|
warn("Received invalid packet from node: %s ret=%d, bytes=%zu, rbytes=%zu", node_name(n), ret, bytes, rbytes);
|
2017-05-24 15:19:23 +00:00
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
out: free(buf);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2017-05-23 09:33:42 +02:00
|
|
|
return ret;
|
2014-12-05 12:30:48 +01:00
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
int socket_write(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2014-12-05 12:30:48 +01:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2015-08-07 01:24:19 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
int ret;
|
2017-10-27 19:15:23 +02:00
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
2017-04-23 21:54:24 +02:00
|
|
|
ssize_t bytes;
|
2017-08-14 14:42:07 +02:00
|
|
|
size_t wbytes;
|
2016-10-30 16:58:45 -04:00
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
buflen = SOCKET_INITIAL_BUFFER_LEN;
|
|
|
|
buf = alloc(buflen);
|
|
|
|
if (!buf)
|
2017-05-23 09:33:42 +02:00
|
|
|
return -1;
|
2016-07-17 01:01:43 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
retry: ret = io_sprint(&s->io, buf, buflen, &wbytes, smps, cnt);
|
2017-10-27 19:15:23 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2017-09-23 23:45:39 -06:00
|
|
|
if (wbytes <= 0)
|
2017-10-27 19:15:23 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (wbytes > buflen) {
|
|
|
|
buflen = wbytes;
|
|
|
|
buf = realloc(buf, buflen);
|
|
|
|
goto retry;
|
|
|
|
}
|
2017-09-23 23:45:39 -06:00
|
|
|
|
2017-04-23 21:54:24 +02:00
|
|
|
/* Send message */
|
2017-10-26 18:32:51 +02:00
|
|
|
socklen_t addrlen = 0;
|
2018-07-16 21:59:23 +02:00
|
|
|
switch(s->local.ss.ss_family) {
|
|
|
|
case AF_INET: addrlen = sizeof(struct sockaddr_in); break;
|
|
|
|
case AF_INET6: addrlen = sizeof(struct sockaddr_in6); break;
|
|
|
|
case AF_UNIX: addrlen = SUN_LEN(&s->local.sun); break;
|
|
|
|
#ifdef __linux__
|
|
|
|
case AF_PACKET: addrlen = sizeof(struct sockaddr_ll); break;
|
|
|
|
#endif
|
|
|
|
default: addrlen = sizeof(s->local); break;
|
2017-10-26 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
2018-06-15 14:56:48 +02:00
|
|
|
bytes = sendto(s->sd, buf, wbytes, MSG_DONTWAIT, (struct sockaddr *) &s->remote, addrlen);
|
2017-08-30 22:22:05 +02:00
|
|
|
if (bytes < 0) {
|
2017-10-26 18:32:51 +02:00
|
|
|
if ((errno == EPERM) ||
|
|
|
|
(errno == ENOENT && s->layer == SOCKET_LAYER_UNIX))
|
2017-08-30 22:22:05 +02:00
|
|
|
warn("Failed send to node %s: %s", node_name(n), strerror(errno));
|
2018-06-15 14:56:48 +02:00
|
|
|
else if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
|
|
|
|
warn("socket: send would block");
|
2017-08-30 22:22:05 +02:00
|
|
|
else
|
|
|
|
serror("Failed send to node %s", node_name(n));
|
|
|
|
}
|
2016-09-10 20:40:37 -04:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
if (bytes != wbytes)
|
|
|
|
warn("Partial send to node %s", node_name(n));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-27 19:15:23 +02:00
|
|
|
out: free(buf);
|
|
|
|
|
|
|
|
return ret;
|
2014-12-05 12:30:48 +01:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int socket_parse(struct node *n, json_t *cfg)
|
2015-03-31 13:48:41 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
const char *local, *remote;
|
|
|
|
const char *layer = NULL;
|
2018-04-09 08:28:54 +02:00
|
|
|
const char *format = "villas.binary";
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2015-03-31 13:48:41 +02:00
|
|
|
int ret;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
json_t *json_multicast = NULL;
|
2017-10-26 18:32:51 +02:00
|
|
|
json_t *json_netem = NULL;
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
s->layer = SOCKET_LAYER_UDP;
|
|
|
|
s->verify_source = 0;
|
2017-10-26 18:32:51 +02:00
|
|
|
#ifdef WITH_NETEM
|
|
|
|
s->tc_qdisc = NULL;
|
|
|
|
#endif /* WITH_NETEM */
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2017-12-14 13:23:19 +01:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s: s, s: s, s?: b, s?: o, s?: o, s?: s }",
|
2017-08-03 00:19:27 +02:00
|
|
|
"layer", &layer,
|
|
|
|
"remote", &remote,
|
|
|
|
"local", &local,
|
|
|
|
"verify_source", &s->verify_source,
|
2017-10-16 08:08:35 +02:00
|
|
|
"multicast", &json_multicast,
|
2017-12-14 13:23:19 +01:00
|
|
|
"netem", &json_netem,
|
2017-08-14 14:42:07 +02:00
|
|
|
"format", &format
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
|
2015-03-31 13:48:41 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
/* Format */
|
2018-05-12 13:56:12 +02:00
|
|
|
s->format = format_type_lookup(format);
|
2017-08-14 14:42:07 +02:00
|
|
|
if (!s->format)
|
|
|
|
error("Invalid format '%s' for node %s", format, node_name(n));
|
|
|
|
|
2016-09-10 20:40:37 -04:00
|
|
|
/* IP layer */
|
2017-08-03 00:19:27 +02:00
|
|
|
if (layer) {
|
2017-07-24 19:21:57 +02:00
|
|
|
if (!strcmp(layer, "ip"))
|
2016-09-10 20:40:37 -04:00
|
|
|
s->layer = SOCKET_LAYER_IP;
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
else if (!strcmp(layer, "eth"))
|
|
|
|
s->layer = SOCKET_LAYER_ETH;
|
|
|
|
#endif /*__linux__ */
|
2016-09-10 20:40:37 -04:00
|
|
|
else if (!strcmp(layer, "udp"))
|
|
|
|
s->layer = SOCKET_LAYER_UDP;
|
2017-10-26 18:32:51 +02:00
|
|
|
else if (!strcmp(layer, "unix") || !strcmp(layer, "local"))
|
|
|
|
s->layer = SOCKET_LAYER_UNIX;
|
2016-09-10 20:40:37 -04:00
|
|
|
else
|
2017-08-03 00:19:27 +02:00
|
|
|
error("Invalid layer '%s' for node %s", layer, node_name(n));
|
2016-09-10 20:40:37 -04: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) {
|
2017-08-03 00:19:27 +02:00
|
|
|
error("Failed to resolve remote address '%s' of node %s: %s",
|
2015-11-29 22:45:46 +01:00
|
|
|
remote, node_name(n), gai_strerror(ret));
|
2015-08-22 17:42:02 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-10-26 18:32:51 +02:00
|
|
|
ret = socket_parse_addr(local, (struct sockaddr *) &s->local, s->layer, AI_PASSIVE);
|
|
|
|
if (ret) {
|
|
|
|
error("Failed to resolve local address '%s' of node %s: %s",
|
|
|
|
local, node_name(n), gai_strerror(ret));
|
|
|
|
}
|
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
if (json_multicast) {
|
2017-08-03 00:19:27 +02:00
|
|
|
const char *group, *interface = NULL;
|
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
s->multicast.enabled = true;
|
|
|
|
s->multicast.mreq.imr_interface.s_addr = INADDR_ANY;
|
|
|
|
s->multicast.loop = 0;
|
|
|
|
s->multicast.ttl = 255;
|
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
ret = json_unpack_ex(json_multicast, &err, 0, "{ s?: b, s: s, s?: s, s?: b, s?: i }",
|
2017-08-03 00:19:27 +02:00
|
|
|
"enabled", &s->multicast.enabled,
|
|
|
|
"group", &group,
|
|
|
|
"interface", &interface,
|
|
|
|
"loop", &s->multicast.loop,
|
|
|
|
"ttl", &s->multicast.ttl
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse setting 'multicast' of node %s", node_name(n));
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
ret = inet_aton(group, &s->multicast.mreq.imr_multiaddr);
|
|
|
|
if (!ret) {
|
|
|
|
error("Failed to resolve multicast group address '%s' of node %s",
|
|
|
|
group, node_name(n));
|
2017-06-28 10:39:41 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
if (interface) {
|
2017-06-28 10:39:41 +02:00
|
|
|
ret = inet_aton(group, &s->multicast.mreq.imr_interface);
|
|
|
|
if (!ret) {
|
2017-08-03 00:19:27 +02:00
|
|
|
error("Failed to resolve multicast interface address '%s' of node %s",
|
2017-06-28 10:39:41 +02:00
|
|
|
interface, node_name(n));
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|
2017-06-28 10:39:41 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|
2015-03-31 13:48:41 +02:00
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
if (json_netem) {
|
2017-10-26 18:32:51 +02:00
|
|
|
#ifdef WITH_NETEM
|
2015-08-07 01:52:13 +02:00
|
|
|
int enabled = 1;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
ret = json_unpack_ex(json_netem, &err, 0, "{ s?: b }", "enabled", &enabled);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse setting 'netem' of node %s", node_name(n));
|
|
|
|
|
|
|
|
if (enabled)
|
2017-12-20 11:23:55 +01:00
|
|
|
tc_netem_parse(&s->tc_qdisc, json_netem);
|
2017-05-24 14:47:24 +00:00
|
|
|
else
|
|
|
|
s->tc_qdisc = NULL;
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* WITH_NETEM */
|
2017-10-26 18:32:51 +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
|
|
|
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
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;
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* __linux__ */
|
2017-10-26 18:32:51 +02:00
|
|
|
case AF_UNIX:
|
|
|
|
strcatf(&buf, "%s", sa->sun.sun_path);
|
|
|
|
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;
|
|
|
|
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
2015-08-22 17:42:02 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* __linux__ */
|
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
|
|
|
|
2017-10-26 18:32:51 +02:00
|
|
|
if (layer == SOCKET_LAYER_UNIX) { /* Format: "/path/to/socket" */
|
|
|
|
sa->sun.sun_family = AF_UNIX;
|
|
|
|
|
2018-04-04 08:53:00 +02:00
|
|
|
if (strlen(addr) > sizeof(sa->sun.sun_path)-1)
|
|
|
|
error("Length of unix socket path is too long!");
|
|
|
|
|
|
|
|
memcpy(sa->sun.sun_path, addr, strlen(sa->sun.sun_path)+1);
|
2017-10-26 18:32:51 +02:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
2017-10-26 18:32:51 +02:00
|
|
|
else 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
|
|
|
|
2017-05-28 18:49:49 +02:00
|
|
|
memcpy(&sa->sll.sll_addr, &mac->ether_addr_octet, ETHER_ADDR_LEN);
|
2016-09-10 20:40:37 -04:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
/* Get interface index from name */
|
2017-05-28 18:49:49 +02:00
|
|
|
nl_init();
|
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
|
|
|
|
2017-05-28 18:49:49 +02:00
|
|
|
sa->sll.sll_protocol = htons(proto ? strtol(proto, NULL, 0) : ETH_P_VILLAS);
|
|
|
|
sa->sll.sll_halen = ETHER_ADDR_LEN;
|
2015-08-22 17:42:02 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* __linux__ */
|
2017-10-26 18:32:51 +02: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);
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 18:32:51 +02:00
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
free(copy);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-09-19 15:26:30 +02:00
|
|
|
|
2017-05-24 15:19:23 +00:00
|
|
|
int socket_compare_addr(struct sockaddr *x, struct sockaddr *y)
|
|
|
|
{
|
|
|
|
#define CMP(a, b) if (a != b) return a < b ? -1 : 1
|
|
|
|
|
|
|
|
union sockaddr_union *xu = (void *) x, *yu = (void *) y;
|
|
|
|
|
|
|
|
CMP(x->sa_family, y->sa_family);
|
|
|
|
|
|
|
|
switch (x->sa_family) {
|
|
|
|
case AF_UNIX:
|
|
|
|
return strcmp(xu->sun.sun_path, yu->sun.sun_path);
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
CMP(ntohl(xu->sin.sin_addr.s_addr), ntohl(yu->sin.sin_addr.s_addr));
|
|
|
|
CMP(ntohs(xu->sin.sin_port), ntohs(yu->sin.sin_port));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
CMP(ntohs(xu->sin6.sin6_port), ntohs(yu->sin6.sin6_port));
|
|
|
|
// CMP(xu->sin6.sin6_flowinfo, yu->sin6.sin6_flowinfo);
|
|
|
|
// CMP(xu->sin6.sin6_scope_id, yu->sin6.sin6_scope_id);
|
|
|
|
|
|
|
|
return memcmp(xu->sin6.sin6_addr.s6_addr, yu->sin6.sin6_addr.s6_addr, sizeof(xu->sin6.sin6_addr.s6_addr));
|
|
|
|
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
2017-05-24 15:19:23 +00:00
|
|
|
case AF_PACKET:
|
2017-05-28 18:48:01 +02:00
|
|
|
CMP(ntohs(xu->sll.sll_protocol), ntohs(yu->sll.sll_protocol));
|
2017-05-24 15:19:23 +00:00
|
|
|
CMP(xu->sll.sll_ifindex, yu->sll.sll_ifindex);
|
|
|
|
// CMP(xu->sll.sll_pkttype, yu->sll.sll_pkttype);
|
|
|
|
// CMP(xu->sll.sll_hatype, yu->sll.sll_hatype);
|
|
|
|
|
2017-05-28 18:48:01 +02:00
|
|
|
CMP(xu->sll.sll_halen, yu->sll.sll_halen);
|
|
|
|
return memcmp(xu->sll.sll_addr, yu->sll.sll_addr, xu->sll.sll_halen);
|
2017-07-24 19:21:57 +02:00
|
|
|
#endif /* __linux__ */
|
2017-10-26 18:32:51 +02:00
|
|
|
|
2017-05-24 15:19:23 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CMP
|
|
|
|
}
|
|
|
|
|
2017-08-30 00:25:42 +02:00
|
|
|
int socket_fd(struct node *n)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct socket *s = (struct socket *) n->_vd;
|
2017-08-30 00:25:42 +02:00
|
|
|
|
|
|
|
return s->sd;
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
static struct plugin p = {
|
2015-11-23 16:44:01 +01:00
|
|
|
.name = "socket",
|
2017-12-14 13:23:19 +01:00
|
|
|
#ifdef WITH_NETEM
|
|
|
|
.description = "BSD network sockets for Ethernet / IP / UDP (libnl3, netem support)",
|
|
|
|
#else
|
|
|
|
.description = "BSD network sockets for Ethernet / IP / UDP",
|
|
|
|
#endif
|
2017-02-12 14:35:05 -03:00
|
|
|
.type = PLUGIN_TYPE_NODE,
|
|
|
|
.node = {
|
|
|
|
.vectorize = 0,
|
|
|
|
.size = sizeof(struct socket),
|
2018-07-16 08:08:17 +02:00
|
|
|
.type.start = socket_type_start,
|
|
|
|
.type.stop = socket_type_stop,
|
2017-02-12 14:35:05 -03:00
|
|
|
.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,
|
2017-08-30 00:25:42 +02:00
|
|
|
.fd = socket_fd
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
2015-11-23 16:44:01 +01:00
|
|
|
};
|
|
|
|
|
2017-04-25 15:11:41 +00:00
|
|
|
REGISTER_PLUGIN(&p)
|
2017-07-24 19:33:35 +02:00
|
|
|
LIST_INIT_STATIC(&p.node.instances)
|
2017-12-14 13:23:19 +01:00
|
|
|
|