address.cpp

来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 2,016 行 · 第 1/4 页

CPP
2,016
字号
/*===================================================================
   
  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

  VERSION 2.8

  DESIGN:
  Peter E. Mellquist

  AUTHOR:
  Peter E Mellquist

  DESCRIPTION:
  Implementation file for Address classes.


  LANGUAGE:
  ANSI C++

  OPERATING SYSTEM(S):
  MS-Windows Win32
  BSD UNIX

=====================================================================*/
char address_cpp_version[]="@(#) SNMP++ 2.8 $Header: address.cpp,v 1.38 96/09/11 14:01:44 hmgr Exp $";

#include "address.h"
extern "C"
{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
}

//=================================================================
//======== Abstract Address Class Implementation ==================
//=================================================================

// allow destruction of derived classes
Address::~Address() 
{};


//-----------------------------------------------------------------
// is the address object valid?
int Address::valid() const
{ return valid_flag; };

//------------[ Address::trim_white_space( char * ptr) ]------------
// destructive trim white space
void Address::trim_white_space( char * ptr)
{
   char *tmp;

   tmp = ptr;
   // skip leading white space
   while (*tmp==' ')tmp++;
   strcpy(ptr,tmp);

   // find end of string
   while ((*tmp!=' ') && (*tmp !=0)) tmp++;
   if (*tmp!=0) *tmp=0;
};

//TM: this is not used nor needed, remove?
//-----[ element access ]----------------------------------------------
unsigned char& Address::operator[]( const int position)
{ 
  if ( position < BUFSIZE)
    return  address_buffer[ position];
  else
    return address_buffer[0]; 
};


//-----------------------------------------------------------------------
// overloaded equivlence operator, are two addresses equal?
int operator==( const Address &lhs, const Address &rhs)
{
  if ( strcmp( (const char*) lhs, (const char*)rhs)==0)
    return TRUE;
  else
    return FALSE;
};

//-----------------------------------------------------------------------
// overloaded equivlence operator, are two addresses equal?
int operator!=( const Address &lhs, const Address &rhs)
{
  return (!( lhs == rhs));
};


//------------------------------------------------------------------
// overloaded > operator, is a1 > a2
int operator>( const Address &lhs, const Address &rhs)
{
   if ( strcmp( (const char*) lhs, (const char*)rhs)>0)
      return TRUE;
   else
      return FALSE;
};

// overloaded >= operator, is a1 > a2
int operator>=( const Address &lhs,const Address &rhs)
{
  if (( lhs > rhs) || ( lhs == rhs))
     return TRUE;
  else
     return FALSE;
};

// overloaded < operator, is a1 <= a2
int operator<=( const Address &lhs,const Address &rhs)
{
  if (( lhs < rhs) || ( lhs == rhs))
     return TRUE;
  else
     return FALSE;

};


//-----------------------------------------------------------------
// overloaded < operator, is a1 < a2
int operator<( const Address &lhs, const Address &rhs)
{
   if ( strcmp( (const char*) lhs, (const char*)rhs)<0)
      return TRUE;
   else
      return FALSE;
};

//------------------------------------------------------------------
// equivlence operator overloaded, are an address and a string equal?
int operator==( const Address &lhs,const char *rhs)
{
  if (!rhs && !lhs.valid())
    return TRUE;
  if (strcmp( (const char *) lhs, rhs)== 0)
     return TRUE;
  else
     return FALSE;
};

//------------------------------------------------------------------
// not equal operator overloaded, are an address and a string not equal?
int operator!=( const Address &lhs,const char *rhs)
{
  return (!( lhs == rhs));
};

//------------------------------------------------------------------
// overloaded > , is a > inaddr
int operator>( const Address &lhs,const char *rhs)
{
  if (!rhs)
    return lhs.valid();  // if lhs valid then > NULL, else invalid !> NULL
  if (strcmp( (const char *) lhs, rhs)> 0)
     return TRUE;
  else
     return FALSE;
};

//------------------------------------------------------------------
// overloaded >= , is a >= inaddr
int operator>=( const Address &lhs,const char *rhs)
{
  if (!rhs)
    return TRUE; // always >= NULL
  if (strcmp( (const char *) lhs, rhs)>= 0)
     return TRUE;
  else
     return FALSE;
};

