1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

socket_zep: don't listen on local address by default

If no local port is specified for socket_zep to listen on, don't listen
on a local port at all instead of listening on a default port.

This does not work with multiple instances of socket_zep anyway.
This commit is contained in:
Benjamin Valentin 2020-10-27 23:20:11 +01:00
parent 36c742d384
commit 22ba75e8be
3 changed files with 54 additions and 24 deletions

View File

@ -34,16 +34,6 @@ extern "C" {
#define SOCKET_ZEP_MAX (1)
#endif
/**
* @name Default parameters for native argument parsing
* @{
*/
#define SOCKET_ZEP_PORT_DEFAULT "17754" /**< default port */
#define SOCKET_ZEP_LOCAL_ADDR_DEFAULT "::" /**< default local address */
/**
* @}
*/
/**
* @brief socket_zep configurations
*/

View File

@ -322,24 +322,24 @@ static const netdev_driver_t socket_zep_driver = {
.set = _set,
};
void socket_zep_setup(socket_zep_t *dev, const socket_zep_params_t *params)
static int _bind_local(const socket_zep_params_t *params)
{
int res;
static const struct addrinfo hints = { .ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM };
struct addrinfo *ai = NULL, *remote;
int res;
struct addrinfo *ai = NULL;
if (params->local_addr == NULL) {
return -1;
}
DEBUG("socket_zep_setup(%p, %p)\n", (void *)dev, (void *)params);
assert((params->local_addr != NULL) && (params->local_port != NULL) &&
(params->remote_addr != NULL) && (params->remote_port != NULL));
memset(dev, 0, sizeof(socket_zep_t));
dev->netdev.netdev.driver = &socket_zep_driver;
/* bind and connect socket */
if ((res = real_getaddrinfo(params->local_addr, params->local_port, &hints,
&ai)) < 0) {
errx(EXIT_FAILURE, "ZEP: unable to get local address: %s\n",
gai_strerror(res));
}
for (struct addrinfo *local = ai; local != NULL; local = local->ai_next) {
if ((res = real_socket(local->ai_family, local->ai_socktype,
local->ai_protocol)) < 0) {
@ -350,26 +350,66 @@ void socket_zep_setup(socket_zep_t *dev, const socket_zep_params_t *params)
}
}
real_freeaddrinfo(ai);
if (res < 0) {
err(EXIT_FAILURE, "ZEP: Unable to bind socket");
}
dev->sock_fd = res;
ai = NULL;
return res;
}
static int _connect_remote(socket_zep_t *dev, const socket_zep_params_t *params)
{
int res;
static const struct addrinfo hints = { .ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM };
struct addrinfo *ai = NULL, *remote;
if (params->remote_addr == NULL) {
return -1;
}
if ((res = real_getaddrinfo(params->remote_addr, params->remote_port, &hints,
&ai)) < 0) {
errx(EXIT_FAILURE, "ZEP: unable to get remote address: %s\n",
gai_strerror(res));
}
for (remote = ai; remote != NULL; remote = remote->ai_next) {
if (real_connect(dev->sock_fd, remote->ai_addr, remote->ai_addrlen) == 0) {
break; /* successfully connected */
}
}
if (remote == NULL) {
err(EXIT_FAILURE, "ZEP: Unable to connect socket");
}
real_freeaddrinfo(ai);
return res;
}
void socket_zep_setup(socket_zep_t *dev, const socket_zep_params_t *params)
{
int res;
DEBUG("socket_zep_setup(%p, %p)\n", (void *)dev, (void *)params);
assert((params->remote_addr != NULL) && (params->remote_port != NULL));
memset(dev, 0, sizeof(socket_zep_t));
dev->netdev.netdev.driver = &socket_zep_driver;
res = _bind_local(params);
if (res < 0) {
dev->sock_fd = socket(AF_INET6, SOCK_DGRAM, 0);
} else {
dev->sock_fd = res;
}
_connect_remote(dev, params);
/* generate hardware address from local address */
uint8_t ss_array[sizeof(struct sockaddr_storage)] = { 0 };
socklen_t ss_len = sizeof(struct sockaddr_storage);

View File

@ -314,8 +314,8 @@ void usage_exit(int status)
#endif
#if defined(MODULE_SOCKET_ZEP) && (SOCKET_ZEP_MAX > 0)
" -z [<laddr>:<lport>,]<raddr>:<rport> --zep=[<laddr>:<lport>,]<raddr>:<rport>\n"
" provide a ZEP interface with local address and port (<laddr>, <lport>)\n"
" and remote address and port (default local: [::]:17754).\n"
" provide a ZEP interface with (optional) local address and port (<laddr>, <lport>).\n"
" The ZEP interface connects to the remote address and may listen on a local address.\n"
" Required to be provided SOCKET_ZEP_MAX times\n"
#endif
);
@ -396,8 +396,8 @@ static void _zep_params_setup(char *zep_str, int zep)
}
second_ep = strtok_r(NULL, ",", &save_ptr);
if (second_ep == NULL) {
socket_zep_params[zep].local_addr = SOCKET_ZEP_LOCAL_ADDR_DEFAULT;
socket_zep_params[zep].local_port = SOCKET_ZEP_PORT_DEFAULT;
socket_zep_params[zep].local_addr = NULL;
socket_zep_params[zep].local_port = NULL;
_parse_ep_str(first_ep, &socket_zep_params[zep].remote_addr,
&socket_zep_params[zep].remote_port);
}