g_svcmds.c 5.3 KB

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