g_svcmds.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "g_local.h"
  16. void Svcmd_Test_f (void)
  17. {
  18. gi.cprintf (NULL, PRINT_HIGH, "Svcmd_Test_f()\n");
  19. }
  20. /*
  21. ==============================================================================
  22. PACKET FILTERING
  23. You can add or remove addresses from the filter list with:
  24. addip <ip>
  25. removeip <ip>
  26. 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".
  27. Removeip will only remove an address specified exactly the same way. You cannot addip a subnet, then removeip a single host.
  28. listip
  29. Prints the current list of filters.
  30. writeip
  31. 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.
  32. filterban <0 or 1>
  33. If 1 (the default), then ip addresses matching the current list will be prohibited from entering the game. This is the default setting.
  34. 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.
  35. ==============================================================================
  36. */
  37. typedef struct
  38. {
  39. unsigned mask;
  40. unsigned compare;
  41. } ipfilter_t;
  42. #define MAX_IPFILTERS 1024
  43. ipfilter_t ipfilters[MAX_IPFILTERS];
  44. int numipfilters;
  45. /*
  46. =================
  47. StringToFilter
  48. =================
  49. */
  50. static qboolean StringToFilter (char *s, ipfilter_t *f)
  51. {
  52. char num[128];
  53. int i, j;
  54. byte b[4];
  55. byte m[4];
  56. for (i=0 ; i<4 ; i++)
  57. {
  58. b[i] = 0;
  59. m[i] = 0;
  60. }
  61. for (i=0 ; i<4 ; i++)
  62. {
  63. if (*s < '0' || *s > '9')
  64. {
  65. gi.cprintf(NULL, PRINT_HIGH, "Bad filter address: %s\n", s);
  66. return false;
  67. }
  68. j = 0;
  69. while (*s >= '0' && *s <= '9')
  70. {
  71. num[j++] = *s++;
  72. }
  73. num[j] = 0;
  74. b[i] = atoi(num);
  75. if (b[i] != 0)
  76. m[i] = 255;
  77. if (!*s)
  78. break;
  79. s++;
  80. }
  81. f->mask = *(unsigned *)m;
  82. f->compare = *(unsigned *)b;
  83. return true;
  84. }
  85. /*
  86. =================
  87. SV_FilterPacket
  88. =================
  89. */
  90. qboolean SV_FilterPacket (char *from)
  91. {
  92. int i;
  93. unsigned in;
  94. byte m[4];
  95. char *p;
  96. i = 0;
  97. p = from;
  98. while (*p && i < 4) {
  99. m[i] = 0;
  100. while (*p >= '0' && *p <= '9') {
  101. m[i] = m[i]*10 + (*p - '0');
  102. p++;
  103. }
  104. if (!*p || *p == ':')
  105. break;
  106. i++, p++;
  107. }
  108. in = *(unsigned *)m;
  109. for (i=0 ; i<numipfilters ; i++)
  110. if ( (in & ipfilters[i].mask) == ipfilters[i].compare)
  111. return (int)filterban->value;
  112. return (int)!filterban->value;
  113. }
  114. /*
  115. =================
  116. SV_AddIP_f
  117. =================
  118. */
  119. void SVCmd_AddIP_f (void)
  120. {
  121. int i;
  122. if (gi.argc() < 3) {
  123. gi.cprintf(NULL, PRINT_HIGH, "Usage: addip <ip-mask>\n");
  124. return;
  125. }
  126. for (i=0 ; i<numipfilters ; i++)
  127. if (ipfilters[i].compare == 0xffffffff)
  128. break; // free spot
  129. if (i == numipfilters)
  130. {
  131. if (numipfilters == MAX_IPFILTERS)
  132. {
  133. gi.cprintf (NULL, PRINT_HIGH, "IP filter list is full\n");
  134. return;
  135. }
  136. numipfilters++;
  137. }
  138. if (!StringToFilter (gi.argv(2), &ipfilters[i]))
  139. ipfilters[i].compare = 0xffffffff;
  140. }
  141. /*
  142. =================
  143. SV_RemoveIP_f
  144. =================
  145. */
  146. void SVCmd_RemoveIP_f (void)
  147. {
  148. ipfilter_t f;
  149. int i, j;
  150. if (gi.argc() < 3) {
  151. gi.cprintf(NULL, PRINT_HIGH, "Usage: sv removeip <ip-mask>\n");
  152. return;
  153. }
  154. if (!StringToFilter (gi.argv(2), &f))
  155. return;
  156. for (i=0 ; i<numipfilters ; i++)
  157. if (ipfilters[i].mask == f.mask
  158. && ipfilters[i].compare == f.compare)
  159. {
  160. for (j=i+1 ; j<numipfilters ; j++)
  161. ipfilters[j-1] = ipfilters[j];
  162. numipfilters--;
  163. gi.cprintf (NULL, PRINT_HIGH, "Removed.\n");
  164. return;
  165. }
  166. gi.cprintf (NULL, PRINT_HIGH, "Didn't find %s.\n", gi.argv(2));
  167. }
  168. /*
  169. =================
  170. SV_ListIP_f
  171. =================
  172. */
  173. void SVCmd_ListIP_f (void)
  174. {
  175. int i;
  176. byte b[4];
  177. gi.cprintf (NULL, PRINT_HIGH, "Filter list:\n");
  178. for (i=0 ; i<numipfilters ; i++)
  179. {
  180. *(unsigned *)b = ipfilters[i].compare;
  181. gi.cprintf (NULL, PRINT_HIGH, "%3i.%3i.%3i.%3i\n", b[0], b[1], b[2], b[3]);
  182. }
  183. }
  184. /*
  185. =================
  186. SV_WriteIP_f
  187. =================
  188. */
  189. void SVCmd_WriteIP_f (void)
  190. {
  191. FILE *f;
  192. char name[MAX_OSPATH];
  193. byte b[4];
  194. int i;
  195. cvar_t *game;
  196. game = gi.cvar("game", "", 0);
  197. if (!*game->string)
  198. sprintf (name, "%s/listip.cfg", GAMEVERSION);
  199. else
  200. sprintf (name, "%s/listip.cfg", game->string);
  201. gi.cprintf (NULL, PRINT_HIGH, "Writing %s.\n", name);
  202. f = fopen (name, "wb");
  203. if (!f)
  204. {
  205. gi.cprintf (NULL, PRINT_HIGH, "Couldn't open %s\n", name);
  206. return;
  207. }
  208. fprintf(f, "set filterban %d\n", (int)filterban->value);
  209. for (i=0 ; i<numipfilters ; i++)
  210. {
  211. *(unsigned *)b = ipfilters[i].compare;
  212. fprintf (f, "sv addip %i.%i.%i.%i\n", b[0], b[1], b[2], b[3]);
  213. }
  214. fclose (f);
  215. }
  216. /*
  217. =================
  218. ServerCommand
  219. ServerCommand will be called when an "sv" command is issued.
  220. The game can issue gi.argc() / gi.argv() commands to get the rest
  221. of the parameters
  222. =================
  223. */
  224. void ServerCommand (void)
  225. {
  226. char *cmd;
  227. cmd = gi.argv(1);
  228. if (Q_stricmp (cmd, "test") == 0)
  229. Svcmd_Test_f ();
  230. else if (Q_stricmp (cmd, "addip") == 0)
  231. SVCmd_AddIP_f ();
  232. else if (Q_stricmp (cmd, "removeip") == 0)
  233. SVCmd_RemoveIP_f ();
  234. else if (Q_stricmp (cmd, "listip") == 0)
  235. SVCmd_ListIP_f ();
  236. else if (Q_stricmp (cmd, "writeip") == 0)
  237. SVCmd_WriteIP_f ();
  238. else
  239. gi.cprintf (NULL, PRINT_HIGH, "Unknown server command \"%s\"\n", cmd);
  240. }