Skip to content

Commit

Permalink
Use new schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed Sep 20, 2024
1 parent c398388 commit 3d3ca79
Show file tree
Hide file tree
Showing 17 changed files with 623 additions and 1,603 deletions.
1 change: 0 additions & 1 deletion mc_dissector.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,4 @@ _U_ void plugin_register() {
plugin.register_handoff = proto_reg_handoff;
proto_register_plugin(&plugin);
}
init_schema_data();
}
18 changes: 0 additions & 18 deletions protocol/new_schema/schema.h

This file was deleted.

31 changes: 19 additions & 12 deletions protocol/protocol_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Created by Nickid2018 on 2023/7/12.
//

#include <epan/conversation.h>
#include "protocol_data.h"

char *STATE_NAME[] = {"Handshake", "Play", "Server List Ping", "Login", "Transfer", "Configuration", "Invalid"};

gint read_var_int(tvbuff_t *tvb, gint offset, gint *result) {
guint8 read;
gint p = 0;
int32_t read_var_int(tvbuff_t *tvb, int32_t offset, int32_t *result) {
uint8_t read;
int32_t p = 0;
*result = 0;
do {
if (p == 5)
Expand All @@ -19,9 +20,9 @@ gint read_var_int(tvbuff_t *tvb, gint offset, gint *result) {
return p;
}

gint read_var_int_with_limit(tvbuff_t *tvb, gint offset, gint max_length, gint *result) {
guint8 read;
gint p = 0;
int32_t read_var_int_with_limit(tvbuff_t *tvb, int32_t offset, int32_t max_length, int32_t *result) {
uint8_t read;
int32_t p = 0;
*result = 0;
do {
if (p == 5 || p >= max_length)
Expand All @@ -32,10 +33,10 @@ gint read_var_int_with_limit(tvbuff_t *tvb, gint offset, gint max_length, gint *
return p;
}

gint read_var_long(tvbuff_t *tvb, gint offset, gint64 *result) {
gint p = 0;
int32_t read_var_long(tvbuff_t *tvb, int32_t offset, int64_t *result) {
int32_t p = 0;
*result = 0;
guint8 read;
uint8_t read;
do {
if (p == 10)
return INVALID_DATA;
Expand All @@ -45,13 +46,19 @@ gint read_var_long(tvbuff_t *tvb, gint offset, gint64 *result) {
return p;
}

gint read_buffer(tvbuff_t *tvb, gint offset, guint8 **result, wmem_allocator_t *allocator) {
gint length;
gint read = read_var_int(tvb, offset, &length);
int32_t read_buffer(tvbuff_t *tvb, int32_t offset, uint8_t **result, wmem_allocator_t *allocator) {
int32_t length;
int32_t read = read_var_int(tvb, offset, &length);
if (is_invalid(read))
return INVALID_DATA;
*result = wmem_alloc(allocator, length + 1);
tvb_memcpy(tvb, *result, offset + read, length);
(*result)[length] = '\0';
return read + length;
}

wmem_map_t *get_global_data(packet_info *pinfo) {
conversation_t *conv = find_or_create_conversation(pinfo);
mc_protocol_context *ctx = conversation_get_proto_data(conv, proto_mcje);
return ctx->global_data;
}
35 changes: 19 additions & 16 deletions protocol/protocol_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <epan/proto.h>
#include <gcrypt.h>
#include "storage/storage.h"
#include "protocol/schema/schema.h"

#define INVALID_DATA (-1)
#define is_invalid(x) ((x) == INVALID_DATA)
Expand All @@ -21,22 +22,22 @@ typedef struct {
je_state client_state;
je_state server_state;

guint32 server_port;
uint32_t server_port;
address server_address;

guint32 protocol_version;
guint32 data_version;
protocol_je_set protocol_set;
uint32_t protocol_version;
uint32_t data_version;
protocol_dissector_set *dissector_set;

gint32 compression_threshold;
int32_t compression_threshold;
bool encrypted;

gcry_cipher_hd_t server_cipher;
gcry_cipher_hd_t client_cipher;
gint server_last_segment_remaining;
gint client_last_segment_remaining;
guint8 *server_last_remains;
guint8 *client_last_remains;
int32_t server_last_segment_remaining;
int32_t client_last_segment_remaining;
uint8_t *server_last_remains;
uint8_t *client_last_remains;

wmem_map_t *global_data;
} mc_protocol_context;
Expand All @@ -46,19 +47,21 @@ typedef struct {
je_state server_state;

bool encrypted;
guint8 *decrypted_data_head;
guint8 *decrypted_data_tail;
gint32 compression_threshold;
uint8_t *decrypted_data_head;
uint8_t *decrypted_data_tail;
int32_t compression_threshold;
} mc_frame_data;

extern char *STATE_NAME[];

gint read_var_int(tvbuff_t *tvb, gint offset, gint *result);
wmem_map_t *get_global_data(packet_info *pinfo);

gint read_var_int_with_limit(tvbuff_t *tvb, gint offset, gint max_length, gint *result);
int32_t read_var_int(tvbuff_t *tvb, int32_t offset, int32_t *result);

gint read_var_long(tvbuff_t *tvb, gint offset, gint64 *result);
int32_t read_var_int_with_limit(tvbuff_t *tvb, int32_t offset, int32_t max_length, int32_t *result);

gint read_buffer(tvbuff_t *tvb, gint offset, guint8 **resul, wmem_allocator_t *allocator);
int32_t read_var_long(tvbuff_t *tvb, int32_t offset, int64_t *result);

int32_t read_buffer(tvbuff_t *tvb, int32_t offset, uint8_t **resul, wmem_allocator_t *allocator);

#endif //MC_DISSECTOR_PROTOCOL_DATA_H
97 changes: 97 additions & 0 deletions protocol/schema/functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// Created by nickid2018 on 24-9-20.
//

#include <epan/conversation.h>
#include "functions.h"
#include "protocol/storage/storage.h"
#include "protocol/protocol_data.h"

extern int hf_generated;
extern int hf_invalid_data;

DISSECT_PROTOCOL(record_entity_id) {
if (!get_settings_flag("registries") && !get_settings_flag("entities"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(get_global_data(pinfo), "#entity_id_record");
if (entity_id_record == NULL) {
entity_id_record = wmem_map_new(wmem_file_scope(), g_str_hash, g_str_equal);
wmem_map_insert(get_global_data(pinfo), "#entity_id_record", entity_id_record);
}
gchar *entity_id = wmem_map_lookup(packet_saves, "entity_id");
gchar *str_type = wmem_map_lookup(packet_saves, "entity_type");
wmem_map_insert(entity_id_record, entity_id, str_type);
return 0;
}

DISSECT_PROTOCOL(record_entity_id_player) {
if (!get_settings_flag("registries") && !get_settings_flag("entities"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(get_global_data(pinfo), "#entity_id_record");
if (entity_id_record == NULL) {
entity_id_record = wmem_map_new(wmem_file_scope(), g_str_hash, g_str_equal);
wmem_map_insert(get_global_data(pinfo), "#entity_id_record", entity_id_record);
}
gchar *entity_id = wmem_map_lookup(packet_saves, "entity_id");
wmem_map_insert(entity_id_record, entity_id, "player");
return 0;
}

DISSECT_PROTOCOL(record_entity_id_experience_orb) {
if (!get_settings_flag("registries") && !get_settings_flag("entities"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(get_global_data(pinfo), "#entity_id_record");
if (entity_id_record == NULL) {
entity_id_record = wmem_map_new(wmem_file_scope(), g_str_hash, g_str_equal);
wmem_map_insert(get_global_data(pinfo), "#entity_id_record", entity_id_record);
}
gchar *entity_id = wmem_map_lookup(packet_saves, "entity_id");
wmem_map_insert(entity_id_record, entity_id, "experience_orb");
return 0;
}

DISSECT_PROTOCOL(record_entity_id_painting) {
if (!get_settings_flag("registries") && !get_settings_flag("entities"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(get_global_data(pinfo), "#entity_id_record");
if (entity_id_record == NULL) {
entity_id_record = wmem_map_new(wmem_file_scope(), g_str_hash, g_str_equal);
wmem_map_insert(get_global_data(pinfo), "#entity_id_record", entity_id_record);
}
gchar *entity_id = wmem_map_lookup(packet_saves, "entity_id");
wmem_map_insert(entity_id_record, entity_id, "painting");
return 0;
}

DISSECT_PROTOCOL(sync_entity_data) {
if (!tree || !get_settings_flag("entity_sync_datas"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(get_global_data(pinfo), "#entity_id_record");
if (entity_id_record == NULL) {
entity_id_record = wmem_map_new(wmem_file_scope(), g_str_hash, g_str_equal);
wmem_map_insert(get_global_data(pinfo), "#entity_id_record", entity_id_record);
}
gchar *entity_id = wmem_map_lookup(packet_saves, "entity_id");
char *type = wmem_map_lookup(entity_id_record, entity_id);
if (type != NULL) {
proto_item *item = proto_tree_add_string(tree, hf_generated, tvb, 0, 0, type);
proto_item_set_generated(item);
proto_item_prepend_text(item, "Entity Type");
} else {
proto_item *item = proto_tree_add_string(tree, hf_generated, tvb, 0, 0, "Unknown");
proto_item_set_generated(item);
proto_item_prepend_text(item, "Entity Type");
return 0;
}
gchar *sync_id = wmem_map_lookup(packet_saves, "sync_id");
gchar *end;
int64_t sync = strtol(sync_id, &end, 10);
uint32_t protocol_version = (uint64_t) wmem_map_lookup(get_global_data(pinfo), "protocol_version");
gchar *found_name = get_entity_sync_data_name(protocol_version, type, sync);
if (found_name == NULL)
found_name = "Unknown Sync Data!";
proto_item *item = proto_tree_add_string(tree, hf_generated, tvb, 0, 0, found_name);
proto_item_set_generated(item);
proto_item_prepend_text(item, "Sync Data Type");
return 0;
}
26 changes: 26 additions & 0 deletions protocol/schema/functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by nickid2018 on 24-9-20.
//

#ifndef MC_DISSECTOR_FUNCTIONS_H
#define MC_DISSECTOR_FUNCTIONS_H

#include "schema.h"

#define DISSECT_PROTOCOL(fn) \
int32_t dissect_##fn( \
proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, int offset, \
protocol_dissector *dissector, gchar *name, wmem_map_t *packet_saves, gchar **value \
)

DISSECT_PROTOCOL(record_entity_id);

DISSECT_PROTOCOL(record_entity_id_player);

DISSECT_PROTOCOL(record_entity_id_experience_orb);

DISSECT_PROTOCOL(record_entity_id_painting);

DISSECT_PROTOCOL(sync_entity_data);

#endif //MC_DISSECTOR_FUNCTIONS_H
Loading

0 comments on commit 3d3ca79

Please sign in to comment.