Public Types | |
enum | { MAX = 101 } |
Public Member Functions | |
nQueryMessageStats (nMachine &machine) | |
tString | GetIP () |
bool | PrintWarning () |
virtual void | OnDestroy () |
called when machine gets destroyed | |
void | Block () |
bool | FloodProtection (REAL timeFactor) |
Public Attributes | |
nMachine & | machine_ |
double | lastTime_ [MAX] |
int | lastTimeIndex_ |
bool | warned_ |
Definition at line 798 of file nServerInfo.cpp.
anonymous enum |
nQueryMessageStats::nQueryMessageStats | ( | nMachine & | machine | ) | [inline] |
Definition at line 801 of file nServerInfo.cpp.
References E10, MAX, and tASSERT.
00802 : nMachineDecorator( machine ) 00803 , machine_( machine ) 00804 , warned_(false) 00805 { 00806 tASSERT( MAX > sn_minPingCounts[ sn_minPingCount-1 ] ); 00807 00808 for ( int i = 0; i < MAX; ++i ) 00809 lastTime_[i] = -1E10; 00810 00811 lastTimeIndex_ = 0; 00812 }
tString nQueryMessageStats::GetIP | ( | void | ) | [inline] |
Definition at line 820 of file nServerInfo.cpp.
References tString::Len().
00821 { 00822 tString ret = machine_.GetIP(); 00823 if ( ret.Len() < 4 ) 00824 ret = "everyone"; 00825 00826 return ret; 00827 }
bool nQueryMessageStats::PrintWarning | ( | ) | [inline] |
Definition at line 830 of file nServerInfo.cpp.
References lastTime, and tSysTimeFloat().
00831 { 00832 if ( warned_ ) 00833 return false; 00834 00835 double time = tSysTimeFloat(); 00836 static double lastTime = time-10; 00837 if ( time < lastTime + 5 ) 00838 return false; 00839 00840 warned_ = true; 00841 return true; 00842 }
virtual void nQueryMessageStats::OnDestroy | ( | void | ) | [inline, virtual] |
called when machine gets destroyed
Reimplemented from nMachineDecorator.
Definition at line 844 of file nServerInfo.cpp.
References con.
00845 { 00846 if ( warned_ ) 00847 con << "Flood protection ban of " << GetIP() << " removed because machine is discarded.\n"; 00848 00849 delete this; 00850 }
void nQueryMessageStats::Block | ( | ) | [inline] |
Definition at line 852 of file nServerInfo.cpp.
References con, MAX, and tSysTimeFloat().
Referenced by FloodProtection().
00853 { 00854 // block a DOS attacker for a while so he thinks his attack 00855 // brought the server down 00856 double time = tSysTimeFloat(); 00857 00858 if ( lastTime_[0] < time ) 00859 { 00860 for ( int i = 0; i < MAX; ++i ) 00861 { 00862 lastTime_[i] = time+10; 00863 } 00864 } 00865 00866 if ( PrintWarning() ) 00867 { 00868 con << "Flood protection bans " << GetIP() << " for sending multiple querty messages with one packet.\n"; 00869 } 00870 }
bool nQueryMessageStats::FloodProtection | ( | REAL | timeFactor | ) | [inline] |
Definition at line 873 of file nServerInfo.cpp.
References con, MAX, REAL, and tSysTimeFloat().
Referenced by FloodProtection().
00874 { 00875 int i; 00876 double now = tSysTimeFloat(); 00877 00878 bool protect = false; 00879 REAL diff = 0; 00880 int count = 0; 00881 00882 // go through the different levels 00883 for ( i = sn_minPingCount-1; i >= 0; --i ) 00884 { 00885 // this many pings should be tracked 00886 count = sn_minPingCounts[i]; 00887 diff = now - lastTime_[(lastTimeIndex_ + MAX - count) % MAX]; 00888 REAL tolerance = sn_minPingTimes[i]*timeFactor; 00889 if ( tolerance > 0 && diff < tolerance ) 00890 { 00891 protect = true; 00892 break; 00893 } 00894 } 00895 00896 lastTimeIndex_ = (lastTimeIndex_ + 1) % MAX; 00897 lastTime_[lastTimeIndex_] = now; 00898 00899 if ( protect ) 00900 { 00901 if ( PrintWarning() ) 00902 con << "Flood protection bans " << GetIP() << " after " << count << " pings in " << int((diff)*100)/100.0 << " seconds.\n"; 00903 00904 return true; 00905 } 00906 00907 // reset warning flag 00908 if ( warned_ && now - lastTime_[(lastTimeIndex_ + MAX-2 ) % MAX] > sn_minPingTimes[ sn_minPingCount-1 ] ) 00909 { 00910 con << "Flood protection ban of " << GetIP() << " removed.\n"; 00911 warned_ = false; 00912 } 00913 00914 return false; 00915 }
Definition at line 815 of file nServerInfo.cpp.
double nQueryMessageStats::lastTime_[MAX] |
Definition at line 816 of file nServerInfo.cpp.
Definition at line 817 of file nServerInfo.cpp.
Definition at line 818 of file nServerInfo.cpp.