NetworkAddress.h
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
- #ifndef NETWORKADDRESS_HXX_
- #define NETWORKADDRESS_HXX_
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- static const char* const NetworkAddress_hxx_Version =
- "$Id: NetworkAddress.h,v 1.5 2001/06/29 03:56:00 bko Exp $";
- #include "global.h"
- #include "VMutex.h"
- #include <iostream>
- #include <string>
- #include "vtypes.h"
- #ifndef WIN32
- #include <netinet/in.h>
- #endif
- #define IPV4_LENGTH 4
- /** Class which implements a network address. includes functionality
- * to look up network addresses from hostnames or ip addresses as
- * strings.
- */
- class NetworkAddress
- {
- public:
- /// default constructor. The host is initially set to localhost.
- NetworkAddress ( int port = -1 );
- /** The host hostName can be in the same form as passed to
- * setHostName()
- */
- NetworkAddress ( const string& hostName, int port = -1 );
- /** takes IP or DNS with optional ":port" syntax. Examples:
- <P>lab1.vovida.com
- <BR>192.168.4.5
- <BR>lab1.vovoda.com:80
- <BR>192.168.4.5:6000
- */
- void setHostName ( const string& theAddress );
- /// set port
- void setPort ( int port );
- /// get hostname in the form "lab1.vovida.com"
- string getHostName () const;
- /// get IP address as a string in the form "192.168.4.5"
- string getIpName () const;
- /** return the IP4 address packed as an unsigned int, in
- * network byte order.
- */
- u_int32_t getIp4Address () const;
- /** return the address in a sockaddr. This value is assigned
- * @param socka returned address
- */
- void getSockAddr(struct sockaddr & socka) const;
- /// get the port
- int getPort () const;
- /// compare two IP addresses
- friend bool operator < (const NetworkAddress & xAddress,
- const NetworkAddress & yAddress );
- /// compare two IP addresses
- friend bool operator == (const NetworkAddress & xAddress,
- const NetworkAddress & yAddress );
- /// compare two IP addresses
- friend bool operator != (const NetworkAddress & xAddress,
- const NetworkAddress & yAddress );
- /// convert to string - returns same as getHostName
- operator string() ;
- /// printing operator -- calls print() fn.
- friend ostream & operator<< ( ostream & xStream,
- const NetworkAddress& rxObj );
- /// copy constructor
- NetworkAddress( const NetworkAddress& x);
- /// assignment operator =
- NetworkAddress& operator=( const NetworkAddress& x );
- /// printing operator. This should be replaced w/ Vocal::IO:Writer
- ostream & print ( ostream & xStr ) const;
- /** hash function, uses last 2 bytes of ip address + 2 bytes
- of port the bits 15-0 of ipaddress (Bigendian) becomes the
- Most Significant Byte(MSB) of hashkey, bits 15-0 of port
- becomes LSB of hashkey.
- */
- u_int32_t hashIpPort( );
- /// hash function, uses last 2 bytes of ip address + 2 bytes of port
- static u_int32_t
- hashIpPort( const string& theAddress, const string& port );
- /// hash function, uses last 2 bytes of ip address + 2 bytes of port
- static u_int32_t
- hashIpPort( const u_int32_t ipAddress, const int port );
- /// Mutexes the gethostbyname(), return 0 when successful
- static int getHostByName(const char* hostName,
- struct hostent* hEnt,
- char* buf,
- int buflen,
- int* thrErrno);
- #ifndef WIN32
- /// value if host lookup failed
- static const int getHostLookupFailed = 1;
- /// value if host lookup succeeded
- static const int getHostLookupOK = 0;
- #else
- /// value if host lookup failed
- static const int getHostLookupFailed; // = 1;
- /// value if host lookup succeeded
- static const int getHostLookupOK; // = 0;
- #endif // WIN32
- private:
- /// Check if parameter is a valid IP address
- bool is_valid_ip_addr (const string& ip_addr) const;
- void initIpAddress() const;
-
- mutable char ipAddress[IPV4_LENGTH]; // it's in network byte order
- int aPort;
- string rawHostName;
- mutable string hostName;
- mutable bool ipAddressSet;
- static VMutex mutex_;
- };
- inline std::ostream &
- operator<< ( std::ostream & xStream, const NetworkAddress& rxObj )
- {
- return rxObj.print ( xStream );
- }
- // NETWORKADDRESS_HXX_
- #endif