diff --git a/acknowledge_packet.c b/acknowledge_packet.c index 0685898..1f31ea5 100644 --- a/acknowledge_packet.c +++ b/acknowledge_packet.c @@ -51,6 +51,9 @@ void send_acknowledge(struct player *pl) *(uint32_t *)ptr = pl->public_id; ptr+=4; *(uint32_t *)ptr = pl->f1_s_counter; ptr+=4; + /* check we filled the whole packet */ + assert((ptr - data) == data_size); + err = sendto(pl->in_chan->in_server->socket_desc, data, data_size, 0, (struct sockaddr *)pl->cli_addr, pl->cli_len); if (err == -1) { logger(LOG_ERR, "send_acknowledge, sending data failed : %s.", strerror(errno)); diff --git a/audio_packet.c b/audio_packet.c index ae515fb..0104909 100644 --- a/audio_packet.c +++ b/audio_packet.c @@ -31,6 +31,7 @@ #include #include #include +#include /** The size of the raw audio block (in bytes) */ size_t codec_audio_size[13] = {153, 51, 165, 132, 0, 27, 50, 75, 100, 138, 188, 228, 308}; @@ -105,7 +106,9 @@ int audio_received(char *in, size_t len, struct server *s) *(uint32_t *)ptr = sender->public_id; ptr += 4; /* ID of sender */ *(uint16_t *)ptr = *(uint16_t *)(in + 12); ptr += 2; /* conversation counter */ memcpy(ptr, in + 16, audio_block_size); ptr += audio_block_size; - + /* assert we filled the whole packet */ + assert((ptr - data) == data_size); + ar_each(struct player *, tmp_pl, iter, ch_in->players) if (tmp_pl != sender && !ar_has(tmp_pl->muted, sender)) { *(uint32_t *)(data + 4) = tmp_pl->private_id; diff --git a/ban.c b/ban.c index b5f44fb..10d7008 100644 --- a/ban.c +++ b/ban.c @@ -120,5 +120,5 @@ int ban_to_data(struct ban *b, char *dest) *(uint16_t *)ptr = b->duration; ptr += 2; /* duration in minutes */ strcpy(ptr, b->reason); ptr += strlen(b->reason) + 1; /* reason */ - return ban_to_data_size(b); + return ptr - dest; } diff --git a/connection_packet.c b/connection_packet.c index 27730d6..3a1c273 100644 --- a/connection_packet.c +++ b/connection_packet.c @@ -45,9 +45,10 @@ static void server_accept_connection(struct player *pl) { char *data, *ptr; + int data_size = 436; struct server *s = pl->in_chan->in_server; - data = (char *)calloc(436, sizeof(char)); + data = (char *)calloc(data_size, sizeof(char)); if (data == NULL) { logger(LOG_WARN, "server_accept_connection : calloc failed : %s.", strerror(errno)); return; @@ -79,6 +80,9 @@ static void server_accept_connection(struct player *pl) *ptr = MIN(255, strlen(s->welcome_msg)); ptr += 1; /* Length of welcome message */ strncpy(ptr, s->welcome_msg, *(ptr - 1)); ptr += 255; /* Welcome message */ + /* check we filled the whole packet */ + assert((ptr - data) == data_size); + /* Add CRC */ packet_add_crc(data, 436, 16); /* Send packet */ @@ -97,8 +101,9 @@ static void server_accept_connection(struct player *pl) static void server_refuse_connection_ban(struct sockaddr_in *cli_addr, int cli_len, struct server *s) { char *data, *ptr; + int data_size = 436; - data = (char *)calloc(436, sizeof(char)); + data = (char *)calloc(data_size, sizeof(char)); if (data == NULL) { logger(LOG_WARN, "server_refuse_connection : calloc failed : %s.", strerror(errno)); return; @@ -127,6 +132,9 @@ static void server_refuse_connection_ban(struct sockaddr_in *cli_addr, int cli_l /* *ptr = 26;*/ ptr += 1; /* Length of welcome message */ /* memcpy(ptr, "Bienvenue sur mon serveur.", 26);*/ ptr += 255; /* Welcome message */ + /* check we filled the whole packet */ + assert((ptr - data) == data_size); + /* Add CRC */ packet_add_crc(data, 436, 16); /* Send packet */ @@ -213,8 +221,9 @@ void handle_player_connect(char *data, unsigned int len, struct sockaddr_in *cli static void s_resp_keepalive(struct player *pl, uint32_t ka_id) { char *data, *ptr; + int data_size = 24; - data = (char *)calloc(24, sizeof(char)); + data = (char *)calloc(data_size, sizeof(char)); if (data == NULL) { logger(LOG_WARN, "s_resp_keepalive : calloc failed : %s.", strerror(errno)); return; @@ -227,6 +236,10 @@ static void s_resp_keepalive(struct player *pl, uint32_t ka_id) *(uint32_t *)ptr = pl->f4_s_counter; ptr += 4; /* Packet counter */ /* Checksum initialize at the end */ ptr += 4; *(uint32_t *)ptr = ka_id; ptr += 4; /* ID of the keepalive to confirm */ + + /* check we filled the whole packet */ + assert((ptr - data) == data_size); + /* Add CRC */ packet_add_crc(data, 24, 16); diff --git a/database/db_server.c b/database/db_server.c index 8cb1f56..df053b3 100644 --- a/database/db_server.c +++ b/database/db_server.c @@ -50,7 +50,6 @@ struct server **db_create_servers(struct config *c) } if (res) { - i = 0; for (i = 0 ; dbi_result_next_row(res) ; i++) { ss[i] = new_server(); ss[i]->conf = c; diff --git a/main_serv.c b/main_serv.c index 346231e..429fb7b 100644 --- a/main_serv.c +++ b/main_serv.c @@ -263,6 +263,7 @@ int main(int argc, char **argv) print_help(argv[0]); terminate = 1; helpshown = 1; + break; case 'v': print_version(); terminate = 1; diff --git a/player.c b/player.c index a35644a..7fd8946 100644 --- a/player.c +++ b/player.c @@ -27,6 +27,7 @@ #include #include #include +#include /** @@ -141,6 +142,9 @@ struct player *new_player_from_data(char *data, int len, struct sockaddr_in *cli password = strndup(ptr, tmp_size); ptr += 29; /* password */ tmp_size = MIN(29, *ptr); ptr += 1; /* size of nickname */ nickname = strndup(ptr, tmp_size); ptr += 29; /* nickname */ + + /* check we filled the whole data */ + assert(ptr - data == len); /* Initialize player */ pl = new_player(nickname, login, machine);