Add 3dslink stdio redirection

This commit is contained in:
oreo639 2022-01-14 15:52:45 -08:00 committed by Dave Murphy
parent c83c12357e
commit 2564d708e6
4 changed files with 95 additions and 0 deletions

View File

@ -99,6 +99,7 @@ extern "C" {
#include <3ds/mii.h>
#include <3ds/gdbhio_dev.h>
#include <3ds/3dslink.h>
#ifdef __cplusplus
}

View File

@ -0,0 +1,34 @@
/**
* @file 3dslink.h
* @brief Netloader (3dslink) utilities
*/
#pragma once
#include <stdbool.h>
struct in_addr;
/// Address of the host connected through 3dslink
extern struct in_addr __3dslink_host;
#define LINK3DS_COMM_PORT 17491 ///< 3dslink TCP server port
/**
* @brief Connects to the 3dslink host, setting up an output stream.
* @param[in] redirStdout Whether to redirect stdout to nxlink output.
* @param[in] redirStderr Whether to redirect stderr to nxlink output.
* @return Socket fd on success, negative number on failure.
* @note The socket should be closed with close() during application cleanup.
*/
int link3dsConnectToHost(bool redirStdout, bool redirStderr);
/// Same as \ref link3dsConnectToHost but redirecting both stdout/stderr.
static inline int link3dsStdio(void) {
return link3dsConnectToHost(true, true);
}
/// Same as \ref link3dsConnectToHost but redirecting only stderr.
static inline int link3dsStdioForDebug(void) {
return link3dsConnectToHost(false, true);
}

48
libctru/source/3dslink.c Normal file
View File

@ -0,0 +1,48 @@
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <3ds/3dslink.h>
struct in_addr __3dslink_host = {0};
static int sock = -1;
int link3dsConnectToHost(bool redirStdout, bool redirStderr)
{
int ret = -1;
struct sockaddr_in srv_addr;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (!sock) {
return ret;
}
bzero(&srv_addr, sizeof srv_addr);
srv_addr.sin_family = AF_INET;
srv_addr.sin_addr = __3dslink_host;
srv_addr.sin_port = htons(LINK3DS_COMM_PORT);
ret = connect(sock, (struct sockaddr *) &srv_addr, sizeof(srv_addr));
if (ret != 0) {
close(sock);
return -1;
}
if (redirStdout) {
// redirect stdout
fflush(stdout);
dup2(sock, STDOUT_FILENO);
}
if (redirStderr) {
// redirect stderr
fflush(stderr);
dup2(sock, STDERR_FILENO);
}
return sock;
}

View File

@ -1,7 +1,10 @@
#include <3ds/types.h>
#include <3ds/env.h>
#include <3ds/3dslink.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
// System globals we define here
int __system_argc;
@ -47,5 +50,14 @@ void __attribute__((weak)) __system_initArgv(void)
for (; *temp; temp ++);
temp ++;
}
// Grab 3dslink host address if avaliable
if ( __system_argc > 1 &&
strlen(__system_argv[__system_argc - 1]) == 17 &&
strncmp(&__system_argv[__system_argc - 1][8], "_3DSLINK_", 8) == 0 )
{
__system_argc--;
__3dslink_host.s_addr = strtoul(__system_argv[__system_argc], NULL, 16);
}
__system_argv[__system_argc] = NULL;
}