2009-10-01 01:10:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Network Operations
|
|
|
|
* for USB Loader GX
|
|
|
|
*
|
|
|
|
* HTTP operations
|
|
|
|
* Written by dhewg/bushing modified by dimok
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <ogc/machine/processor.h>
|
2011-01-25 21:19:15 +01:00
|
|
|
#include <fcntl.h>
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
#include "prompts/PromptWindows.h"
|
|
|
|
#include "language/gettext.h"
|
2010-09-19 22:25:12 +02:00
|
|
|
#include "settings/CSettings.h"
|
2011-01-25 21:19:15 +01:00
|
|
|
#include "utils/timer.h"
|
2010-12-03 19:38:57 +01:00
|
|
|
#include "networkops.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "http.h"
|
2009-11-18 10:01:14 +01:00
|
|
|
#include "svnrev.h"
|
2010-12-03 19:38:57 +01:00
|
|
|
#include "gecko.h"
|
2009-12-10 21:27:36 +01:00
|
|
|
#include "update.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
#define PORT 4299
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
/*** Incomming filesize ***/
|
|
|
|
u32 infilesize = 0;
|
|
|
|
u32 uncfilesize = 0;
|
|
|
|
|
|
|
|
bool updateavailable = false;
|
|
|
|
s32 connection;
|
|
|
|
static s32 socket;
|
|
|
|
static bool updatechecked = false;
|
|
|
|
static bool networkinitialized = false;
|
|
|
|
static bool checkincomming = false;
|
|
|
|
static bool waitforanswer = false;
|
|
|
|
static char IP[16];
|
|
|
|
char incommingIP[50];
|
|
|
|
char wiiloadVersion[2];
|
|
|
|
|
|
|
|
static lwp_t networkthread = LWP_THREAD_NULL;
|
|
|
|
static bool networkHalt = true;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Initialize_Network
|
|
|
|
***************************************************************************/
|
2017-12-14 15:26:57 +01:00
|
|
|
void Initialize_Network(int retries)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (networkinitialized) return;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
s32 result;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2017-12-14 15:26:57 +01:00
|
|
|
result = if_config(IP, NULL, NULL, true, retries);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
networkinitialized = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
networkinitialized = true;
|
|
|
|
return;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-12-05 20:24:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* DeinitNetwork
|
|
|
|
***************************************************************************/
|
|
|
|
void DeinitNetwork(void)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
net_wc24cleanup();
|
|
|
|
net_deinit();
|
|
|
|
networkinitialized = false;
|
2010-12-05 20:24:32 +01:00
|
|
|
}
|
|
|
|
|
2009-10-01 01:10:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Check if network was initialised
|
|
|
|
***************************************************************************/
|
2010-09-24 02:48:03 +02:00
|
|
|
bool IsNetworkInit(void)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
return networkinitialized;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Get network IP
|
|
|
|
***************************************************************************/
|
2010-09-24 02:48:03 +02:00
|
|
|
char * GetNetworkIP(void)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
return IP;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Get incomming IP
|
|
|
|
***************************************************************************/
|
2010-09-24 02:48:03 +02:00
|
|
|
char * GetIncommingIP(void)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
return incommingIP;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-12-03 19:38:57 +01:00
|
|
|
s32 network_request(s32 connect, const char * request, char * filename)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-06-25 21:50:57 +02:00
|
|
|
if(connect == NET_DEFAULT_SOCK)
|
|
|
|
connect = connection;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char buf[1024];
|
|
|
|
char *ptr = NULL;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u32 cnt, size;
|
|
|
|
s32 ret;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* Send request */
|
|
|
|
ret = net_send(connect, request, strlen(request), 0);
|
|
|
|
if (ret < 0) return ret;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* Clear buffer */
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* Read HTTP header */
|
|
|
|
for (cnt = 0; !strstr(buf, "\r\n\r\n"); cnt++)
|
|
|
|
if (net_recv(connect, buf + cnt, 1, 0) <= 0) return -1;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* HTTP request OK? */
|
|
|
|
if (!strstr(buf, "HTTP/1.1 200 OK")) return -1;
|
2009-11-02 23:15:28 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
/* Get filename */
|
|
|
|
ptr = strstr(buf, "filename=\"");
|
2009-11-02 23:15:28 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
ptr += sizeof("filename=\"") - 1;
|
2009-11-02 23:15:28 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
for (cnt = 0; ptr[cnt] != '\r' && ptr[cnt] != '\n' && ptr[cnt] != '"'; cnt++)
|
|
|
|
{
|
|
|
|
filename[cnt] = ptr[cnt];
|
|
|
|
filename[cnt + 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-24 18:32:09 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* Retrieve content size */
|
|
|
|
ptr = strstr(buf, "Content-Length:");
|
|
|
|
if (!ptr) return NET_SIZE_UNKNOWN;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2017-12-14 15:26:57 +01:00
|
|
|
sscanf(ptr, "Content-Length: %lu", &size);
|
2011-07-26 00:28:22 +02:00
|
|
|
return size;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-12-03 19:38:57 +01:00
|
|
|
s32 network_read(s32 connect, u8 *buf, u32 len)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-06-25 21:50:57 +02:00
|
|
|
if(connect == NET_DEFAULT_SOCK)
|
|
|
|
connect = connection;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u32 read = 0;
|
|
|
|
s32 ret = -1;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* Data to be read */
|
|
|
|
while (read < len)
|
|
|
|
{
|
|
|
|
/* Read network data */
|
|
|
|
ret = net_read(connect, buf + read, len - read);
|
|
|
|
if (ret < 0) return ret;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* Read finished */
|
|
|
|
if (!ret) break;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
/* Increment read variable */
|
|
|
|
read += ret;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return read;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Download request
|
|
|
|
***************************************************************************/
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 download_request(const char * url, char * filename)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
//Check if the url starts with "http://", if not it is not considered a valid url
|
|
|
|
if (strncmp(url, "http://", strlen("http://")) != 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Locate the path part of the url by searching for '/' past "http://"
|
|
|
|
char *path = strchr(url + strlen("http://"), '/');
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//At the very least the url has to end with '/', ending with just a domain is invalid
|
|
|
|
if (path == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Extract the domain part out of the url
|
|
|
|
int domainlength = path - url - strlen("http://");
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (domainlength == 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char domain[domainlength + 1];
|
|
|
|
strlcpy(domain, url + strlen("http://"), domainlength + 1);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
connection = GetConnection(domain);
|
|
|
|
if (connection < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Form a nice request header to send to the webserver
|
|
|
|
char header[strlen(path) + strlen(domain) + strlen(url) + 100];
|
|
|
|
sprintf(header, "GET %s HTTP/1.1\r\nHost: %s\r\nReferer: %s\r\nUser-Agent: USBLoaderGX\r\nConnection: close\r\n\r\n", path, domain, url);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
s32 filesize = network_request(connection, header, filename);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return filesize;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-12-03 19:38:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* HTML HEAD request
|
|
|
|
***************************************************************************/
|
|
|
|
char * HEAD_Request(const char * url)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(strncmp(url, "http://", strlen("http://")) != 0)
|
|
|
|
{
|
|
|
|
gprintf("Not a valid URL");
|
2010-12-03 19:38:57 +01:00
|
|
|
return NULL;
|
2011-07-26 00:28:22 +02:00
|
|
|
}
|
2010-12-03 19:38:57 +01:00
|
|
|
char *path = strchr(url + strlen("http://"), '/');
|
|
|
|
|
|
|
|
if(!path)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
gprintf("Not a valid URL path");
|
|
|
|
return NULL;
|
2010-12-03 19:38:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int domainlength = path - url - strlen("http://");
|
|
|
|
|
|
|
|
if(domainlength == 0)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
gprintf("Not a valid domain");
|
2010-12-03 19:38:57 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char domain[domainlength + 1];
|
|
|
|
strncpy(domain, url + strlen("http://"), domainlength);
|
|
|
|
domain[domainlength] = '\0';
|
|
|
|
|
|
|
|
connection = GetConnection(domain);
|
2011-07-26 00:28:22 +02:00
|
|
|
if(connection < 0)
|
|
|
|
{
|
|
|
|
gprintf("Could not connect to the server.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char header[strlen(path)+strlen(domain)*2+150];
|
|
|
|
sprintf(header, "HEAD %s HTTP/1.1\r\nHost: %s\r\nReferer: %s\r\nUser-Agent: USB Loader GX\r\nConnection: close\r\n\r\n", path, domain, domain);
|
|
|
|
|
|
|
|
/* Send request */
|
|
|
|
s32 ret = net_send(connection, header, strlen(header), 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
CloseConnection();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 cnt = 0;
|
|
|
|
char * buf = (char *) malloc(1024);
|
|
|
|
memset(buf, 0, 1024);
|
|
|
|
|
|
|
|
for (cnt = 0; !strstr(buf, "\r\n\r\n") && cnt < 1024; cnt++)
|
|
|
|
{
|
|
|
|
if(net_recv(connection, buf + cnt, 1, 0) <= 0)
|
|
|
|
{
|
|
|
|
CloseConnection();
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseConnection();
|
|
|
|
|
|
|
|
return buf;
|
2010-12-03 19:38:57 +01:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void CloseConnection()
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
net_close(connection);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (waitforanswer)
|
|
|
|
{
|
|
|
|
net_close(socket);
|
|
|
|
waitforanswer = false;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2011-01-25 21:19:15 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Test if connection to the address is available (PING)
|
|
|
|
***************************************************************************/
|
|
|
|
bool CheckConnection(const char *url, float timeout)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
//Check if the url starts with "http://", if not it is not considered a valid url
|
|
|
|
if (strncmp(url, "http://", strlen("http://")) != 0)
|
|
|
|
return false;
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Locate the path part of the url by searching for '/' past "http://"
|
|
|
|
char *path = strchr(url + strlen("http://"), '/');
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//At the very least the url has to end with '/', ending with just a domain is invalid
|
|
|
|
if (path == NULL)
|
|
|
|
return false;
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Extract the domain part out of the url
|
|
|
|
int domainlength = path - url - strlen("http://");
|
|
|
|
if (domainlength == 0)
|
|
|
|
return false;
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char domain[domainlength + 1];
|
|
|
|
strlcpy(domain, url + strlen("http://"), domainlength + 1);
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Parsing of the URL is done, start making an actual connection
|
|
|
|
u32 ipaddress = getipbynamecached(domain);
|
|
|
|
if (ipaddress == 0)
|
|
|
|
return false;
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Initialize socket
|
|
|
|
s32 connection = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
|
|
|
if (connection < 0) return connection;
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
s32 flags = net_fcntl(connection, F_GETFL, 0);
|
|
|
|
if (flags >= 0) flags = net_fcntl(connection, F_SETFL, flags | 4);
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
struct sockaddr_in connect_addr;
|
|
|
|
memset(&connect_addr, 0, sizeof(connect_addr));
|
|
|
|
connect_addr.sin_family = AF_INET;
|
|
|
|
connect_addr.sin_port = 80;
|
|
|
|
connect_addr.sin_addr.s_addr = getipbynamecached(domain);
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Timer netTime;
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
int res = -1;
|
|
|
|
while(res < 0 && res != -127 && netTime.elapsed() < timeout)
|
|
|
|
{
|
|
|
|
res = net_connect(connection, (struct sockaddr*) &connect_addr, sizeof(connect_addr));
|
|
|
|
usleep(1000);
|
|
|
|
}
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
net_close(connection);
|
2011-01-25 21:19:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return !(res < 0 && res != -127);
|
2011-01-25 21:19:15 +01:00
|
|
|
}
|
|
|
|
|
2011-06-25 21:50:57 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Download request
|
|
|
|
***************************************************************************/
|
|
|
|
s32 DownloadWithResponse(const char * url, u8 **outbuffer, u32 *outsize)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
//Check if the url starts with "http://", if not it is not considered a valid url
|
|
|
|
if (strncmp(url, "http://", strlen("http://")) != 0)
|
|
|
|
return -1;
|
2011-06-25 21:50:57 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Locate the path part of the url by searching for '/' past "http://"
|
|
|
|
char *path = strchr(url + strlen("http://"), '/');
|
2011-06-25 21:50:57 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//At the very least the url has to end with '/', ending with just a domain is invalid
|
|
|
|
if (path == NULL)
|
|
|
|
return -1;
|
2011-06-25 21:50:57 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Extract the domain part out of the url
|
|
|
|
int domainlength = path - url - strlen("http://");
|
2011-06-25 21:50:57 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (domainlength == 0)
|
|
|
|
return -1;
|
2011-06-25 21:50:57 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char domain[domainlength + 1];
|
|
|
|
strlcpy(domain, url + strlen("http://"), domainlength + 1);
|
2011-06-25 21:50:57 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
int connect = GetConnection(domain);
|
|
|
|
if (connect < 0)
|
|
|
|
return -1;
|
2011-06-25 21:50:57 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Form a nice request header to send to the webserver
|
|
|
|
char header[strlen(path) + strlen(domain) + strlen(url) + 100];
|
|
|
|
sprintf(header, "GET %s HTTP/1.1\r\nHost: %s\r\nReferer: %s\r\nUser-Agent: USBLoaderGX\r\nConnection: close\r\n\r\n", path, domain, url);
|
2011-06-25 21:50:57 +02:00
|
|
|
|
|
|
|
int ret = net_send(connect, header, strlen(header), 0);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
net_close(connect);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int blocksize = 4096;
|
|
|
|
|
|
|
|
u8 *buffer = (u8 *) malloc(blocksize);
|
|
|
|
if(!buffer)
|
|
|
|
{
|
|
|
|
net_close(connect);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
int ret = network_read(connect, buffer+done, blocksize);
|
|
|
|
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
net_close(connect);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
done += ret;
|
|
|
|
u8 *tmp = (u8 *) realloc(buffer, done+blocksize);
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
net_close(connect);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_close(connect);
|
|
|
|
|
|
|
|
u8 *tmp = (u8 *) realloc(buffer, done+1);
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = tmp;
|
|
|
|
buffer[done] = 0;
|
|
|
|
|
|
|
|
*outbuffer = buffer;
|
|
|
|
*outsize = done;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return done;
|
2011-06-25 21:50:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-01 01:10:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* NetworkWait
|
|
|
|
***************************************************************************/
|
2010-09-19 01:16:05 +02:00
|
|
|
int NetworkWait()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!checkincomming) return -3;
|
|
|
|
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in client_address;
|
|
|
|
socklen_t addrlen = sizeof(client_address);
|
|
|
|
|
|
|
|
//Open socket
|
|
|
|
socket = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
|
|
|
|
|
|
|
if (socket == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons( PORT );
|
|
|
|
sin.sin_addr.s_addr = htonl( INADDR_ANY );
|
|
|
|
|
|
|
|
if (net_bind(socket, (struct sockaddr*) &sin, sizeof(sin)) < 0)
|
|
|
|
{
|
|
|
|
net_close(socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (net_listen(socket, 3) < 0)
|
|
|
|
{
|
|
|
|
net_close(socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection = net_accept(socket, (struct sockaddr*) &client_address, &addrlen);
|
|
|
|
|
|
|
|
sprintf(incommingIP, "%s", inet_ntoa(client_address.sin_addr));
|
|
|
|
|
|
|
|
if (connection < 0)
|
|
|
|
{
|
|
|
|
net_close(connection);
|
|
|
|
net_close(socket);
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
unsigned char haxx[9];
|
|
|
|
//skip haxx
|
|
|
|
net_read(connection, &haxx, 8);
|
|
|
|
wiiloadVersion[0] = haxx[4];
|
|
|
|
wiiloadVersion[1] = haxx[5];
|
|
|
|
|
|
|
|
net_read(connection, &infilesize, 4);
|
|
|
|
|
|
|
|
if (haxx[4] > 0 || haxx[5] > 4)
|
|
|
|
{
|
|
|
|
net_read(connection, &uncfilesize, 4); // Compressed protocol, read another 4 bytes
|
|
|
|
}
|
|
|
|
waitforanswer = true;
|
|
|
|
checkincomming = false;
|
|
|
|
networkHalt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* HaltNetwork
|
|
|
|
***************************************************************************/
|
2010-09-19 01:16:05 +02:00
|
|
|
void HaltNetworkThread()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
networkHalt = true;
|
|
|
|
checkincomming = false;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (waitforanswer) CloseConnection();
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
// wait for thread to finish
|
|
|
|
while (!LWP_ThreadIsSuspended(networkthread))
|
|
|
|
usleep(100);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* ResumeNetworkThread
|
|
|
|
***************************************************************************/
|
2010-09-19 01:16:05 +02:00
|
|
|
void ResumeNetworkThread()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
networkHalt = false;
|
|
|
|
LWP_ResumeThread(networkthread);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Resume NetworkWait
|
|
|
|
***************************************************************************/
|
2010-09-19 01:16:05 +02:00
|
|
|
void ResumeNetworkWait()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
networkHalt = true;
|
|
|
|
checkincomming = true;
|
|
|
|
waitforanswer = true;
|
|
|
|
infilesize = 0;
|
|
|
|
connection = -1;
|
|
|
|
LWP_ResumeThread(networkthread);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************
|
|
|
|
* Networkthread for background network initialize and update check with idle prio
|
|
|
|
*********************************************************************************/
|
2010-09-24 02:48:03 +02:00
|
|
|
static void * networkinitcallback(void *arg)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
while (1)
|
|
|
|
{
|
2012-05-06 12:59:58 +02:00
|
|
|
if (!checkincomming && networkHalt)
|
|
|
|
LWP_SuspendThread(networkthread);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Initialize_Network();
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (networkinitialized == true && updatechecked == false)
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (CheckUpdate() > 0) updateavailable = true;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//suspend thread
|
|
|
|
updatechecked = true;
|
|
|
|
networkHalt = true;
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (checkincomming) NetworkWait();
|
2012-05-06 12:59:58 +02:00
|
|
|
|
|
|
|
usleep(100000);
|
2011-07-26 00:28:22 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* InitNetworkThread with priority 0 (idle)
|
|
|
|
***************************************************************************/
|
2010-09-19 01:16:05 +02:00
|
|
|
void InitNetworkThread()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
LWP_CreateThread(&networkthread, networkinitcallback, NULL, NULL, 16384, 0);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* ShutdownThread
|
|
|
|
***************************************************************************/
|
2010-09-19 01:16:05 +02:00
|
|
|
void ShutdownNetworkThread()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
LWP_JoinThread(networkthread, NULL);
|
|
|
|
networkthread = LWP_THREAD_NULL;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|