📄 waripmask.h
字号:
/** * NB: Deals with in_addr in machine byte order*/#ifndef WAR_IP_MASK_H#define WAR_IP_MASK_H/* SYSTEM INCLUDES */#ifndef WAR_SET_INCLUDED# define WAR_SET_INCLUDED# include <set>#endif#ifndef WAR_LIST_INCLUDED# define WAR_LIST_INCLUDED# include <list>#endif#ifdef HAVE_SOCKET_H# ifndef WAR_SOCKET_H_INCLUDED# define WAR_SOCKET_H_INCLUDED# include <socket.h># endif#elif defined(HAVE_SYS_SOCKET_H)# ifndef WAR_SYS_SOCKET_H_INCLUDED# define WAR_SYS_SOCKET_H_INCLUDED# include <sys/socket.h># endif#endif#ifdef HAVE_NETINET_IN_H# ifndef WAR_NETINET_IN_H_INCLUDED# define WAR_NETINET_IN_H_INCLUDED# include <netinet/in.h># endif#endif/* PROJECT INCLUDES */#ifndef WAR_EXCEPTION_H# include "WarException.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********//****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplusclass WarIpMask {public: // LIFECYCLE /** * Default constructor. */ WarIpMask() { mIpAddress.s_addr = 0; mIpMask.s_addr = 0xffffffff; } WarIpMask(const struct in_addr& ipAddr) { mIpAddress.s_addr = ipAddr.s_addr; mIpMask.s_addr = 0xffffffff; } WarIpMask(const struct in_addr& ipAddr, const struct in_addr& ipMask) { mIpAddress.s_addr = ipAddr.s_addr; mIpMask.s_addr = ipMask.s_addr; } /** * Copy constructor. * * @param from The value to copy to this object. */ WarIpMask(const WarIpMask& from) { operator = (from); } /** * Destructor. */ ~WarIpMask() {} // OPERATORS /** * Assignment operator. * * @param from THe value to assign to this object. * * @return A reference to this object. */ WarIpMask& operator = (const WarIpMask& from) { mIpAddress.s_addr = from.mIpAddress.s_addr; mIpMask.s_addr = from.mIpMask.s_addr; return *this; } template <class charT> WarIpMask& operator = (const charT *strMask) throw(WarException) { const charT *p = strMask; mIpAddress.s_addr = 0; mIpMask.s_addr = 0xffffffff; unsigned char *pbyte = (unsigned char *)&mIpAddress; int width = 0; int index; // Put IP number into mIpAddress for(index = 0; index < 4; index++, pbyte++) { width = 0; while(isdigit(*p)) { *pbyte *= 10; *pbyte += (*p++ - '0'); if (++width > 3) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); } if (*p == '/') break; if ((index < 3) && ('.' != *p++)) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); } if ('/' == *p) { ++p; pbyte = (unsigned char *)&mIpMask; // Get subnet mask into mIpMask for(index = 0; index < 4; index++, pbyte++) { width = 0; *pbyte = 0; while(isdigit(*p)) { *pbyte *= 10; *pbyte += (*p++ - '0'); if (++width > 3) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); } if (!index && !*p) { // new notation /nn mIpMask.s_addr = ntohl(0xffffffff << (32 - *pbyte)); break; } if ((index < 3) && ('.' != *p++)) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); } } return *this; } bool operator < (const WarIpMask& from) const { if (mIpAddress.s_addr < from.mIpAddress.s_addr) return true; if ((mIpAddress.s_addr == from.mIpAddress.s_addr) && (mIpMask.s_addr < from.mIpMask.s_addr)) return true; return false; } bool operator == (const WarIpMask& from) const { return ((mIpAddress.s_addr == from.mIpAddress.s_addr) && (mIpMask.s_addr == from.mIpMask.s_addr)); } // OPERATIONS // CALLBACK // ACCESS // INQUIRY bool DoesInclude(const struct in_addr& host) const { if (mIpAddress.s_addr == INADDR_ANY) return true; return ((host.s_addr & mIpMask.s_addr) == (mIpAddress.s_addr & mIpMask.s_addr)); } // PROPERTIES struct in_addr mIpAddress; struct in_addr mIpMask;protected:private:};/* INLINE METHODS *//* EXTERNAL REFERENCES */typedef std::set<WarIpMask> war_ip_mask_set_t;typedef std::list<WarIpMask> war_ip_mask_list_t;#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* WAR_IP_MASK_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -