myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2921] add IP address range helper class and it's test


From: Alexandru IANCU
Subject: [myserver-commit] [2921] add IP address range helper class and it's tests
Date: Wed, 29 Oct 2008 06:16:51 +0000

Revision: 2921
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2921
Author:   andu
Date:     2008-10-29 06:16:50 +0000 (Wed, 29 Oct 2008)

Log Message:
-----------
add IP address range helper class and it's tests

Modified Paths:
--------------
    trunk/myserver/include/conf/vhost/Makefile.am
    trunk/myserver/src/conf/vhost/Makefile.am
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/include/conf/vhost/ip.h
    trunk/myserver/src/conf/vhost/ip.cpp
    trunk/myserver/tests/test_ip.cpp

Modified: trunk/myserver/include/conf/vhost/Makefile.am
===================================================================
--- trunk/myserver/include/conf/vhost/Makefile.am       2008-10-28 19:51:27 UTC 
(rev 2920)
+++ trunk/myserver/include/conf/vhost/Makefile.am       2008-10-29 06:16:50 UTC 
(rev 2921)
@@ -1,4 +1,4 @@
 vhostincludedir=$(includedir)/myserver/include/conf/vhost
-vhostinclude_HEADERS = vhost.h vhost_manager.h
+vhostinclude_HEADERS = vhost.h vhost_manager.h ip.h
 SUBDIRS =
 

