From 5e6e8bdb53dc01601639962a7b63ab859085d09a Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 30 Jan 2022 21:05:09 +0100 Subject: [PATCH] Formatting and cleanup --- src/BackgroundThread.cpp | 15 +++++++-------- src/BackgroundThread.hpp | 11 ++--------- src/ftp.c | 9 ++++----- src/main.cpp | 18 +++++++++--------- src/net.c | 19 +++++++++++++------ src/net.h | 5 ++--- src/utils/BackgroundThreadWrapper.hpp | 5 ----- 7 files changed, 37 insertions(+), 45 deletions(-) diff --git a/src/BackgroundThread.cpp b/src/BackgroundThread.cpp index 12ec5f2..f399e22 100644 --- a/src/BackgroundThread.cpp +++ b/src/BackgroundThread.cpp @@ -11,35 +11,34 @@ BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread: this->serverSocket = create_server(PORT); DCFlushRange(&(this->serverSocket), 4); mutex.unlock(); + DEBUG_FUNCTION_LINE("Resume Thread"); CThread::resumeThread(); } BackgroundThread::~BackgroundThread() { DEBUG_FUNCTION_LINE("Shutting down FTP Server"); - if (this->serverSocket != -1) { - mutex.lock(); + mutex.lock(); + if (this->serverSocket >= 0) { cleanup_ftp(); network_close(this->serverSocket); - mutex.unlock(); this->serverSocket = -1; } + mutex.unlock(); } BOOL BackgroundThread::whileLoop() { - if (this->serverSocket != -1) { - mutex.lock(); + mutex.lock(); + if (this->serverSocket >= 0) { network_down = process_ftp_events(this->serverSocket); - mutex.unlock(); if (network_down) { DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket); - mutex.lock(); cleanup_ftp(); network_close(this->serverSocket); this->serverSocket = -1; DCFlushRange(&(this->serverSocket), 4); - mutex.unlock(); } } + mutex.unlock(); OSSleepTicks(OSMillisecondsToTicks(16)); return true; } diff --git a/src/BackgroundThread.hpp b/src/BackgroundThread.hpp index 3f4c541..a631d22 100644 --- a/src/BackgroundThread.hpp +++ b/src/BackgroundThread.hpp @@ -9,25 +9,18 @@ class BackgroundThread : BackgroundThreadWrapper { public: static BackgroundThread *getInstance() { - DCFlushRange(&instance, sizeof(BackgroundThread)); - ICInvalidateRange(&instance, sizeof(BackgroundThread)); if (instance == nullptr) { instance = new BackgroundThread(); - DCFlushRange(&instance, sizeof(BackgroundThread)); - ICInvalidateRange(&instance, sizeof(BackgroundThread)); + DCFlushRange(&instance, 4); } return instance; } static void destroyInstance() { - DCFlushRange(&instance, sizeof(BackgroundThread)); - ICInvalidateRange(&instance, sizeof(BackgroundThread)); - OSSleepTicks(OSSecondsToTicks(1)); if (instance != nullptr) { delete instance; instance = nullptr; - DCFlushRange(&instance, sizeof(BackgroundThread)); - ICInvalidateRange(&instance, sizeof(BackgroundThread)); + DCFlushRange(&instance, 4); } } diff --git a/src/ftp.c b/src/ftp.c index b98df05..b8b3bcd 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -56,8 +56,7 @@ static uint8_t num_clients = 0; static uint16_t passive_port = 1024; static char *password = NULL; -void console_printf(const char *format, ...) { -} +#define console_printf(FMT, ARGS...) DEBUG_FUNCTION_LINE_WRITE(FMT, ## ARGS); typedef int32_t (*data_connection_callback)(int32_t data_socket, void *arg); @@ -330,7 +329,7 @@ static int32_t ftp_PASV(client_t *client, char *rest UNUSED) { char reply[49]; uint16_t port = bindAddress.sin_port; uint32_t ip = network_gethostip(); - struct in_addr addr; + struct in_addr addr = {}; addr.s_addr = ip; console_printf("Listening for data connections at %s:%u...\n", inet_ntoa(addr), port); sprintf(reply, "Entering Passive Mode (%u,%u,%u,%u,%u,%u).", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, (port >> 8) & 0xff, port & 0xff); @@ -748,7 +747,7 @@ void cleanup_ftp() { static bool process_accept_events(int32_t server) { int32_t peer; struct sockaddr_in client_address; - int32_t addrlen = sizeof(client_address); + socklen_t addrlen = sizeof(client_address); while ((peer = network_accept(server, (struct sockaddr *) &client_address, &addrlen)) != -EAGAIN) { if (peer < 0) { console_printf("Error accepting connection: [%i] %s\n", -peer, strerror(-peer)); @@ -807,7 +806,7 @@ static void process_data_events(client_t *client) { if (!client->data_connection_connected) { if (client->passive_socket >= 0) { struct sockaddr_in data_peer_address; - int32_t addrlen = sizeof(data_peer_address); + socklen_t addrlen = sizeof(data_peer_address); result = network_accept(client->passive_socket, (struct sockaddr *) &data_peer_address, &addrlen); if (result >= 0) { client->data_socket = result; diff --git a/src/main.cpp b/src/main.cpp index 9044e63..2ec83a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,9 +11,6 @@ #include "virtualpath.h" #include "BackgroundThread.hpp" -#define MAX_CONSOLE_LINES_TV 27 -#define MAX_CONSOLE_LINES_DRC 18 - WUPS_PLUGIN_NAME("FTPiiU"); WUPS_PLUGIN_DESCRIPTION("FTP Server"); WUPS_PLUGIN_VERSION("0.1"); @@ -39,11 +36,15 @@ ON_APPLICATION_START() { //! Initialize FS * //!******************************************************************* + VirtualMountDevice("fs:/"); + AddVirtualFSPath("vol", nullptr, nullptr); + AddVirtualFSVOLPath("external01", nullptr, nullptr); + AddVirtualFSVOLPath("content", nullptr, nullptr); + DEBUG_FUNCTION_LINE("IOSUHAX_Open"); int res = IOSUHAX_Open(nullptr); if (res < 0) { DEBUG_FUNCTION_LINE("IOSUHAX_open failed"); - VirtualMountDevice("fs:/"); } else { iosuhaxMount = 1; //fatInitDefault(); @@ -65,7 +66,6 @@ ON_APPLICATION_START() { mount_fs("storage_mlc", fsaFd, nullptr, "/vol/storage_mlc01"); mount_fs("storage_usb", fsaFd, nullptr, "/vol/storage_usb01"); - VirtualMountDevice("fs:/"); VirtualMountDevice("slccmpt01:/"); VirtualMountDevice("storage_odd_tickets:/"); VirtualMountDevice("storage_odd_updates:/"); @@ -75,10 +75,6 @@ ON_APPLICATION_START() { VirtualMountDevice("storage_mlc:/"); VirtualMountDevice("storage_usb:/"); VirtualMountDevice("usb:/"); - - AddVirtualFSPath("vol", nullptr, nullptr); - AddVirtualFSVOLPath("external01", nullptr, nullptr); - AddVirtualFSVOLPath("content", nullptr, nullptr); } thread = BackgroundThread::getInstance(); @@ -93,6 +89,8 @@ ON_APPLICATION_REQUESTS_EXIT() { DEBUG_FUNCTION_LINE("Ending ftp server"); stopThread(); + DEBUG_FUNCTION_LINE("Ended ftp Server."); + if (iosuhaxMount) { IOSUHAX_sdio_disc_interface.shutdown(); IOSUHAX_usb_disc_interface.shutdown(); @@ -109,7 +107,9 @@ ON_APPLICATION_REQUESTS_EXIT() { IOSUHAX_Close(); } + DEBUG_FUNCTION_LINE("Unmount virtual paths"); UnmountVirtualPaths(); + deinitLogging(); } diff --git a/src/net.c b/src/net.c index 13693f0..87beb7e 100644 --- a/src/net.c +++ b/src/net.c @@ -67,7 +67,7 @@ void initialise_network() { } #endif -int32_t network_socket(uint32_t domain, uint32_t type, uint32_t protocol) { +int32_t network_socket(int32_t domain, int32_t type, int32_t protocol) { int sock = socket(domain, type, protocol); if (sock < 0) { int err = -wiiu_geterrno(); @@ -94,7 +94,7 @@ int32_t network_listen(int32_t s, uint32_t backlog) { return res; } -int32_t network_accept(int32_t s, struct sockaddr *addr, int32_t *addrlen) { +int32_t network_accept(int32_t s, struct sockaddr *addr, socklen_t *addrlen) { int res = accept(s, addr, addrlen); if (res < 0) { int err = -wiiu_geterrno(); @@ -144,10 +144,17 @@ int32_t network_write(int32_t s, const void *mem, int32_t len) { } int32_t network_close(int32_t s) { - if (s < 0) + if (s < 0) { return -1; + } - return close(s); + int res = close(s); + + if (res < 0) { + int err = -wiiu_geterrno(); + return (err < 0) ? err : res; + } + return res; } int32_t set_blocking(int32_t s, bool blocking) { @@ -163,9 +170,9 @@ int32_t network_close_blocking(int32_t s) { int32_t create_server(uint16_t port) { int32_t server = network_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); - if (server < 0) + if (server < 0) { return -1; - + } set_blocking(server, false); uint32_t enable = 1; diff --git a/src/net.h b/src/net.h index 20a76c5..3d4c84d 100644 --- a/src/net.h +++ b/src/net.h @@ -32,7 +32,6 @@ extern "C"{ #include #include #include -#include #include #include #include @@ -43,13 +42,13 @@ extern "C"{ void initialise_network(); #endif -int32_t network_socket(uint32_t domain, uint32_t type, uint32_t protocol); +int32_t network_socket(int32_t domain, int32_t type, int32_t protocol); int32_t network_bind(int32_t s, struct sockaddr *name, int32_t namelen); int32_t network_listen(int32_t s, uint32_t backlog); -int32_t network_accept(int32_t s, struct sockaddr *addr, int32_t *addrlen); +int32_t network_accept(int32_t s, struct sockaddr *addr, socklen_t *addrlen); int32_t network_connect(int32_t s, struct sockaddr *, int32_t); diff --git a/src/utils/BackgroundThreadWrapper.hpp b/src/utils/BackgroundThreadWrapper.hpp index 0088465..dec7f5d 100644 --- a/src/utils/BackgroundThreadWrapper.hpp +++ b/src/utils/BackgroundThreadWrapper.hpp @@ -23,12 +23,7 @@ protected: private: void executeThread() override; - /** - Called when a connection has be accepted. - **/ virtual BOOL whileLoop() = 0; volatile int32_t exitThread = 0; - - };