Skip to content

Commit

Permalink
Add new composite types
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed Sep 15, 2024
1 parent abde8e6 commit 92cb518
Show file tree
Hide file tree
Showing 15 changed files with 510 additions and 102 deletions.
382 changes: 366 additions & 16 deletions protocol/new_schema/schema.c

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions protocol/new_schema/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
#include <cJSON.h>

typedef struct protocol_dissector_struct protocol_dissector;
typedef struct protocol_dissector_set_struct protocol_dissector_set;

protocol_dissector_set *create_protocol(uint32_t protocol_version);

void destroy_protocol(protocol_dissector_set *dissector_set);

#endif //MC_DISSECTOR_SCHEMA_H
6 changes: 3 additions & 3 deletions protocol/protocol_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ typedef struct {
guint8 *server_last_remains;
guint8 *client_last_remains;

void *extra;
} mcje_protocol_context;
wmem_map_t *global_data;
} mc_protocol_context;

typedef struct {
je_state client_state;
Expand All @@ -49,7 +49,7 @@ typedef struct {
guint8 *decrypted_data_head;
guint8 *decrypted_data_tail;
gint32 compression_threshold;
} mcje_frame_data;
} mc_frame_data;

extern char *STATE_NAME[];

Expand Down
28 changes: 14 additions & 14 deletions protocol/schema/protocol_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ extern int hf_generated;
FIELD_MAKE_TREE(record_entity_id) {
if (!get_settings_flag("registries") && !get_settings_flag("entities"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(extra->data, "entity_id_record");
wmem_map_t *entity_id_record = wmem_map_lookup(extra, "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(extra->data, "entity_id_record", entity_id_record);
wmem_map_insert(extra, "entity_id_record", entity_id_record);
}
char *id_path[] = {"entityId", NULL};
gchar *id = record_query(recorder, id_path);
char *type_path[] = {"type", NULL};
gchar *type = record_query(recorder, type_path);
guint protocol_version = GPOINTER_TO_UINT(wmem_map_lookup(extra->data, "protocol_version"));
guint protocol_version = GPOINTER_TO_UINT(wmem_map_lookup(extra, "protocol_version"));
guint type_uint = strtol(type, NULL, 10);
char *str_type = get_registry_data(protocol_version, "entity_type", type_uint);
wmem_map_insert(entity_id_record, id, str_type);
Expand All @@ -34,10 +34,10 @@ FIELD_MAKE_TREE(record_entity_id) {
FIELD_MAKE_TREE(record_entity_id_player) {
if (!get_settings_flag("registries") && !get_settings_flag("entities"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(extra->data, "entity_id_record");
wmem_map_t *entity_id_record = wmem_map_lookup(extra, "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(extra->data, "entity_id_record", entity_id_record);
wmem_map_insert(extra, "entity_id_record", entity_id_record);
}
char *id_path[] = {"entityId", NULL};
gchar *id = record_query(recorder, id_path);
Expand All @@ -48,10 +48,10 @@ FIELD_MAKE_TREE(record_entity_id_player) {
FIELD_MAKE_TREE(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(extra->data, "entity_id_record");
wmem_map_t *entity_id_record = wmem_map_lookup(extra, "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(extra->data, "entity_id_record", entity_id_record);
wmem_map_insert(extra, "entity_id_record", entity_id_record);
}
char *id_path[] = {"entityId", NULL};
gchar *id = record_query(recorder, id_path);
Expand All @@ -62,10 +62,10 @@ FIELD_MAKE_TREE(record_entity_id_experience_orb) {
FIELD_MAKE_TREE(record_entity_id_painting) {
if (!get_settings_flag("registries") && !get_settings_flag("entities"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(extra->data, "entity_id_record");
wmem_map_t *entity_id_record = wmem_map_lookup(extra, "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(extra->data, "entity_id_record", entity_id_record);
wmem_map_insert(extra, "entity_id_record", entity_id_record);
}
char *id_path[] = {"entityId", NULL};
gchar *id = record_query(recorder, id_path);
Expand All @@ -76,16 +76,16 @@ FIELD_MAKE_TREE(record_entity_id_painting) {
FIELD_MAKE_TREE(sync_entity_data) {
if (!tree || !get_settings_flag("entity_sync_datas"))
return 0;
wmem_map_t *entity_id_record = wmem_map_lookup(extra->data, "entity_id_record");
wmem_map_t *entity_id_record = wmem_map_lookup(extra, "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(extra->data, "entity_id_record", entity_id_record);
wmem_map_insert(extra, "entity_id_record", entity_id_record);
}
char *id_path[] = {"..", "entityId", NULL};
gchar *id = record_query(recorder, id_path);
char *key_path[] = {"key", NULL};
gchar *key = record_query(recorder, key_path);
guint protocol_version = GPOINTER_TO_UINT(wmem_map_lookup(extra->data, "protocol_version"));
guint protocol_version = GPOINTER_TO_UINT(wmem_map_lookup(extra, "protocol_version"));
guint key_int = strtol(key, NULL, 10);
char *type = wmem_map_lookup(entity_id_record, id);
if (type != NULL) {
Expand Down Expand Up @@ -113,7 +113,7 @@ FIELD_MAKE_TREE(entity_event) {
char *event_id_path[] = {"entityStatus", NULL};
gchar *event_id = record_query(recorder, event_id_path);
gchar *event_name = get_entity_event_data(
GPOINTER_TO_UINT(wmem_map_lookup(extra->data, "protocol_version")),
GPOINTER_TO_UINT(wmem_map_lookup(extra, "protocol_version")),
event_id
);
if (event_name == NULL)
Expand All @@ -130,7 +130,7 @@ FIELD_MAKE_TREE(level_event) {
char *event_id_path[] = {"effectId", NULL};
gchar *event_id = record_query(recorder, event_id_path);
gchar *event_name = get_level_event_data(
GPOINTER_TO_UINT(wmem_map_lookup(extra->data, "protocol_version")),
GPOINTER_TO_UINT(wmem_map_lookup(extra, "protocol_version")),
event_id
);
if (event_name == NULL)
Expand Down
2 changes: 1 addition & 1 deletion protocol/schema/protocol_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "protocol_schema.h"

#define FIELD_MAKE_TREE(name) \
gint make_tree_##name(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, extra_data *extra, protocol_field field, gint offset, gint remaining, data_recorder recorder, bool is_je)
gint make_tree_##name(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, wmem_map_t *extra, protocol_field field, gint offset, gint remaining, data_recorder recorder, bool is_je)

#define SINGLE_LENGTH_FIELD_MAKE(id_name, hf, len, func_add, func_parse, record) \
FIELD_MAKE_TREE(id_name) { \
Expand Down
16 changes: 8 additions & 8 deletions protocol/schema/protocol_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ FIELD_MAKE_TREE(void) {

FIELD_MAKE_TREE(nbt) {
if (pref_do_nbt_decode && is_je)
return do_nbt_tree(tree, pinfo, tvb, offset, field->name, is_je, true);
return do_nbt_tree(tree, pinfo, tvb, offset, field->name, true);
else {
gint length = count_nbt_length(tvb, offset);
if (tree)
Expand All @@ -185,7 +185,7 @@ FIELD_MAKE_TREE(optional_nbt) {
guint8 present = tvb_get_uint8(tvb, offset);
if (present != TAG_END) {
if (pref_do_nbt_decode && is_je)
return do_nbt_tree(tree, pinfo, tvb, offset, field->name, is_je, true);
return do_nbt_tree(tree, pinfo, tvb, offset, field->name, true);
else {
gint length = count_nbt_length(tvb, offset);
if (tree)
Expand Down Expand Up @@ -215,7 +215,7 @@ FIELD_MAKE_TREE(nbt_any_type) {
guint8 present = tvb_get_uint8(tvb, offset);
if (present != TAG_END) {
if (pref_do_nbt_decode && is_je)
return do_nbt_tree(tree, pinfo, tvb, offset, field->name, is_je, false);
return do_nbt_tree(tree, pinfo, tvb, offset, field->name, false);
else {
gint length = count_nbt_length_with_type(tvb, offset + 1, present) + 1;
if (tree)
Expand Down Expand Up @@ -249,7 +249,7 @@ FIELD_MAKE_TREE(container) {
if (tree && not_top)
tree = proto_tree_add_subtree(
tree, tvb, offset, remaining,
is_je ? ett_sub_je : ett_sub_be, NULL, field->name
is_je ? ett_sub : ett_sub_be, NULL, field->name
);
gint length = GPOINTER_TO_UINT(wmem_map_lookup(field->additional_info, 0));
gint total_length = 0;
Expand Down Expand Up @@ -342,7 +342,7 @@ FIELD_MAKE_TREE(array) {
if (tree) {
sub_tree = proto_tree_add_subtree(
tree, tvb, offset, remaining,
is_je ? ett_sub_je : ett_sub_be, NULL,
is_je ? ett_sub : ett_sub_be, NULL,
g_strdup_printf("%s (%d entries)", field->name, data_count)
);
}
Expand Down Expand Up @@ -444,7 +444,7 @@ FIELD_MAKE_TREE(top_bit_set_terminated_array) {
if (tree)
sub_tree = proto_tree_add_subtree(
tree, tvb, offset, remaining,
is_je ? ett_sub_je : ett_sub_be, NULL, field->name
is_je ? ett_sub : ett_sub_be, NULL, field->name
);
do {
now = tvb_get_uint8(tvb, offset++);
Expand Down Expand Up @@ -490,7 +490,7 @@ FIELD_MAKE_TREE(entity_metadata_loop) {
if (tree)
sub_tree = proto_tree_add_subtree(
tree, tvb, offset, remaining,
is_je ? ett_sub_je : ett_sub_be, NULL, field->name
is_je ? ett_sub : ett_sub_be, NULL, field->name
);
while (tvb_get_uint8(tvb, offset) != end_val) {
record_start(recorder, g_strconcat(recording, "[", g_strdup_printf("%d", count), "]", NULL));
Expand Down Expand Up @@ -916,7 +916,7 @@ protocol_entry get_protocol_entry(protocol_set set, guint packet_id, bool is_cli
return wmem_map_lookup(packet_map, GUINT_TO_POINTER(packet_id));
}

bool make_tree(protocol_entry entry, proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, extra_data *extra,
bool make_tree(protocol_entry entry, proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, wmem_map_t *extra,
gint remaining) {
if (entry->field != NULL) {
data_recorder recorder = create_data_recorder(pinfo->pool);
Expand Down
9 changes: 2 additions & 7 deletions protocol/schema/protocol_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ typedef struct _protocol_set protocol_set_t, *protocol_set;
typedef struct _protocol_entry protocol_entry_t, *protocol_entry;
typedef struct _protocol_field protocol_field_t, *protocol_field;

typedef struct {
wmem_map_t *data;
bool visited;
} extra_data;

struct _protocol_field {
gchar *name;
wmem_map_t *additional_info;

gint (*make_tree)(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, extra_data *extra, protocol_field field,
gint (*make_tree)(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, wmem_map_t *extra, protocol_field field,
gint offset, gint remaining, data_recorder recorder, bool is_je);
};

Expand All @@ -43,6 +38,6 @@ gint get_packet_id(protocol_set set, gchar *name, bool is_client);
protocol_entry get_protocol_entry(protocol_set set, guint packet_id, bool is_client);

bool
make_tree(protocol_entry entry, proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, extra_data *extra, gint remaining);
make_tree(protocol_entry entry, proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, wmem_map_t *extra, gint remaining);

#endif //MC_DISSECTOR_PROTOCOL_SCHEMA_H
62 changes: 62 additions & 0 deletions protocol/storage/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ DATA_CACHED_STR(registry_data_mapping)

DATA_CACHED_STR(registry_data)

wmem_map_t *index_mappings;

void ensure_init_index_mappings() {
if (index_mappings) return;
index_mappings = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
gchar *file = g_build_filename(pref_protocol_data_dir, "java_edition/indexes.csv", NULL);
gchar *content = NULL;
if (!g_file_get_contents(file, &content, NULL, NULL)) {
ws_log("MC-Dissector", LOG_LEVEL_WARNING, "Cannot read file %s", file);
g_free(file);
return;
}
g_free(file);

gchar **split_lines = g_strsplit(content, "\n", 10000);
gchar **header = g_strsplit(split_lines[0], ",", 1000);
gchar *now_line;
for (int i = 1; (now_line = split_lines[i]) != NULL; i++) {
gchar **now_line_split = g_strsplit(now_line, ",", 10000);
wmem_map_t *map = wmem_map_new(wmem_epan_scope(), g_str_hash, g_str_equal);
wmem_map_insert(index_mappings, g_strdup(now_line_split[0]), map);
gchar *now_value;
for (int j = 1; (now_value = now_line_split[j]) != NULL; j++)
wmem_map_insert(map, header[j], now_value);
g_strfreev(now_line_split);
}
g_strfreev(split_lines);
g_free(content);
}

gboolean nop(gpointer key _U_, gpointer value _U_, gpointer user_data _U_) {
return true;
}
Expand Down Expand Up @@ -155,6 +185,38 @@ gchar *get_readable_packet_name(bool to_client, gchar *packet_name) {
return found->valuestring;
}

cJSON *get_protocol_source(uint32_t protocol_version) {
cJSON *cached = get_cached_protocol(protocol_version);
if (cached != NULL)
return cached;

ensure_cached_protocol_data_mapping();
gchar *find_key = g_strdup_printf("%d", protocol_version);
cJSON *found = cJSON_GetObjectItem(cached_protocol_data_mapping, find_key);
g_free(find_key);
if (found == NULL)
return NULL;

gchar *file = g_build_filename(
pref_protocol_data_dir,
"java_edition/indexed_data",
found->valuestring,
"protocol.json",
NULL
);
gchar *content = NULL;
if (!g_file_get_contents(file, &content, NULL, NULL)) {
ws_log("MC-Dissector", LOG_LEVEL_WARNING, "Cannot read file %s", file);
g_free(file);
return NULL;
}

cJSON *json = cJSON_Parse(content);
g_free(content);

return json;
}

protocol_je_set get_protocol_set_je(guint protocol_version, protocol_settings settings) {
protocol_je_set cached = get_cached_protocol(protocol_version);
if (cached != NULL)
Expand Down
25 changes: 12 additions & 13 deletions protocol_je/je_dissect.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void proto_reg_handoff_mcje() {
dissector_add_uint_range_with_preference("tcp.port", MCJE_PORT, mcje_handle);
}

void sub_dissect_je(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, mcje_frame_data *frame_data,
mcje_protocol_context *ctx, bool is_server,
void sub_dissect_je(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, mc_frame_data *frame_data,
mc_protocol_context *ctx, bool is_server,
bool visited) {
if (is_server) {
switch (frame_data->server_state) {
Expand Down Expand Up @@ -90,20 +90,20 @@ void sub_dissect_je(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, mcje_fr

void mark_invalid(packet_info *pinfo) {
conversation_t *conv = find_or_create_conversation(pinfo);
mcje_protocol_context *ctx = conversation_get_proto_data(conv, proto_mcje);
mc_protocol_context *ctx = conversation_get_proto_data(conv, proto_mcje);
ctx->client_state = INVALID;
ctx->server_state = INVALID;
}

void dissect_je_core(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset, gint packet_len_len, gint len) {
conversation_t *conv = find_or_create_conversation(pinfo);
mcje_protocol_context *ctx = conversation_get_proto_data(conv, proto_mcje);
mcje_frame_data *frame_data = p_get_proto_data(wmem_file_scope(), pinfo, proto_mcje, 0);
mc_protocol_context *ctx = conversation_get_proto_data(conv, proto_mcje);
mc_frame_data *frame_data = p_get_proto_data(wmem_file_scope(), pinfo, proto_mcje, 0);

proto_tree *mcje_tree;
if (tree) {
proto_item *ti = proto_tree_add_item(tree, proto_mcje, tvb, 0, -1, FALSE);
mcje_tree = proto_item_add_subtree(ti, ett_mcje);
mcje_tree = proto_item_add_subtree(ti, ett_mc);
proto_tree_add_uint(mcje_tree, hf_packet_length_je, tvb, offset - packet_len_len, packet_len_len, len);
proto_item_append_text(
ti, ", Client State: %s, Server State: %s",
Expand Down Expand Up @@ -149,7 +149,7 @@ void dissect_je_core(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint o
if (tree) {
proto_item *packet_item = proto_tree_add_item(mcje_tree, proto_mcje, new_tvb, 0, -1, FALSE);
proto_item_set_text(packet_item, "Minecraft JE Packet");
proto_tree *sub_mcje_tree = proto_item_add_subtree(packet_item, ett_je_proto);
proto_tree *sub_mcje_tree = proto_item_add_subtree(packet_item, ett_proto);
sub_dissect_je(new_tvb, pinfo, sub_mcje_tree, frame_data, ctx, is_server, pinfo->fd->visited);
} else {
sub_dissect_je(new_tvb, pinfo, NULL, frame_data, ctx, is_server, pinfo->fd->visited);
Expand All @@ -158,9 +158,9 @@ void dissect_je_core(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint o

int dissect_je_conv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void *data _U_) {
conversation_t *conv = find_or_create_conversation(pinfo);
mcje_protocol_context *ctx = conversation_get_proto_data(conv, proto_mcje);
mc_protocol_context *ctx = conversation_get_proto_data(conv, proto_mcje);
if (!ctx) {
ctx = wmem_alloc(wmem_file_scope(), sizeof(mcje_protocol_context));
ctx = wmem_alloc(wmem_file_scope(), sizeof(mc_protocol_context));
ctx->client_state = is_compatible_protocol_data() ? HANDSHAKE : NOT_COMPATIBLE;
ctx->server_state = is_compatible_protocol_data() ? HANDSHAKE : NOT_COMPATIBLE;
ctx->compression_threshold = -1;
Expand All @@ -172,14 +172,13 @@ int dissect_je_conv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, voi
ctx->server_last_remains = NULL;
ctx->client_last_remains = NULL;
copy_address(&ctx->server_address, &pinfo->dst);
ctx->extra = wmem_alloc(wmem_file_scope(), sizeof(extra_data));
((extra_data *) ctx->extra)->data = wmem_map_new(wmem_file_scope(), g_str_hash, g_str_equal);
ctx->global_data = wmem_map_new(wmem_file_scope(), g_str_hash, g_str_equal);
conversation_add_proto_data(conv, proto_mcje, ctx);
}

mcje_frame_data *frame_data = p_get_proto_data(wmem_file_scope(), pinfo, proto_mcje, 0);
mc_frame_data *frame_data = p_get_proto_data(wmem_file_scope(), pinfo, proto_mcje, 0);
if (!frame_data) {
frame_data = wmem_alloc(wmem_file_scope(), sizeof(mcje_frame_data));
frame_data = wmem_alloc(wmem_file_scope(), sizeof(mc_frame_data));
frame_data->client_state = ctx->client_state;
frame_data->server_state = ctx->server_state;
frame_data->encrypted = ctx->encrypted;
Expand Down
6 changes: 3 additions & 3 deletions protocol_je/je_dissect.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

extern dissector_handle_t mcje_handle;

extern int ett_mcje;
extern int ett_je_proto;
extern int ett_sub_je;
extern int ett_mc;
extern int ett_proto;
extern int ett_sub;

void proto_register_mcje();

Expand Down
Loading

0 comments on commit 92cb518

Please sign in to comment.