diff --git a/nx/include/switch/runtime/nxlink.h b/nx/include/switch/runtime/nxlink.h index 4c5da29f..363ec256 100644 --- a/nx/include/switch/runtime/nxlink.h +++ b/nx/include/switch/runtime/nxlink.h @@ -6,3 +6,5 @@ extern struct in_addr __nxlink_host; #define NXLINK_SERVER_PORT 28280 #define NXLINK_CLIENT_PORT 28771 + +int nxlinkStdio(void); diff --git a/nx/source/runtime/nxlink_stdio.c b/nx/source/runtime/nxlink_stdio.c new file mode 100644 index 00000000..a67d1adf --- /dev/null +++ b/nx/source/runtime/nxlink_stdio.c @@ -0,0 +1,41 @@ +#include "runtime/nxlink.h" + +#include +#include +#include +#include +#include +#include + +static int sock = -1; + +int nxlinkStdio(void) +{ + 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 = __nxlink_host; + srv_addr.sin_port = htons(NXLINK_CLIENT_PORT); + + ret = connect(sock, (struct sockaddr *) &srv_addr, sizeof(srv_addr)); + if (ret != 0) { + close(sock); + return -1; + } + + // redirect stdout + fflush(stdout); + dup2(sock, STDOUT_FILENO); + // redirect stderr + fflush(stderr); + dup2(sock, STDERR_FILENO); + + return ret; +}