📄 address.cpp
字号:
/*_############################################################################ _## _## address.cpp _## _## 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. A D D R E S S. C P P ADDRESS CLASS IMPLEMENTATION DESIGN + AUTHOR: Peter E. Mellquist DESCRIPTION: Implementation file for Address classes. LANGUAGE: ANSI C++=====================================================================*/char address_cpp_version[]="@(#) SNMP++ $Id: address.cpp,v 1.19 2006/03/25 11:46:19 katz Exp $";#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include "snmp_pp/address.h"#include "snmp_pp/v3.h"#ifdef SNMP_PP_NAMESPACEnamespace Snmp_pp {#endif/* Borlands isdigit has a bug */#ifdef __BCPLUSPLUS__#define my_isdigit(c) ((c) >= '0' && (c) <= '9')#else#define my_isdigit isdigit#endif#ifdef ADDRESS_DEBUG#define ADDRESS_TRACE debugprintf(0, "ADDRESS %p Enter %s", this, __PRETTY_FUNCTION__)#define ADDRESS_TRACE2 debugprintf(0, "ADDRESS op Enter %s", __PRETTY_FUNCTION__)#else#define ADDRESS_TRACE#define ADDRESS_TRACE2#endif#if !defined HAVE_GETHOSTBYNAME_R || !defined HAVE_GETHOSTBYADDR_R || !defined HAVE_REENTRANT_GETHOSTBYNAME || !defined HAVE_REENTRANT_GETHOSTBYADDR#ifdef _THREADSSnmpSynchronized Address::syscall_mutex;#endif#endif//=================================================================//======== Abstract Address Class Implementation ==================//=================================================================Address::Address() : addr_changed(true), valid_flag(false){ ADDRESS_TRACE; memset(address_buffer, 0, sizeof(unsigned char)*ADDRBUF);}//------------[ Address::trim_white_space( char * ptr) ]------------// destructive trim white spacevoid Address::trim_white_space(char *ptr){ ADDRESS_TRACE; char *tmp = ptr; // init while (*tmp==' ') tmp++; // skip leading white space while (*tmp && (*tmp != ' ')) *ptr++ = *tmp++; // move string to beginning *ptr = 0; // set end of string}// Reset the objectvoid Address::clear(){ addr_changed = true; valid_flag = false; memset(address_buffer, 0, sizeof(unsigned char)*ADDRBUF);}//-----------------------------------------------------------------------// overloaded equivlence operator, are two addresses equal?int operator==(const Address &lhs, const Address &rhs){ ADDRESS_TRACE2; return (strcmp((const char*)lhs, (const char*)rhs) == 0);}//------------------------------------------------------------------// overloaded > operator, is a1 > a2int operator>(const Address &lhs, const Address &rhs){ ADDRESS_TRACE2; return (strcmp((const char*)lhs, (const char*)rhs) > 0);}//-----------------------------------------------------------------// overloaded < operator, is a1 < a2int operator<(const Address &lhs, const Address &rhs){ ADDRESS_TRACE2; return (strcmp((const char*)lhs, (const char*)rhs) < 0);}//------------------------------------------------------------------// equivlence operator overloaded, are an address and a string equal?int operator==(const Address &lhs, const char *rhs){ ADDRESS_TRACE2; if (!rhs && !lhs.valid()) return TRUE; if (strcmp((const char *)lhs, rhs) == 0) return TRUE; return FALSE;}//------------------------------------------------------------------// overloaded > , is a > inaddrint operator>(const Address &lhs, const char *rhs){ ADDRESS_TRACE2; if (!rhs) return lhs.valid(); // if lhs valid then > NULL, else invalid !> NULL if (strcmp((const char *)lhs, rhs) > 0) return TRUE; return FALSE;}//------------------------------------------------------------------// overloaded >= , is a >= inaddrint operator>=(const Address &lhs, const char *rhs){ ADDRESS_TRACE2; if (!rhs) return TRUE; // always >= NULL if (strcmp((const char *)lhs, rhs) >= 0) return TRUE; return FALSE;}//-----------------------------------------------------------------// overloaded < , are an address and a string equal?int operator<(const Address &lhs, const char *rhs){ ADDRESS_TRACE2; if (!rhs) return FALSE; // always >= NULL if (strcmp((const char *)lhs, rhs) < 0) return TRUE; return FALSE;}//-----------------------------------------------------------------// overloaded <= , is a <= inaddrint operator<=(const Address &lhs, const char *rhs){ ADDRESS_TRACE2; if (!rhs) return !lhs.valid(); // invalid == NULL, else valid > NULL if (strcmp((const char *)lhs, rhs) <= 0) return TRUE; return FALSE;}//=====================================================================//============ IPAddress Implementation ===============================//=====================================================================//-------[ construct an IP address with no agrs ]----------------------IpAddress::IpAddress() : Address(), iv_friendly_name_status(0), ip_version(version_ipv4){ ADDRESS_TRACE; // always initialize what type this object is smival.syntax = sNMP_SYNTAX_IPADDR; smival.value.string.len = IPLEN; smival.value.string.ptr = address_buffer; memset(iv_friendly_name, 0, sizeof(char) * MAX_FRIENDLY_NAME);}//-------[ construct an IP address with a string ]---------------------IpAddress::IpAddress(const char *inaddr) : Address(){ ADDRESS_TRACE; // always initialize what type this object is smival.syntax = sNMP_SYNTAX_IPADDR; smival.value.string.len = IPLEN; smival.value.string.ptr = address_buffer; // parse_address initializes valid, address_buffer & iv_friendly_name valid_flag = parse_address(inaddr);}//-----[ IP Address copy constructor ]---------------------------------IpAddress::IpAddress(const IpAddress &ipaddr) : iv_friendly_name_status(0), ip_version(ipaddr.ip_version){ ADDRESS_TRACE; // always initialize what type this object is smival.syntax = sNMP_SYNTAX_IPADDR; smival.value.string.len = ipaddr.smival.value.string.len; smival.value.string.ptr = address_buffer; memset(iv_friendly_name, 0, sizeof(char) * MAX_FRIENDLY_NAME); valid_flag = ipaddr.valid_flag; if (valid_flag) { // copy the address data MEMCPY(address_buffer, ipaddr.address_buffer, smival.value.string.len); // and the friendly name strcpy(iv_friendly_name, ipaddr.iv_friendly_name); if (!ipaddr.addr_changed) { memcpy(output_buffer, ipaddr.output_buffer, sizeof(unsigned char) * OUTBUFF); addr_changed = false; } }}//-----[ construct an IP address with a GenAddress ]---------------------IpAddress::IpAddress(const GenAddress &genaddr) : iv_friendly_name_status(0){ ADDRESS_TRACE; // always initialize what type this object is smival.syntax = sNMP_SYNTAX_IPADDR; smival.value.string.len = IPLEN; smival.value.string.ptr = address_buffer; memset(iv_friendly_name, 0, sizeof(char) * MAX_FRIENDLY_NAME); output_buffer[0]=0; // allow use of an ip or udp genaddress valid_flag = genaddr.valid(); if (valid_flag) { if (genaddr.get_type() == type_ip) { // copy in the IP address data *this = genaddr.cast_ipaddress(); return; } else if (genaddr.get_type() == type_udp) { // copy in the IP address data *this = genaddr.cast_udpaddress(); return; } } valid_flag = false; addr_changed = true;}//-----[ IP Address general = operator ]-------------------------------SnmpSyntax& IpAddress::operator=(const SnmpSyntax &val){ ADDRESS_TRACE; if (this == &val) return *this; // protect against assignment from itself addr_changed = true; valid_flag = false; // will get set TRUE if really valid memset(iv_friendly_name, 0, sizeof(char) * MAX_FRIENDLY_NAME); if (val.valid()) { switch (val.get_syntax()) { case sNMP_SYNTAX_IPADDR: case sNMP_SYNTAX_OCTETS: if ((((IpAddress &)val).smival.value.string.len == IPLEN) || (((IpAddress &)val).smival.value.string.len == UDPIPLEN)) { MEMCPY(address_buffer, ((IpAddress &)val).smival.value.string.ptr, IPLEN); valid_flag = true; ip_version = version_ipv4; smival.value.string.len = IPLEN; } else if ((((IpAddress &)val).smival.value.string.len == IP6LEN) || (((IpAddress &)val).smival.value.string.len == UDPIP6LEN)) { MEMCPY(address_buffer, ((IpAddress &)val).smival.value.string.ptr, IP6LEN); valid_flag = true; ip_version = version_ipv6; smival.value.string.len = IP6LEN; } break; // NOTE: as a value add, other types could have "logical" // mappings, i.e. integer32 and unsigned32 } } return *this;}//------[ assignment to another ipaddress object overloaded ]-----------------IpAddress& IpAddress::operator=(const IpAddress &ipaddr){ ADDRESS_TRACE; if (this == &ipaddr) return *this; // protect against assignment from itself valid_flag = ipaddr.valid_flag; memset(iv_friendly_name, 0, sizeof(char) * MAX_FRIENDLY_NAME); if (valid_flag) { if (ipaddr.ip_version == version_ipv4) { MEMCPY(address_buffer, ipaddr.address_buffer, IPLEN); ip_version = version_ipv4; smival.value.string.len = IPLEN; } else { MEMCPY(address_buffer, ipaddr.address_buffer, IP6LEN); ip_version = version_ipv6; smival.value.string.len = IP6LEN; } strcpy(iv_friendly_name, ipaddr.iv_friendly_name); if (ipaddr.addr_changed) addr_changed = true; else { memcpy(output_buffer, ipaddr.output_buffer, sizeof(unsigned char) * OUTBUFF); addr_changed = false; } } else addr_changed = true; return *this;}IpAddress& IpAddress::operator=(const char *inaddr){ ADDRESS_TRACE; valid_flag = parse_address(inaddr); addr_changed = true; return *this;}//-------[ return the friendly name ]----------------------------------char *IpAddress::friendly_name(int &status){ ADDRESS_TRACE; if ((iv_friendly_name[0]==0) && (valid_flag)) this->addr_to_friendly(); status = iv_friendly_name_status; return iv_friendly_name;}// parse a dotted stringint IpAddress::parse_dotted_ipstring(const char *inaddr){ ADDRESS_TRACE; int token_count=0; char temp[30]; // temp buffer for destruction // check len, an ip can never be bigger than 15 // 123456789012345 // XXX.XXX.XXX.XXX if (!inaddr || (strlen(inaddr) > 30)) return FALSE; strcpy(temp, inaddr); trim_white_space(temp); if (strlen(temp) > 15) return FALSE; /* Check for the following: * - exactly three dots * - no dot at begin or end * - at least a digit between two dots * - only dots and digits allowed */ char *ptr = temp; int dot_count = 0; bool last_char_was_dot = true; while (*ptr) { if (*ptr == '.') { if (last_char_was_dot) return FALSE; ++dot_count; last_char_was_dot = true; } else if (my_isdigit(*ptr)) { last_char_was_dot = false; } else return FALSE; ++ptr; } if ((dot_count != 3) || (last_char_was_dot)) return FALSE; ptr = temp; while (*ptr) { unsigned long number = 0; if (*ptr == '.') ++ptr; // skip over the dot // grab a digit token and convert it to a long int int digits = 0; while ((*ptr) && (*ptr != '.')) { number = (number * 10) + *(ptr++) - '0'; ++digits; } if (digits > 3) return FALSE; if (number > 255) return FALSE; // stuff the value into the array and bump the counter address_buffer[token_count++]= (unsigned char) number; } ip_version = version_ipv4; smival.value.string.len = IPLEN; return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -