📄 address.h
字号:
/*_############################################################################ _## _## address.h _## _## SNMP++v3.2.21a _## ----------------------------------------------- _## Copyright (c) 2001-2006 Jochen Katz, Frank Fock _## _## This software is based on SNMP++2.6 from Hewlett Packard: _## _## Copyright (c) 1996 _## Hewlett-Packard Company _## _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. _## Permission to use, copy, modify, distribute and/or sell this software _## and/or its documentation is hereby granted without fee. User agrees _## to display the above copyright notice and this license notice in all _## copies of the software and any documentation of the software. User _## agrees to assume all liability for the use of the software; _## Hewlett-Packard and Jochen Katz make no representations about the _## suitability of this software for any purpose. It is provided _## "AS-IS" without warranty of any kind, either express or implied. User _## hereby grants a royalty-free license to any and all derivatives based _## upon this software code base. _## _## Stuttgart, Germany, Tue Nov 21 22:12:16 CET 2006 _## _##########################################################################*//* Copyright (c) 1999 Hewlett-Packard Company ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. Permission to use, copy, modify, distribute and/or sell this software and/or its documentation is hereby granted without fee. User agrees to display the above copyright notice and this license notice in all copies of the software and any documentation of the software. User agrees to assume all liability for the use of the software; Hewlett-Packard makes no representations about the suitability of this software for any purpose. It is provided "AS-IS without warranty of any kind,either express or implied. User hereby grants a royalty-free license to any and all derivatives based upon this software code base. SNMP++ A D D R E S S . H ADDRESS CLASS DEFINITION DESIGN + AUTHOR: Peter E Mellquist DESCRIPTION: Address class definition. Encapsulates various network addresses into easy to use, safe and portable classes.=====================================================================*/// $Id: address.h,v 1.17 2006/04/04 20:44:19 katz Exp $#ifndef _ADDRESS#define _ADDRESS//----[ includes ]-----------------------------------------------------#include <string.h>#if defined (CPU) && CPU == PPC603#undef HASH1#undef HASH2#else#include <memory.h>#endif#include "snmp_pp/config_snmp_pp.h" // for _IPX_ADDRESS and _MAC_ADDRESS#include "snmp_pp/smival.h"#include "snmp_pp/collect.h"#include "snmp_pp/reentrant.h"// include sockets header files// for Windows16 and Windows32 include Winsock// otherwise assume UNIX#if defined (CPU) && CPU == PPC603#include <inetLib.h>#include <hostLib.h>#endif#ifdef __unix#if !defined(_AIX) && !defined(__QNX_NEUTRINO)#include <unistd.h>#endif#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <arpa/inet.h>#if defined _AIX#include <strings.h> // This is needed for FD_SET, bzero#endif#if !defined __CYGWIN32__ && !defined __hpux && !defined linux && !defined _AIXextern int h_errno; // defined in WinSock header, but not for UX?!#endif#endif // __unix#ifdef WIN32#ifndef __unix // __unix overrides WIN32 if both options are present#include <winsock.h>#endif#endif#ifdef SNMP_PP_NAMESPACEnamespace Snmp_pp {#endif//----[ macros ]-------------------------------------------------------#define ADDRBUF 40 // worst case of address lens#define OUTBUFF 80 // worst case of output lens#define IPLEN 4#define IP6LEN 16#define UDPIPLEN 6#define UDPIP6LEN 18#define IPXLEN 10#define IPXSOCKLEN 12#define MACLEN 6#define MAX_FRIENDLY_NAME 80#define HASH0 19#define HASH1 13#define HASH2 7//---[ forward declarations ]-----------------------------------------class GenAddress;//----[ Address class ]-----------------------------------------------/** * Base class of all Address classes. */class DLLOPT Address : public SnmpSyntax{ friend class GenAddress; public: //----[ enumerated types for address types ]--------------------------- /** * Type returned by Address::get_type(). */ enum addr_type { type_ip, ///< IpAddress (IPv4 or IPv6) type_ipx, ///< IpxAddress type_udp, ///< UdpAddress (IPv4 or IPv6) type_ipxsock, ///< IpxSockAddress type_mac, ///< MacAddress type_invalid ///< Used by GenAddress::get_type() if address is not valid }; /** * Type returned by IpAddress::get_ip_version() and * UdpAddress::get_ip_version(). */ enum version_type { version_ipv4, ///< IPv4 version_ipv6 ///< IPv6 }; /** * Default constructor, clears the buffer and sets valid flag to false. */ Address(); /** * Allow destruction of derived classes. */ virtual ~Address() {}; /// overloaded equivlence operator, are two addresses equal? DLLOPT friend int operator==(const Address &lhs,const Address &rhs); /// overloaded not equivlence operator, are two addresses not equal? DLLOPT friend int operator!=(const Address &lhs, const Address &rhs) { return !(lhs == rhs); }; /// overloaded > operator, is a1 > a2 DLLOPT friend int operator>(const Address &lhs,const Address &rhs); /// overloaded >= operator, is a1 >= a2 DLLOPT friend int operator>=(const Address &lhs,const Address &rhs) { if ((lhs > rhs) || (lhs == rhs)) return true; return false; }; /// overloaded < operator, is a1 < a2 DLLOPT friend int operator<(const Address &lhs,const Address &rhs); /// overloaded <= operator, is a1 <= a2 DLLOPT friend int operator<=(const Address &lhs, const Address &rhs) { if ((lhs < rhs) || (lhs == rhs)) return true; return false; }; /// equivlence operator overloaded, are an address and a string equal? DLLOPT friend int operator==(const Address &lhs,const char *rhs); /// overloaded not equivlence operator, are an address and string not equal? DLLOPT friend int operator!=(const Address &lhs,const char *rhs) { return !(lhs == rhs); }; /// overloaded < , is an address greater than a string? DLLOPT friend int operator>(const Address &lhs,const char *rhs); /// overloaded >=, is an address greater than or equal to a string? DLLOPT friend int operator>=(const Address &lhs,const char *rhs); /// overloaded < , is an address less than a string? DLLOPT friend int operator<(const Address &lhs,const char *rhs); /// overloaded <=, is an address less than or equal to a string? DLLOPT friend int operator<=(const Address &lhs,const char *rhs); /** * Overloaded operator for streaming output. * * @return String containing the numerical address */ virtual operator const char *() const = 0; /** * Return if the object contains a valid address. * * @return true if the object is valid */ virtual bool valid() const { return valid_flag; }; /** * Return the space needed for serialization. */ virtual int get_asn1_length() const = 0; /** * Access as an array (read and write). * @note Only pass in values between 0 and get_length(). * * @param position - pos to return * @return reference to the byte at the given position */ unsigned char& operator[](const int position) { addr_changed = true; valid_flag = true; return (position < ADDRBUF) ? address_buffer[position] : address_buffer[0]; }; /** * Access as an array (read only). * @note Only pass in values between 0 and get_length(). * * @param position - pos to return * @return the byte at the given position */ unsigned char operator[](const int position) const { return (position < ADDRBUF) ? address_buffer[ position] : 0; } /** * Get the length of the binary address (accessible through operator[]). */ virtual int get_length() const = 0; /** * Get the type of the address. * @see Address::addr_type */ virtual addr_type get_type() const = 0; /** * Overloaded assignment operator. */ virtual SnmpSyntax& operator=(const SnmpSyntax &val) = 0; // return a hash key virtual unsigned int hashFunction() const { return 0;}; protected: SNMP_PP_MUTABLE bool addr_changed; bool valid_flag; unsigned char address_buffer[ADDRBUF]; // internal representation // parse the address string // redefined for each specific address subclass virtual bool parse_address(const char * inaddr) = 0; // format the output // redefined for each specific address subclass virtual void format_output() const = 0; /** * Trim of whitespaces at the start and the end of the string. * * @param ptr - string to trim */ void trim_white_space(char * ptr); /** * Is this a GenAddress object. */ virtual bool is_gen_address() const { return false; }; /** * Reset the object. */ void clear();#if !defined HAVE_GETHOSTBYNAME_R || !defined HAVE_GETHOSTBYADDR_R || !defined HAVE_REENTRANT_GETHOSTBYNAME || !defined HAVE_REENTRANT_GETHOSTBYADDR#ifdef _THREADS static SnmpSynchronized syscall_mutex;#endif#endif};//-----------------------------------------------------------------------//---------[ IP Address Class ]------------------------------------------//-----------------------------------------------------------------------class DLLOPT IpAddress : public Address{ public: /** * Construct an empty invalid IP address. */ IpAddress(); /** * Construct an IP address from a string. * * The following formats can be used: * - hostname with or without domain ("www.agentpp.com", "printsrv") * - Numerical IPv4 address ("192.168.17.1") * - Numerical IPv6 address ("abcd:1234::a:b:1", "::abcd:1") * * @param inaddr - Hostname or IP address */ IpAddress(const char *inaddr); /** * Construct an IP address from another IP address. * * @param ipaddr - address to copy */ IpAddress(const IpAddress &ipaddr); /** * Construct an IP address from a GenAddress. * * @param genaddr - address to copy */ IpAddress(const GenAddress &genaddr); /** * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden). */ ~IpAddress() {}; /** * Map other SnmpSyntax objects to IpAddress. */ SnmpSyntax& operator=(const SnmpSyntax &val); /** * Overloaded assignment operator for other IP addresses. */ IpAddress& operator=(const IpAddress &ipaddress); /** * Overloaded assignment operator for strings. */ IpAddress& operator=(const char *inaddr); /** * Clone this object. * * @return Pointer to the newly created object (allocated through new). */ SnmpSyntax *clone() const { return (SnmpSyntax *) new IpAddress(*this); }; /** * Return the friendly name. Does a reverse DNS lookup for the IP address. * * @param status - The errno value for the lookup *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -