aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/game/GameSVCmds.java
blob: 6d8b623a0740e62a27074a8272839efb7548a12c (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright (C) 1997-2001 Id Software, Inc.

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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

// Created on 01.02.2004 by RST.
// $Id: GameSVCmds.java,v 1.1.1.1.2.1 2004-07-09 08:38:29 hzi Exp $

package jake2.game;

import jake2.qcommon.Com;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.StringTokenizer;

public class GameSVCmds extends GameSpawn {

	public static void Svcmd_Test_f() {
		gi.cprintf(null, PRINT_HIGH, "Svcmd_Test_f()\n");
	}

	/*
	==============================================================================
	
	PACKET FILTERING
	 
	
	You can add or remove addresses from the filter list with:
	
	addip <ip>
	removeip <ip>
	
	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".
	
	Removeip will only remove an address specified exactly the same way.  You cannot addip a subnet, then removeip a single host.
	
	listip
	Prints the current list of filters.
	
	writeip
	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.
	
	filterban <0 or 1>
	
	If 1 (the default), then ip addresses matching the current list will be prohibited from entering the game.  This is the default setting.
	
	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.
	
	
	==============================================================================
	*/

	public static class ipfilter_t {
		int mask;
		int compare;
	};

	public static final int MAX_IPFILTERS = 1024;

	static ipfilter_t ipfilters[] = new ipfilter_t[MAX_IPFILTERS];
	static {
		for (int n = 0; n < MAX_IPFILTERS; n++)
			ipfilters[n] = new ipfilter_t();
	}

	static int numipfilters;

	/*
	=================
	StringToFilter
	=================
	*/
	static boolean StringToFilter(String s, ipfilter_t f) {
		//char num[128];
		String num;
		int i, j;
		byte b[] = { 0, 0, 0, 0 };
		byte m[] = { 0, 0, 0, 0 };

		try {
			StringTokenizer tk = new StringTokenizer(s, ". ");

			for (int n = 0; n < 4; n++) {
				b[n] = (byte) atoi(tk.nextToken());
				if (b[n] != 0)
					m[n] = -1;
			}

			f.mask = java.nio.ByteBuffer.wrap(m).getInt();
			f.compare = java.nio.ByteBuffer.wrap(b).getInt();
		}
		catch (Exception e) {
			gi.cprintf(null, PRINT_HIGH, "Bad filter address: " + s + "\n");
			return false;
		}

		return true;
	}

	/*
	=================
	SV_FilterPacket
	=================
	*/
	static boolean SV_FilterPacket(String from) {
		int i;
		int in;
		int m[] = {0,0,0,0};
		
		int p=0;
		char c;

		i = 0;
	
		while (p < from.length() && i < 4) {
			m[i] = 0;
			
			
			c = from.charAt(p);
			while(c >= '0' && c <= '9') {
				m[i] = m[i] * 10 + (c - '0');
				c = from.charAt(p++);
			}
			if (p == from.length() || c == ':')
				break;
				
			i++;
			p++;
		}

		in =(m[0] & 0xff) | ((m[1] & 0xff) << 8) | ((m[2] & 0xff) << 16) | ((m[3] & 0xff) << 24);

		for (i = 0; i < numipfilters; i++)
			if ((in & ipfilters[i].mask) == ipfilters[i].compare)
				return ((int) filterban.value)!=0;

		return ((int) 1-filterban.value)!=0;
	}

	/*
	=================
	SV_AddIP_f
	=================
	*/
	static void SVCmd_AddIP_f() {
		int i;

		if (gi.argc() < 3) {
			gi.cprintf(null, PRINT_HIGH, "Usage:  addip <ip-mask>\n");
			return;
		}

		for (i = 0; i < numipfilters; i++)
			if (ipfilters[i].compare == 0xffffffff)
				break; // free spot
		if (i == numipfilters) {
			if (numipfilters == MAX_IPFILTERS) {
				gi.cprintf(null, PRINT_HIGH, "IP filter list is full\n");
				return;
			}
			numipfilters++;
		}

		if (!StringToFilter(gi.argv(2), ipfilters[i]))
			ipfilters[i].compare = 0xffffffff;
	}

	/*
	=================
	SV_RemoveIP_f
	=================
	*/
	static void SVCmd_RemoveIP_f() {
		ipfilter_t f = new ipfilter_t();
		int i, j;

		if (gi.argc() < 3) {
			gi.cprintf(null, PRINT_HIGH, "Usage:  sv removeip <ip-mask>\n");
			return;
		}

		if (!StringToFilter(gi.argv(2), f))
			return;

		for (i = 0; i < numipfilters; i++)
			if (ipfilters[i].mask == f.mask && ipfilters[i].compare == f.compare) {
				for (j = i + 1; j < numipfilters; j++)
					ipfilters[j - 1] = ipfilters[j];
				numipfilters--;
				gi.cprintf(null, PRINT_HIGH, "Removed.\n");
				return;
			}
		gi.cprintf(null, PRINT_HIGH, "Didn't find " + gi.argv(2) + ".\n");
	}

	/*
	=================
	SV_ListIP_f
	=================
	*/
	static void SVCmd_ListIP_f() {
		int i;
		byte b[];

		gi.cprintf(null, PRINT_HIGH, "Filter list:\n");
		for (i = 0; i < numipfilters; i++) {
			b = getIntBytes(ipfilters[i].compare);
			gi.cprintf(null, PRINT_HIGH, (b[0] & 0xff) + "." + (b[1] & 0xff) + "." + (b[2] & 0xff) + "." + (b[3] & 0xff));
		}
	}

	/*
	=================
	SV_WriteIP_f
	=================
	*/
	static void SVCmd_WriteIP_f() {
		RandomAccessFile f;
		//char name[MAX_OSPATH];
		String name;
		byte b[];

		int i;
		cvar_t game;

		game = gi.cvar("game", "", 0);

		if (game.string == null)
			name = GAMEVERSION + "/listip.cfg";
		else
			name = game.string + "/listip.cfg";

		gi.cprintf(null, PRINT_HIGH, "Writing " + name + ".\n");

		f = fopen(name, "rw");
		if (f == null) {
			gi.cprintf(null, PRINT_HIGH, "Couldn't open " + name + "\n");
			return;
		}

		try {
			f.writeChars("set filterban " + (int) filterban.value + "\n");

			for (i = 0; i < numipfilters; i++) {
				b = getIntBytes(ipfilters[i].compare);
				f.writeChars("sv addip " + (b[0] & 0xff) + "." + (b[1] & 0xff) + "." + (b[2] & 0xff) + "." + (b[3] & 0xff) + "\n");
			}

		}
		catch (IOException e) {
			Com.Printf("IOError in SVCmd_WriteIP_f:" + e);
		}

		fclose(f);
	}

	/*
	=================
	ServerCommand
	
	ServerCommand will be called when an "sv" command is issued.
	The game can issue gi.argc() / gi.argv() commands to get the rest
	of the parameters
	=================
	*/
	public static void ServerCommand() {
		String cmd;

		cmd = gi.argv(1);
		if (Q_stricmp(cmd, "test") == 0)
			Svcmd_Test_f();
		else if (Q_stricmp(cmd, "addip") == 0)
			SVCmd_AddIP_f();
		else if (Q_stricmp(cmd, "removeip") == 0)
			SVCmd_RemoveIP_f();
		else if (Q_stricmp(cmd, "listip") == 0)
			SVCmd_ListIP_f();
		else if (Q_stricmp(cmd, "writeip") == 0)
			SVCmd_WriteIP_f();
		else
			gi.cprintf(null, PRINT_HIGH, "Unknown server command \"" + cmd + "\"\n");
	}

}