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 | #include "g_local.h" |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | typedef struct { |
53 | unsigned mask; |
54 | unsigned compare; |
55 | } ipfilter_t; |
56 | |
57 | #define MAX_IPFILTERS1024 1024 |
58 | |
59 | static ipfilter_t ipfilters[MAX_IPFILTERS1024]; |
60 | static int numipfilters; |
61 | |
62 | static bool StringToFilter (const char *s, ipfilter_t * f) |
63 | { |
64 | char num[128]; |
65 | int i, j; |
66 | byte b[4]; |
67 | byte m[4]; |
68 | |
69 | for (i = 0; i < 4; i++) { |
70 | b[i] = 0; |
71 | m[i] = 0; |
72 | } |
73 | |
74 | for (i = 0; i < 4; i++) { |
75 | if (*s < '0' || *s > '9') { |
76 | G_ClientPrintf(NULL__null, PRINT_CONSOLE2, "Bad filter address: %s\n", s); |
77 | return false; |
78 | } |
79 | |
80 | j = 0; |
81 | while (isdigit(*s)) { |
82 | num[j++] = *s++; |
83 | } |
84 | num[j] = 0; |
85 | b[i] = atoi(num); |
86 | if (b[i] != 0) |
87 | m[i] = 0xFF; |
88 | |
89 | if (!*s) |
90 | break; |
91 | s++; |
92 | } |
93 | |
94 | f->mask = *(unsigned *) m; |
95 | f->compare = *(unsigned *) b; |
96 | |
97 | return true; |
98 | } |
99 | |
100 | bool SV_FilterPacket (const char *from) |
101 | { |
102 | int i; |
103 | unsigned in; |
104 | byte m[4]; |
105 | const char *p; |
106 | |
107 | i = 0; |
108 | p = from; |
109 | while (*p && i < 4) { |
110 | m[i] = 0; |
111 | while (*p >= '0' && *p <= '9') { |
112 | m[i] = m[i] * 10 + (*p - '0'); |
113 | p++; |
114 | } |
115 | if (!*p || *p == ':') |
116 | break; |
117 | i++, p++; |
118 | } |
119 | |
120 | in = *(unsigned *) m; |
121 | |
122 | for (i = 0; i < numipfilters; i++) |
123 | if ((in & ipfilters[i].mask) == ipfilters[i].compare) |
124 | return sv_filterban->integer; |
125 | |
126 | return !sv_filterban->integer; |
127 | } |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | static void SVCmd_AddIP_f (void) |
134 | { |
135 | int i; |
136 | |
137 | if (gi.Cmd_Argc() < 3) { |
138 | gi.DPrintf("Usage: %s <ip-mask>\n", gi.Cmd_Argv(1)); |
139 | return; |
140 | } |
141 | |
142 | for (i = 0; i < numipfilters; i++) |
143 | if (ipfilters[i].compare == ~0x0) |
144 | break; |
145 | if (i == numipfilters) { |
146 | if (numipfilters == MAX_IPFILTERS1024) { |
147 | gi.DPrintf("IP filter list is full\n"); |
148 | return; |
149 | } |
150 | numipfilters++; |
151 | } |
152 | |
153 | if (!StringToFilter(gi.Cmd_Argv(2), &ipfilters[i])) |
154 | ipfilters[i].compare = ~0x0; |
155 | } |
156 | |
157 | |
158 | |
159 | |
160 | static void SVCmd_RemoveIP_f (void) |
161 | { |
162 | ipfilter_t f; |
163 | int i, j; |
164 | |
165 | if (gi.Cmd_Argc() < 3) { |
166 | gi.DPrintf("Usage: %s <ip-mask>\n", gi.Cmd_Argv(1)); |
167 | return; |
168 | } |
169 | |
170 | if (!StringToFilter(gi.Cmd_Argv(2), &f)) |
171 | return; |
172 | |
173 | for (i = 0; i < numipfilters; i++) |
174 | if (ipfilters[i].mask == f.mask && ipfilters[i].compare == f.compare) { |
175 | for (j = i + 1; j < numipfilters; j++) |
176 | ipfilters[j - 1] = ipfilters[j]; |
177 | numipfilters--; |
178 | gi.DPrintf("Removed.\n"); |
179 | return; |
180 | } |
181 | gi.DPrintf("Didn't find %s.\n", gi.Cmd_Argv(2)); |
182 | } |
183 | |
184 | |
185 | |
186 | |
187 | static void SVCmd_ListIP_f (void) |
188 | { |
189 | int i; |
190 | byte b[4]; |
191 | |
192 | gi.DPrintf("Filter list:\n"); |
193 | for (i = 0; i < numipfilters; i++) { |
194 | *(unsigned *) b = ipfilters[i].compare; |
195 | gi.DPrintf("%3i.%3i.%3i.%3i\n", b[0], b[1], b[2], b[3]); |
196 | } |
197 | } |
198 | |
199 | |
200 | |
201 | |
202 | static void SVCmd_WriteIP_f (void) |
203 | { |
204 | FILE *f; |
205 | char name[MAX_OSPATH256]; |
206 | byte b[4]; |
207 | int i; |
208 | |
209 | Com_sprintf(name, sizeof(name), "%s/listip.cfg", gi.FS_Gamedir()); |
210 | |
211 | gi.DPrintf("Writing %s.\n", name); |
212 | |
213 | f = fopen(name, "wb"); |
214 | if (!f) { |
| |
215 | gi.DPrintf("Couldn't open %s\n", name); |
216 | return; |
217 | } |
218 | |
219 | fprintf(f, "set sv_filterban %d\n", sv_filterban->integer); |
220 | |
221 | for (i = 0; i < numipfilters; i++) { |
| 7 | Loop condition is true. Entering loop body |
|
222 | *(unsigned *) b = ipfilters[i].compare; |
223 | fprintf(f, "sv addip %i.%i.%i.%i\n", b[0], b[1], b[2], b[3]); |
| 8 | Function call argument is an uninitialized value |
|
224 | } |
225 | |
226 | fclose(f); |
227 | } |
228 | |
229 | |
230 | |
231 | |
232 | |
233 | |
234 | static void SVCmd_AI_Add_f (void) |
235 | { |
236 | int team; |
237 | |
238 | if (gi.Cmd_Argc() < 3) { |
239 | gi.DPrintf("Usage: %s <teamnum>\n", gi.Cmd_Argv(1)); |
240 | return; |
241 | } |
242 | team = atoi(gi.Cmd_Argv(2)); |
243 | if (team > TEAM_CIVILIAN0 && team < MAX_TEAMS8) { |
244 | if (!AI_CreatePlayer(team)) |
245 | gi.DPrintf("Couldn't create AI player.\n"); |
246 | } else |
247 | gi.DPrintf("Bad team number.\n"); |
248 | } |
249 | |
250 | |
251 | |
252 | |
253 | |
254 | |
255 | |
256 | static void SVCmd_Win_f (void) |
257 | { |
258 | int team; |
259 | |
260 | if (gi.Cmd_Argc() < 3) { |
261 | gi.DPrintf("Usage: %s <teamnum>\n", gi.Cmd_Argv(1)); |
262 | return; |
263 | } |
264 | team = atoi(gi.Cmd_Argv(2)); |
265 | if (team > TEAM_CIVILIAN0 && team < MAX_TEAMS8) |
266 | G_MatchEndTrigger(team, 0); |
267 | else |
268 | gi.DPrintf("Bad team number.\n"); |
269 | } |
270 | |
271 | #ifdef DEBUG1 |
272 | static void SVCmd_ShowAll_f (void) |
273 | { |
274 | edict_t *ent = NULL__null; |
275 | |
276 | |
277 | while ((ent = G_EdictsGetNextInUse(ent))) { |
278 | G_AppearPerishEvent(~G_VisToPM(ent->visflags), 1, ent, NULL__null); |
279 | G_VisFlagsAdd(ent, ~ent->visflags); |
280 | } |
281 | gi.DPrintf("All items and creatures revealed to all sides\n"); |
282 | } |
283 | |
284 | static void SVCmd_AddItem_f (void) |
285 | { |
286 | const int team = TEAM_DEFAULT1; |
287 | edict_t *ent = G_EdictsGetNextLivingActorOfTeam(NULL__null, team); |
288 | |
289 | if (gi.Cmd_Argc() < 3) { |
290 | gi.DPrintf("Usage: %s <item-id>\n", gi.Cmd_Argv(1)); |
291 | return; |
292 | } |
293 | |
294 | if (!ent) { |
295 | gi.DPrintf("Could not add item, no living members of team %i left\n", team); |
296 | return; |
297 | } |
298 | |
299 | G_AddItemToFloor(ent->pos, gi.Cmd_Argv(2)); |
300 | } |
301 | |
302 | |
303 | |
304 | |
305 | static void SVCmd_ActorInvList_f (void) |
306 | { |
307 | player_t* player; |
308 | int i; |
309 | |
310 | |
311 | for (i = 0, player = game.players; i < game.sv_maxplayersperteam * 2; i++, player++) { |
312 | if (!player->inuse) |
313 | continue; |
314 | G_InvList_f(player); |
315 | } |
316 | } |
317 | |
318 | static void SVCmd_ListEdicts_f (void) |
319 | { |
320 | edict_t *ent = NULL__null; |
321 | int i = 0; |
322 | Com_Printf("number | entnum | mapnum | type | inuse | pnum | team | size | HP | state | classname | model/ptl | pos\n"); |
323 | while ((ent = G_EdictsGetNext(ent))) { |
324 | char buf[128]; |
325 | const char *model; |
326 | if (ent->type == ET_PARTICLE) |
327 | model = ent->particle; |
328 | else if (ent->model) |
329 | model = ent->model; |
330 | else |
331 | model = "no mdl"; |
332 | Com_sprintf(buf, sizeof(buf), "#%5i | #%5i | #%5i | %4i | %5i | %4i | %4i | %4i | %3i | %5i | %14s | %21s | %i:%i:%i", |
333 | i, ent->number, ent->mapNum, ent->type, ent->inuse, ent->pnum, ent->team, ent->fieldSize, |
334 | ent->HP, ent->state, ent->classname, model, ent->pos[0], ent->pos[1], ent->pos[2]); |
335 | Com_Printf("%s\n", buf); |
336 | i++; |
337 | } |
338 | } |
339 | #endif |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | |
347 | void G_ServerCommand (void) |
348 | { |
349 | const char *cmd; |
350 | |
351 | cmd = gi.Cmd_Argv(1); |
352 | if (Q_strcasecmp(cmd, "addip")strcasecmp((cmd), ("addip")) == 0) |
| |
353 | SVCmd_AddIP_f(); |
354 | else if (Q_strcasecmp(cmd, "removeip")strcasecmp((cmd), ("removeip")) == 0) |
| |
355 | SVCmd_RemoveIP_f(); |
356 | else if (Q_strcasecmp(cmd, "listip")strcasecmp((cmd), ("listip")) == 0) |
| |
357 | SVCmd_ListIP_f(); |
358 | else if (Q_strcasecmp(cmd, "writeip")strcasecmp((cmd), ("writeip")) == 0) |
| |
359 | SVCmd_WriteIP_f(); |
| 5 | Calling 'SVCmd_WriteIP_f' |
|
360 | else if (Q_strcasecmp(cmd, "ai_add")strcasecmp((cmd), ("ai_add")) == 0) |
361 | SVCmd_AI_Add_f(); |
362 | else if (Q_strcasecmp(cmd, "win")strcasecmp((cmd), ("win")) == 0) |
363 | SVCmd_Win_f(); |
364 | #ifdef DEBUG1 |
365 | else if (Q_strcasecmp(cmd, "debug_showall")strcasecmp((cmd), ("debug_showall")) == 0) |
366 | SVCmd_ShowAll_f(); |
367 | else if (Q_strcasecmp(cmd, "debug_additem")strcasecmp((cmd), ("debug_additem")) == 0) |
368 | SVCmd_AddItem_f(); |
369 | else if (Q_strcasecmp(cmd, "debug_actorinvlist")strcasecmp((cmd), ("debug_actorinvlist")) == 0) |
370 | SVCmd_ActorInvList_f(); |
371 | else if (Q_strcasecmp(cmd, "debug_listedicts")strcasecmp((cmd), ("debug_listedicts")) == 0) |
372 | SVCmd_ListEdicts_f(); |
373 | #endif |
374 | else |
375 | gi.DPrintf("Unknown server command \"%s\"\n", cmd); |
376 | } |