[go: up one dir, main page]

Menu

[ab7a0e]: / src / cidr.cc  Maximize  Restore  History

Download this file

126 lines (109 with data), 2.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* cidr.cc
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
* Orlando Bassotto
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id: cidr.cc,v 1.3 2009/07/26 18:30:37 mrbean_ Exp $
*/
#include <string>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "cidr.h"
#include "misc.h"
#include "StringTokenizer.h"
#include "gnuworld_config.h"
RCSTAG( "$Id: cidr.cc,v 1.3 2009/07/26 18:30:37 mrbean_ Exp $" ) ;
namespace gnuworld
{
using std::string ;
xCIDR::xCIDR( const std::string& IP ) : str(IP), prefix(0)
{
std::size_t pos = IP.find_first_of("/");
if(pos == string::npos || pos == IP.length()) //Failed to find / or its at the end of the string
{
valid = false;
return;
}
if(!(valid = ExtractMask(IP.substr(pos+1))))
{
return;
}
unsigned int pref;
if(ExtractPrefix(IP.substr(0,pos),mask,pref) == 0)
{
valid = false;
return;
}
prefix = xIP(ntohl(pref));
valid = true;
}
bool xCIDR::ExtractMask(const std::string& Mask)
{
if(Mask.length() ==0 || Mask.length() > 2) //Mask is maximum of 2 chars
{
return false;
}
if(!IsNumeric(Mask))
{
return false;
}
mask = atoi(Mask.c_str());
return mask >=0 && mask <= 32; //Verify its in range
}
bool xCIDR::ExtractPrefix(const std::string& Prefix,const unsigned int& Mask,unsigned int& pref)
{
StringTokenizer st(Prefix,'.');
if(st.size() != 4) //Must have 4 parts
{
return false;
}
in_addr addr;
if(inet_aton(Prefix.c_str(),&addr) > 0)
{
unsigned int tAddr = htonl(addr.s_addr);
int tMask = 0;
for(unsigned int i=0; i < 32-Mask;i++) { //Build the mask
tMask = tMask << 1;
tMask = tMask + 1;
}
pref = tAddr & ~tMask; //extract the prefix
return true;
}
return false;
}
bool xCIDR::matches(const std::string& IP)
{
if(!valid)
{
return false;
}
unsigned int clientIp;
if(ExtractPrefix(IP,mask,clientIp) > 0)
{
return clientIp == prefix.GetLongIP();
}
return false;
}
} // namespace gnuworld