Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
ipban.qh File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

float Ban_Delete (float i)
float Ban_Insert (string ip, float bantime, string reason, float dosync)
void Ban_KickBanClient (entity client, float bantime, float masksize, string reason)
void Ban_LoadBans ()
float Ban_MaybeEnforceBan (entity client)
float Ban_MaybeEnforceBanOnce (entity client)
void Ban_SaveBans ()
void Ban_View ()
float BanCommand (string command)
void OnlineBanList_URI_Get_Callback (float id, float status, string data)

Function Documentation

◆ Ban_Delete()

float Ban_Delete ( float i)

Definition at line 286 of file ipban.qc.

287{
288 if(i < 0)
289 return false;
290 if(i >= ban_count)
291 return false;
292 if(ban_expire[i] == 0)
293 return false;
294 if(ban_expire[i] > 0)
295 {
297 strunzone(ban_ip[i]);
298 }
299 ban_expire[i] = 0;
300 ban_ip[i] = "";
301 Ban_SaveBans();
302 return true;
303}
string ban_ip[BAN_MAX]
Definition ipban.qc:253
float ban_expire[BAN_MAX]
Definition ipban.qc:254
void OnlineBanList_SendUnban(string ip)
Definition ipban.qc:55
void Ban_SaveBans()
Definition ipban.qc:263
float ban_count
Definition ipban.qc:255
void strunzone(string s)

References ban_count, ban_expire, ban_ip, Ban_SaveBans(), OnlineBanList_SendUnban(), and strunzone().

Referenced by Ban_Insert(), Ban_LoadBans(), and BanCommand_unban().

◆ Ban_Insert()

float Ban_Insert ( string ip,
float bantime,
string reason,
float dosync )

Definition at line 505 of file ipban.qc.

506{
507 float i;
508 float j;
509 float bestscore;
510
511 // already banned?
512 for(i = 0; i < ban_count; ++i)
513 if(ban_ip[i] == ip)
514 {
515 // prolong the ban
516 if(time + bantime > ban_expire[i])
517 {
518 ban_expire[i] = time + bantime;
519 LOG_TRACE(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now");
520 }
521 else
522 LOG_TRACE(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now");
523
524 // and enforce
525 reason = Ban_Enforce(i, reason);
526
527 // and abort
528 if(dosync)
529 if(reason != "")
530 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
531 OnlineBanList_SendBan(ip, bantime, reason);
532
533 return false;
534 }
535
536 // do we have a free slot?
537 for(i = 0; i < ban_count; ++i)
538 if(time > ban_expire[i])
539 break;
540 // no free slot? Then look for the one who would get unbanned next
541 if(i >= BAN_MAX)
542 {
543 i = 0;
544 bestscore = ban_expire[i];
545 for(j = 1; j < ban_count; ++j)
546 {
547 if(ban_expire[j] < bestscore)
548 {
549 i = j;
550 bestscore = ban_expire[i];
551 }
552 }
553 }
554 // if we replace someone, will we be banned longer than them (so long-term
555 // bans never get overridden by short-term bans)
556 if(i < ban_count)
557 if(ban_expire[i] > time + bantime)
558 {
559 LOG_INFO(ip, " could not get banned due to no free ban slot");
560 return false;
561 }
562 // okay, insert our new victim as i
563 Ban_Delete(i);
564 LOG_TRACE(ip, " has been banned for ", ftos(bantime), " seconds");
565 ban_expire[i] = time + bantime;
566 ban_ip[i] = strzone(ip);
567 ban_count = max(ban_count, i + 1);
568
569 Ban_SaveBans();
570
571 reason = Ban_Enforce(i, reason);
572
573 // and abort
574 if(dosync)
575 if(reason != "")
576 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
577 OnlineBanList_SendBan(ip, bantime, reason);
578
579 return true;
580}
float time
float Ban_Delete(float i)
Definition ipban.qc:286
string Ban_Enforce(float j, string reason)
Definition ipban.qc:478
void OnlineBanList_SendBan(string ip, float bantime, string reason)
Definition ipban.qc:33
const float BAN_MAX
Definition ipban.qc:251
#define LOG_INFO(...)
Definition log.qh:65
#define LOG_TRACE(...)
Definition log.qh:76
string substring(string s, float start, float length)
string ftos(float f)
string strzone(string s)
float max(float f,...)

References ban_count, Ban_Delete(), Ban_Enforce(), ban_expire, ban_ip, BAN_MAX, Ban_SaveBans(), ftos(), LOG_INFO, LOG_TRACE, max(), OnlineBanList_SendBan(), strzone(), substring(), and time.

Referenced by Ban_KickBanClient(), BanCommand_ban(), and OnlineBanList_URI_Get_Callback().

◆ Ban_KickBanClient()

void Ban_KickBanClient ( entity client,
float bantime,
float masksize,
string reason )

Definition at line 582 of file ipban.qc.

583{
584 string ip, id;
585 if(!Ban_GetClientIP(client))
586 {
587 sprint(client, strcat("Kickbanned: ", reason, "\n"));
588 dropclient(client);
589 return;
590 }
591
592 // who to ban?
593 switch(masksize)
594 {
595 case 1:
596 ip = strcat(ban_ip1);
597 break;
598 case 2:
599 ip = strcat(ban_ip2);
600 break;
601 case 3:
602 ip = strcat(ban_ip3);
603 break;
604 case 4:
605 default:
606 ip = strcat(ban_ip4);
607 break;
608 }
609 if(ban_idfp)
610 id = strcat(ban_idfp);
611 else
612 id = string_null;
613
614 Ban_Insert(ip, bantime, reason, 1);
615 if(id)
616 Ban_Insert(id, bantime, reason, 1);
617 /*
618 * not needed, as we enforce the ban in Ban_Insert anyway
619 // and kick him
620 sprint(client, strcat("Kickbanned: ", reason, "\n"));
621 dropclient(client);
622 */
623}
string ban_ip2
Definition ipban.qc:258
float Ban_GetClientIP(entity client)
Definition ipban.qc:353
float Ban_Insert(string ip, float bantime, string reason, float dosync)
Definition ipban.qc:505
string ban_ip1
Definition ipban.qc:257
string ban_ip4
Definition ipban.qc:260
string ban_ip3
Definition ipban.qc:259
string ban_idfp
Definition ipban.qc:261
void sprint(float clientnum, string text,...)
string string_null
Definition nil.qh:9
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))

References Ban_GetClientIP(), ban_idfp, Ban_Insert(), ban_ip1, ban_ip2, ban_ip3, ban_ip4, entity(), sprint(), strcat(), and string_null.

Referenced by BanCommand_kickban(), and MUTATOR_HOOKFUNCTION().

◆ Ban_LoadBans()

void Ban_LoadBans ( )

Definition at line 305 of file ipban.qc.

306{
307 float i, n;
308 for(i = 0; i < ban_count; ++i)
309 Ban_Delete(i);
310 ban_count = 0;
311 ban_loaded = true;
313 if(stof(argv(0)) == 1)
314 {
315 ban_count = (n - 1) / 2;
316 for(i = 0; i < ban_count; ++i)
317 {
318 ban_ip[i] = strzone(argv(2*i+1));
319 ban_expire[i] = time + stof(argv(2*i+2));
320 }
321 }
322
323 entity e = new(bansyncer);
325 e.nextthink = time + 1;
326}
string autocvar_g_banned_list
Definition banning.qh:11
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
#define tokenize_console
void OnlineBanList_Think(entity this)
Definition ipban.qc:197
float ban_loaded
Definition ipban.qc:252
float stof(string val,...)
string argv(float n)
#define setthink(e, f)

References argv(), autocvar_g_banned_list, ban_count, Ban_Delete(), ban_expire, ban_ip, ban_loaded, entity(), OnlineBanList_Think(), setthink, stof(), strzone(), time, and tokenize_console.

Referenced by Ban_IsClientBanned(), and spawnfunc().

◆ Ban_MaybeEnforceBan()

float Ban_MaybeEnforceBan ( entity client)

Definition at line 451 of file ipban.qc.

452{
453 if (Ban_IsClientBanned(client, -1))
454 {
455 if (!client.crypto_idfp)
456 LOG_INFOF("^1NOTE:^7 banned client %s just tried to enter\n",
457 client.netaddress);
458 else
459 LOG_INFOF("^1NOTE:^7 banned client %s (%s) just tried to enter\n",
460 client.netaddress, client.crypto_idfp);
461
463 sprint(client, "You are banned from this server.\n");
464 dropclient(client);
465 return true;
466 }
467 return false;
468}
bool autocvar_g_ban_telluser
Definition banning.qh:10
float Ban_IsClientBanned(entity client, float idx)
Definition ipban.qc:411
#define LOG_INFOF(...)
Definition log.qh:66

References autocvar_g_ban_telluser, Ban_IsClientBanned(), entity(), LOG_INFOF, and sprint().

Referenced by Ban_MaybeEnforceBanOnce().

◆ Ban_MaybeEnforceBanOnce()

float Ban_MaybeEnforceBanOnce ( entity client)

Definition at line 471 of file ipban.qc.

472{
473 if (client.ban_checked) return false;
474 client.ban_checked = true;
475 return Ban_MaybeEnforceBan(client);
476}
bool Ban_MaybeEnforceBan(entity client)
Definition ipban.qc:451

References Ban_MaybeEnforceBan(), and entity().

Referenced by ClientConnect(), and SV_ParseClientCommand().

◆ Ban_SaveBans()

void Ban_SaveBans ( )

Definition at line 263 of file ipban.qc.

264{
265 string out;
266 float i;
267
268 if(!ban_loaded)
269 return;
270
271 // version of list
272 out = "1";
273 for(i = 0; i < ban_count; ++i)
274 {
275 if(time > ban_expire[i])
276 continue;
277 out = strcat(out, " ", ban_ip[i]);
278 out = strcat(out, " ", ftos(ban_expire[i] - time));
279 }
280 if(strlen(out) <= 1) // no real entries
281 cvar_set("g_banned_list", "");
282 else
283 cvar_set("g_banned_list", out);
284}
#define strlen
void cvar_set(string name, string value)

References ban_count, ban_expire, ban_ip, ban_loaded, cvar_set(), ftos(), strcat(), strlen, and time.

Referenced by Ban_Delete(), Ban_Insert(), and Shutdown().

◆ Ban_View()

void Ban_View ( )

Definition at line 328 of file ipban.qc.

329{
330 float i, n;
331 string msg;
332
333 LOG_INFO("^2Listing all existing active bans:");
334
335 n = 0;
336 for(i = 0; i < ban_count; ++i)
337 {
338 if(time > ban_expire[i])
339 continue;
340
341 ++n; // total number of existing bans
342
343 msg = strcat("#", ftos(i), ": ");
344 msg = strcat(msg, ban_ip[i], " is still banned for ");
345 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
346
347 LOG_INFO(" ", msg);
348 }
349
350 LOG_INFO("^2Done listing all active (", ftos(n), ") bans.");
351}

References ban_count, ban_expire, ban_ip, ftos(), LOG_INFO, strcat(), and time.

Referenced by BanCommand_banlist().

◆ BanCommand()

float BanCommand ( string command)

Definition at line 520 of file banning.qc.

521{
522 int argc = tokenize_console(command);
523
524 // Guide for working with argc arguments by example:
525 // argc: 1 - 2 - 3 - 4
526 // argv: 0 - 1 - 2 - 3
527 // cmd vote - master - login - password
528
529 if (BanCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
530 return true; // handled by one of the above GenericCommand_* functions
531
532 return false;
533}
float BanCommand_macro_command(int argc, string command)
Definition banning.qc:489

References BanCommand_macro_command(), and tokenize_console.

Referenced by GameCommand().

◆ OnlineBanList_URI_Get_Callback()

void OnlineBanList_URI_Get_Callback ( float id,
float status,
string data )

Definition at line 79 of file ipban.qc.

80{
81 float n, i, j, l;
82 string ip;
83 float timeleft;
84 string reason;
85 string serverip;
86 float syncinterval;
87 string uri;
88
89 id -= URI_GET_IPBAN;
90
91 if(id >= MAX_IPBAN_URIS)
92 {
93 LOG_INFO("Received ban list for invalid ID");
94 return;
95 }
96
98 uri = argv(id);
99
100 string prelude = strcat("Received ban list from ", uri, ": ");
101
103 {
104 LOG_INFO(prelude, "rejected (unexpected)");
105 return;
106 }
107
109
111 {
112 LOG_INFO(prelude, "rejected (too late)");
113 return;
114 }
115
116 syncinterval = autocvar_g_ban_sync_interval;
117 if(syncinterval == 0)
118 {
119 LOG_INFO(prelude, "rejected (syncing disabled)");
120 return;
121 }
122 if(syncinterval > 0)
123 syncinterval *= 60;
124
125 if(status != 0)
126 {
127 LOG_INFO(prelude, "error: status is ", ftos(status));
128 return;
129 }
130
131 if(substring(data, 0, 1) == "<")
132 {
133 LOG_INFO(prelude, "error: received HTML instead of a ban list");
134 return;
135 }
136
137 if(strstrofs(data, "\r", 0) != -1)
138 {
139 LOG_INFO(prelude, "error: received carriage returns");
140 return;
141 }
142
143 if(data == "")
144 n = 0;
145 else
146 n = tokenizebyseparator(data, "\n");
147
148 if((n % 4) != 0)
149 {
150 LOG_INFO(prelude, "error: received invalid item count: ", ftos(n));
151 return;
152 }
153
154 LOG_INFO(prelude, "OK, ", ftos(n / 4), " items");
155
156 for(i = 0; i < n; i += 4)
157 {
158 ip = argv(i);
159 timeleft = stof(argv(i + 1));
160 reason = argv(i + 2);
161 serverip = argv(i + 3);
162
163 LOG_TRACE("received ban list item ", ftos(i / 4), ": ip=", ip);
164 LOG_TRACE(" timeleft=", ftos(timeleft), " reason=", reason);
165 LOG_TRACE(" serverip=", serverip);
166
167 timeleft -= 1.5 * autocvar_g_ban_sync_timeout;
168 if(timeleft < 0)
169 continue;
170
171 l = strlen(ip);
172 if(l != 44) // length 44 is a cryptographic ID
173 {
174 for(j = 0; j < l; ++j)
175 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
176 {
177 LOG_INFO("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.");
178 goto skip;
179 }
180 }
181
183 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
184 continue;
185
186 if(syncinterval > 0)
187 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
188 // the ban will be prolonged on the next sync
189 // or expire 5 seconds after the next timeout
190 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
191 LOG_INFO("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ", reason);
192
193LABEL(skip)
194 }
195}
bool autocvar_g_ban_sync_trusted_servers_verify
Definition banning.qh:8
float autocvar_g_ban_sync_timeout
Definition banning.qh:6
float autocvar_g_ban_sync_interval
Definition banning.qh:5
string autocvar_g_ban_sync_uri
Definition banning.qh:9
#define LABEL(id)
Definition compiler.qh:34
#define strstrofs
#define tokenizebyseparator
float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS]
Definition ipban.qc:77
#define MAX_IPBAN_URIS
Definition ipban.qc:31
float OnlineBanList_Timeout
Definition ipban.qc:76
string OnlineBanList_Servers
Definition ipban.qc:75
float min(float f,...)
const int URI_GET_IPBAN
Definition urllib.qh:5

References argv(), autocvar_g_ban_sync_interval, autocvar_g_ban_sync_timeout, autocvar_g_ban_sync_trusted_servers_verify, autocvar_g_ban_sync_uri, Ban_Insert(), ftos(), LABEL, LOG_INFO, LOG_TRACE, MAX_IPBAN_URIS, min(), OnlineBanList_RequestWaiting, OnlineBanList_Servers, OnlineBanList_Timeout, stof(), strcat(), strlen, strstrofs, substring(), time, tokenize_console, tokenizebyseparator, and URI_GET_IPBAN.

Referenced by URI_Get_Callback().