From 3eee81953a9f4d12dfc113b90fc84be010adb623 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 3 Jul 2016 12:38:55 -0700 Subject: [PATCH] http: Report local address to server. This allows matching inside a network. --- UI/RemoteISOScreen.cpp | 25 ++++++++++++++++++++++++- ext/native/file/fd_util.cpp | 20 +++++++++++++++++++- ext/native/file/fd_util.h | 2 ++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/UI/RemoteISOScreen.cpp b/UI/RemoteISOScreen.cpp index 242649f816..8a1e90cc5d 100644 --- a/UI/RemoteISOScreen.cpp +++ b/UI/RemoteISOScreen.cpp @@ -16,6 +16,8 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "i18n/i18n.h" +#include "file/fd_util.h" +#include "net/http_client.h" #include "net/http_server.h" #include "net/resolve.h" #include "net/sinks.h" @@ -51,6 +53,26 @@ static ServerStatus RetrieveStatus() { return serverStatus; } +// This reports the local IP address to report.ppsspp.org, which can then +// relay that address to a mobile device searching for the server. +static void RegisterServer(int port) { + bool result = false; + net::AutoInit netInit; + http::Client http; + Buffer theVoid; + + if (http.Resolve("report.ppsspp.org", 80)) { + http.Connect(); + + char resource[1024] = {}; + std::string ip = fd_util::GetLocalIP(http.sock()); + snprintf(resource, sizeof(resource) - 1, "/match/update?local=%s&port=%d", ip.c_str(), port); + + http.GET(resource, &theVoid); + http.Disconnect(); + } +} + static void ExecuteServer() { setCurrentThreadName("HTTPServer"); @@ -131,9 +153,10 @@ static void ExecuteServer() { } http->Listen(0); - // TODO: Report local IP and port. UpdateStatus(ServerStatus::RUNNING); + RegisterServer(http->Port()); + while (RetrieveStatus() == ServerStatus::RUNNING) { http->RunSlice(5.0); } diff --git a/ext/native/file/fd_util.cpp b/ext/native/file/fd_util.cpp index ecb0027e98..8663d44760 100644 --- a/ext/native/file/fd_util.cpp +++ b/ext/native/file/fd_util.cpp @@ -4,11 +4,16 @@ #include #include #ifndef _WIN32 -#include +#include +#include +#include +#include #include +#include #else #include #include +#include #endif #include @@ -127,4 +132,17 @@ void SetNonBlocking(int sock, bool non_blocking) { #endif } +std::string GetLocalIP(int sock) { + struct sockaddr_in server_addr; + memset(&server_addr, 0, sizeof(server_addr)); + socklen_t len = sizeof(server_addr); + if (getsockname(sock, (struct sockaddr *)&server_addr, &len) == 0) { + char *result = inet_ntoa(*(in_addr *)&server_addr.sin_addr); + if (result) { + return result; + } + } + return ""; +} + } // fd_util diff --git a/ext/native/file/fd_util.h b/ext/native/file/fd_util.h index 6d64e018ab..922a50eb70 100644 --- a/ext/native/file/fd_util.h +++ b/ext/native/file/fd_util.h @@ -22,6 +22,8 @@ bool WaitUntilReady(int fd, double timeout, bool for_write = false); void SetNonBlocking(int fd, bool non_blocking); +std::string GetLocalIP(int sock); + } // fd_util #endif // _FD_UTIL