Skip to content

Commit

Permalink
Add a callback so that players can list current bans. NB : doesn t wo…
Browse files Browse the repository at this point in the history
…rk with TeamSpeex, but the official server doesn t either...
  • Loading branch information
Youx committed Jan 26, 2009
1 parent 82bb63c commit 29fbd6b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 7 deletions.
33 changes: 28 additions & 5 deletions ban.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ struct ban *new_ban(uint16_t duration, struct in_addr ip, char *reason)
return b;
}

/**
* Generate a testing ban
*
* @param x 0 or 1 to get different values
*
* @return the created ban
*/
struct ban *test_ban(int x)
{
struct ban *b = (struct ban *)calloc(sizeof(struct ban), 1);

if (x == 0) {
b->duration = 0;
b->ip = "192.168.100.100";
b->reason = "CBIENFAIT";
} else {
b->duration = 200;
b->ip = "192.168.1.1";
b->reason = "CBIENFAIT caca culotte blablablablabla";
}
return b;
}

/**
* Return the size a ban will take once converted to
* raw data (to be transmitted).
Expand All @@ -37,7 +60,8 @@ struct ban *new_ban(uint16_t duration, struct in_addr ip, char *reason)
*/
int ban_to_data_size(struct ban *b)
{
return 2 + 15 + 2 + strlen(b->reason) + 1;
/* IP (*) + Duration (2) + reason (*) */
return (strlen(b->ip) +1) + 2 + (strlen(b->reason) +1);
}

/**
Expand All @@ -53,10 +77,9 @@ int ban_to_data(struct ban *b, char *dest)
char *ptr;

ptr = dest;
*(uint16_t *)ptr = b->id; ptr += 2; /* ID of ban */
strncpy(ptr, b->ip, 15); ptr += 15; /* IP string */
*(uint16_t *)ptr = b->duration; ptr += 2; /* duration of ban */
strcpy(ptr, b->reason); ptr += strlen(b->reason) + 1;
strcpy(ptr, b->ip); ptr += strlen(b->ip) + 1; /* ip */
*(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);
}
1 change: 1 addition & 0 deletions ban.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct ban
};

struct ban *new_ban(uint16_t duration, struct in_addr ip, char *reason);
struct ban *test_ban(int x);

int ban_to_data_size(struct ban *b);
int ban_to_data(struct ban *b, char *dest);
Expand Down
73 changes: 72 additions & 1 deletion control_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ void s_notify_ban(struct server *s, struct player *pl, struct player *target, ui
* @param data the request packet
* @param len the length of data
* @param cli_addr the address of the sender
* @param cli_len the length of cli_adr
* @param cli_len the length of cli_addr
*/
void *c_req_ban(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len)
{
Expand Down Expand Up @@ -696,3 +696,74 @@ void *c_req_ban(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsi
}
return NULL;
}


/**
* Send the list of bans to a player
*
* @param pl the player who asked for the list of bans
*/
void s_resp_bans(struct server *s, struct player *pl)
{
char *data, *ptr;
int data_size, tmp_size;
struct ban *b;

data_size = 24;
data_size += 4; /* number of bans */

ar_each(struct ban *, b, s->bans)
data_size += ban_to_data_size(b);
ar_end_each;

data = (char *)calloc(data_size, sizeof(char));
ptr = data;

*(uint32_t *)ptr = 0x019bbef0; ptr += 4; /* function code */
*(uint32_t *)ptr = pl->private_id; ptr += 4; /* private ID */
*(uint32_t *)ptr = pl->public_id; ptr += 4; /* public ID */
*(uint32_t *)ptr = pl->f0_s_counter; ptr += 4; /* packet counter */
/* packet version */ ptr += 4; /* filled later */
/* checksum */ ptr += 4; /* filled at the end */
*(uint16_t *)ptr = s->bans->used_slots; ptr += 4; /* number of bans */
printf("number of bans : %i\n", s->bans->used_slots);
ar_each(struct ban *, b, s->bans)
tmp_size = ban_to_data(b, ptr);
ptr += tmp_size;
ar_end_each;

packet_add_crc_d(data, data_size);
printf("list of bans : sending %i bytes\n", data_size);
sendto(socket_desc, data, data_size, 0,
(struct sockaddr *)pl->cli_addr, pl->cli_len);

pl->f0_s_counter++;

free(data);
}

/**
* Handle a player request for the list of bans*
*
* @param data the request packet
* @param len the length of data
* @param cli_addr the address of the sender
* @param cli_len the length of cli_addr
*/
void *c_req_list_bans(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len)
{
uint32_t pub_id, priv_id;
struct player *pl;

memcpy(&priv_id, data + 4, 4);
memcpy(&pub_id, data + 8, 4);

pl = get_player_by_ids(ts_server, pub_id, priv_id);

if (pl != NULL) {
send_acknowledge(pl); /* ACK */
s_resp_bans(ts_server, pl);
}
printf("LIST OF BANS\n");
return NULL;
}
1 change: 1 addition & 0 deletions control_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ void *c_req_kick_channel(char *data, unsigned int len, struct sockaddr_in *cli_a
void *c_req_switch_channel(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len);
void *c_req_delete_channel(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len);
void *c_req_ban(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len);
void *c_req_list_bans(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len);

#endif
5 changes: 4 additions & 1 deletion main_serv.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ static void test_init_server()
add_channel(ts_server, new_predef_channel());
add_channel(ts_server,
new_channel("Name", "Topic", "Desc", CHANNEL_FLAG_DEFAULT, CODEC_SPEEX_16_3, 0, 16));

add_ban(ts_server, test_ban(0));
add_ban(ts_server, test_ban(1));
add_ban(ts_server, test_ban(1));
/* Add players */
/*
new_default_player();
Expand All @@ -56,6 +58,7 @@ static void init_callbacks()
f0_callbacks[0x2d] = &c_req_kick_server; /* client wants to kick someone from the server */
f0_callbacks[0x2e] = &c_req_kick_channel; /* client wants to kick someone from the channel */
f0_callbacks[0x2f] = &c_req_switch_channel; /* client wants to switch channels */
f0_callbacks[0x9a] = &c_req_list_bans; /* client wants the list of bans */
//f4be0300
/* callbacks[0] = myfunc1; ... */
}
Expand Down

0 comments on commit 29fbd6b

Please sign in to comment.