//-----------------------------------------------------------------
// overloaded < , are an address and a string equal?
int operator<( const Address &lhs,const char *rhs)
{
  if (!rhs)
    return FALSE; // always >= NULL
  if (strcmp( (const char *) lhs, rhs)< 0)
     return TRUE;
  else
     return FALSE;
};

//-----------------------------------------------------------------
// overloaded <= , is a <= inaddr
int operator<=( const Address &lhs,const char *rhs)
{
  if (!rhs)
    return !lhs.valid(); // invalid == NULL, else valid > NULL
  if (strcmp( (const char *) lhs, rhs)<= 0)
     return TRUE;
  else
     return FALSE;
};


//=====================================================================
//============ IPAddress Implementation ===============================
//=====================================================================

//-----------[ syntax type ]----------------------------------------------
SmiUINT32 IpAddress::get_syntax()
{ return sNMP_SYNTAX_IPADDR; };

//-----[ IP Address copy constructor ]---------------------------------
IpAddress::IpAddress(const IpAddress &ipaddr)
{
  // always initialize what type this object is
  smival.syntax = sNMP_SYNTAX_IPADDR;
  smival.value.string.len = IPLEN;
  smival.value.string.ptr = address_buffer;

  iv_friendly_name[0]=0;
  iv_friendly_name_status=0;  
  valid_flag = ipaddr.valid_flag; 
  if (valid_flag)
  {
    // copy the address data    
    MEMCPY(address_buffer, ipaddr.address_buffer, IPLEN);
    // and the friendly name
    strcpy( iv_friendly_name, ipaddr.iv_friendly_name);
  }

  IpAddress::format_output();
};

//-------[ construct an IP address with no agrs ]----------------------
IpAddress::IpAddress( void):Address()
{ 
  // always initialize what type this object is
  smival.syntax = sNMP_SYNTAX_IPADDR;
  smival.value.string.len = IPLEN;
  smival.value.string.ptr = address_buffer;

  valid_flag=FALSE; 
  iv_friendly_name[0]=0; 
  iv_friendly_name_status=0; 
  IpAddress::format_output();
};

//-------[ construct an IP address with a string ]---------------------
IpAddress::IpAddress( const char *inaddr):Address()
{ 
  // 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); 
  IpAddress::format_output();
};


//-----[ construct an IP address with a GenAddress ]---------------------
IpAddress::IpAddress( const GenAddress &genaddr)
{
  // always initialize what type this object is
  smival.syntax = sNMP_SYNTAX_IPADDR;
  smival.value.string.len = IPLEN;
  smival.value.string.ptr = address_buffer;

  valid_flag = FALSE;
  iv_friendly_name[0]=0;
  iv_friendly_name_status=0;
  // allow use of an ip or udp genaddress
  if (genaddr.get_type() == type_ip)
  {
    valid_flag = genaddr.valid();
    if ( valid_flag)
    {
      // copy in the IP address data
      IpAddress temp_ip( (const char *) genaddr);
      *this = temp_ip;
    }
  } 
  else
  if (genaddr.get_type() == type_udp)
  {
    valid_flag = genaddr.valid();
    if ( valid_flag)
    {
      // copy in the IP address data
      UdpAddress temp_udp( (const char *) genaddr);
      *this = temp_udp;
    }
  } 
  IpAddress::format_output();
};

//-----[ destructor ]--------------------------------------------------
IpAddress::~IpAddress()
{};

//-----[ IP Address general = operator ]-------------------------------
SnmpSyntax& IpAddress::operator=( SnmpSyntax &val)
{
  // protect against assignment from itself
  if ( this == &val )
      return *this;

  valid_flag = 0;	// will get set TRUE if really valid
  iv_friendly_name[0]=0;  

  if (val.valid()){
    switch (val.get_syntax()){
    case sNMP_SYNTAX_IPADDR:
    case sNMP_SYNTAX_OCTETS:
      if (((IpAddress &)val).smival.value.string.len == IPLEN){
         MEMCPY(address_buffer, ((IpAddress &)val).smival.value.string.ptr, IPLEN);
         valid_flag=1;
      }
    break;

    // NOTE: as a value add, other types could have "logical"
    // mappings, i.e. integer32 and unsigned32 
    }
  }
  IpAddress::format_output();
  return *this;
};

//------[ assignment to another ipaddress object overloaded ]-----------------
IpAddress& IpAddress::operator=( const IpAddress &ipaddress)
{
  // protect against assignment from itself
  if ( this == &ipaddress )
      return *this;

  valid_flag = ipaddress.valid_flag; 
  iv_friendly_name[0]=0;  

  if (valid_flag){
    MEMCPY(address_buffer, ipaddress.address_buffer, IPLEN);
    strcpy(iv_friendly_name, ipaddress.iv_friendly_name);
  }
  IpAddress::format_output();
  return *this;
};


//--------[ create a new instance of this Value ]-----------------------
SnmpSyntax WINFAR *IpAddress::clone() const 
    { return (SnmpSyntax *) new IpAddress(*this); };

//-------[ return the friendly name ]----------------------------------
char *IpAddress::friendly_name(int &status)
{  
  if ((iv_friendly_name[0]==0) && (valid_flag))
    this->addr_to_friendly();
  status = iv_friendly_name_status;
  return iv_friendly_name; 
};

// parse a dotted string
int IpAddress::parse_dotted_ipstring( const char *inaddr)
{
   char *ip_token;
   int token_count=0;
   unsigned int value;
   int error_status = FALSE;
   char temp[30];  // temp buffer for destruction
   int z,w;

   // 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;

   // must only have three dots
   // strtok will not catch this !
   char *ptr = temp;
   int dot_count = 0;
   while ( *ptr != 0)
   {
	   if ( *ptr == '.') dot_count++;
       ptr++;
   }
   if ( dot_count != 3)
	   return FALSE;

   // look for dot token separator
   ip_token = strtok( (char *) temp,".");

   // while more tokens..
   while ( ip_token != NULL)
   {
     // verify that the token is all numerics
     w = strlen( ip_token);
     if (w>3) return FALSE;
     for (z=0;z<w;z++)
       if (( ip_token[z] < '0') || ( ip_token[z] > '9'))
	 return FALSE;

     value = ( unsigned int) strtod(ip_token,NULL);
     if (( value > 0)&& ( value <=255))
       address_buffer[token_count] = (unsigned char) value;
     else
       if ( strcmp(ip_token,"0")==0)
	 address_buffer[token_count]= (unsigned char) 0;
       else
	 error_status = TRUE;
     token_count++;
     ip_token = strtok( NULL, ".");
   }

   // gota be four in len
   if ( token_count != 4)
     return FALSE;

   // any parsing errors?
   if ( error_status)
     return FALSE;

  return TRUE;
};

//-----[ IP Address parse Address ]---------------------------------
int IpAddress::parse_address( const char *inaddr)
{
   // parse the input char array
   // fill up internal buffer with four ip bytes
   // set and return validity flag

   IN_ADDR ipAddr;
   hostent *lookupResult;
   char	   *namePtr = NULL;
   char ds[30];


   // intialize the friendly_name member variable
   iv_friendly_name[0] = 0;
   iv_friendly_name_status = 0;

   // is this a dotted IP notation string or
   // a friendly name
   if ( parse_dotted_ipstring( inaddr))
   {

     // since this is a valid dotted string
     // don't do any DNS
     return TRUE;
   }
   else
   // not a dotted string, try to resolve it via DNS
   {
   
      lookupResult = gethostbyname( inaddr);

      if ( lookupResult)
      {
	 if (lookupResult->h_length == sizeof(in_addr))
	 {
	   memcpy((void *) &ipAddr, (void *) lookupResult->h_addr_list[0],
		  sizeof(IN_ADDR));

	   // now lets check out the dotted string
	   strcpy( ds,inet_ntoa(ipAddr));

	   if ( !parse_dotted_ipstring( ds))
	     return FALSE;

	   // save the friendly name
	   strcpy( iv_friendly_name, inaddr);

	   return TRUE;
	 }
      }	 // end if lookup result
      else
      {
	iv_friendly_name_status = h_errno;
	return FALSE;
      }

   }  // end else not a dotted string
   return TRUE;
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?