Advertisement
RyDeR_

FloodControl.inc

Feb 23rd, 2012
6,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.28 KB | None | 0 0
  1. /*
  2.     SA-MP FloodControl Include
  3.     Copyright © 2012 RyDeR`, JernejL
  4. */
  5.  
  6. #if !defined MAX_JOIN_LOGS
  7.     #define MAX_JOIN_LOGS (30)
  8. #endif
  9.  
  10. #if !defined MAX_THRESHOLD
  11.     #define MAX_THRESHOLD (8000) // The amount of time in which all joins are valid and counted
  12. #endif
  13.  
  14. enum e_JoinLog {
  15.     e_iIP,
  16.     e_iTimeStamp
  17. };
  18.  
  19. static stock
  20.     g_eaJoinLog[MAX_JOIN_LOGS][e_JoinLog]
  21. ;
  22.  
  23. public OnPlayerConnect(playerid) {
  24.     static
  25.         s_iJoinSeq,
  26.         s_szIP[16]
  27.     ;
  28.     GetPlayerIp(playerid, s_szIP, sizeof(s_szIP));
  29.    
  30.     g_eaJoinLog[s_iJoinSeq][e_iIP] = s_szIP[0] = IpToInt(s_szIP);
  31.     g_eaJoinLog[s_iJoinSeq][e_iTimeStamp] = GetTickCount();
  32.    
  33.     s_iJoinSeq = ++s_iJoinSeq % MAX_JOIN_LOGS;
  34.    
  35.     s_szIP[1] = s_szIP[2] = 0;
  36.    
  37.     for(new i = 0; i < (MAX_JOIN_LOGS - 1); ++i) {
  38.         if(g_eaJoinLog[i][e_iIP] != s_szIP[0]) {
  39.             continue;
  40.         }
  41.         s_szIP[3] = GetTickCount();
  42.  
  43.         if(floatround(floatabs(s_szIP[3] - g_eaJoinLog[i][e_iTimeStamp])) < MAX_THRESHOLD) {
  44.             if(floatround(floatabs(s_szIP[3] - g_eaJoinLog[i + 1][e_iTimeStamp])) < MAX_THRESHOLD) {
  45.                 s_szIP[2] += floatround(floatabs(g_eaJoinLog[i][e_iTimeStamp] - g_eaJoinLog[i + 1][e_iTimeStamp]));
  46.                 s_szIP[1] += 1;
  47.             }
  48.         }
  49.     }
  50.     // Eventually initialize and call if exists
  51.     static
  52.         bool: s_bInit,
  53.        
  54.         s_iHasOPFC = -1,
  55.         s_iHasOPC = -1
  56.     ;
  57.     if(!s_bInit) {
  58.         s_iHasOPFC = funcidx("OnPlayerFloodControl");
  59.         s_iHasOPC = funcidx("FC_OnPlayerConnect");
  60.  
  61.         s_bInit = !s_bInit;
  62.     }
  63.     if(s_bInit) {
  64.         if(s_iHasOPFC != -1) {
  65.             CallRemoteFunction("OnPlayerFloodControl", "iii", playerid, s_szIP[1], s_szIP[2]);
  66.         }
  67.         if(s_iHasOPC != -1) {
  68.             return CallLocalFunction("FC_OnPlayerConnect", "i", playerid);
  69.         }
  70.     }
  71.     return 1;
  72. }
  73.  
  74. #if defined _ALS_OnPlayerConnect
  75.     #undef OnPlayerConnect
  76. #else
  77.     #define _ALS_OnPlayerConnect
  78. #endif
  79.  
  80. #define OnPlayerConnect FC_OnPlayerConnect
  81.  
  82. static stock IpToInt(const s_szIP[]) {
  83.     new
  84.         aiBytes[1],
  85.         iPos = 0
  86.     ;
  87.     aiBytes{0} = strval(s_szIP[iPos]);
  88.     while(iPos < 15 && s_szIP[iPos++] != '.') {}
  89.     aiBytes{1} = strval(s_szIP[iPos]);
  90.     while(iPos < 15 && s_szIP[iPos++] != '.') {}
  91.     aiBytes{2} = strval(s_szIP[iPos]);
  92.     while(iPos < 15 && s_szIP[iPos++] != '.') {}
  93.     aiBytes{3} = strval(s_szIP[iPos]);
  94.    
  95.     return aiBytes[0];
  96. }
  97.  
  98. forward OnPlayerConnect(playerid);
  99. forward OnPlayerFloodControl(playerid, iCount, iTimeSpan);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement