2017-02-21 00:08:31 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2022-01-10 14:52:15 +00:00
|
|
|
* Copyright (C) 2017-2017 - Gregor Richards
|
|
|
|
* Copyright (C) 2021-2022 - Roberto V. Rampim
|
2017-02-21 00:08:31 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-02-21 04:27:50 +00:00
|
|
|
#include <stdlib.h>
|
2017-02-21 00:08:31 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
#include <string/stdstring.h>
|
|
|
|
|
2017-02-21 00:08:31 +00:00
|
|
|
#include "tasks_internal.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-02-21 04:27:50 +00:00
|
|
|
#ifdef HAVE_NETWORKING
|
2022-01-17 12:46:03 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_SOCKET_LEGACY
|
|
|
|
#include <net/net_ifinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-19 02:23:40 +00:00
|
|
|
#include "../network/natt.h"
|
2017-02-21 00:08:31 +00:00
|
|
|
#include "../network/netplay/netplay.h"
|
|
|
|
|
2022-01-10 14:52:15 +00:00
|
|
|
/* Find the most suitable address within the device's network. */
|
2022-01-17 12:46:03 +00:00
|
|
|
static bool find_local_address(struct natt_device *device,
|
|
|
|
struct natt_request *request)
|
2022-01-10 14:52:15 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
bool ret = false;
|
|
|
|
/* TODO/FIXME: Find a way to get the network's interface on
|
|
|
|
HAVE_SOCKET_LEGACY platforms */
|
|
|
|
#ifndef HAVE_SOCKET_LEGACY
|
|
|
|
struct net_ifinfo interfaces = {0};
|
|
|
|
struct addrinfo **addrs = NULL;
|
|
|
|
uint32_t *scores = NULL;
|
|
|
|
|
|
|
|
if (net_ifinfo_new(&interfaces) && interfaces.size > 0)
|
2022-01-10 14:52:15 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
size_t i, j, k;
|
|
|
|
uint32_t highest_score = 0;
|
|
|
|
struct addrinfo hints = {0};
|
2022-03-30 13:45:48 +00:00
|
|
|
uint8_t *dev_addr8 = (uint8_t *)&device->addr.sin_addr;
|
2022-01-17 12:46:03 +00:00
|
|
|
|
2022-03-30 15:30:20 +00:00
|
|
|
addrs = (struct addrinfo**)calloc(interfaces.size, sizeof(*addrs));
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!addrs)
|
|
|
|
goto done;
|
2022-03-30 13:45:48 +00:00
|
|
|
scores = (uint32_t*)calloc(interfaces.size, sizeof(*scores));
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!scores)
|
|
|
|
goto done;
|
|
|
|
|
2022-03-30 13:45:48 +00:00
|
|
|
hints.ai_family = AF_INET;
|
2022-01-17 12:46:03 +00:00
|
|
|
|
|
|
|
/* Score interfaces based on how "close" their address
|
|
|
|
is from the device's address. */
|
|
|
|
for (i = 0; i < interfaces.size; i++)
|
2022-01-10 14:52:15 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
struct net_ifinfo_entry *entry = &interfaces.entries[i];
|
|
|
|
struct addrinfo **addr = &addrs[i];
|
|
|
|
uint32_t *score = &scores[i];
|
2022-01-10 14:52:15 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (getaddrinfo_retro(entry->host, NULL, &hints, addr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*addr)
|
2022-01-10 14:52:15 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
uint8_t *addr8 = (uint8_t *)
|
|
|
|
&((struct sockaddr_in *) (*addr)->ai_addr)->sin_addr;
|
|
|
|
bool stop_score = false;
|
|
|
|
|
|
|
|
for (j = 0; j < sizeof(device->addr.sin_addr) && !stop_score; j++)
|
|
|
|
{
|
|
|
|
uint8_t bits_dev = dev_addr8[j];
|
|
|
|
uint8_t bits_addr = addr8[j];
|
|
|
|
|
|
|
|
for (k = 0; k < 8; k++)
|
|
|
|
{
|
|
|
|
/* Each matched bit (from high to low bits)
|
|
|
|
means +1 to score.
|
|
|
|
Stop scoring when a bit mismatch. */
|
|
|
|
uint8_t bit_mask = 0x80 >> k;
|
|
|
|
uint8_t bit_dev = bits_dev & bit_mask;
|
|
|
|
uint8_t bit_addr = bits_addr & bit_mask;
|
|
|
|
|
|
|
|
if (bit_addr != bit_dev)
|
|
|
|
{
|
|
|
|
stop_score = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*score)++;
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
/* Get the highest scored interface. */
|
|
|
|
for (j = 0; j < interfaces.size; j++)
|
|
|
|
{
|
|
|
|
uint32_t score = scores[j];
|
2022-01-10 14:52:15 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (score > highest_score)
|
|
|
|
{
|
|
|
|
highest_score = score;
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Skip a highest score of less than 8. */
|
|
|
|
if (highest_score >= 8)
|
2022-01-10 14:52:15 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
/* Copy the interface's address to our request. */
|
|
|
|
memcpy(&request->addr.sin_addr,
|
|
|
|
&((struct sockaddr_in *) addrs[i]->ai_addr)->sin_addr,
|
|
|
|
sizeof(request->addr.sin_addr));
|
|
|
|
ret = true;
|
2022-01-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
for (i = 0; i < interfaces.size; i++)
|
|
|
|
freeaddrinfo_retro(addrs[i]);
|
|
|
|
}
|
2022-01-10 14:52:15 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
free(scores);
|
2022-01-17 12:46:03 +00:00
|
|
|
free(addrs);
|
|
|
|
net_ifinfo_free(&interfaces);
|
|
|
|
#endif
|
2022-01-10 14:52:15 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-04 20:16:34 +00:00
|
|
|
static void task_netplay_nat_traversal_handler(retro_task_t *task)
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
static struct natt_discovery discovery = {-1};
|
|
|
|
static struct natt_device device = {0};
|
2022-03-30 13:45:48 +00:00
|
|
|
struct nat_traversal_data *data = (struct nat_traversal_data*)task->task_data;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
/* Try again on the next call. */
|
2022-01-17 12:46:03 +00:00
|
|
|
if (device.busy)
|
2021-12-09 04:52:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (data->status)
|
|
|
|
{
|
|
|
|
case NAT_TRAVERSAL_STATUS_DISCOVERY:
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!natt_init(&discovery))
|
2021-12-09 04:52:42 +00:00
|
|
|
goto finished;
|
|
|
|
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_SELECT_DEVICE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAT_TRAVERSAL_STATUS_SELECT_DEVICE:
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
if (!natt_device_next(&discovery, &device))
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
natt_device_end(&discovery);
|
2021-12-09 04:52:42 +00:00
|
|
|
goto finished;
|
|
|
|
}
|
2022-01-17 12:46:03 +00:00
|
|
|
|
|
|
|
if (string_is_empty(device.desc))
|
|
|
|
break;
|
|
|
|
if (!find_local_address(&device, &data->request))
|
|
|
|
break;
|
|
|
|
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_QUERY_DEVICE;
|
2021-12-09 04:52:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAT_TRAVERSAL_STATUS_QUERY_DEVICE:
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
if (natt_query_device(&device, false))
|
2021-12-09 04:52:42 +00:00
|
|
|
data->status = NAT_TRAVERSAL_STATUS_EXTERNAL_ADDRESS;
|
|
|
|
else
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_SELECT_DEVICE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAT_TRAVERSAL_STATUS_EXTERNAL_ADDRESS:
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
if (string_is_empty(device.service_type))
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_SELECT_DEVICE;
|
|
|
|
break;
|
|
|
|
}
|
2022-01-17 12:46:03 +00:00
|
|
|
|
|
|
|
if (natt_external_address(&device, false))
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
data->forward_type = NATT_FORWARD_TYPE_ANY;
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_OPEN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_SELECT_DEVICE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAT_TRAVERSAL_STATUS_OPEN:
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
if (device.ext_addr.sin_family != AF_INET)
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_SELECT_DEVICE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (natt_open_port(&device, &data->request,
|
|
|
|
data->forward_type, false))
|
2021-12-09 04:52:42 +00:00
|
|
|
data->status = NAT_TRAVERSAL_STATUS_OPENING;
|
|
|
|
else
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_SELECT_DEVICE;
|
|
|
|
}
|
|
|
|
break;
|
2021-12-04 20:16:34 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
case NAT_TRAVERSAL_STATUS_OPENING:
|
|
|
|
{
|
|
|
|
if (data->request.success)
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
natt_device_end(&discovery);
|
|
|
|
|
|
|
|
/* Copy the external address into the request. */
|
|
|
|
memcpy(&data->request.addr.sin_addr,
|
|
|
|
&device.ext_addr.sin_addr,
|
|
|
|
sizeof(data->request.addr.sin_addr));
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_OPENED;
|
2022-01-10 14:52:15 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
goto finished;
|
|
|
|
}
|
2022-01-17 12:46:03 +00:00
|
|
|
else if (data->forward_type == NATT_FORWARD_TYPE_ANY)
|
2021-12-09 04:52:42 +00:00
|
|
|
{
|
|
|
|
data->forward_type = NATT_FORWARD_TYPE_NONE;
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_OPEN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_SELECT_DEVICE;
|
|
|
|
}
|
|
|
|
break;
|
2022-01-10 14:52:15 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
case NAT_TRAVERSAL_STATUS_CLOSE:
|
|
|
|
{
|
2022-01-17 12:46:03 +00:00
|
|
|
natt_close_port(&device, &data->request, false);
|
2021-12-09 04:52:42 +00:00
|
|
|
|
|
|
|
data->status = NAT_TRAVERSAL_STATUS_CLOSING;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAT_TRAVERSAL_STATUS_CLOSING:
|
|
|
|
{
|
2022-01-18 12:03:32 +00:00
|
|
|
memset(&data->request, 0, sizeof(data->request));
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
data->status = NAT_TRAVERSAL_STATUS_CLOSED;
|
2022-01-10 14:52:15 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
goto finished;
|
|
|
|
}
|
|
|
|
break;
|
2022-01-10 14:52:15 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
finished:
|
2021-12-04 20:16:34 +00:00
|
|
|
task_set_progress(task, 100);
|
|
|
|
task_set_finished(task, true);
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:00:32 +00:00
|
|
|
static void netplay_nat_traversal_callback(retro_task_t *task,
|
2021-12-09 04:52:42 +00:00
|
|
|
void *task_data, void *user_data, const char *error)
|
2017-02-21 00:08:31 +00:00
|
|
|
{
|
|
|
|
netplay_driver_ctl(RARCH_NETPLAY_CTL_FINISHED_NAT_TRAVERSAL, NULL);
|
|
|
|
}
|
|
|
|
|
2021-12-04 20:16:34 +00:00
|
|
|
static bool nat_task_finder(retro_task_t *task, void *userdata)
|
2017-02-21 00:08:31 +00:00
|
|
|
{
|
2021-12-04 20:16:34 +00:00
|
|
|
if (!task)
|
|
|
|
return false;
|
2017-02-21 00:08:31 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
return task->handler == task_netplay_nat_traversal_handler;
|
2021-12-04 20:16:34 +00:00
|
|
|
}
|
2017-02-21 00:08:31 +00:00
|
|
|
|
2021-12-04 20:16:34 +00:00
|
|
|
static bool nat_task_queued(void *data)
|
|
|
|
{
|
|
|
|
task_finder_data_t find_data = {nat_task_finder, NULL};
|
|
|
|
|
|
|
|
return task_queue_find(&find_data);
|
2017-02-21 00:08:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
bool task_push_netplay_nat_traversal(void *data, uint16_t port)
|
2017-02-21 00:08:31 +00:00
|
|
|
{
|
2022-03-30 13:45:48 +00:00
|
|
|
retro_task_t *task = NULL;
|
|
|
|
struct nat_traversal_data *natt_data = (struct nat_traversal_data*)data;
|
2017-02-21 00:08:31 +00:00
|
|
|
|
2021-12-04 20:16:34 +00:00
|
|
|
/* Do not run more than one NAT task at a time. */
|
|
|
|
task_queue_wait(nat_task_queued, NULL);
|
|
|
|
|
2022-03-30 13:45:48 +00:00
|
|
|
task = task_init();
|
2017-02-21 00:08:31 +00:00
|
|
|
if (!task)
|
|
|
|
return false;
|
|
|
|
|
2022-03-30 13:45:48 +00:00
|
|
|
natt_data->request.addr.sin_family = AF_INET;
|
|
|
|
natt_data->request.addr.sin_port = htons(port);
|
|
|
|
natt_data->request.proto = SOCKET_PROTOCOL_TCP;
|
|
|
|
natt_data->request.device = NULL;
|
|
|
|
natt_data->status = NAT_TRAVERSAL_STATUS_DISCOVERY;
|
2017-02-21 00:08:31 +00:00
|
|
|
|
2022-03-30 13:45:48 +00:00
|
|
|
task->handler = task_netplay_nat_traversal_handler;
|
|
|
|
task->callback = netplay_nat_traversal_callback;
|
|
|
|
task->task_data = data;
|
2017-02-21 00:08:31 +00:00
|
|
|
|
2017-05-14 18:43:39 +00:00
|
|
|
task_queue_push(task);
|
2017-02-21 00:08:31 +00:00
|
|
|
|
|
|
|
return true;
|
2020-10-10 15:55:06 +00:00
|
|
|
}
|
2021-12-04 14:45:29 +00:00
|
|
|
|
2021-12-09 04:52:42 +00:00
|
|
|
bool task_push_netplay_nat_close(void *data)
|
2021-12-04 14:45:29 +00:00
|
|
|
{
|
2022-03-30 13:45:48 +00:00
|
|
|
retro_task_t *task = NULL;
|
|
|
|
struct nat_traversal_data *natt_data = (struct nat_traversal_data*)data;
|
2021-12-04 20:16:34 +00:00
|
|
|
|
|
|
|
/* Do not run more than one NAT task at a time. */
|
|
|
|
task_queue_wait(nat_task_queued, NULL);
|
2021-12-04 14:45:29 +00:00
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
if (natt_data->status != NAT_TRAVERSAL_STATUS_OPENED)
|
|
|
|
return false;
|
|
|
|
if (natt_data->request.addr.sin_family != AF_INET)
|
|
|
|
return false;
|
2021-12-09 04:52:42 +00:00
|
|
|
if (!natt_data->request.addr.sin_port)
|
|
|
|
return false;
|
2022-01-17 12:46:03 +00:00
|
|
|
if (natt_data->request.proto != SOCKET_PROTOCOL_TCP)
|
|
|
|
return false;
|
|
|
|
if (!natt_data->request.device)
|
|
|
|
return false;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2021-12-04 20:16:34 +00:00
|
|
|
task = task_init();
|
2021-12-04 14:45:29 +00:00
|
|
|
if (!task)
|
|
|
|
return false;
|
|
|
|
|
2022-01-17 12:46:03 +00:00
|
|
|
natt_data->status = NAT_TRAVERSAL_STATUS_CLOSE;
|
2021-12-09 04:52:42 +00:00
|
|
|
|
2022-03-30 13:45:48 +00:00
|
|
|
task->handler = task_netplay_nat_traversal_handler;
|
|
|
|
task->task_data = data;
|
2021-12-04 14:45:29 +00:00
|
|
|
|
|
|
|
task_queue_push(task);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-21 04:27:50 +00:00
|
|
|
#else
|
2021-12-09 04:52:42 +00:00
|
|
|
bool task_push_netplay_nat_traversal(void *data, uint16_t port) { return false; }
|
|
|
|
bool task_push_netplay_nat_close(void *data) { return false; }
|
2017-02-21 04:27:50 +00:00
|
|
|
#endif
|