Skip to content

Commit

Permalink
use pinfo->pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed Jun 2, 2024
1 parent adbacfb commit b392640
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 70 deletions.
4 changes: 2 additions & 2 deletions protocol_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ gint read_var_long(tvbuff_t *tvb, gint offset, gint64 *result) {
return p;
}

gint read_buffer(tvbuff_t *tvb, gint offset, guint8 **result) {
gint read_buffer(tvbuff_t *tvb, gint offset, guint8 **result, wmem_allocator_t *allocator) {
gint length;
gint read = read_var_int(tvb, offset, &length);
if (is_invalid(read))
return INVALID_DATA;
*result = wmem_alloc(wmem_packet_scope(), length + 1);
*result = wmem_alloc(allocator, length + 1);
tvb_memcpy(tvb, *result, offset + read, length);
(*result)[length] = '\0';
return read + length;
Expand Down
2 changes: 1 addition & 1 deletion protocol_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ gint read_var_int_with_limit(tvbuff_t *tvb, gint offset, gint max_length, gint *

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

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

#endif //MC_DISSECTOR_PROTOCOL_DATA_H
16 changes: 8 additions & 8 deletions protocol_je/je_dissect.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void sub_dissect_je(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, mcje_pr
if (!visited && is_invalid(handle_server_handshake_switch(tvb, ctx)))
return;
if (tree)
handle_server_handshake(tree, tvb);
handle_server_handshake(tree, pinfo, tvb);
return;
case PING:
if (tree)
Expand All @@ -35,19 +35,19 @@ void sub_dissect_je(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, mcje_pr
if (!visited && is_invalid(handle_server_login_switch(tvb, ctx)))
return;
if (tree)
handle_login(tree, tvb, ctx, false);
handle_login(tree, pinfo, tvb, ctx, false);
return;
case PLAY:
if (!visited && is_invalid(handle_server_play_switch(tvb, ctx)))
return;
if (tree)
handle_play(tree, tvb, ctx, false);
handle_play(tree, pinfo, tvb, ctx, false);
return;
case CONFIGURATION:
if (!visited && is_invalid(handle_server_configuration_switch(tvb, ctx)))
return;
if (tree)
handle_configuration(tree, tvb, ctx, false);
handle_configuration(tree, pinfo, tvb, ctx, false);
return;
default:
col_add_str(pinfo->cinfo, COL_INFO, "[Invalid State]");
Expand All @@ -57,26 +57,26 @@ void sub_dissect_je(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, mcje_pr
switch (ctx->client_state) {
case PING:
if (tree)
handle_client_slp(tree, tvb);
handle_client_slp(tree, pinfo, tvb);
return;
case LOGIN:
case TRANSFER:
if (!visited && is_invalid(handle_client_login_switch(tvb, ctx)))
return;
if (tree)
handle_login(tree, tvb, ctx, true);
handle_login(tree, pinfo, tvb, ctx, true);
return;
case PLAY:
if (!visited && is_invalid(handle_client_play_switch(tvb, ctx)))
return;
if (tree)
handle_play(tree, tvb, ctx, true);
handle_play(tree, pinfo, tvb, ctx, true);
return;
case CONFIGURATION:
if (!visited && is_invalid(handle_client_configuration_switch(tvb, ctx)))
return;
if (tree)
handle_configuration(tree, tvb, ctx, true);
handle_configuration(tree, pinfo, tvb, ctx, true);
return;
default:
col_add_str(pinfo->cinfo, COL_INFO, "[Invalid State]");
Expand Down
26 changes: 13 additions & 13 deletions protocol_je/je_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int handle_server_handshake_switch(tvbuff_t *tvb, mcje_protocol_context *ctx) {
return INVALID_DATA;
}

void handle_server_handshake(proto_tree *packet_tree, tvbuff_t *tvb) {
void handle_server_handshake(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb) {
gint packet_id;
gint p;
gint read = p = read_var_int(tvb, 0, &packet_id);
Expand All @@ -75,7 +75,7 @@ void handle_server_handshake(proto_tree *packet_tree, tvbuff_t *tvb) {
p += read;

guint8 *server_address;
read = read_buffer(tvb, p, &server_address);
read = read_buffer(tvb, p, &server_address, pinfo->pool);
if (is_invalid(read)) {
proto_tree_add_string(packet_tree, hf_server_address_je, tvb, p, -1, "Invalid Server Address");
return;
Expand Down Expand Up @@ -121,7 +121,7 @@ void handle_server_slp(proto_tree *packet_tree, tvbuff_t *tvb) {
proto_tree_add_string(packet_tree, hf_packet_name_je, tvb, 0, read, "Unknown Packet ID");
}

void handle_client_slp(proto_tree *packet_tree, tvbuff_t *tvb) {
void handle_client_slp(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb) {
gint packet_id;
gint p;
gint read = p = read_var_int(tvb, 0, &packet_id);
Expand All @@ -134,7 +134,7 @@ void handle_client_slp(proto_tree *packet_tree, tvbuff_t *tvb) {
if (packet_id == PACKET_ID_CLIENT_SERVER_INFO) {
proto_tree_add_string(packet_tree, hf_packet_name_je, tvb, 0, read, "Client Server Info");
guint8 *server_info;
read = read_buffer(tvb, p, &server_info);
read = read_buffer(tvb, p, &server_info, pinfo->pool);
if (is_invalid(read)) {
proto_tree_add_string(packet_tree, hf_invalid_data_je, tvb, p, -1, "Invalid Server Info");
return;
Expand Down Expand Up @@ -215,7 +215,7 @@ int handle_server_login_switch(tvbuff_t *tvb, mcje_protocol_context *ctx) {
return 0;
}

void handle(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx, protocol_set protocol_set, bool is_client) {
void handle(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb, mcje_protocol_context *ctx, protocol_set protocol_set, bool is_client) {
gint packet_id;
gint p;
gint read = p = read_var_int(tvb, 0, &packet_id);
Expand Down Expand Up @@ -252,18 +252,18 @@ void handle(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx,
gint length = (gint) tvb_reported_length(tvb);
if (ignore)
proto_tree_add_string(packet_tree, hf_ignored_packet_je, tvb, p, length - p, "Ignored by user");
else if (!make_tree(protocol, packet_tree, tvb, ctx->extra, length))
else if (!make_tree(protocol, packet_tree, pinfo, tvb, ctx->extra, length))
proto_tree_add_string(packet_tree, hf_ignored_packet_je, tvb, p, length - p,
"Protocol hasn't been implemented yet");
}

void handle_login(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client) {
void handle_login(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client) {
if (ctx->protocol_set == NULL) {
proto_tree_add_string(packet_tree, hf_invalid_data_je, tvb, 0, 1, "Can't find protocol set for this version");
ctx->client_state = ctx->server_state = INVALID;
return;
}
handle(packet_tree, tvb, ctx, ctx->protocol_set->login, is_client);
handle(packet_tree, pinfo, tvb, ctx, ctx->protocol_set->login, is_client);
}

int handle_client_play_switch(tvbuff_t *tvb, mcje_protocol_context *ctx) {
Expand Down Expand Up @@ -294,16 +294,16 @@ int handle_server_play_switch(tvbuff_t *tvb, mcje_protocol_context *ctx) {
return 0;
}

void handle_play(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client) {
void handle_play(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client) {
if (ctx->protocol_set == NULL) {
proto_tree_add_string(packet_tree, hf_invalid_data_je, tvb, 0, 1, "Can't find protocol set for this version");
ctx->client_state = ctx->server_state = INVALID;
return;
}
handle(packet_tree, tvb, ctx, ctx->protocol_set->play, is_client);
handle(packet_tree, pinfo, tvb, ctx, ctx->protocol_set->play, is_client);
}

int handle_client_configuration_switch(tvbuff_t *tvb, mcje_protocol_context *ctx) {
int handle_client_configuration_switch(tvbuff_t *tvb, mcje_protocol_context *ctx) {
if (ctx->protocol_set == NULL) {
ctx->client_state = ctx->server_state = INVALID;
return -1;
Expand Down Expand Up @@ -331,11 +331,11 @@ int handle_server_configuration_switch(tvbuff_t *tvb, mcje_protocol_context *ctx
return 0;
}

void handle_configuration(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client) {
void handle_configuration(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client) {
if (ctx->protocol_set == NULL) {
proto_tree_add_string(packet_tree, hf_invalid_data_je, tvb, 0, 1, "Can't find protocol set for this version");
ctx->client_state = ctx->server_state = INVALID;
return;
}
handle(packet_tree, tvb, ctx, ctx->protocol_set->configuration, is_client);
handle(packet_tree, pinfo, tvb, ctx, ctx->protocol_set->configuration, is_client);
}
10 changes: 5 additions & 5 deletions protocol_je/je_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@

int handle_server_handshake_switch(tvbuff_t *tvb, mcje_protocol_context *ctx);

void handle_server_handshake(proto_tree *packet_tree, tvbuff_t *tvb);
void handle_server_handshake(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb);

void handle_server_slp(proto_tree *packet_tree, tvbuff_t *tvb);

void handle_client_slp(proto_tree *packet_tree, tvbuff_t *tvb);
void handle_client_slp(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb);

int handle_client_login_switch(tvbuff_t *tvb, mcje_protocol_context *ctx);

int handle_server_login_switch(tvbuff_t *tvb, mcje_protocol_context *ctx);

void handle_login(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client);
void handle_login(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client);

int handle_client_play_switch(tvbuff_t *tvb, mcje_protocol_context *ctx);

int handle_server_play_switch(tvbuff_t *tvb, mcje_protocol_context *ctx);

void handle_play(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client);
void handle_play(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client);

int handle_client_configuration_switch(tvbuff_t *tvb, mcje_protocol_context *ctx);

int handle_server_configuration_switch(tvbuff_t *tvb, mcje_protocol_context *ctx);

void handle_configuration(proto_tree *packet_tree, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client);
void handle_configuration(proto_tree *packet_tree, packet_info *pinfo, tvbuff_t *tvb, mcje_protocol_context *ctx, bool is_client);

#endif //MC_DISSECTOR_JE_PROTOCOL_H
2 changes: 1 addition & 1 deletion protocols/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, 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, extra_data *extra, protocol_field field, gint offset, gint remaining, data_recorder recorder, bool is_je)

#define SINGLE_LENGTH_FIELD_MAKE(name, len, func_add, func_parse, record) \
FIELD_MAKE_TREE(name) { \
Expand Down
Loading

0 comments on commit b392640

Please sign in to comment.