Add MCPI_API_PORT

This commit is contained in:
TheBrokenRail 2022-09-21 17:54:40 -04:00
parent 58f329bb4f
commit 6994671c6d
2 changed files with 24 additions and 0 deletions

View File

@ -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.

View File

@ -1,6 +1,8 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <GLES/gl.h>
@ -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() {
}