2022-05-19 02:23:40 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2021-2022 - Roberto V. Rampim
|
2018-05-12 15:56:33 +00:00
|
|
|
*
|
2022-05-19 02:23:40 +00:00
|
|
|
* RetroArch 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 Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2018-05-12 15:56:33 +00:00
|
|
|
*
|
2022-05-19 02:23:40 +00:00
|
|
|
* RetroArch 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.
|
2018-05-12 15:56:33 +00:00
|
|
|
*
|
2022-05-19 02:23:40 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2018-05-12 15:56:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2022-01-17 12:46:03 +00:00
|
|
|
#include <stdio.h>
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2022-06-12 05:13:34 +00:00
|
|
|
#include <string/stdstring.h>
|
2021-12-09 04:52:42 +00:00
|
|
|
#include <formats/rxml.h>
|
|
|
|
#include <features/features_cpu.h>
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2022-06-12 05:13:34 +00:00
|
|
|
#include <retro_miscellaneous.h>
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-27 10:38:31 +00:00
|
|
|
#ifndef HAVE_SOCKET_LEGACY
|
|
|
|
#include <net/net_ifinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-19 02:23:40 +00:00
|
|
|
#include "../tasks/tasks_internal.h"
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-05-19 02:23:40 +00:00
|
|
|
#include "natt.h"
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
static bool translate_addr(struct sockaddr_in *addr,
|
|
|
|
char *host, size_t hostlen, char *port, size_t portlen)
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
#ifndef HAVE_SOCKET_LEGACY
|
|
|
|
if (getnameinfo((struct sockaddr *) addr, sizeof(*addr),
|
|
|
|
host, hostlen, port, portlen,
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV))
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
/* We need to do the conversion/translation manually. */
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
uint8_t *addr8 = (uint8_t *) &addr->sin_addr;
|
|
|
|
uint16_t port16 = ntohs(addr->sin_port);
|
|
|
|
|
|
|
|
if (host)
|
|
|
|
{
|
|
|
|
res = snprintf(host, hostlen, "%d.%d.%d.%d",
|
|
|
|
(int) addr8[0], (int) addr8[1],
|
|
|
|
(int) addr8[2], (int) addr8[3]);
|
|
|
|
if (res < 0 || res >= hostlen)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (port)
|
|
|
|
{
|
|
|
|
res = snprintf(port, portlen, "%hu", port16);
|
|
|
|
if (res < 0 || res >= portlen)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
bool natt_init(struct natt_discovery *discovery)
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
static const char msearch[] =
|
|
|
|
"M-SEARCH * HTTP/1.1\r\n"
|
|
|
|
"HOST: 239.255.255.250:1900\r\n"
|
|
|
|
"MAN: \"ssdp:discover\"\r\n"
|
2021-12-24 10:56:51 +00:00
|
|
|
"ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n"
|
|
|
|
"MX: 5\r\n"
|
|
|
|
"\r\n";
|
2022-01-17 12:46:03 +00:00
|
|
|
bool ret;
|
|
|
|
int fd = -1;
|
|
|
|
struct addrinfo *msearch_addr = NULL;
|
|
|
|
struct addrinfo *bind_addr = NULL;
|
|
|
|
struct addrinfo hints = {0};
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!discovery)
|
|
|
|
return false;
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
if (getaddrinfo_retro("239.255.255.250", "1900", &hints, &msearch_addr))
|
2021-12-09 04:52:42 +00:00
|
|
|
goto failure;
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!msearch_addr)
|
2021-12-09 04:52:42 +00:00
|
|
|
goto failure;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
fd = socket_init((void **) &bind_addr, 0, NULL, SOCKET_TYPE_DATAGRAM);
|
|
|
|
if (fd < 0)
|
2021-12-09 04:52:42 +00:00
|
|
|
goto failure;
|
|
|
|
if (!bind_addr)
|
|
|
|
goto failure;
|
|
|
|
|
2022-01-27 10:38:31 +00:00
|
|
|
#ifndef HAVE_SOCKET_LEGACY
|
2021-12-23 20:28:30 +00:00
|
|
|
{
|
2022-01-27 10:38:31 +00:00
|
|
|
struct sockaddr_in *addr = (struct sockaddr_in *) bind_addr->ai_addr;
|
2021-12-23 20:28:30 +00:00
|
|
|
|
2022-01-27 10:38:31 +00:00
|
|
|
if (net_ifinfo_best("223.255.255.255", &addr->sin_addr, false))
|
2021-12-23 20:28:30 +00:00
|
|
|
{
|
2021-12-24 13:15:20 +00:00
|
|
|
#ifdef IP_MULTICAST_IF
|
2022-01-27 10:38:31 +00:00
|
|
|
setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
|
|
|
|
(const char *) &addr->sin_addr, sizeof(addr->sin_addr));
|
2021-12-24 13:15:20 +00:00
|
|
|
#endif
|
2021-12-23 20:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-09 16:52:54 +00:00
|
|
|
#ifdef IP_MULTICAST_TTL
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2021-12-09 16:52:54 +00:00
|
|
|
unsigned long ttl = 2;
|
2022-01-17 12:46:03 +00:00
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
|
|
|
|
(const char *) &ttl, sizeof(ttl)) < 0) { }
|
2021-12-09 04:52:42 +00:00
|
|
|
#else
|
2021-12-09 16:52:54 +00:00
|
|
|
unsigned char ttl = 2;
|
2022-01-17 12:46:03 +00:00
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
|
2021-12-09 16:52:54 +00:00
|
|
|
&ttl, sizeof(ttl)) < 0) { }
|
2018-05-12 15:56:33 +00:00
|
|
|
#endif
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
2021-12-09 16:52:54 +00:00
|
|
|
#endif
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!socket_bind(fd, bind_addr))
|
2021-12-09 04:52:42 +00:00
|
|
|
goto failure;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (sendto(fd, msearch, STRLEN_CONST(msearch), 0,
|
|
|
|
msearch_addr->ai_addr, msearch_addr->ai_addrlen)
|
|
|
|
!= STRLEN_CONST(msearch))
|
2021-12-09 04:52:42 +00:00
|
|
|
goto failure;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!socket_nonblock(fd))
|
2021-12-09 04:52:42 +00:00
|
|
|
goto failure;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
discovery->fd = fd;
|
|
|
|
discovery->timeout = cpu_features_get_time_usec() + 5000000;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
ret = true;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
goto done;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
failure:
|
2022-01-17 12:46:03 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
socket_close(fd);
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
discovery->fd = -1;
|
|
|
|
discovery->timeout = -1;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
ret = false;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
done:
|
|
|
|
freeaddrinfo_retro(msearch_addr);
|
|
|
|
freeaddrinfo_retro(bind_addr);
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
return ret;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
2021-07-09 02:52:19 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
bool natt_device_next(struct natt_discovery *discovery,
|
|
|
|
struct natt_device *device)
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
char buf[2048];
|
|
|
|
ssize_t recvd;
|
|
|
|
char *data;
|
|
|
|
size_t remaining;
|
2022-01-10 14:52:15 +00:00
|
|
|
socklen_t addr_size = sizeof(device->addr);
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!discovery || !device)
|
2021-12-09 04:52:42 +00:00
|
|
|
return false;
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (discovery->fd < 0)
|
2021-12-09 04:52:42 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* This is faster than memsetting the whole thing. */
|
2022-01-17 12:46:03 +00:00
|
|
|
memset(&device->addr, 0, sizeof(device->addr));
|
|
|
|
memset(&device->ext_addr, 0, sizeof(device->ext_addr));
|
2021-12-09 04:52:42 +00:00
|
|
|
*device->desc = '\0';
|
|
|
|
*device->control = '\0';
|
|
|
|
*device->service_type = '\0';
|
|
|
|
device->busy = false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
recvd = recvfrom(discovery->fd, buf, sizeof(buf), 0,
|
2022-06-12 05:13:34 +00:00
|
|
|
(struct sockaddr*)&device->addr, &addr_size);
|
2022-06-01 06:24:46 +00:00
|
|
|
if (recvd < 0)
|
2022-06-12 05:13:34 +00:00
|
|
|
{
|
|
|
|
/* If there was no data, check for timeout. */
|
|
|
|
if (isagain((int)recvd))
|
|
|
|
return cpu_features_get_time_usec() < discovery->timeout;
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
return false;
|
2022-06-12 05:13:34 +00:00
|
|
|
}
|
2022-06-01 06:24:46 +00:00
|
|
|
/* Zero-length datagrams are valid, but we can't do anything with them.
|
|
|
|
Don't treat them as an error. */
|
|
|
|
if (!recvd)
|
|
|
|
return true;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
/* Parse the data we received.
|
|
|
|
We are only looking for the 'Location' HTTP header. */
|
|
|
|
data = buf;
|
2022-06-12 05:13:34 +00:00
|
|
|
remaining = (size_t)recvd;
|
2021-12-09 04:52:42 +00:00
|
|
|
do
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2022-06-12 05:13:34 +00:00
|
|
|
char *lnbreak = (char*)memchr(data, '\n', remaining);
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!lnbreak)
|
|
|
|
break;
|
2022-06-12 05:13:34 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
*lnbreak++ = '\0';
|
|
|
|
|
|
|
|
/* This also gets rid of any trailing carriage return. */
|
2022-01-10 14:52:15 +00:00
|
|
|
string_trim_whitespace(data);
|
|
|
|
|
|
|
|
if (string_starts_with_case_insensitive(data, "Location:"))
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2022-01-10 14:52:15 +00:00
|
|
|
char *location = string_trim_whitespace_left(
|
2021-12-09 04:52:42 +00:00
|
|
|
data + STRLEN_CONST("Location:"));
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2022-01-10 14:52:15 +00:00
|
|
|
if (string_starts_with_case_insensitive(location, "http://"))
|
2021-07-09 02:52:19 +00:00
|
|
|
{
|
2022-01-10 14:52:15 +00:00
|
|
|
/* Make sure the description URL isn't too long. */
|
|
|
|
if (strlcpy(device->desc, location, sizeof(device->desc)) <
|
|
|
|
sizeof(device->desc))
|
|
|
|
return true;
|
|
|
|
*device->desc = '\0';
|
2021-07-09 02:52:19 +00:00
|
|
|
}
|
2018-05-12 15:56:33 +00:00
|
|
|
}
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2022-06-12 05:13:34 +00:00
|
|
|
remaining -= (size_t)lnbreak - (size_t)data;
|
2021-12-09 04:52:42 +00:00
|
|
|
data = lnbreak;
|
|
|
|
} while (remaining);
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
/* This is not a failure.
|
|
|
|
We just don't yet have a valid device to report. */
|
|
|
|
return true;
|
2018-05-12 15:56:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
void natt_device_end(struct natt_discovery *discovery)
|
2021-12-04 14:45:29 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
if (discovery)
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
if (discovery->fd >= 0)
|
|
|
|
socket_close(discovery->fd);
|
|
|
|
|
|
|
|
discovery->fd = -1;
|
|
|
|
discovery->timeout = -1;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
2021-12-04 14:45:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
static bool build_control_url(rxml_node_t *control_url,
|
|
|
|
struct natt_device *device)
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
if (string_is_empty(control_url->data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Do we already have the full url? */
|
|
|
|
if (string_starts_with_case_insensitive(control_url->data, "http://"))
|
|
|
|
{
|
2022-01-10 14:52:15 +00:00
|
|
|
/* Make sure the control URL isn't too long. */
|
|
|
|
if (strlcpy(device->control, control_url->data,
|
|
|
|
sizeof(device->control)) >= sizeof(device->control))
|
|
|
|
{
|
|
|
|
*device->control = '\0';
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We don't have a full url.
|
|
|
|
Build one using the desc url. */
|
|
|
|
char *control_path;
|
|
|
|
|
|
|
|
strlcpy(device->control, device->desc,
|
|
|
|
sizeof(device->control));
|
|
|
|
|
2022-01-10 14:52:15 +00:00
|
|
|
control_path = (char *) strchr(device->control +
|
|
|
|
STRLEN_CONST("http://"), '/');
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
if (control_path)
|
|
|
|
*control_path = '\0';
|
|
|
|
if (control_url->data[0] != '/')
|
|
|
|
strlcat(device->control, "/", sizeof(device->control));
|
2022-01-10 14:52:15 +00:00
|
|
|
/* Make sure the control URL isn't too long. */
|
|
|
|
if (strlcat(device->control, control_url->data,
|
|
|
|
sizeof(device->control)) >= sizeof(device->control))
|
|
|
|
{
|
|
|
|
*device->control = '\0';
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:56:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
static bool parse_desc_node(rxml_node_t *node,
|
|
|
|
struct natt_device *device)
|
|
|
|
{
|
|
|
|
rxml_node_t *child = node->children;
|
|
|
|
|
|
|
|
if (!child)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* We only care for services. */
|
|
|
|
if (string_is_equal_case_insensitive(node->name, "service"))
|
|
|
|
{
|
|
|
|
rxml_node_t *service_type = NULL;
|
|
|
|
rxml_node_t *control_url = NULL;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (string_is_equal_case_insensitive(child->name, "serviceType"))
|
|
|
|
service_type = child;
|
|
|
|
else if (string_is_equal_case_insensitive(child->name, "controlURL"))
|
|
|
|
control_url = child;
|
|
|
|
if (service_type && control_url)
|
|
|
|
break;
|
|
|
|
} while ((child = child->next));
|
|
|
|
|
|
|
|
if (!service_type || !control_url)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* These two are the only IGD service types we can work with. */
|
2022-01-12 14:30:16 +00:00
|
|
|
if (!strstr(service_type->data, ":WANIPConnection:") &&
|
|
|
|
!strstr(service_type->data, ":WANPPPConnection:"))
|
2021-12-09 04:52:42 +00:00
|
|
|
return false;
|
|
|
|
if (!build_control_url(control_url, device))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
strlcpy(device->service_type, service_type->data,
|
|
|
|
sizeof(device->service_type));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XML recursion */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (parse_desc_node(child, device))
|
|
|
|
return true;
|
|
|
|
} while ((child = child->next));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void natt_query_device_cb(retro_task_t *task, void *task_data,
|
|
|
|
void *user_data, const char *error)
|
2021-12-04 14:45:29 +00:00
|
|
|
{
|
2022-03-30 13:32:23 +00:00
|
|
|
char *xml = NULL;
|
|
|
|
rxml_document_t *document = NULL;
|
|
|
|
http_transfer_data_t *data = (http_transfer_data_t*)task_data;
|
|
|
|
struct natt_device *device = (struct natt_device*)user_data;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-03-30 13:32:23 +00:00
|
|
|
*device->control = '\0';
|
|
|
|
*device->service_type = '\0';
|
2022-01-17 12:46:03 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
if (!data || !data->data || !data->len)
|
|
|
|
goto done;
|
|
|
|
if (data->status != 200)
|
|
|
|
goto done;
|
|
|
|
|
2022-03-30 13:32:23 +00:00
|
|
|
xml = (char*)malloc(data->len + 1);
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!xml)
|
|
|
|
goto done;
|
|
|
|
memcpy(xml, data->data, data->len);
|
|
|
|
xml[data->len] = '\0';
|
|
|
|
|
|
|
|
/* Parse the device's description XML. */
|
|
|
|
document = rxml_load_document_string(xml);
|
|
|
|
if (document)
|
|
|
|
{
|
|
|
|
rxml_node_t *root = rxml_root_node(document);
|
|
|
|
if (root)
|
|
|
|
parse_desc_node(root, device);
|
|
|
|
|
|
|
|
rxml_free_document(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(xml);
|
|
|
|
|
|
|
|
done:
|
|
|
|
device->busy = false;
|
2021-12-04 14:45:29 +00:00
|
|
|
}
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
bool natt_query_device(struct natt_device *device, bool block)
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!device)
|
2021-12-04 20:16:34 +00:00
|
|
|
return false;
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (string_is_empty(device->desc))
|
2018-05-12 15:56:33 +00:00
|
|
|
return false;
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (device->busy)
|
2018-05-12 15:56:33 +00:00
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
device->busy = true;
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!task_push_http_transfer(device->desc,
|
2022-01-17 12:46:03 +00:00
|
|
|
true, NULL, natt_query_device_cb, device))
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
device->busy = false;
|
2021-12-04 20:16:34 +00:00
|
|
|
return false;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (block)
|
|
|
|
task_queue_wait(NULL, NULL);
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_external_address_node(rxml_node_t *node,
|
|
|
|
struct natt_device *device)
|
|
|
|
{
|
|
|
|
if (string_is_equal_case_insensitive(node->name, "NewExternalIPAddress"))
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
struct addrinfo *addr = NULL;
|
|
|
|
struct addrinfo hints = {0};
|
|
|
|
|
|
|
|
if (string_is_empty(node->data))
|
2021-12-04 20:16:34 +00:00
|
|
|
return false;
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
if (getaddrinfo_retro(node->data, "0", &hints, &addr))
|
|
|
|
return false;
|
|
|
|
if (!addr)
|
|
|
|
return false;
|
|
|
|
memcpy(&device->ext_addr, addr->ai_addr,
|
|
|
|
sizeof(device->ext_addr));
|
|
|
|
freeaddrinfo_retro(addr);
|
|
|
|
|
|
|
|
return true;
|
2021-12-04 20:16:34 +00:00
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* XML recursion */
|
|
|
|
rxml_node_t *child = node->children;
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2022-02-22 07:22:10 +00:00
|
|
|
if (child)
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
2022-02-22 07:22:10 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (parse_external_address_node(child, device))
|
|
|
|
return true;
|
|
|
|
} while ((child = child->next));
|
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
static void natt_external_address_cb(retro_task_t *task, void *task_data,
|
|
|
|
void *user_data, const char *error)
|
|
|
|
{
|
2022-03-30 13:32:23 +00:00
|
|
|
char *xml = NULL;
|
|
|
|
rxml_document_t *document = NULL;
|
|
|
|
http_transfer_data_t *data = (http_transfer_data_t*)task_data;
|
|
|
|
struct natt_device *device = (struct natt_device*)user_data;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
memset(&device->ext_addr, 0, sizeof(device->ext_addr));
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
if (!data || !data->data || !data->len)
|
|
|
|
goto done;
|
|
|
|
if (data->status != 200)
|
|
|
|
goto done;
|
|
|
|
|
2022-03-30 13:32:23 +00:00
|
|
|
xml = (char*)malloc(data->len + 1);
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!xml)
|
|
|
|
goto done;
|
|
|
|
memcpy(xml, data->data, data->len);
|
|
|
|
xml[data->len] = '\0';
|
|
|
|
|
|
|
|
/* Parse the returned external ip address. */
|
|
|
|
document = rxml_load_document_string(xml);
|
|
|
|
if (document)
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
rxml_node_t *root = rxml_root_node(document);
|
|
|
|
if (root)
|
|
|
|
parse_external_address_node(root, device);
|
|
|
|
|
|
|
|
rxml_free_document(document);
|
2018-05-12 15:56:33 +00:00
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
free(xml);
|
|
|
|
|
|
|
|
done:
|
|
|
|
device->busy = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_open_port_node(rxml_node_t *node,
|
|
|
|
struct natt_request *request)
|
|
|
|
{
|
|
|
|
if (string_is_equal_case_insensitive(node->name, "u:AddPortMappingResponse"))
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
request->success = true;
|
|
|
|
|
|
|
|
return true;
|
2018-05-12 15:56:33 +00:00
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
else if (string_is_equal_case_insensitive(node->name, "NewReservedPort"))
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
uint16_t ext_port = 0;
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (string_is_empty(node->data))
|
|
|
|
return false;
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
sscanf(node->data, "%hu", &ext_port);
|
|
|
|
if (!ext_port)
|
|
|
|
return false;
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
request->addr.sin_port = htons(ext_port);
|
2022-01-17 12:46:03 +00:00
|
|
|
request->success = true;
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* XML recursion */
|
|
|
|
rxml_node_t *child = node->children;
|
|
|
|
|
2022-02-22 07:22:10 +00:00
|
|
|
if (child)
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
2022-02-22 07:22:10 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (parse_open_port_node(child, request))
|
|
|
|
return true;
|
|
|
|
} while ((child = child->next));
|
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2018-05-12 15:56:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
static void natt_open_port_cb(retro_task_t *task, void *task_data,
|
|
|
|
void *user_data, const char *error)
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2022-03-30 13:32:23 +00:00
|
|
|
char *xml = NULL;
|
|
|
|
rxml_document_t *document = NULL;
|
|
|
|
http_transfer_data_t *data = (http_transfer_data_t*)task_data;
|
|
|
|
struct natt_request *request = (struct natt_request*)user_data;
|
|
|
|
struct natt_device *device = (struct natt_device*)request->device;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-03-30 13:32:23 +00:00
|
|
|
request->success = false;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
if (!data || !data->data || !data->len)
|
|
|
|
goto done;
|
|
|
|
if (data->status != 200)
|
|
|
|
goto done;
|
|
|
|
|
2022-03-30 13:32:23 +00:00
|
|
|
xml = (char*)malloc(data->len + 1);
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!xml)
|
|
|
|
goto done;
|
|
|
|
memcpy(xml, data->data, data->len);
|
|
|
|
xml[data->len] = '\0';
|
|
|
|
|
|
|
|
/* Parse the device's port forwarding response. */
|
|
|
|
document = rxml_load_document_string(xml);
|
|
|
|
if (document)
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
rxml_node_t *root = rxml_root_node(document);
|
|
|
|
if (root)
|
|
|
|
parse_open_port_node(root, request);
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
rxml_free_document(document);
|
|
|
|
}
|
2021-12-05 02:47:48 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
free(xml);
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
done:
|
|
|
|
device->busy = false;
|
|
|
|
}
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
static void natt_close_port_cb(retro_task_t *task, void *task_data,
|
|
|
|
void *user_data, const char *error)
|
|
|
|
{
|
2022-03-30 13:32:23 +00:00
|
|
|
http_transfer_data_t *data = (http_transfer_data_t*)task_data;
|
|
|
|
struct natt_request *request = (struct natt_request*)user_data;
|
|
|
|
struct natt_device *device = (struct natt_device*)request->device;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-03-30 13:32:23 +00:00
|
|
|
request->success = false;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
if (!data || !data->data || !data->len)
|
|
|
|
goto done;
|
|
|
|
if (data->status != 200)
|
|
|
|
goto done;
|
|
|
|
|
2022-01-18 12:03:32 +00:00
|
|
|
/* We don't need to do anything special here. */
|
2022-03-30 13:32:23 +00:00
|
|
|
request->success = true;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
done:
|
2022-03-30 13:32:23 +00:00
|
|
|
device->busy = false;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
2021-12-05 02:47:48 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
static bool natt_action(struct natt_device *device,
|
|
|
|
const char *action, const char *data, retro_task_callback_t cb,
|
|
|
|
struct natt_request *request)
|
|
|
|
{
|
2022-03-30 15:30:20 +00:00
|
|
|
static const char headers_tmpl[] =
|
2021-12-09 04:52:42 +00:00
|
|
|
"Content-Type: text/xml\r\n"
|
|
|
|
"SOAPAction: \"%s#%s\"\r\n";
|
|
|
|
char headers[512];
|
|
|
|
void *obj;
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (string_is_empty(device->control))
|
|
|
|
return false;
|
|
|
|
|
2022-03-30 15:30:20 +00:00
|
|
|
snprintf(headers, sizeof(headers), headers_tmpl,
|
2021-12-09 04:52:42 +00:00
|
|
|
device->service_type, action);
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (request)
|
|
|
|
{
|
|
|
|
request->device = device;
|
2022-03-30 13:32:23 +00:00
|
|
|
obj = request;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
else
|
2022-03-30 13:32:23 +00:00
|
|
|
obj = device;
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
return task_push_http_post_transfer_with_headers(device->control,
|
|
|
|
data, true, NULL, headers, cb, obj) != NULL;
|
2018-05-12 15:56:33 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
bool natt_external_address(struct natt_device *device, bool block)
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2022-03-30 15:30:20 +00:00
|
|
|
static const char tmpl[] =
|
2021-12-09 04:52:42 +00:00
|
|
|
"<?xml version=\"1.0\"?>"
|
|
|
|
"<s:Envelope "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" "
|
|
|
|
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""
|
|
|
|
">"
|
|
|
|
"<s:Body>"
|
|
|
|
"<u:GetExternalIPAddress xmlns:u=\"%s\"/>"
|
|
|
|
"</s:Body>"
|
|
|
|
"</s:Envelope>";
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
if (!device)
|
2021-12-04 14:45:29 +00:00
|
|
|
return false;
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2022-03-30 15:30:20 +00:00
|
|
|
snprintf(buf, sizeof(buf), tmpl,
|
2022-01-17 12:46:03 +00:00
|
|
|
device->service_type);
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (device->busy)
|
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
device->busy = true;
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!natt_action(device, "GetExternalIPAddress", buf,
|
2022-01-17 12:46:03 +00:00
|
|
|
natt_external_address_cb, NULL))
|
2018-05-12 15:56:33 +00:00
|
|
|
{
|
2021-12-09 04:52:42 +00:00
|
|
|
device->busy = false;
|
|
|
|
return false;
|
2018-05-12 15:56:33 +00:00
|
|
|
}
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
if (block)
|
|
|
|
task_queue_wait(NULL, NULL);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool natt_open_port(struct natt_device *device,
|
|
|
|
struct natt_request *request, enum natt_forward_type forward_type,
|
|
|
|
bool block)
|
|
|
|
{
|
2022-03-30 15:30:20 +00:00
|
|
|
static const char tmpl[] =
|
2021-12-09 04:52:42 +00:00
|
|
|
"<?xml version=\"1.0\"?>"
|
|
|
|
"<s:Envelope "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" "
|
|
|
|
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""
|
|
|
|
">"
|
|
|
|
"<s:Body>"
|
|
|
|
"<u:%s xmlns:u=\"%s\">"
|
|
|
|
"<NewRemoteHost></NewRemoteHost>"
|
|
|
|
"<NewExternalPort>%s</NewExternalPort>"
|
|
|
|
"<NewProtocol>%s</NewProtocol>"
|
|
|
|
"<NewInternalPort>%s</NewInternalPort>"
|
|
|
|
"<NewInternalClient>%s</NewInternalClient>"
|
|
|
|
"<NewEnabled>1</NewEnabled>"
|
|
|
|
"<NewPortMappingDescription>retroarch</NewPortMappingDescription>"
|
|
|
|
"<NewLeaseDuration>0</NewLeaseDuration>"
|
|
|
|
"</u:%s>"
|
|
|
|
"</s:Body>"
|
|
|
|
"</s:Envelope>";
|
2022-01-11 07:48:59 +00:00
|
|
|
char buf[1280];
|
2022-01-17 12:46:03 +00:00
|
|
|
const char *action, *protocol;
|
2021-12-09 04:52:42 +00:00
|
|
|
char host[256], port[6];
|
|
|
|
|
|
|
|
if (!device || !request)
|
2021-12-04 14:45:29 +00:00
|
|
|
return false;
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!request->addr.sin_port)
|
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!translate_addr(&request->addr,
|
|
|
|
host, sizeof(host), port, sizeof(port)))
|
2021-12-09 04:52:42 +00:00
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
action = (forward_type == NATT_FORWARD_TYPE_ANY) ?
|
2021-12-09 04:52:42 +00:00
|
|
|
"AddAnyPortMapping" : "AddPortMapping";
|
2022-01-17 12:46:03 +00:00
|
|
|
protocol = (request->proto == SOCKET_PROTOCOL_UDP) ?
|
|
|
|
"UDP" : "TCP";
|
2022-03-30 15:30:20 +00:00
|
|
|
snprintf(buf, sizeof(buf), tmpl,
|
2022-01-17 12:46:03 +00:00
|
|
|
action, device->service_type,
|
|
|
|
port, protocol, port, host,
|
2021-12-09 04:52:42 +00:00
|
|
|
action);
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (device->busy)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
device->busy = true;
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!natt_action(device, action, buf,
|
2022-01-17 12:46:03 +00:00
|
|
|
natt_open_port_cb, request))
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
device->busy = false;
|
2021-12-04 14:45:29 +00:00
|
|
|
return false;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (block)
|
|
|
|
task_queue_wait(NULL, NULL);
|
2018-05-12 15:56:33 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
return true;
|
2021-12-04 14:45:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
bool natt_close_port(struct natt_device *device,
|
|
|
|
struct natt_request *request, bool block)
|
2021-12-04 14:45:29 +00:00
|
|
|
{
|
2022-03-30 15:30:20 +00:00
|
|
|
static const char tmpl[] =
|
2021-12-09 04:52:42 +00:00
|
|
|
"<?xml version=\"1.0\"?>"
|
|
|
|
"<s:Envelope "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" "
|
|
|
|
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""
|
|
|
|
">"
|
|
|
|
"<s:Body>"
|
|
|
|
"<u:DeletePortMapping xmlns:u=\"%s\">"
|
|
|
|
"<NewRemoteHost></NewRemoteHost>"
|
|
|
|
"<NewExternalPort>%s</NewExternalPort>"
|
|
|
|
"<NewProtocol>%s</NewProtocol>"
|
|
|
|
"</u:DeletePortMapping>"
|
|
|
|
"</s:Body>"
|
|
|
|
"</s:Envelope>";
|
|
|
|
char buf[1024];
|
2022-01-17 12:46:03 +00:00
|
|
|
const char *protocol;
|
2021-12-09 04:52:42 +00:00
|
|
|
char port[6];
|
|
|
|
|
|
|
|
if (!device || !request)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!request->addr.sin_port)
|
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!translate_addr(&request->addr,
|
|
|
|
NULL, 0, port, sizeof(port)))
|
2021-12-09 04:52:42 +00:00
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
protocol = (request->proto == SOCKET_PROTOCOL_UDP) ?
|
|
|
|
"UDP" : "TCP";
|
2022-03-30 15:30:20 +00:00
|
|
|
snprintf(buf, sizeof(buf), tmpl,
|
2022-01-17 12:46:03 +00:00
|
|
|
device->service_type, port, protocol);
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
if (device->busy)
|
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
device->busy = true;
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!natt_action(device, "DeletePortMapping", buf,
|
2022-01-17 12:46:03 +00:00
|
|
|
natt_close_port_cb, request))
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
device->busy = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block)
|
|
|
|
task_queue_wait(NULL, NULL);
|
|
|
|
|
|
|
|
return true;
|
2021-12-04 14:45:29 +00:00
|
|
|
}
|