⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipaddress.cxx

📁 一个SIP协议栈
💻 CXX
字号:
/********************************************************************* $Id: IPAddress.cxx,v 1.3 1999/08/31 02:22:05 cullen Exp $ *********************************************************************  This library is free software; you can redistribute it and/or modify  it under the terms of the GNU Lesser General Public License as  published by the Free Software Foundation; either version 2 of the  License, or (at your option) any later version.  In addition to the  terms and conditions set forth in the GNU Lesser General Public  License, as a condition of using this library you are required to  grant to all users of this library or any implementation utilizing  or derived from this library a reciprocal, no cost, worldwide,  perpetual, non-exclusive, non-transferable, unrestricted license to  your claims of all patents and patent applications throughout the  world that are infringed by the library or any implementation  utilizing or derived from this library.  In the event you  redistribute this library or any implementation utilizing or derived  from this library, you must prominently display the foregoing terms  and conditions with the library or the implementation utilizing or  derived from this library.   In the event of a conflict of terms between the foregoing license  grant and the terms set forth in the GNU Lesser General Public  License, the foregoing terms and conditions shall be deemed to  govern.   This library is distributed in the hope that it will be useful, but  WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  Lesser General Public License for more details.   You should have received a copy of the GNU Lesser General Public  License along with this library; if not; write to the Free Software  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.   Copyright 1999 Vovida Networks, Inc.  All Rights Reserved. ********************************************************************* $Log: IPAddress.cxx,v $ Revision 1.3  1999/08/31 02:22:05  cullen updated header **********************************************************************/#include <iostream.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string>#include <netdb.h>#include <unistd.h>#include "IPAddress.h"#include "TCPSocket.h"#include "MHError.h"#include "Trace.h"using namespace mh323;//------------------------------------------------------------------------------// IPAddress constructor//IPAddress::IPAddress(){    IN("IPAddress::IPAddress()");    char cMyHost[101];    if (gethostname(cMyHost,100) == -1)    {        SOCKET_EXCEPTION("Can't get host name" , errno);    }    std::string xMyHost(cMyHost);    set(xMyHost);    m_iPort=0;};//------------------------------------------------------------------------------// IPAddress costructor//IPAddress::IPAddress(const std::string  & xAddress){    IN("IPAddress::IPAddress(const std::string)");    m_iPort=0;    set(xAddress);};//------------------------------------------------------------------------------// IPAddress construstor//IPAddress::IPAddress(char* pcAddress, unsigned int iPort){    IN("IPAddress::IPAddress( char * , unsigned int)");    set(pcAddress,iPort);};//------------------------------------------------------------------------------// IPAddress construstor//IPAddress::IPAddress(unsigned long lAddress, unsigned int iPort){    IN("IPAddress::IPAddress( unsigned long , unsigned int)");    set(lAddress,iPort);};//------------------------------------------------------------------------------//IPAddress set value method//void IPAddress::set(const char* pcAddress, unsigned int iPort){    IN("IPAddress::set( char * , unsigned int)");    memcpy(m_pcAddress,pcAddress,4);    m_iPort=iPort;};//------------------------------------------------------------------------------// IPAddress set value method//void IPAddress::set(unsigned long lAddress, unsigned int iPort){    IN("IPAddress::set( unsigned long, unsigned int)");    memcpy(m_pcAddress,(void *) &lAddress,4);    m_iPort=iPort;}//------------------------------------------------------------------------------// IPAddress setPort value method//void IPAddress::setPort( unsigned int iPort ){    IN("IPAddress::setPort( unsigned int)");    m_iPort=iPort;}//------------------------------------------------------------------------------//  IPAddress set value method//void IPAddress::set(ulong lAddress){    IN("IPAddress::set( ulong )");    memcpy(m_pcAddress,(void *) &lAddress,4);}//------------------------------------------------------------------------------//IPAddress set value method//void IPAddress::set(const std::string & xAddress){    IN("IPAddress::set(const std::string )");    struct in_addr temp;    struct hostent * pxHostEnt;    std::string Address, Port;    int iPos;    MHTRACE(9,"Input  ["<< xAddress << "]");    iPos=xAddress.find(":");    if (iPos != std::string::npos)    {        Address=xAddress.substr(0,iPos);        Port=xAddress.substr(iPos+1);        MHTRACE(9,"Address["<< Address << "]");        MHTRACE(9,"Port   ["<< Port << "]");        iPos=atoi(Port.c_str());        if(iPos)         {            m_iPort=iPos;         }        else        {            SOCKET_EXCEPTION ("Incorrect Port ", -1);        }    }    else // No ':' in input string;    {        Address=xAddress;    }    if (pxHostEnt=gethostbyname(Address.c_str()))    {        memcpy((void *)&m_pcAddress,(void *)pxHostEnt->h_addr,pxHostEnt->h_length);    }    else     {        SOCKET_EXCEPTION ("Incorrect Address ", -1);    }};//------------------------------------------------------------------------------//IPAddress address to char translation//void IPAddress::addressToChar(char * pcAddress) const{    IN("IPAddress::addressToChar(char *)");    memcpy(pcAddress,m_pcAddress,4);};//------------------------------------------------------------------------------//IPAddress getAddr value method//ulong IPAddress::getAddress() const{    IN("IPAddress::getAddress()");    long lTmp;    memcpy((void *) &lTmp,m_pcAddress,4);    return lTmp;};//------------------------------------------------------------------------------//IPAddress get value method//unsigned int  IPAddress::getPort() const{    IN("IPAddress::getPort()");    return m_iPort;};//------------------------------------------------------------------------------//IPAddress operator []//unsigned char IPAddress::operator [] ( int nth) const{    IN("IPAddress::operator []");    return m_pcAddress[nth];};//------------------------------------------------------------------------------//IPAddress::operator < - Compare two IPAddresses//bool IPAddress::operator < (const IPAddress & xAddress ) const{    IN("IPAddress::operator < (const IPAddress & )" );    for (int i = 0 ; i<= 3 ; i++ )    {        if (m_pcAddress[i] < xAddress.m_pcAddress[i])         {            return true;        }        if (xAddress.m_pcAddress[i] < m_pcAddress[i])         {            return false;        }    }//    return false;    return m_iPort < xAddress.m_iPort;  }//------------------------------------------------------------------------------//IPAddress typecast to std::string//IPAddress::operator std::string() {    IN("IPAddress::operator std::string()");    std::string xStr;    std::strstream xStream;    xStream << (*this);    xStr=xStream.str();    xStream.freeze();    return xStr;};//------------------------------------------------------------------------------ostream & IPAddress::printOn(ostream & xStr) const{    struct hostent * pxHostEnt;    struct in_addr temp;    memcpy((void *)&temp.s_addr,m_pcAddress,4);    char * cpBuf = inet_ntoa(temp);    if (pxHostEnt=gethostbyaddr((char*)m_pcAddress,4,AF_INET))    {        xStr<<pxHostEnt->h_name<<"("<<cpBuf<<")";    }    else    {        MHTRACE(9,"gethostbyaddr skipped");        xStr<< cpBuf ;     }    if(m_iPort) xStr << ":" << m_iPort;    return xStr;}//------------------------------------------------------------------------------//IPAddress::getHost - get host name as std::string//std::string IPAddress::getHost( ) const{    struct in_addr temp;    memcpy((void *)&temp.s_addr,m_pcAddress,4);    std::string Address(inet_ntoa(temp));    return  Address;}

⌨️ 快捷键说明

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