Commit f7e6b190 authored by Simon Wüllhorst's avatar Simon Wüllhorst

Ensure received packet has at least the size of the promoted type

parent ada77ebc
......@@ -26,6 +26,8 @@
#define MSG_HEARTBEAT_ACK 7
#define MSG_UNKNOWN_MSG_TYPE 8
#define MSG_MAX_TYPE_ID 8
#define LOCAL_START 1
#define REMOTE_START 0
......@@ -134,6 +136,7 @@ struct game_peers_info_t {
struct game_timer_t timer;
struct message_buffer_t message;
struct credentials_t credentials;
uint16_t msg_min_size[MSG_MAX_TYPE_ID];
};
typedef struct game_peers_info_t game_peers_info;
......@@ -155,6 +158,7 @@ struct game_server_info_t {
int server_fd;
struct client_info_t client_queue;
struct credentials_t credentials;
uint16_t msg_min_size[MSG_MAX_TYPE_ID];
};
typedef struct game_server_info_t game_server_info;
......
......@@ -296,6 +296,7 @@ void handle_rec_msg(game_peers_info* peers_info, struct msg_meta_t* meta_data)
void handle_rec_udp(game_peers_info* peers_info)
{
ssize_t n;
uint16_t msg_len;
struct msg_meta_t* meta_data;
//printf("GOT MESSAGE\n");
......@@ -313,7 +314,8 @@ void handle_rec_udp(game_peers_info* peers_info)
meta_data = (struct msg_meta_t*)peers_info->message.buf;
meta_data->length = ntohs(meta_data->length);
meta_data->type = ntohs(meta_data->type);
if (n != meta_data->length + (ssize_t)sizeof(struct msg_meta_t) + (4 - meta_data->length % 4) % 4) {
msg_len = meta_data->length + (ssize_t)sizeof(struct msg_meta_t) + (4 - meta_data->length % 4) % 4;
if (n < peers_info->msg_min_size[meta_data->type] || n != msg_len) {
send_primitive_message(peers_info, MSG_UNKNOWN_MSG_TYPE);
return;
}
......@@ -339,6 +341,8 @@ ssize_t packet_sequenzer(game_peers_info* peers_info, ssize_t n)
return len;
handle_rec_msg(peers_info, a_msg);
// Workaround for "fd problem"
if (peers_info->net_fd == -1) {
//tcp session has been finalized
init_4gw(peers_info);
......@@ -517,10 +521,25 @@ int init_4gw(game_peers_info* peers_info)
return 0;
}
void init_min_msg_size(uint16_t* field)
{
field[MSG_REG] = (uint16_t)sizeof(msg_reg);
field[MSG_REG_ACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_REG_NACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_PEER_INFO] = (uint16_t)sizeof(msg_peer_info);
field[MSG_PEER_INFO_ACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_MOVE] = (uint16_t)sizeof(msg_move);
field[MSG_MOVE_ACK] = (uint16_t)sizeof(msg_move_ack);
field[MSG_HEARTBEAT] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_HEARTBEAT_ACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_UNKNOWN_MSG_TYPE] = (uint16_t)sizeof(struct msg_meta_t);
}
int main(int argc, char** argv)
{
game_peers_info peers_info;
memset(&peers_info, 0, sizeof(peers_info));
init_min_msg_size(peers_info.msg_min_size);
init_4clib();
init_cblib();
peers_info.state.current_mode = parse_args(argc, argv, &peers_info);
......
......@@ -208,7 +208,7 @@ ssize_t packet_sequenzer(client_info* a_client, ssize_t n)
a_msg->type = ntohs(a_msg->type);
msg_len = sizeof(struct msg_meta_t) + a_msg->length + (4 - a_msg->length % 4) % 4;
if (len < msg_len)
if (a_msg->length < a_client->server_info->msg_min_size[a_msg->type] || len != msg_len)
return len;
handle_client_message(a_client, a_msg);
......@@ -264,10 +264,26 @@ int init_gs(game_server_info* server_info)
handle_events();
return 0;
}
void init_min_msg_size(uint16_t* field)
{
field[MSG_REG] = (uint16_t)sizeof(msg_reg);
field[MSG_REG_ACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_REG_NACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_PEER_INFO] = (uint16_t)sizeof(msg_peer_info);
field[MSG_PEER_INFO_ACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_MOVE] = (uint16_t)sizeof(msg_move);
field[MSG_MOVE_ACK] = (uint16_t)sizeof(msg_move_ack);
field[MSG_HEARTBEAT] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_HEARTBEAT_ACK] = (uint16_t)sizeof(struct msg_meta_t);
field[MSG_UNKNOWN_MSG_TYPE] = (uint16_t)sizeof(struct msg_meta_t);
}
int main(int argc, char** argv)
{
int val;
game_server_info server_info;
init_min_msg_size(server_info.msg_min_size);
if (parse_args(argc, argv, &server_info) != 0)
return -1;
val = init_gs(&server_info);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment