-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathban.c
131 lines (117 loc) · 3.07 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* soliloque-server, an open source implementation of the TeamSpeak protocol.
* Copyright (C) 2009 Hugo Camboulive <hugo.camboulive AT gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ban.h"
#include "log.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
void destroy_ban(struct ban *b)
{
free(b->ip);
free(b->reason);
free(b);
}
/**
* 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(1, sizeof(struct ban));
if (b == NULL) {
logger(LOG_ERR, "new_ban, calloc failed : %s.", strerror(errno));
return NULL;
}
b->duration = duration;
b->ip = strdup(inet_ntoa(ip));
b->reason = strdup(reason);
if (b->ip == NULL || b->reason == NULL) {
if (b->ip != NULL)
free(b->ip);
if (b->reason != NULL)
free(b->reason);
free(b);
return NULL;
}
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(1, sizeof(struct ban));
if (b == NULL) {
logger(LOG_ERR, "test_ban, calloc failed : %s.", strerror(errno));
return NULL;
}
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).
*
* @param b the ban
*
* @return the number of bytes it will take
*/
int ban_to_data_size(struct ban *b)
{
/* IP (*) + Duration (2) + reason (*) */
return (strlen(b->ip) +1) + 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;
strcpy(ptr, b->ip); ptr += strlen(b->ip) + 1; /* ip */
wu16(b->duration, &ptr); /* duration in minutes */
strcpy(ptr, b->reason); ptr += strlen(b->reason) + 1; /* reason */
return ptr - dest;
}