From 4fe95cc1e7412e309d026e53cd44239bd3ef031d Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 16 Sep 2017 16:11:15 -0400 Subject: Wii can now send a POST request to a website The extractor now uses libfat to read a config file off the root of the SD card, a model for which is included in the repository. The extractor is capable of negotiating an HTTPS connection, but it requires a download of the target site's root CA certificate to be on the SD card and pointed at by the config file. TLS/SSL functionality is provided by wolfSSL. I had to make a couple of minor changes to wolfSSL for it to work properly, and those changes are located in the devkitpro branch of my fork, hatkirby/wolfssl. The Wii program now arranges some of the information that the GBA sends it into a JSON object, which is then sent off (along with some authentication information from the config file) to the endpoint defined in the config file. The code used to maintain the network connection is from the WiiTweet project, which was licensed under the GPL. Some of the code used to send the HTTP request also comes from said project. --- source/netinf.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 source/netinf.c (limited to 'source/netinf.c') diff --git a/source/netinf.c b/source/netinf.c new file mode 100644 index 0000000..4e4ec6f --- /dev/null +++ b/source/netinf.c @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2008 Tantric + * Copyright (C) 2012 Pedro Aguiar + * Copyright (C) 2017 hatkirby + * + * Originally part of the WiiTweet project, which was distributed under the + * GPL. + */ +#include "netinf.h" +#include +#include +#include +#include +#include +#include +#include + +static bool networkInit = false; +static lwp_t networkThread = LWP_THREAD_NULL; +static u8 netStack[32768] ATTRIBUTE_ALIGN(32); +static char wiiIP[16] = {0}; +static bool netHalt = false; + +static void* interface(void* arg) +{ + static bool prevInit = false; + + while (!netHalt) + { + int retry = 5; + int result = -1; + + while ((retry > 0) && (!netHalt)) + { + if (prevInit) + { + net_deinit(); + + for (int i=0; (i<400) && !netHalt; i++) + { + if (net_get_status() != -EBUSY) + { + usleep(2000); + net_wc24cleanup(); + prevInit = false; + usleep(20000); + + break; + } + + usleep(20000); + } + } + + usleep(2000); + + if (net_init_async(NULL, NULL)) + { + sleep(1); + + retry--; + + continue; + } + + result = net_get_status(); + for (int wait = 400; (wait > 0) && (result == -EBUSY) && !netHalt; wait++) + { + usleep(20000); + + result = net_get_status(); + } + + if (result == 0) + { + break; + } + + retry--; + + usleep(2000); + } + + if (result == 0) + { + struct in_addr hostip; + hostip.s_addr = net_gethostip(); + + if (hostip.s_addr) + { + strcpy(wiiIP, inet_ntoa(hostip)); + networkInit = true; + prevInit = true; + } + } + + if (!netHalt) + { + LWP_SuspendThread(networkThread); + } + } + + return NULL; +} + +static void startNetworkThread() +{ + netHalt = false; + + if (networkThread == LWP_THREAD_NULL) + { + LWP_CreateThread(&networkThread, interface, NULL, netStack, 8192, 40); + } else { + LWP_ResumeThread(networkThread); + } +} + +static void stopNetworkThread() +{ + if ((networkThread == LWP_THREAD_NULL) + || (!LWP_ThreadIsSuspended(networkThread))) + { + return; + } + + netHalt = true; + + LWP_ResumeThread(networkThread); + LWP_JoinThread(networkThread, NULL); + + networkThread = LWP_THREAD_NULL; +} + +bool initializeNetwork() +{ + stopNetworkThread(); + + if (networkInit && (net_gethostip() > 0)) + { + return true; + } + + networkInit = false; + + printf("Initializing network...\n"); + + u64 start = gettime(); + startNetworkThread(); + + while (!LWP_ThreadIsSuspended(networkThread)) + { + usleep(50 * 1000); + + if (diff_sec(start, gettime()) > 10) + { + break; + } + } + + return networkInit; +} -- cgit 1.4.1