From 5c8a81067b40c9d3c85ee96208685ff71759c9cf Mon Sep 17 00:00:00 2001 From: Hugo Camboulive Date: Mon, 26 Jan 2009 00:10:06 +0100 Subject: [PATCH] Add ban structures. --- ban.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ban.h | 20 ++++++++++++++++++ server.c | 23 +++++++++++++++++++++ server.h | 7 ++++++- wscript | 2 +- 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 ban.c create mode 100644 ban.h diff --git a/ban.c b/ban.c new file mode 100644 index 0000000..e4d794d --- /dev/null +++ b/ban.c @@ -0,0 +1,62 @@ +#include "ban.h" + +#include +#include +#include +#include +#include + +/** + * Create and initialize a new ban. + * The ID is assigned only when the ban is added to the server. + * + * @param duration the duration of the ban (0 = unlimited) + * @param ip the ip of the banned player + * @param reason the reason/description of the ban + * + * @return the new ban + */ +struct ban *new_ban(uint16_t duration, struct in_addr ip, char *reason) +{ + struct ban *b = (struct ban *)calloc(sizeof(struct ban), 1); + + b->duration = duration; + b->ip = strdup(inet_ntoa(ip)); + b->reason = strdup(reason); + + return b; +} + +/** + * Return the size a ban will take once converted to + * raw data (to be transmitted). + * + * @param b the ban + * + * @return the number of bytes it will take + */ +int ban_to_data_size(struct ban *b) +{ + return 2 + 15 + 2 + strlen(b->reason) + 1; +} + +/** + * Convert a ban to raw data and put it into dest. + * + * @param b the ban + * @param dest the destination buffer (already allocated) + * + * @return the number of bytes written + */ +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; + + return ban_to_data_size(b); +} diff --git a/ban.h b/ban.h new file mode 100644 index 0000000..b1b0770 --- /dev/null +++ b/ban.h @@ -0,0 +1,20 @@ +#ifndef __BAN_H__ +#define __BAN_H__ + + +#include + +struct ban +{ + uint16_t id; + uint16_t duration; + char *ip; + char *reason; +}; + +struct ban *new_ban(uint16_t duration, struct in_addr ip, char *reason); + +int ban_to_data_size(struct ban *b); +int ban_to_data(struct ban *b, char *dest); + +#endif diff --git a/server.c b/server.c index 4386924..73ac2b7 100644 --- a/server.c +++ b/server.c @@ -16,6 +16,7 @@ struct server *new_server() serv->chans = ar_new(4); serv->players = ar_new(8); + serv->bans = ar_new(4); return serv; } @@ -278,6 +279,28 @@ int move_player(struct player *p, struct channel *to) return 1; } +int add_ban(struct server *s, struct ban *b) +{ + struct ban *tmp_b; + char *used_ids; + int new_id; + + /* Find the next available public ID */ + used_ids = (char *)calloc(s->bans->total_slots, sizeof(char)); + ar_each(struct ban *, tmp_b, s->bans) + used_ids[tmp_b->id - 1] = 1; /* ID start at 1 */ + ar_end_each; + + new_id = 0; + while (used_ids[new_id] == 1) + new_id++; + b->id = new_id + 1; /* ID start at 1 */ + free(used_ids); + + ar_insert(s->bans, (void *)b); + return 1; +} + /** * Prints information about the server (channels, etc) * diff --git a/server.h b/server.h index c68dd21..d295c29 100644 --- a/server.h +++ b/server.h @@ -1,6 +1,7 @@ #ifndef __SERVER_H__ #define __SERVER_H__ +#include "ban.h" #include "channel.h" #include "player.h" #include "array.h" @@ -8,7 +9,8 @@ struct server { struct array *chans; struct array *players; - + struct array *bans; + char password[30]; char server_name[30]; char machine[30]; @@ -34,6 +36,9 @@ int add_player(struct server *serv, struct player *pl); void remove_player(struct server *s, struct player *p); int move_player(struct player *p, struct channel *to); +/* Server - ban functions */ +int add_ban(struct server *s, struct ban *b); + void print_server(struct server *s); #endif diff --git a/wscript b/wscript index ccbbc61..524aba2 100644 --- a/wscript +++ b/wscript @@ -5,7 +5,7 @@ VERSION='0.1' APPNAME='soliloque-server' srcdir = '.' blddir = 'output' -SOURCES='main_serv.c server.c channel.c player.c array.c connection_packet.c crc.c packet_tools.c acknowledge_packet.c control_packet.c strndup.c audio_packet.c' +SOURCES='main_serv.c server.c channel.c player.c array.c connection_packet.c crc.c packet_tools.c acknowledge_packet.c control_packet.c strndup.c audio_packet.c ban.c' def set_options(opt): pass