diff --git a/server/include/gtfpga.h b/server/include/gtfpga.h new file mode 100644 index 000000000..b449d5f60 --- /dev/null +++ b/server/include/gtfpga.h @@ -0,0 +1,16 @@ +/** Node type: GTFPGA (Xilinx ML507) + * + * This file implements the gtfpga subtype for nodes. + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#ifndef _GTFPGA_H_ +#define _GTFPGA_H_ + +struct gtfpga { + +}; + +#endif /* _GTFPGA_H_ */ diff --git a/server/include/opal.h b/server/include/opal.h new file mode 100644 index 000000000..7b0d74c7b --- /dev/null +++ b/server/include/opal.h @@ -0,0 +1,16 @@ +/** Node type: OPAL (AsyncApi) + * + * This file implements the opal subtype for nodes. + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#ifndef _OPAL_H_ +#define _OPAL_H_ + +struct opal { + +}; + +#endif /* _OPAL_H_ */ diff --git a/server/include/socket.h b/server/include/socket.h new file mode 100644 index 000000000..baa5e3e0a --- /dev/null +++ b/server/include/socket.h @@ -0,0 +1,105 @@ +/** Node type: socket + * + * This file implements the socket subtype for nodes. + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +#include + +#include "node.h" + +struct socket { + /** The socket descriptor */ + int sd; + /** Socket mark for netem, routing and filtering */ + int mark; + + /** Local address of the socket */ + struct sockaddr_storage local; + /** Remote address of the socket */ + struct sockaddr_storage remote; + + /** Network emulator settings */ + struct netem *netem; + + /* Linked list _per_interface_ */ + struct socket *next; +}; + +/** Create new socket and connect(), bind(), accept(). + * + * @param n A pointer to the node. + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ +int socket_open(struct node *n); + +/** Close the socket. + * + * @param n A pointer to the node. + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ +int socket_close(struct node *n); + +/** Send a message over a socket connection. + * + * @param m A pointer to the message + * @param n A pointer to the node + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ +int socket_write(struct node *n, struct msg *m); + +/** Receive a message over a socket connection. + * + * @param m A pointer to the message + * @param n A pointer to the node + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ +int socket_read(struct node *n, struct msg *m); + +/** Print details of socket connection + * + * @param n A pointer to the node structure + * @param buf A buffer to be filled. + * @param len The length of the supplied buffer. + * @return The length of the address. + */ +int socket_print(struct node *n, char *buf, int len); + +/** Generate printable socket address depending on the address family + * + * A IPv4 address is formatted as dotted decimals followed by the port/protocol number + * A link layer address is formatted in hexadecimals digits seperated by colons and the inferface name + * + * @param buf A buffer to be filled. + * @param len The length of the supplied buffer. + * @param sa A pointer to the socket address. + * @return The length of the address. + */ +int socket_print_addr(char *buf, int len, struct sockaddr *sa); + +/** Parse a socket address depending on the address family + * + * A IPv4 address has the follwing format: [hostname/ip]:[port/protocol] + * A link layer address has the following format: [mac]%[interface]:[ethertype] + * + * @todo Add support for autodetection of address type + * + * @param str A string specifiying the socket address. See description for allowed formats. + * @param sa A pointer to the resolved address + * @param type Specifies the address type in which the addr is given + * @param flags Flags for getaddrinfo(2) + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ +int socket_parse_addr(const char *str, struct sockaddr *sa, enum node_type type, int flags); + +#endif /* _SOCKET_H_ */ diff --git a/server/src/gtfpga.c b/server/src/gtfpga.c new file mode 100644 index 000000000..1c911e730 --- /dev/null +++ b/server/src/gtfpga.c @@ -0,0 +1,9 @@ +/** Node type: GTFPGA (Xilinx ML507) + * + * This file implements the gtfpga subtype for nodes. + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#include "gtfpga.h" diff --git a/server/src/opal.c b/server/src/opal.c new file mode 100644 index 000000000..8539723c0 --- /dev/null +++ b/server/src/opal.c @@ -0,0 +1,9 @@ +/** Node type: OPAL (AsyncApi) + * + * This file implements the opal subtype for nodes. + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#include "opal.h" diff --git a/server/src/socket.c b/server/src/socket.c new file mode 100644 index 000000000..96dc7acab --- /dev/null +++ b/server/src/socket.c @@ -0,0 +1,255 @@ +/** Various socket related functions + * + * Parse and print addresses, connect, close, etc... + * + * S2SS uses these functions to setup the network emulation feature. + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "utils.h" +#include "socket.h" +#include "if.h" + +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]; + + socket_print_addr(local, sizeof(local), (struct sockaddr*) &s->local); + socket_print_addr(remote, sizeof(remote), (struct sockaddr*) &s->remote); + + return snprintf(buf, len, "local=%s, remote=%s", local, remote); +} + +int socket_open(struct node *n) +{ + struct socket *s = n->socket; + int af = s->local.ss_family; + + /* Create socket */ + switch (node_type(n)) { + case TCPD: + case TCP: s->sd = socket(af, SOCK_STREAM, 0); break; + case UDP: s->sd = socket(af, SOCK_DGRAM, 0); break; + case IP: s->sd = socket(af, SOCK_RAW, IPPROTO_S2SS); break; + case IEEE_802_3:s->sd = socket(af, SOCK_DGRAM, ETH_P_S2SS); break; + default: + error("Invalid socket type!"); + } + + if (s->sd < 0) + perror("Failed to create socket"); + + /* Bind socket for receiving */ + if (bind(s->sd, (struct sockaddr *) &s->local, sizeof(s->local))) + perror("Failed to bind to socket"); + + /* Determine outgoing interface */ + int index = if_getegress((struct sockaddr *) &s->remote); + if (index < 0) + error("Failed to get egress interface for node '%s'", n->name); + + struct interface *i = if_lookup_index(index); + if (!i) + i = if_create(index); + + list_add(i->sockets, s); + i->refcnt++; + + /* 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))) + perror("Failed to set type of service (QoS)"); + 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))) + perror("Failed to set socket priority"); + else + debug(4, "Set socket priority for node '%s' to %u", n->name, prio); + break; + } + + return 0; +} + +int socket_close(struct node *n) +{ + return close(n->socket->sd); +} + +int socket_read(struct node* n, struct msg *m) +{ + /** @todo Fix this for multiple paths calling msg_recv. */ + + /* Receive message from socket */ + if (recv(n->socket->sd, m, sizeof(struct msg), 0) < 0) { + if (errno == EINTR) + return -EINTR; + + perror("Failed recv"); + } + + /* Convert headers to host byte order */ + m->sequence = ntohs(m->sequence); + + /* Convert message to host endianess */ + if (m->endian != MSG_ENDIAN_HOST) + msg_swap(m); + + debug(10, "Message received from node '%s': version=%u, type=%u, endian=%u, length=%u, sequence=%u", + n->name, m->version, m->type, m->endian, m->length, m->sequence); + + return 0; +} + +int socket_write(struct node* n, struct msg *m) +{ + /* Convert headers to network byte order */ + m->sequence = htons(m->sequence); + + if (sendto(n->socket->sd, m, MSG_LEN(m->length), 0, + (struct sockaddr *) &n->socket->remote, + sizeof(struct sockaddr_in)) < 0) + perror("Failed sendto"); + + debug(10, "Message sent to node '%s': version=%u, type=%u, endian=%u, length=%u, sequence=%u", + n->name, m->version, m->type, m->endian, m->length, ntohs(m->sequence)); + + return 0; +} + +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]; + + return snprintf(buf, len, "%s%%%s:%hu", + ether_ntoa((struct ether_addr *) &sll->sll_addr), + if_indextoname(sll->sll_ifindex, ifname), + ntohs(sll->sll_protocol)); + } + + default: + error("Unsupported address family"); + } + + 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, "%"); + char *ifname = strtok(NULL, "\0"); + + /* 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); + + sll->sll_protocol = ETH_P_S2SS; + sll->sll_halen = 6; + sll->sll_family = AF_PACKET; + sll->sll_ifindex = if_nametoindex(ifname); + + ret = 0; + } + else { + //struct sockaddr_in *sin = (struct sockaddr_in *) sa; + struct addrinfo hint = { + .ai_flags = flags, + .ai_family = AF_UNSPEC + }; + + /* 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: + hint.ai_socktype = 0; + hint.ai_protocol = IPPROTO_S2SS; + 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; + + case INVALID: + default: + error("Invalid address type"); + } + + /* Lookup address */ + struct addrinfo *result; + ret = getaddrinfo(node, service, &hint, &result); + if (!ret) { + memcpy(sa, result->ai_addr, result->ai_addrlen); + freeaddrinfo(result); + } + } + + free(copy); + + return ret; +}