From f2b3948b398c9796a9f5ec02022a24514077f469 Mon Sep 17 00:00:00 2001 From: Cthulhu-throwaway <96153783+Cthulhu-throwaway@users.noreply.github.com> Date: Sun, 26 Jun 2022 16:38:52 -0300 Subject: [PATCH] (UPnP) Attempt support for remaining platforms (#14107) --- libretro-common/include/net/net_compat.h | 1 + tasks/task_netplay_nat_traversal.c | 27 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/libretro-common/include/net/net_compat.h b/libretro-common/include/net/net_compat.h index 11f2494524..591f4cedbd 100644 --- a/libretro-common/include/net/net_compat.h +++ b/libretro-common/include/net/net_compat.h @@ -133,6 +133,7 @@ #define socklen_t unsigned int #define socket(a,b,c) sceNetSocket("unknown",a,b,c) +#define getsockname sceNetGetsockname #define getsockopt sceNetGetsockopt #define setsockopt sceNetSetsockopt #define bind sceNetBind diff --git a/tasks/task_netplay_nat_traversal.c b/tasks/task_netplay_nat_traversal.c index c8ab7eaac7..bb0a401693 100644 --- a/tasks/task_netplay_nat_traversal.c +++ b/tasks/task_netplay_nat_traversal.c @@ -39,8 +39,6 @@ static bool find_local_address(struct natt_device *device, { bool ret = false; -/* TODO/FIXME: Find a way to get the network's interface on - HAVE_SOCKET_LEGACY platforms */ #if !defined(HAVE_SOCKET_LEGACY) || defined(GEKKO) struct net_ifinfo interfaces = {0}; struct addrinfo **addrs = NULL; @@ -135,6 +133,31 @@ done: free(scores); free(addrs); net_ifinfo_free(&interfaces); +#else + int dummy_fd = socket_create("dummy", + SOCKET_DOMAIN_INET, SOCKET_TYPE_DATAGRAM, SOCKET_PROTOCOL_UDP); + + if (dummy_fd >= 0) + { + struct sockaddr_in addr = {0}; + socklen_t addrlen = sizeof(addr); + + if (!connect(dummy_fd, (struct sockaddr*)&device->addr, + sizeof(device->addr)) && + !getsockname(dummy_fd, (struct sockaddr*)&addr, &addrlen)) + { + /* Make sure this is not "0.0.0.0". */ + if (addr.sin_addr.s_addr) + { + /* Copy the address to our request. */ + memcpy(&request->addr.sin_addr, &addr.sin_addr, + sizeof(request->addr.sin_addr)); + ret = true; + } + } + + socket_close(dummy_fd); + } #endif return ret;