2019-04-05 10:44:08 +02:00
|
|
|
/** Various functions to work with socket addresses
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2019-04-05 10:44:08 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netdb.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2019-04-05 10:44:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <villas/socket_addr.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2020-07-04 16:22:10 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2020-07-01 15:36:51 +02:00
|
|
|
|
|
|
|
#ifdef WITH_SOCKET_LAYER_ETH
|
2020-09-13 11:01:20 +02:00
|
|
|
#include <villas/kernel/nl.hpp>
|
2020-07-01 15:36:51 +02:00
|
|
|
#endif /* WITH_SOCKET_LAYER_ETH */
|
2019-04-05 10:44:08 +02:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2019-04-05 10:44:08 +02:00
|
|
|
char * socket_print_addr(struct sockaddr *saddr)
|
|
|
|
{
|
|
|
|
union sockaddr_union *sa = (union sockaddr_union *) saddr;
|
2020-01-21 16:26:51 +01:00
|
|
|
char *buf = new char[64];
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!buf)
|
|
|
|
throw MemoryAllocationError();
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
/* Address */
|
|
|
|
switch (sa->sa.sa_family) {
|
|
|
|
case AF_INET6:
|
|
|
|
inet_ntop(AF_INET6, &sa->sin6.sin6_addr, buf, 64);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
inet_ntop(AF_INET, &sa->sin.sin_addr, buf, 64);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef WITH_SOCKET_LAYER_ETH
|
|
|
|
case AF_PACKET:
|
|
|
|
strcatf(&buf, "%02x", sa->sll.sll_addr[0]);
|
|
|
|
for (int i = 1; i < sa->sll.sll_halen; i++)
|
|
|
|
strcatf(&buf, ":%02x", sa->sll.sll_addr[i]);
|
|
|
|
break;
|
|
|
|
#endif /* WITH_SOCKET_LAYER_ETH */
|
|
|
|
case AF_UNIX:
|
|
|
|
strcatf(&buf, "%s", sa->sun.sun_path);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Unknown address family: '{}'", sa->sa.sa_family);
|
2019-04-05 10:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Port / Interface */
|
|
|
|
switch (sa->sa.sa_family) {
|
|
|
|
case AF_INET6:
|
|
|
|
case AF_INET:
|
|
|
|
strcatf(&buf, ":%hu", ntohs(sa->sin.sin_port));
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef WITH_SOCKET_LAYER_ETH
|
|
|
|
case AF_PACKET: {
|
|
|
|
struct nl_cache *cache = nl_cache_mngt_require("route/link");
|
|
|
|
struct rtnl_link *link = rtnl_link_get(cache, sa->sll.sll_ifindex);
|
|
|
|
if (!link)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Failed to get interface for index: {}", sa->sll.sll_ifindex);
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
strcatf(&buf, "%%%s", rtnl_link_get_name(link));
|
|
|
|
strcatf(&buf, ":%hu", ntohs(sa->sll.sll_protocol));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* WITH_SOCKET_LAYER_ETH */
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
int socket_parse_address(const char *addr, struct sockaddr *saddr, enum SocketLayer layer, int flags)
|
2019-04-05 10:44:08 +02:00
|
|
|
{
|
2021-07-09 19:04:46 +02:00
|
|
|
/** @todo Add support for IPv6 */
|
2019-04-05 10:44:08 +02:00
|
|
|
union sockaddr_union *sa = (union sockaddr_union *) saddr;
|
|
|
|
|
|
|
|
char *copy = strdup(addr);
|
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (layer == SocketLayer::UNIX) { /* Format: "/path/to/socket" */
|
2019-04-05 10:44:08 +02:00
|
|
|
sa->sun.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
if (strlen(addr) > sizeof(sa->sun.sun_path) - 1)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Length of unix socket path is too long!");
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
memcpy(sa->sun.sun_path, addr, strlen(addr) + 1);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
#ifdef WITH_SOCKET_LAYER_ETH
|
2019-06-23 16:13:23 +02:00
|
|
|
else if (layer == SocketLayer::ETH) { /* Format: "ab:cd:ef:12:34:56%ifname:protocol" */
|
2019-04-05 10:44:08 +02:00
|
|
|
/* Split string */
|
|
|
|
char *lasts;
|
|
|
|
char *node = strtok_r(copy, "%", &lasts);
|
2019-04-07 15:13:40 +02:00
|
|
|
char *ifname = strtok_r(nullptr, ":", &lasts);
|
|
|
|
char *proto = strtok_r(nullptr, "\0", &lasts);
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
/* Parse link layer (MAC) address */
|
|
|
|
struct ether_addr *mac = ether_aton(node);
|
|
|
|
if (!mac)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Failed to parse MAC address: {}", node);
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
memcpy(&sa->sll.sll_addr, &mac->ether_addr_octet, ETHER_ADDR_LEN);
|
|
|
|
|
|
|
|
/* Get interface index from name */
|
2020-09-13 11:01:20 +02:00
|
|
|
kernel::nl::init();
|
|
|
|
|
2019-04-05 10:44:08 +02:00
|
|
|
struct nl_cache *cache = nl_cache_mngt_require("route/link");
|
|
|
|
struct rtnl_link *link = rtnl_link_get_by_name(cache, ifname);
|
|
|
|
if (!link)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Failed to get network interface: '{}'", ifname);
|
2019-04-05 10:44:08 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
sa->sll.sll_protocol = htons(proto ? strtol(proto, nullptr, 0) : ETH_P_VILLAS);
|
2019-04-05 10:44:08 +02:00
|
|
|
sa->sll.sll_halen = ETHER_ADDR_LEN;
|
|
|
|
sa->sll.sll_family = AF_PACKET;
|
|
|
|
sa->sll.sll_ifindex = rtnl_link_get_ifindex(link);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
#endif /* WITH_SOCKET_LAYER_ETH */
|
|
|
|
else { /* Format: "192.168.0.10:12001" */
|
|
|
|
struct addrinfo hint = {
|
|
|
|
.ai_flags = flags,
|
|
|
|
.ai_family = AF_UNSPEC
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Split string */
|
|
|
|
char *lasts;
|
|
|
|
char *node = strtok_r(copy, ":", &lasts);
|
2019-04-07 15:13:40 +02:00
|
|
|
char *service = strtok_r(nullptr, "\0", &lasts);
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
if (node && !strcmp(node, "*"))
|
2019-04-07 15:13:40 +02:00
|
|
|
node = nullptr;
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
if (service && !strcmp(service, "*"))
|
2019-04-07 15:13:40 +02:00
|
|
|
service = nullptr;
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
switch (layer) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case SocketLayer::IP:
|
2019-04-05 10:44:08 +02:00
|
|
|
hint.ai_socktype = SOCK_RAW;
|
2019-04-07 15:13:40 +02:00
|
|
|
hint.ai_protocol = (service) ? strtol(service, nullptr, 0) : IPPROTO_VILLAS;
|
2019-04-05 10:44:08 +02:00
|
|
|
hint.ai_flags |= AI_NUMERICSERV;
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SocketLayer::UDP:
|
2019-04-05 10:44:08 +02:00
|
|
|
hint.ai_socktype = SOCK_DGRAM;
|
|
|
|
hint.ai_protocol = IPPROTO_UDP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Invalid address type");
|
2019-04-05 10:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup address */
|
|
|
|
struct addrinfo *result;
|
2019-06-23 16:13:23 +02:00
|
|
|
ret = getaddrinfo(node, (layer == SocketLayer::IP) ? nullptr : service, &hint, &result);
|
2019-04-05 10:44:08 +02:00
|
|
|
if (!ret) {
|
2019-06-23 16:13:23 +02:00
|
|
|
if (layer == SocketLayer::IP) {
|
2019-04-05 10:44:08 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(sa, result->ai_addr, result->ai_addrlen);
|
|
|
|
freeaddrinfo(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(copy);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket_compare_addr(struct sockaddr *x, struct sockaddr *y)
|
|
|
|
{
|
|
|
|
#define CMP(a, b) if (a != b) return a < b ? -1 : 1
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
union sockaddr_union *xu = (union sockaddr_union *) x;
|
|
|
|
union sockaddr_union *yu = (union sockaddr_union *) y;
|
2019-04-05 10:44:08 +02:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
#ifdef WITH_SOCKET_LAYER_ETH
|
|
|
|
case AF_PACKET:
|
|
|
|
CMP(ntohs(xu->sll.sll_protocol), ntohs(yu->sll.sll_protocol));
|
|
|
|
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);
|
|
|
|
|
|
|
|
CMP(xu->sll.sll_halen, yu->sll.sll_halen);
|
|
|
|
return memcmp(xu->sll.sll_addr, yu->sll.sll_addr, xu->sll.sll_halen);
|
|
|
|
#endif /* WITH_SOCKET_LAYER_ETH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CMP
|
|
|
|
}
|