-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathban.c
62 lines (54 loc) · 1.38 KB
/
ban.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);
}