Added: trunk/myserver/include/conf/vhost/ip.h
===================================================================
--- trunk/myserver/include/conf/vhost/ip.h                              (rev 0)
+++ trunk/myserver/include/conf/vhost/ip.h      2008-10-29 06:16:50 UTC (rev 
2921)
@@ -0,0 +1,57 @@
+/* -*- mode: c++ -*- */
+/*
+  MyServer
+  Copyright (C) 2008 Free Software Foundation, Inc.
+  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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef IP_H
+#define IP_H
+
+#include "stdafx.h"
+#include <string>
+
+class IpRange
+{
+ public:
+  virtual bool InRange(const std::string &ip) = 0;
+  virtual bool InRange(const IpRange *pRange) = 0;
+};
+
+class Ipv4Range : public IpRange
+{
+public:
+  Ipv4Range(const std::string &sRange);
+  Ipv4Range(const std::string &sStartHost, const std::string &sEndHost);
+
+  virtual ~Ipv4Range();
+  virtual bool InRange(const std::string &ip);
+  virtual bool InRange(const IpRange *pRange);
+
+  const unsigned char* GetStart() const { return m_nStart; }
+  const unsigned char* GetEnd() const { return m_nEnd; }
+protected:
+  bool Init();
+  bool InRange(const unsigned char addr[4]);
+  bool SetRange(const std::string &sRange);
+  bool SetRange(const std::string &sStartHost, const std::string &sEndHost);
+  unsigned char m_nStart[4], m_nEnd[4], m_nMask[4];
+};
+
+//TODO: implement
+class Ipv6Range : public IpRange
+{
+};
+
+#endif //IP_H

Modified: trunk/myserver/src/conf/vhost/Makefile.am
===================================================================
--- trunk/myserver/src/conf/vhost/Makefile.am   2008-10-28 19:51:27 UTC (rev 
2920)
+++ trunk/myserver/src/conf/vhost/Makefile.am   2008-10-29 06:16:50 UTC (rev 
2921)
@@ -1,5 +1,5 @@
 lib_LIBRARIES = libvhost.a
-libvhost_a_SOURCES = vhost.cpp vhost_manager.cpp
+libvhost_a_SOURCES = vhost.cpp vhost_manager.cpp ip.cpp
 SUBDIRS =
 INCLUDES = $(all_includes)
 

Added: trunk/myserver/src/conf/vhost/ip.cpp
===================================================================
--- trunk/myserver/src/conf/vhost/ip.cpp                                (rev 0)
+++ trunk/myserver/src/conf/vhost/ip.cpp        2008-10-29 06:16:50 UTC (rev 
2921)
@@ -0,0 +1,180 @@
+/*
+  MyServer
+  Copyright (C) 2008 Free Software Foundation, Inc.
+  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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <include/conf/vhost/ip.h>
+#include <sstream>
+
+/*!
+ * Ipv4Range c-tor
+ */
+Ipv4Range::Ipv4Range(const std::string &sRange)
+{
+  Init();
+  SetRange(sRange);
+}
+
+/*!
+ * range given as x.x.x.x-y.y.y.y or x.x.x.x/y
+ */
+bool Ipv4Range::SetRange(const std::string &sRange)
+{
+  std::string::size_type nPos = sRange.find('-');
+  if ( nPos != std::string::npos )// x.x.x.x-y.y.y.y form
+    {
+      std::string start(sRange.substr(0, nPos));
+      std::string end(sRange.substr(nPos + 1));
+      SetRange(start, end);
+    }
+  else// x.x.x.x/y form
+    {
+      std::istringstream istream(sRange);
+      char nSep = 0;
+      unsigned char nAddr[4];
+      int nTemp = 0;
+      for ( int i = 0; i < 4 && !istream.eof(); i++ )
+       {
+         istream >> nTemp;
+         nAddr[i] = nTemp;
+         istream >> nSep;
+       }
+      if ( nSep == '/' )
+       istream >> nTemp;
+
+      for ( int i = 0, nByte = 0; i < nTemp && nByte < 4; i++ )
+       {
+         m_nMask[nByte] += (1 << (i+1)%8);
+         if ( (i+1)%8 == 0 )
+           nByte++;
+       }
+      for ( int i = 0; i < 4; i++ )
+       {
+         if ( m_nMask[i] != 0 )
+           {
+             m_nStart[i] = nAddr[i];
+             m_nEnd[i] = nAddr[i];
+           }
+       }
+    }
+  return true;
+}
+
+bool Ipv4Range::SetRange(const std::string &sStartHost, const std::string 
&sEndHost)
+{
+  if ( !Init() )
+    return false;
+
+  std::istringstream start(sStartHost), end(sEndHost);
+  char nSep = 0;
+  int nTemp = 0;
+  for ( int i = 0; i < 4 && !start.eof() && !end.eof(); i++ )
+    {
+      start >> nTemp;
+      m_nStart[i] = nTemp;
+      start >> nSep;
+      end >> nTemp;
+      m_nEnd[i] = nTemp;
+      end >> nSep;
+    }
+
+  // get mask lenght(max common addr part)
+  char bs = 0;
+  for ( bs = 0; bs < 32; bs++ )
+    {
+      if ( (m_nStart[bs/8] & (1 << bs%8)) != (m_nEnd[bs/8] & (1 << bs%8)) )
+       break;
+    }
+  for ( int i = 0, nByte = 0; i < bs && nByte < 4; i++ )
+    {
+      m_nMask[nByte] += (1 << (i+1)%8);
+      if ( (i+1)%8 == 0 )
+       nByte++;
+    }
+  return true;
+}
+
+/*!
+ * Ipv4Range initializer
+ * return false on error
+ */
+bool Ipv4Range::Init()
+{
+  for ( int i = 0; i < 4; i++ )
+    {
+      m_nStart[i] = 0;
+      m_nEnd[i] = 255;
+      m_nMask[i] = 0;
+    }
+  return true;
+}
+
+/*!
+ *d-tor
+ */
+Ipv4Range::~Ipv4Range()
+{
+}
+
+/*!
+ *checks if addr from param belongs the same network address
+ */
+bool Ipv4Range::InRange(const unsigned char addr[4])
+{
+  unsigned char hostMask[4];
+  for ( int i = 0; i < 4; i++ )
+    {
+      hostMask[i] = ~m_nMask[i];
+      if ( (m_nStart[i] & hostMask[i]) < (addr[i] & hostMask[i]) )
+         break;
+      if ( (m_nStart[i] & hostMask[i]) > (addr[i] & hostMask[i]) )
+         return false;
+    }
+  for ( int i = 0; i < 4; i++ )
+    {
+      hostMask[i] = ~m_nMask[i];
+      if ( (m_nEnd[i] & hostMask[i]) > (addr[i] & hostMask[i]) )
+       return true;
+      if ( (m_nEnd[i] & hostMask[i]) < (addr[i] & hostMask[i]) )
+       return false;
+    }
+  return true;//equal to start or end
+}
+
+bool Ipv4Range::InRange(const std::string &ip)
+{
+  unsigned char addr[4];
+  std::istringstream stream(ip);
+  char nSep = 0;
+  int nTemp = 0;
+  for ( int i = 0; i < 4 && !stream.eof(); i++ )
+    {
+      stream >> nTemp;
+      addr[i] = nTemp;
+      if ( (addr[i] & m_nMask[i]) != (m_nStart[i] & m_nMask[i]) )
+       return false;//networks differ
+      stream >> nSep;
+    }
+
+  return InRange(addr);
+}
+
+bool Ipv4Range::InRange(const IpRange *pRange)
+{
+  if ( pRange == NULL )
+    return false;
+  const Ipv4Range *pLocal = static_cast<const Ipv4Range *>(pRange);
+  return InRange(pLocal->GetStart()) && InRange(pLocal->GetEnd());
+}

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2008-10-28 19:51:27 UTC (rev 2920)
+++ trunk/myserver/tests/Makefile.am    2008-10-29 06:16:50 UTC (rev 2921)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_utility.cpp  test_xml_validator.cpp
+tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_utility.cpp  test_xml_validator.cpp test_ip.cpp
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) 
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(OPENSSL_LIB) $(ZLIB_LIB) 
$(XML_LIBS) $(LDFLAGS)

Added: trunk/myserver/tests/test_ip.cpp
===================================================================
--- trunk/myserver/tests/test_ip.cpp                            (rev 0)
+++ trunk/myserver/tests/test_ip.cpp    2008-10-29 06:16:50 UTC (rev 2921)
@@ -0,0 +1,44 @@
+/*
+ MyServer
+ Copyright (C) 2008 Free Software Foundation, Inc.
+ 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <include/conf/vhost/ip.h>
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+class TestIpRange : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestIpRange );
+  CPPUNIT_TEST( testRangeInclusion );
+  CPPUNIT_TEST( testIpInRange );
+  CPPUNIT_TEST_SUITE_END();
+
+public:
+  void setUp() {}
+  void tearDown() {}
+  void testRangeInclusion()
+  {
+    Ipv4Range LargeRange("192.168.0.0/24"), 
InsideRange("192.168.0.100-192.168.0.200");
+    CPPUNIT_ASSERT( LargeRange.InRange(&InsideRange) );
+  }
+  void testIpInRange()
+  {
+    Ipv4Range testRange("192.168.0.0/24");
+    CPPUNIT_ASSERT( testRange.InRange("192.168.0.127") );
+  }
+};






reply via email to

[Prev in Thread] Current Thread [Next in Thread]