Skip to content

Commit

Permalink
Add 3dslink stdio redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
oreo639 committed Jan 16, 2022
1 parent 5f13628 commit 97273ac
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions libctru/include/3ds.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ extern "C" {
#include <3ds/mii.h>

#include <3ds/gdbhio_dev.h>
#include <3ds/3dslink.h>

#ifdef __cplusplus
}
Expand Down
34 changes: 34 additions & 0 deletions libctru/include/3ds/3dslink.h
Original file line number Diff line number Diff line change
@@ -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 changes: 48 additions & 0 deletions libctru/source/3dslink.c
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions libctru/source/system/initArgv.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

0 comments on commit 97273ac

Please sign in to comment.