first commit
This commit is contained in:
commit
c33c2a90d7
4 changed files with 177 additions and 0 deletions
25
.clang-format
Normal file
25
.clang-format
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
BasedOnStyle: WebKit
|
||||||
|
IndentWidth: 4
|
||||||
|
TabWidth: 4
|
||||||
|
UseTab: Never
|
||||||
|
AlignConsecutiveDeclarations: false
|
||||||
|
AlignConsecutiveAssignments: false
|
||||||
|
AlignTrailingComments: true
|
||||||
|
ColumnLimit: 105
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
AllowShortIfStatementsOnASingleLine: false
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: false
|
||||||
|
AllowShortLambdasOnASingleLine: false
|
||||||
|
PointerAlignment: Left
|
||||||
|
SpaceBeforeParens: Never
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInConditionalStatement: false
|
||||||
|
SpacesInContainerLiterals: false
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeSquareBrackets: false
|
||||||
|
SpacesBeforeTrailingComments: 2
|
||||||
|
PenaltyBreakAssignment: 1000
|
||||||
|
NamespaceIndentation: All
|
||||||
|
|
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Xmake cache
|
||||||
|
.xmake/
|
||||||
|
build/
|
||||||
|
|
||||||
|
# MacOS Cache
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
.session.vim
|
139
src/main.c
Normal file
139
src/main.c
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define MAX_CONNECTIONS 5
|
||||||
|
#define STRING_LEN 64
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t packet_id;
|
||||||
|
uint8_t protocol_version;
|
||||||
|
char username[STRING_LEN];
|
||||||
|
char key[STRING_LEN];
|
||||||
|
uint8_t unused;
|
||||||
|
} __attribute__((packed)) player_ident_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t packet_id;
|
||||||
|
uint8_t protocol_version;
|
||||||
|
char name[STRING_LEN];
|
||||||
|
char motd[STRING_LEN];
|
||||||
|
uint8_t user_type;
|
||||||
|
} __attribute__((packed)) server_ident_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t packet_id;
|
||||||
|
} __attribute__((packed)) level_initialize_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t packet_id;
|
||||||
|
uint16_t length;
|
||||||
|
uint8_t data[1024];
|
||||||
|
uint8_t percent_complete;
|
||||||
|
} __attribute__((packed)) chunk_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t packet_id;
|
||||||
|
uint16_t x_size;
|
||||||
|
uint16_t y_size;
|
||||||
|
uint16_t z_size;
|
||||||
|
} __attribute__((packed)) level_finalize_t;
|
||||||
|
|
||||||
|
void trim_string(char* str) {
|
||||||
|
for(int i = STRING_LEN - 1; i > 0; i--) {
|
||||||
|
if(str[i] != 0x20)
|
||||||
|
break;
|
||||||
|
str[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
assert(sock >= 0 && "Failed to create socket");
|
||||||
|
|
||||||
|
int opt = 1;
|
||||||
|
int result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||||
|
assert(result == 0 && "Failed to set option to reuse socket");
|
||||||
|
|
||||||
|
struct sockaddr_in serv_addr = {
|
||||||
|
.sin_family = AF_INET,
|
||||||
|
.sin_addr.s_addr = INADDR_ANY,
|
||||||
|
.sin_port = htons(25565),
|
||||||
|
};
|
||||||
|
|
||||||
|
result = bind(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
|
||||||
|
assert(result == 0 && "Socket bind failed");
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
result = listen(sock, MAX_CONNECTIONS);
|
||||||
|
assert(result == 0 && "Failed to listen on socket");
|
||||||
|
|
||||||
|
struct sockaddr_in client_addr = { 0 };
|
||||||
|
socklen_t size = sizeof(client_addr);
|
||||||
|
int conn = accept(sock, (struct sockaddr*)&client_addr, &size);
|
||||||
|
assert(conn >= 0 && "Failed to accept connection");
|
||||||
|
|
||||||
|
char client_ip[INET_ADDRSTRLEN];
|
||||||
|
inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, sizeof(client_ip));
|
||||||
|
printf("Connection accepted from %s\n", client_ip);
|
||||||
|
|
||||||
|
char buf[1024] = { 0 };
|
||||||
|
result = recv(conn, buf, 1024, 0);
|
||||||
|
assert(result >= 0 && "Failed to recieve data");
|
||||||
|
|
||||||
|
if(buf[0] == 0x00) {
|
||||||
|
player_ident_t* player_ident = (player_ident_t*)&buf;
|
||||||
|
trim_string(player_ident->username);
|
||||||
|
trim_string(player_ident->key);
|
||||||
|
|
||||||
|
if(player_ident->protocol_version == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Protocol Version: %x\nUsername: %s\nVerification Key: %s\n",
|
||||||
|
player_ident->protocol_version, player_ident->username, player_ident->key);
|
||||||
|
|
||||||
|
send(conn,
|
||||||
|
&(server_ident_t){
|
||||||
|
.packet_id = 0x00,
|
||||||
|
.protocol_version = 0x07,
|
||||||
|
.name = "hello",
|
||||||
|
.motd = "world",
|
||||||
|
.user_type = 0x00,
|
||||||
|
},
|
||||||
|
sizeof(server_ident_t), 0);
|
||||||
|
|
||||||
|
send(conn,
|
||||||
|
&(level_initialize_t){
|
||||||
|
.packet_id = 0x02,
|
||||||
|
},
|
||||||
|
sizeof(level_initialize_t), 0);
|
||||||
|
|
||||||
|
send(conn,
|
||||||
|
&(chunk_t){
|
||||||
|
.packet_id = 0x03,
|
||||||
|
.length = 0,
|
||||||
|
.data = { 0 },
|
||||||
|
.percent_complete = 100,
|
||||||
|
},
|
||||||
|
sizeof(chunk_t), 0);
|
||||||
|
|
||||||
|
send(conn,
|
||||||
|
&(level_finalize_t){
|
||||||
|
.packet_id = 0x04,
|
||||||
|
.x_size = htons(0),
|
||||||
|
.y_size = htons(0),
|
||||||
|
.z_size = htons(0),
|
||||||
|
},
|
||||||
|
sizeof(level_finalize_t), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(sock);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
5
xmake.lua
Normal file
5
xmake.lua
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
add_rules("mode.debug", "mode.release")
|
||||||
|
|
||||||
|
target("minecraft-server")
|
||||||
|
set_kind("binary")
|
||||||
|
add_files("src/*.c")
|
Loading…
Add table
Reference in a new issue