Skip to content

Commit

Permalink
Add ban structures.
Browse files Browse the repository at this point in the history
  • Loading branch information
Youx committed Jan 25, 2009
1 parent 05629db commit 5c8a810
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
62 changes: 62 additions & 0 deletions ban.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "ban.h"

#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/**
* 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);
}
20 changes: 20 additions & 0 deletions ban.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __BAN_H__
#define __BAN_H__


#include <netinet/in.h>

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
23 changes: 23 additions & 0 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
*
Expand Down
7 changes: 6 additions & 1 deletion server.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#ifndef __SERVER_H__
#define __SERVER_H__

#include "ban.h"
#include "channel.h"
#include "player.h"
#include "array.h"

struct server {
struct array *chans;
struct array *players;

struct array *bans;

char password[30];
char server_name[30];
char machine[30];
Expand All @@ -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
2 changes: 1 addition & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c8a810

Please sign in to comment.