16e9f12a34
Also rename tests and rename some constants, and modify Makefile to handle the new structure.
59 lines
1.6 KiB
C
59 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#include "rcon.h"
|
|
|
|
const char *LOCALHOST = "127.0.0.1";
|
|
|
|
int read_env_int(const char *var) {
|
|
const char* port_str = getenv(var);
|
|
if (port_str == NULL) {
|
|
fprintf(stderr, "error: %s environment variable not set.\n", var);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
char* endptr;
|
|
long val = strtol(port_str, &endptr, 10);
|
|
if (endptr == port_str || *endptr != '\0') {
|
|
fprintf(stderr, "error: invalid number format for %s.\n", var);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
return (int)val;
|
|
}
|
|
|
|
int main(void) {
|
|
const int PORT_NUM = read_env_int("RCON_PORT_NUMBER");
|
|
const char *password = getenv("RCON_PASSWORD");
|
|
|
|
if (password == NULL) {
|
|
fprintf(stderr, "error: RCON_PASSWORD environment variable not set.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
printf("=== RCON Configuration ===\n");
|
|
printf("port: %d\n", PORT_NUM);
|
|
printf("password: '%s'\n", password);
|
|
printf("==========================\n\n");
|
|
|
|
int sockfd = rcon_connect(LOCALHOST, PORT_NUM);
|
|
|
|
if (rcon_authenticate(sockfd, password) < 0) {
|
|
close(sockfd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
const char *get_player_positions_cmd = "execute as @a run data get entity @s Pos";
|
|
printf("\nexecuting command: %s\n", get_player_positions_cmd);
|
|
char *response = rcon_command_multipacket(sockfd, get_player_positions_cmd);
|
|
|
|
if (response) {
|
|
printf("\n=== player positions ===\n");
|
|
printf("%s\n", response);
|
|
} else {
|
|
fprintf(stderr, "failed to get player positions\n");
|
|
}
|
|
|
|
close(sockfd);
|
|
return 0;
|
|
}
|