From 6994671c6dfcde7354552a45edbb99433822f209 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Wed, 21 Sep 2022 17:54:40 -0400 Subject: [PATCH] Add MCPI_API_PORT --- docs/COMMAND_LINE.md | 3 +++ mods/src/misc/misc.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/docs/COMMAND_LINE.md b/docs/COMMAND_LINE.md index b9eee1b93..80a3e1aaf 100644 --- a/docs/COMMAND_LINE.md +++ b/docs/COMMAND_LINE.md @@ -30,6 +30,9 @@ The world used will always be re-created on start and uses a hard-coded seed. ### ``MCPI_DEBUG`` This enables debug logging if it is set. +### ``MCPI_API_PORT`` +This configures the API to use a different port (the default is 4711). + ### Client Mode Only If any of the following variables aren't set, one configuration dialog will open on startup for each unset variable. diff --git a/mods/src/misc/misc.c b/mods/src/misc/misc.c index f1461782d..0fa1e3954 100644 --- a/mods/src/misc/misc.c +++ b/mods/src/misc/misc.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include @@ -309,6 +311,25 @@ static void HumanoidMobRenderer_render_injection(unsigned char *model_renderer, *(bool *) (model + HumanoidModel_is_sneaking_property_offset) = 0; } +// Custom API Port +HOOK(bind, int, (int sockfd, const struct sockaddr *addr, socklen_t addrlen)) { + const struct sockaddr *new_addr = addr; + struct sockaddr_in in_addr; + if (addr->sa_family == AF_INET) { + in_addr = *(const struct sockaddr_in *) new_addr; + if (in_addr.sin_port == ntohs(4711)) { + const char *new_port_str = getenv("MCPI_API_PORT"); + long int new_port; + if (new_port_str != NULL && (new_port = strtol(new_port_str, NULL, 0)) != 0L) { + in_addr.sin_port = htons(new_port); + } + } + new_addr = (const struct sockaddr *) &in_addr; + } + ensure_bind(); + return (*real_bind)(sockfd, new_addr, addrlen); +} + // Init static void nop() { }