1 
2 #include "g_local.h"
3 #include "bot.h"
4 
5 
6 
7 /*
8 ==============================================================================
9 
10 PACKET FILTERING
11 
12 
13 You can add or remove addresses from the filter list with:
14 
15 addip <ip>
16 removeip <ip>
17 
18 The ip address is specified in dot format, and any unspecified digits will match any value, so you can specify an entire class C network with "addip 192.246.40".
19 
20 Removeip will only remove an address specified exactly the same way.  You cannot addip a subnet, then removeip a single host.
21 
22 listip
23 Prints the current list of filters.
24 
25 writeip
26 Dumps "addip <ip>" commands to listip.cfg so it can be execed at a later date.  The filter lists are not saved and restored by default, because I beleive it would cause too much confusion.
27 
28 filterban <0 or 1>
29 
30 If 1 (the default), then ip addresses matching the current list will be prohibited from entering the game.  This is the default setting.
31 
32 If 0, then only addresses matching the list will be allowed.  This lets you easily set up a private game, or a game that only allows players from your local network.
33 
34 
35 ==============================================================================
36 */
37 
38 typedef struct
39 {
40 	unsigned	mask;
41 	unsigned	compare;
42 } ipfilter_t;
43 
44 #define	MAX_IPFILTERS	1024
45 
46 ipfilter_t	ipfilters[MAX_IPFILTERS];
47 int			numipfilters;
48 
49 /*
50 =================
51 StringToFilter
52 =================
53 */
StringToFilter(char * s,ipfilter_t * f)54 static qboolean StringToFilter (char *s, ipfilter_t *f)
55 {
56 	char	num[128];
57 	int		i, j;
58 	byte	b[4];
59 	byte	m[4];
60 
61 	for (i=0 ; i<4 ; i++)
62 	{
63 		b[i] = 0;
64 		m[i] = 0;
65 	}
66 
67 	for (i=0 ; i<4 ; i++)
68 	{
69 		if (*s < '0' || *s > '9')
70 		{
71 			gi.cprintf(NULL, PRINT_HIGH, "Bad filter address: %s\n", s);
72 			return false;
73 		}
74 
75 		j = 0;
76 		while (*s >= '0' && *s <= '9')
77 		{
78 			num[j++] = *s++;
79 		}
80 		num[j] = 0;
81 		b[i] = atoi(num);
82 		if (b[i] != 0)
83 			m[i] = 255;
84 
85 		if (!*s)
86 			break;
87 		s++;
88 	}
89 
90 	f->mask = *(unsigned *)m;
91 	f->compare = *(unsigned *)b;
92 
93 	return true;
94 }
95 
96 /*
97 =================
98 SV_FilterPacket
99 =================
100 */
SV_FilterPacket(char * from)101 qboolean SV_FilterPacket (char *from)
102 {
103 	int		i;
104 	unsigned	in;
105 	byte m[4];
106 	char *p;
107 
108 	i = 0;
109 	p = from;
110 	while (*p && i < 4) {
111 		m[i] = 0;
112 		while (*p >= '0' && *p <= '9') {
113 			m[i] = m[i]*10 + (*p - '0');
114 			p++;
115 		}
116 		if (!*p || *p == ':')
117 			break;
118 		i++, p++;
119 	}
120 
121 	in = *(unsigned *)m;
122 
123 	for (i=0 ; i<numipfilters ; i++)
124 		if ( (in & ipfilters[i].mask) == ipfilters[i].compare)
125 			return (int)filterban->value;
126 
127 	return (int)!filterban->value;
128 }
129 
130 
131 /*
132 =================
133 SV_AddIP_f
134 =================
135 */
SVCmd_AddIP_f(void)136 void SVCmd_AddIP_f (void)
137 {
138 	int		i;
139 
140 	if (gi.argc() < 3) {
141 		gi.cprintf(NULL, PRINT_HIGH, "Usage:  addip <ip-mask>\n");
142 		return;
143 	}
144 
145 	for (i=0 ; i<numipfilters ; i++)
146 		if (ipfilters[i].compare == 0xffffffff)
147 			break;		// free spot
148 	if (i == numipfilters)
149 	{
150 		if (numipfilters == MAX_IPFILTERS)
151 		{
152 			gi.cprintf (NULL, PRINT_HIGH, "IP filter list is full\n");
153 			return;
154 		}
155 		numipfilters++;
156 	}
157 
158 	if (!StringToFilter (gi.argv(2), &ipfilters[i]))
159 		ipfilters[i].compare = 0xffffffff;
160 }
161 
162 /*
163 =================
164 SV_RemoveIP_f
165 =================
166 */
SVCmd_RemoveIP_f(void)167 void SVCmd_RemoveIP_f (void)
168 {
169 	ipfilter_t	f;
170 	int			i, j;
171 
172 	if (gi.argc() < 3) {
173 		gi.cprintf(NULL, PRINT_HIGH, "Usage:  sv removeip <ip-mask>\n");
174 		return;
175 	}
176 
177 	if (!StringToFilter (gi.argv(2), &f))
178 		return;
179 
180 	for (i=0 ; i<numipfilters ; i++)
181 		if (ipfilters[i].mask == f.mask
182 		&& ipfilters[i].compare == f.compare)
183 		{
184 			for (j=i+1 ; j<numipfilters ; j++)
185 				ipfilters[j-1] = ipfilters[j];
186 			numipfilters--;
187 			gi.cprintf (NULL, PRINT_HIGH, "Removed.\n");
188 			return;
189 		}
190 	gi.cprintf (NULL, PRINT_HIGH, "Didn't find %s.\n", gi.argv(2));
191 }
192 
193 /*
194 =================
195 SV_ListIP_f
196 =================
197 */
SVCmd_ListIP_f(void)198 void SVCmd_ListIP_f (void)
199 {
200 	int		i;
201 	byte	b[4];
202 
203 	gi.cprintf (NULL, PRINT_HIGH, "Filter list:\n");
204 	for (i=0 ; i<numipfilters ; i++)
205 	{
206 		*(unsigned *)b = ipfilters[i].compare;
207 		gi.cprintf (NULL, PRINT_HIGH, "%3i.%3i.%3i.%3i\n", b[0], b[1], b[2], b[3]);
208 	}
209 }
210 
211 /*
212 =================
213 SV_WriteIP_f
214 =================
215 */
SVCmd_WriteIP_f(void)216 void SVCmd_WriteIP_f (void)
217 {
218 	FILE	*f;
219 	char	name[MAX_OSPATH];
220 	byte	b[4];
221 	int		i;
222 	cvar_t	*game;
223 
224 	game = gi.cvar("game", "", 0);
225 
226 	if (!*game->string)
227 		sprintf (name, "%s/listip.cfg", GAMEVERSION);
228 	else
229 		sprintf (name, "%s/listip.cfg", game->string);
230 
231 	gi.cprintf (NULL, PRINT_HIGH, "Writing %s.\n", name);
232 
233 	f = fopen (name, "wb");
234 	if (!f)
235 	{
236 		gi.cprintf (NULL, PRINT_HIGH, "Couldn't open %s\n", name);
237 		return;
238 	}
239 
240 	fprintf(f, "set filterban %d\n", (int)filterban->value);
241 
242 	for (i=0 ; i<numipfilters ; i++)
243 	{
244 		*(unsigned *)b = ipfilters[i].compare;
245 		fprintf (f, "sv addip %i.%i.%i.%i\n", b[0], b[1], b[2], b[3]);
246 	}
247 
248 	fclose (f);
249 }
250 
251 
252 
253 
254 
255 //���[�g�C��
256 //�m�[�}���|�b�h�͑S�Đ؂�̂�
Move_LastRouteIndex()257 void Move_LastRouteIndex()
258 {
259 	int	i;
260 
261 	for(i = CurrentIndex - 1 ; i >= 0;i--)
262 	{
263 		if(Route[i].state) break;
264 		else if(!Route[i].index) break;
265 	}
266 	if(!CurrentIndex || !Route[i].index) CurrentIndex = i;
267 	else CurrentIndex = i + 1;
268 
269 	if(CurrentIndex < MAXNODES)
270 	{
271 		memset(&Route[CurrentIndex],0,sizeof(route_t));
272 		if(CurrentIndex > 0) Route[CurrentIndex].index = Route[CurrentIndex - 1].index + 1;
273 	}
274 }
275 
276 //����t���ɕϊ�����
RouteTreepointSet()277 void	RouteTreepointSet()
278 {
279 	int	i;
280 
281 	for(i = 0;i < CurrentIndex;i++)
282 	{
283 		if(Route[i].state == GRS_NORMAL)
284 		{
285 
286 
287 		}
288 	}
289 }
290 
291 
292 
293 
Svcmd_Test_f(void)294 void	Svcmd_Test_f (void)
295 {
296 	gi.cprintf (NULL, PRINT_HIGH, "Svcmd_Test_f()\n");
297 }
298 
299 //chain�t�@�C���̃Z�[�u
SaveChain()300 void SaveChain()
301 {
302 	char name[256];
303 	FILE *fpout;
304 	unsigned int size;
305 
306 	if(!chedit->value)
307 	{
308 		gi.cprintf (NULL, PRINT_HIGH, "Not a chaining mode.\n");
309 		return;
310 	}
311 
312 	//�Ƃ肠����CTF����
313 	if(ctf->value) 	sprintf(name,"%s/chctf/%s.chf",gamepath->string,level.mapname);
314 	else 	sprintf(name,"%s/chdtm/%s.chn",gamepath->string,level.mapname);
315 
316 	fpout = fopen(name,"wb");
317 	if(fpout == NULL) gi.cprintf(NULL,PRINT_HIGH,"Can't open %s\n",name);
318 	else
319 	{
320 		if(!ctf->value)	fwrite("3ZBRGDTM",sizeof(char),8,fpout);
321 		else fwrite("3ZBRGCTF",sizeof(char),8,fpout);
322 
323 		fwrite(&CurrentIndex,sizeof(int),1,fpout);
324 
325 		size = (unsigned int)CurrentIndex * sizeof(route_t);
326 
327 		fwrite(Route,size,1,fpout);
328 
329 		gi.cprintf (NULL, PRINT_HIGH,"%s Saving done.\n",name);
330 		fclose(fpout);
331 	}
332 }
333 //Spawn Command
SpawnCommand(int i)334 void SpawnCommand(int i)
335 {
336 	int	j;
337 
338 	if(chedit->value){ gi.cprintf(NULL,PRINT_HIGH,"Can't spawn.");return;}
339 
340 	if(i <= 0) {gi.cprintf(NULL,PRINT_HIGH,"Specify num of bots.");return;}
341 
342 	for(j = 0;j < i;j++)
343 	{
344 		SpawnBotReserving();
345 	}
346 }
347 
348 //Random Spawn Command
349 
RandomSpawnCommand(int i)350 void RandomSpawnCommand(int i)
351 {
352 	int	j,k,red = 0,blue = 0;
353 
354 	edict_t	*e;
355 
356 	if(chedit->value){ gi.cprintf(NULL,PRINT_HIGH,"Can't spawn.");return;}
357 
358 	if(i <= 0) {gi.cprintf(NULL,PRINT_HIGH,"Specify num of bots.");return;}
359 
360 	//count current teams
361 	for ( k = 1 ; k <= maxclients->value ; k++)
362 	{
363 		e = &g_edicts[k];
364 		if(e->inuse && e->client)
365 		{
366 			if(e->client->resp.ctf_team == CTF_TEAM1) red++;
367 			else if(e->client->resp.ctf_team == CTF_TEAM2) blue++;
368 		}
369 	}
370 
371 	for(j = 0;j < i;j++)
372 	{
373 		SpawnBotReserving2(&red,&blue);
374 //gi.cprintf(NULL,PRINT_HIGH,"R B %i %i\n",red,blue);
375 	}
376 }
377 
378 //Remove Command
RemoveCommand(int i)379 void RemoveCommand(int i)
380 {
381 	int	j;
382 
383 	if(i <= 0) i = 1;//gi.cprintf(NULL,PRINT_HIGH,"Specify num of bots.");
384 
385 
386 	for(j = 0;j < i;j++)
387 	{
388 		RemoveBot();
389 	}
390 }
391 
392 //Debug Spawn Command
DebugSpawnCommand(int i)393 void DebugSpawnCommand(int i)
394 {
395 	if(!chedit->value) {gi.cprintf(NULL,PRINT_HIGH,"Can't debug.");return;}
396 
397 	if(targetindex) {gi.cprintf(NULL,PRINT_HIGH,"Now debugging.");return;}
398 
399 	if(i < 1) i = 1;
400 
401 	targetindex = i;
402 
403 	SpawnBotReserving();
404 }
405 
406 
407 /*
408 =================
409 ServerCommand
410 
411 ServerCommand will be called when an "sv" command is issued.
412 The game can issue gi.argc() / gi.argv() commands to get the rest
413 of the parameters
414 =================
415 */
ServerCommand(void)416 void	ServerCommand (void)
417 {
418 	char	*cmd;
419 
420 	cmd = gi.argv(1);
421 	if (Q_stricmp (cmd, "test") == 0)
422 		Svcmd_Test_f ();
423 	else if (Q_stricmp (cmd, "savechain") == 0)
424 		SaveChain ();
425 	else if (Q_stricmp (cmd, "spb") == 0)
426 	{
427 		if(gi.argc() <= 1) SpawnCommand(1);
428 		else SpawnCommand (atoi(gi.argv(2)));
429 	}
430 	else if (Q_stricmp (cmd, "rspb") == 0)
431 	{
432 		if(gi.argc() <= 1) RandomSpawnCommand(1);
433 		else RandomSpawnCommand (atoi(gi.argv(2)));
434 	}
435 	else if (Q_stricmp (cmd, "rmb") == 0)
436 	{
437 		if(gi.argc() <= 1) RemoveCommand(1);
438 		else RemoveCommand (atoi(gi.argv(2)));
439 	}
440 	else if (Q_stricmp (cmd, "dsp") == 0)
441 	{
442 		if(gi.argc() <= 1) DebugSpawnCommand(1);
443 		else DebugSpawnCommand (atoi(gi.argv(2)));
444 	}
445 	else if (Q_stricmp (cmd, "addip") == 0)
446 		SVCmd_AddIP_f ();
447 	else if (Q_stricmp (cmd, "removeip") == 0)
448 		SVCmd_RemoveIP_f ();
449 	else if (Q_stricmp (cmd, "listip") == 0)
450 		SVCmd_ListIP_f ();
451 	else if (Q_stricmp (cmd, "writeip") == 0)
452 		SVCmd_WriteIP_f ();
453 	else
454 		gi.cprintf (NULL, PRINT_HIGH, "Unknown server command \"%s\"\n", cmd);
455 }
456 
457