2014-12-05 12:30:48 +01:00
|
|
|
/** Various socket related functions
|
|
|
|
*
|
|
|
|
* Parse and print addresses, connect, close, etc...
|
|
|
|
*
|
|
|
|
* S2SS uses these functions to setup the network emulation feature.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2015-06-02 21:53:04 +02:00
|
|
|
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
|
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
*********************************************************************************/
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2015-05-06 11:48:30 +02:00
|
|
|
#include <poll.h>
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
#include <linux/if_packet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-05-06 11:48:30 +02:00
|
|
|
#include <sys/ioctl.h>
|
2014-12-05 12:30:48 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "if.h"
|
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
/** Linked list of interfaces */
|
|
|
|
extern struct list interfaces;
|
|
|
|
|
2015-06-02 22:04:03 +02:00
|
|
|
/** Linked list of all sockets nodes */
|
|
|
|
static struct list sockets;
|
2015-05-06 11:48:30 +02:00
|
|
|
|
|
|
|
int socket_init(int argc, char * argv[], struct settings *set)
|
|
|
|
{ INDENT
|
|
|
|
list_init(&interfaces, (dtor_cb_t) if_destroy);
|
|
|
|
|
|
|
|
/* Gather list of used network interfaces */
|
|
|
|
FOREACH(&sockets, it) {
|
|
|
|
struct socket *s = it->socket;
|
|
|
|
|
|
|
|
/* Determine outgoing interface */
|
|
|
|
int index = if_getegress((struct sockaddr *) &s->remote);
|
|
|
|
if (index < 0) {
|
|
|
|
char buf[128];
|
|
|
|
socket_print_addr(buf, sizeof(buf), (struct sockaddr *) &s->remote);
|
|
|
|
error("Failed to get interface for socket address '%s'", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct interface *i = if_lookup_index(index);
|
|
|
|
if (!i)
|
|
|
|
i = if_create(index);
|
|
|
|
|
|
|
|
list_push(&i->sockets, s);
|
|
|
|
i->refcnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
FOREACH(&interfaces, it)
|
|
|
|
if_start(it->interface, set->affinity);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket_deinit()
|
|
|
|
{ INDENT
|
|
|
|
FOREACH(&interfaces, it)
|
|
|
|
if_stop(it->interface);
|
|
|
|
|
|
|
|
list_destroy(&interfaces);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
int socket_print(struct node *n, char *buf, int len)
|
|
|
|
{
|
|
|
|
struct socket *s = n->socket;
|
|
|
|
|
|
|
|
char local[INET6_ADDRSTRLEN + 16];
|
|
|
|
char remote[INET6_ADDRSTRLEN + 16];
|
|
|
|
|
2015-03-21 15:29:00 +01:00
|
|
|
socket_print_addr(local, sizeof(local), (struct sockaddr *) &s->local);
|
|
|
|
socket_print_addr(remote, sizeof(remote), (struct sockaddr *) &s->remote);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
return snprintf(buf, len, "local=%s, remote=%s", local, remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket_open(struct node *n)
|
|
|
|
{
|
|
|
|
struct socket *s = n->socket;
|
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-03-18 16:16:44 +01:00
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
/* Create socket */
|
|
|
|
switch (node_type(n)) {
|
|
|
|
case TCPD:
|
2014-12-09 17:19:07 +00:00
|
|
|
case TCP: s->sd = socket(sin->sin_family, SOCK_STREAM, IPPROTO_TCP); break;
|
|
|
|
case UDP: s->sd = socket(sin->sin_family, SOCK_DGRAM, IPPROTO_UDP); break;
|
|
|
|
case IP: s->sd = socket(sin->sin_family, SOCK_RAW, ntohs(sin->sin_port)); break;
|
2015-03-31 18:28:39 +02:00
|
|
|
case IEEE_802_3: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!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->sd < 0)
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed to create socket");
|
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");
|
2014-12-09 17:19:07 +00:00
|
|
|
|
|
|
|
/* Connect socket for sending */
|
2015-03-31 18:29:07 +02:00
|
|
|
if (node_type(n) == TCP) {
|
2014-12-09 17:19:07 +00:00
|
|
|
s->sd2 = s->sd;
|
|
|
|
ret = connect(s->sd, (struct sockaddr *) &s->remote, sizeof(s->remote));
|
|
|
|
if (ret < 0)
|
|
|
|
serror("Failed to connect socket");
|
|
|
|
}
|
2015-05-07 12:58:35 +02:00
|
|
|
|
|
|
|
/* Set fwmark for outgoing packets */
|
|
|
|
if (setsockopt(s->sd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)))
|
|
|
|
serror("Failed to set fwmark for outgoing packets");
|
|
|
|
else
|
|
|
|
debug(4, "Set fwmark for socket (sd=%u) to %u", s->sd, s->mark);
|
2014-12-05 12:30:48 +01:00
|
|
|
|
|
|
|
/* Set socket priority, QoS or TOS IP options */
|
|
|
|
int prio;
|
|
|
|
switch (node_type(n)) {
|
|
|
|
case TCPD:
|
|
|
|
case TCP:
|
|
|
|
case UDP:
|
|
|
|
case IP:
|
|
|
|
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
|
|
|
|
debug(4, "Set QoS/TOS IP option for node '%s' to %#x", n->name, prio);
|
|
|
|
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
|
|
|
|
debug(4, "Set socket priority for node '%s' to %u", n->name, prio);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket_close(struct node *n)
|
|
|
|
{
|
2014-12-09 17:19:07 +00:00
|
|
|
struct socket *s = n->socket;
|
|
|
|
|
2014-12-09 17:48:33 +00:00
|
|
|
if (s->sd >= 0)
|
2014-12-09 17:19:07 +00:00
|
|
|
close(s->sd);
|
|
|
|
|
2014-12-09 17:48:33 +00:00
|
|
|
if (s->sd2 >= 0)
|
2014-12-09 17:19:07 +00:00
|
|
|
close(s->sd2);
|
|
|
|
|
|
|
|
return 0;
|
2014-12-05 12:30:48 +01:00
|
|
|
}
|
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
int socket_read(struct node *n, struct msg *pool, int poolsize, int first, int cnt)
|
2014-12-05 12:30:48 +01:00
|
|
|
{
|
2015-05-06 11:48:30 +02:00
|
|
|
struct socket *s = n->socket;
|
|
|
|
|
|
|
|
int bytes;
|
|
|
|
struct iovec iov[cnt];
|
|
|
|
struct msghdr mhdr = {
|
|
|
|
.msg_iov = iov,
|
|
|
|
.msg_iovlen = ARRAY_LEN(iov)
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Wait until next packet received */
|
|
|
|
poll(&(struct pollfd) { .fd = s->sd, .events = POLLIN }, 1, -1);
|
|
|
|
/* Get size of received packet in bytes */
|
|
|
|
ioctl(s->sd, FIONREAD, &bytes);
|
|
|
|
|
|
|
|
/* Check packet integrity */
|
|
|
|
if (bytes % (cnt * 4) != 0)
|
|
|
|
error("Packet length not dividable by 4!");
|
|
|
|
if (bytes / cnt > sizeof(struct msg))
|
|
|
|
error("Packet length is too large!");
|
|
|
|
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
/* All messages of a packet must have equal length! */
|
2015-05-06 13:21:10 +02:00
|
|
|
iov[i].iov_base = &pool[(first+poolsize+i) % poolsize];
|
2015-05-06 11:48:30 +02:00
|
|
|
iov[i].iov_len = bytes / cnt;
|
|
|
|
}
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
/* Receive message from socket */
|
2015-05-06 11:48:30 +02:00
|
|
|
int ret = recvmsg(s->sd, &mhdr, 0);
|
2014-12-09 17:19:07 +00:00
|
|
|
if (ret == 0)
|
|
|
|
error("Remote node '%s' closed the connection", n->name);
|
|
|
|
else if (ret < 0)
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed recv");
|
2014-12-05 12:30:48 +01:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
for (int i = 0; i < cnt; i++) {
|
2015-05-06 13:21:10 +02:00
|
|
|
struct msg *n = &pool[(first+poolsize+i) % poolsize];
|
2014-12-05 12:30:48 +01:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
/* Check integrity of packet */
|
|
|
|
bytes -= MSG_LEN(n->length);
|
|
|
|
|
|
|
|
/* Convert headers to host byte order */
|
|
|
|
n->sequence = ntohs(n->sequence);
|
|
|
|
|
|
|
|
/* Convert message to host endianess */
|
|
|
|
if (n->endian != MSG_ENDIAN_HOST)
|
|
|
|
msg_swap(n);
|
|
|
|
}
|
2014-12-05 12:30:48 +01:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
/* Check packet integrity */
|
|
|
|
if (bytes != 0)
|
|
|
|
error("Packet length does not match message header length!");
|
2014-12-05 12:30:48 +01:00
|
|
|
|
2015-05-06 13:20:02 +02:00
|
|
|
return cnt;
|
2014-12-05 12:30:48 +01:00
|
|
|
}
|
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
int socket_write(struct node *n, struct msg *pool, int poolsize, int first, int cnt)
|
2014-12-05 12:30:48 +01:00
|
|
|
{
|
2014-12-09 17:19:07 +00:00
|
|
|
struct socket *s = n->socket;
|
2015-03-31 18:29:07 +02:00
|
|
|
int ret = -1;
|
2015-05-06 11:48:30 +02:00
|
|
|
|
|
|
|
struct iovec iov[cnt];
|
|
|
|
struct msghdr mhdr = {
|
|
|
|
.msg_iov = iov,
|
|
|
|
.msg_iovlen = ARRAY_LEN(iov)
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
struct msg *n = &pool[(first+poolsize+i) % poolsize];
|
|
|
|
|
|
|
|
/* Convert headers to host byte order */
|
|
|
|
n->sequence = htons(n->sequence);
|
|
|
|
|
|
|
|
iov[i].iov_base = n;
|
|
|
|
iov[i].iov_len = MSG_LEN(n->length);
|
|
|
|
}
|
2014-12-09 17:19:07 +00:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
/* Specify destination address for connection-less procotols */
|
2015-03-31 18:29:07 +02:00
|
|
|
switch (node_type(n)) {
|
2015-05-06 11:48:30 +02:00
|
|
|
case IEEE_802_3:
|
2015-03-31 18:29:07 +02:00
|
|
|
case IP:
|
|
|
|
case UDP:
|
2015-05-06 11:48:30 +02:00
|
|
|
mhdr.msg_name = (struct sockaddr *) &s->remote;
|
|
|
|
mhdr.msg_namelen = sizeof(s->remote);
|
2015-03-31 18:29:07 +02:00
|
|
|
break;
|
2015-05-06 11:48:30 +02:00
|
|
|
default:
|
2015-03-31 18:29:07 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-05-06 11:48:30 +02:00
|
|
|
|
|
|
|
ret = sendmsg(s->sd, &mhdr, 0);
|
2014-12-09 17:19:07 +00:00
|
|
|
if (ret < 0)
|
2015-03-31 18:29:07 +02:00
|
|
|
serror("Failed send");
|
2014-12-05 12:30:48 +01:00
|
|
|
|
2015-05-06 13:20:02 +02:00
|
|
|
return cnt;
|
2014-12-05 12:30:48 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 13:48:41 +02:00
|
|
|
int socket_parse(config_setting_t *cfg, struct node *n)
|
|
|
|
{
|
|
|
|
const char *local, *remote;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
struct socket *s = alloc(sizeof(struct socket));
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "remote", &remote))
|
|
|
|
cerror(cfg, "Missing remote address for node '%s'", n->name);
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "local", &local))
|
|
|
|
cerror(cfg, "Missing local address for node '%s'", n->name);
|
|
|
|
|
|
|
|
ret = socket_parse_addr(local, (struct sockaddr *) &s->local, node_type(n), AI_PASSIVE);
|
|
|
|
if (ret)
|
|
|
|
cerror(cfg, "Failed to resolve local address '%s' of node '%s': %s",
|
|
|
|
local, n->name, gai_strerror(ret));
|
|
|
|
|
|
|
|
ret = socket_parse_addr(remote, (struct sockaddr *) &s->remote, node_type(n), 0);
|
|
|
|
if (ret)
|
|
|
|
cerror(cfg, "Failed to resolve remote address '%s' of node '%s': %s",
|
|
|
|
remote, n->name, gai_strerror(ret));
|
|
|
|
|
|
|
|
/** @todo Netem settings are not usable AF_UNIX */
|
|
|
|
config_setting_t *cfg_netem = config_setting_get_member(cfg, "netem");
|
|
|
|
if (cfg_netem) {
|
|
|
|
s->netem = alloc(sizeof(struct netem));
|
|
|
|
|
|
|
|
tc_parse(cfg_netem, s->netem);
|
|
|
|
}
|
|
|
|
|
|
|
|
n->socket = s;
|
2015-05-06 11:48:30 +02:00
|
|
|
|
|
|
|
list_push(&sockets, s);
|
2015-03-31 13:48:41 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
int socket_print_addr(char *buf, int len, struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET: {
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
|
|
|
|
inet_ntop(sin->sin_family, &sin->sin_addr, buf, len);
|
|
|
|
return snprintf(buf+strlen(buf), len-strlen(buf), ":%hu", ntohs(sin->sin_port));
|
|
|
|
}
|
|
|
|
|
|
|
|
case AF_PACKET: {
|
|
|
|
struct sockaddr_ll *sll = (struct sockaddr_ll *) sa;
|
|
|
|
char ifname[IF_NAMESIZE];
|
|
|
|
|
2014-12-09 17:19:07 +00:00
|
|
|
return snprintf(buf, len, "%s%%%s:%#hx",
|
2014-12-05 12:30:48 +01:00
|
|
|
ether_ntoa((struct ether_addr *) &sll->sll_addr),
|
|
|
|
if_indextoname(sll->sll_ifindex, ifname),
|
|
|
|
ntohs(sll->sll_protocol));
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2015-05-06 11:48:30 +02:00
|
|
|
return snprintf(buf, len, "address family: %u", sa->sa_family);
|
2014-12-05 12:30:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket_parse_addr(const char *addr, struct sockaddr *sa, enum node_type type, int flags)
|
|
|
|
{
|
|
|
|
/** @todo: Add support for IPv6 */
|
|
|
|
|
|
|
|
char *copy = strdup(addr);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (type == IEEE_802_3) { /* Format: "ab:cd:ef:12:34:56%ifname:protocol" */
|
|
|
|
struct sockaddr_ll *sll = (struct sockaddr_ll *) sa;
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
error("Failed to parse mac address: %s", node);
|
|
|
|
|
|
|
|
memcpy(&sll->sll_addr, &mac->ether_addr_octet, 6);
|
|
|
|
|
2014-12-09 17:19:07 +00:00
|
|
|
sll->sll_protocol = htons((proto) ? strtol(proto, NULL, 0) : ETH_P_S2SS);
|
2014-12-05 12:30:48 +01:00
|
|
|
sll->sll_halen = 6;
|
|
|
|
sll->sll_family = AF_PACKET;
|
|
|
|
sll->sll_ifindex = if_nametoindex(ifname);
|
|
|
|
|
|
|
|
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-03-20 18:55:14 +01:00
|
|
|
.ai_family = AF_INET
|
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;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case IP:
|
2014-12-09 17:19:07 +00:00
|
|
|
hint.ai_socktype = SOCK_RAW;
|
|
|
|
hint.ai_protocol = (service) ? strtol(service, NULL, 0) : IPPROTO_S2SS;
|
|
|
|
hint.ai_flags |= AI_NUMERICSERV;
|
2014-12-05 12:30:48 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TCPD:
|
|
|
|
case TCP:
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
|
|
|
hint.ai_protocol = IPPROTO_TCP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UDP:
|
|
|
|
hint.ai_socktype = SOCK_DGRAM;
|
|
|
|
hint.ai_protocol = IPPROTO_UDP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Invalid address type");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup address */
|
|
|
|
struct addrinfo *result;
|
2014-12-09 17:19:07 +00:00
|
|
|
ret = getaddrinfo(node, (type == IP) ? NULL : service, &hint, &result);
|
2014-12-05 12:30:48 +01:00
|
|
|
if (!ret) {
|
2014-12-09 17:19:07 +00:00
|
|
|
|
|
|
|
if (type == IP) {
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(sa, result->ai_addr, result->ai_addrlen);
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
freeaddrinfo(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(copy);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|