Advertisement
Guest User

CIDR Include 1.0

a guest
Jun 6th, 2013
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.38 KB | None | 0 0
  1. /***                                                    ***
  2.  * CIDR Include - Check precise IP ranges                 *
  3.  ***                                                    ***
  4.  * @Author  Rafael 'R@f' Keramidas <rafael@keramid.as>    *
  5.  * @Date    6th June 2013                                 *
  6.  * @Version 1.0                                           *
  7.  * @Licence MIT License                                   *
  8.  *                                                        *
  9.  * Copyright (C) 2013 Rafael Keramidas                    *
  10.  *                                                        *
  11.  * Permission is hereby granted, free of charge, to any   *
  12.  * person obtaining a copy of this software and           *
  13.  * associated documentation files (the "Software"), to    *
  14.  * deal in the Software without restriction, including    *
  15.  * without limitation the rights to use, copy, modify,    *
  16.  * merge, publish, distribute, sublicense, and/or sell    *
  17.  * copies of the Software, and to permit persons to whom  *
  18.  * the Software is furnished to do so, subject to the     *
  19.  * following conditions:                                  *
  20.  *                                                        *
  21.  * The above copyright notice and this permission notice  *
  22.  * shall be included in all copies or substantial         *
  23.  * portions of the Software.                              *
  24.  *                                                        *
  25.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF  *
  26.  * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT        *
  27.  * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS  *
  28.  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO    *
  29.  * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE *
  30.  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN  *
  31.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      *
  32.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  33.  * USE OR OTHER DEALINGS IN THE SOFTWARE.                 *
  34.  ***                                                    ***/
  35.  
  36. #include <a_samp>
  37.  
  38. stock ip2long(const sIP[])
  39. {
  40.     new
  41.         iCount = 0,
  42.         iIPAddress = 0,
  43.         iIPLenght = strlen(sIP);
  44.        
  45.     if(iIPLenght > 0 && iIPLenght < 17)
  46.     {
  47.         for(new i = 0; i < iIPLenght; i++)
  48.             if(sIP[i] == '.')
  49.                 iCount++;
  50.                
  51.         if(iCount == 3)
  52.         {
  53.             iIPAddress = strval(sIP) << 24;
  54.             iCount = strfind(sIP, ".", false, 0) + 1;
  55.             iIPAddress += strval(sIP[iCount]) << 16;
  56.             iCount = strfind(sIP, ".", false, iCount) + 1;
  57.             iIPAddress += strval(sIP[iCount]) << 8;
  58.             iCount = strfind(sIP, ".", false, iCount) + 1;
  59.             iIPAddress += strval(sIP[iCount]);
  60.         }
  61.     }
  62.    
  63.     return iIPAddress;
  64. }
  65.  
  66. stock split(const sSrc[], sDest[][], sDelimiter = ' ')
  67. {
  68.     new
  69.         i = 0,
  70.         j = 0,
  71.         k = 0,
  72.         iSourceLen = strlen(sSrc),
  73.         iLenght = 0;
  74.        
  75.     while(i <= iSourceLen)
  76.     {
  77.         if(sSrc[i] == sDelimiter || i == iSourceLen)
  78.         {
  79.             iLenght = strmid(sDest[j], sSrc, k, i, 128);
  80.             sDest[j][iLenght] = 0;
  81.             k = i + 1;
  82.             j++;
  83.         }
  84.         i++;
  85.     }
  86.        
  87.     return true;
  88. }
  89.  
  90. stock cidr_match(sIP[], sRange[])
  91. {
  92.     new
  93.         sRangeInfo[2][18],
  94.         iIP = 0,
  95.         iSubnet = 0,
  96.         iBits = 0,
  97.         iMask = 0;
  98.        
  99.     split(sRange, sRangeInfo, '/');
  100.     iIP = ip2long(sIP);
  101.     iSubnet = ip2long(sRangeInfo[0]);
  102.     iBits = strval(sRangeInfo[1]);
  103.    
  104.     iMask = -1 << (32 - iBits);
  105.     iSubnet &= iMask;
  106.    
  107.     return (iIP & iMask) == iSubnet;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement