-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters