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

📄 networkaddress.cpp

📁 symbian平台S60_2nd_FP2_SC rtp实现
💻 CPP
字号:

#include "NetworkAddress.h"
#include "RtpTools.h"
#include "cpLog.h"


NetworkAddress::NetworkAddress ( int port /* = -1 */) :
   iPort(port),
   rawHostName(getLocalHostName()),
   hostName(rawHostName),
   ipAddressSet(false)
{
   memset(ipAddress, 0, sizeof(ipAddress));

   getHostLookupFailed = 1;
   getHostLookupOK = 0;
}


NetworkAddress::NetworkAddress ( const TDesC8& hostname, int port /* = -1 */) :
   iPort(port),
   ipAddressSet(false)
{

   getHostLookupFailed = 1;
   getHostLookupOK = 0;
   
   rawHostName.Copy(hostname);
   hostName = rawHostName;
   memset(ipAddress, 0, sizeof(ipAddress));
}

void
NetworkAddress::setHostName ( const TDesC8& hostname )
{
	TInt pos = hostname.Find(_L8(":"));
	if(pos != -1)
	{
		TLex8 hTo(hostName.Right(hostName.Length() - pos -1));
		hTo.Val(iPort); 

		TBuf<32> aIP;
		TPtrC8 prt(hostname);
		UTF82Unicode(aIP,prt);
		Address.Input(aIP);
		Address.SetPort(iPort);
	}
	else
	{
		rawHostName.Copy(hostname);
	
		TBuf<32> aIP;
		TPtrC8 prt(hostname);
		UTF82Unicode(aIP,prt);
		Address.Input(aIP);
	}

   ipAddressSet = false;
}


TDesC8&
NetworkAddress::getLocalHostName() 
{
	TBuf8<256> abuf(_L8(""));
   char buffer[256];
   if (gethostname(buffer,256) == -1)
   {
      return abuf;
   }
   
   abuf.Append((TUint8 *)buffer,256);
   abuf.ZeroTerminate();
   return abuf;
}

TDesC8&
NetworkAddress::getHostName( ) 
{
   cpLog(LOG_DEBUG_STACK, "NetworkAddress::getHostName()");
   initIpAddress();
   return *(TDes8*)&hostName;
}


TDesC8&
NetworkAddress::getIpName () //把网络字节地址转换为字符地址
{ // ZHAOSONG
	/*
    initIpAddress();
 
	char hostname[256];
	char ipName[256];
 
	strcpy(ipName,inet_ntop(AF_INET, &ipAddress, hostname, sizeof(hostname)));
	
	TBuf8<256> abuf;
	abuf.Append((TUint8 *)ipName,256);
	abuf.ZeroTerminate();
    return abuf;
	*/
	TBuf8<2> abuf(_L8(""));
	return abuf;
}


u_int32_t
NetworkAddress::getIp4Address () 
{
    u_int32_t lTmp;
    initIpAddress();
    memcpy((void *) &lTmp, ipAddress, IPV4_LENGTH);
    return lTmp;
}


void
NetworkAddress::getSockAddr (struct sockaddr & socka)
{
    short tmp_s = htons (iPort);
    char * tmp_p;

    tmp_p = (char*) &tmp_s;
    socka.sa_family = AF_INET;
    socka.sa_data[0] = tmp_p[0];
    socka.sa_data[1] = tmp_p[1];
    initIpAddress();
    memcpy((void *)&socka.sa_data[2], ipAddress, IPV4_LENGTH);
}


bool
operator < (  NetworkAddress & xAddress,
              NetworkAddress & yAddress )
{
    xAddress.initIpAddress();
    yAddress.initIpAddress();
    for (int i = 0 ; i < IPV4_LENGTH; i++ )
    {
        if ( xAddress.ipAddress[i] < yAddress.ipAddress[i])
        {
            return true;
        }
        if ( xAddress.ipAddress[i] > yAddress.ipAddress[i])
        {
            return false;
        }
    }
    return xAddress.iPort < yAddress.iPort ;
}


bool
operator == ( NetworkAddress & xAddress,
              NetworkAddress & yAddress )
{
    xAddress.initIpAddress();
    yAddress.initIpAddress();
    for (int i = 0 ; i < IPV4_LENGTH ; i++ )
    {
        if ( xAddress.ipAddress[i] != yAddress.ipAddress[i])
        {
            return false;
        }
    }
    return xAddress.iPort == yAddress.iPort ;
}


bool
operator != (  NetworkAddress & xAddress,
               NetworkAddress & yAddress )
{
    return !(xAddress == yAddress);
}


NetworkAddress& 
NetworkAddress::operator=( NetworkAddress& x )
{
    this->iPort = x.iPort;
    memcpy( this->ipAddress, x.ipAddress, sizeof(this->ipAddress) );
    ipAddressSet = x.ipAddressSet;
    rawHostName = x.rawHostName;
    hostName = x.hostName;

	Address = x.Address;

	strcpy( remoteHost ,x.remoteHost);
	remotePort = x.remotePort;
	localPort = x.localPort;
    
    return ( *this );
}

void
NetworkAddress::initIpAddress() 
{
    if(!ipAddressSet)
    {
		bool b =   is_valid_ip_addr(rawHostName);
        if (true == b )
        {
           hostName.Copy( rawHostName);
           ipAddressSet = true;
        }
        else //m by zs
        {/*
           struct hostent hostbuf, *hp;
           size_t hstbuflen;
           char *tmphstbuf;
           int res=0;
           int herr;
           
	       hstbuflen = 4024;
           tmphstbuf = (char*)malloc (hstbuflen);

		   (void)herr; 
		   (void)hostbuf;
		   static Mutex	m;
		   {
			 Lock lock(m);
			 hp = gethostbyname(rawHostName.c_str());
		   }
           
           if (res == 0 && hp)
           {
              memcpy((void *)&ipAddress,(void *)hp->h_addr,hp->h_length);
              hostName = hp->h_name;
              ipAddressSet = true;
              free(tmphstbuf);
           }
           else
           {
              cpLog (LOG_ERR, "Could not resolve Address for (%s)", rawHostName.c_str());
           }
			*/
        }
    }
}

NetworkAddress::NetworkAddress(  NetworkAddress& x)
{
    ipAddressSet = x.ipAddressSet;
    rawHostName = x.rawHostName;
    hostName = x.hostName;
    memcpy( ipAddress, x.ipAddress, sizeof(ipAddress) );

	Address = x.Address;
	
	strcpy( remoteHost ,x.remoteHost);
	remotePort = x.remotePort;
	localPort = x.localPort;
}

int
NetworkAddress::getHostByName(const char* hostName,
                              struct hostent* hEnt,
                              char* /*buf*/,
                              int /*buflen*/,
                              int* /*thrErrno*/)
{
    struct hostent* x;
    
    x = gethostbyname(hostName);
    if (x == 0) 
    {
	cpLog(LOG_DEBUG, "********* Failed to get host by name:%s", hostName); 
	return getHostLookupFailed;
    }
    *hEnt = *x;
    return getHostLookupOK;
}
/**
  * Return TRUE if the address is a valid IP address (dot-decimal form)
  * such as 128.128.128.128. Checks for invalid or ill formed addresses 
  * If the address is valid, copy it into ipAddress
  */
bool NetworkAddress::is_valid_ip_addr(const TDesC8& addr)
{
    unsigned long maskcheck = ~255; // mask to check if 'tween 0 and 255
 
	//   const char *advancer = addr.c_str();
	const char *advancer =(char*)addr.Ptr();
    unsigned long octet;
    char *nextchar;

    // always check for spaces and right number
    // first and last fields must be 1-255, middle two 0-255
    
    if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t'))
    {
        return false;
    }
    octet = strtoul(advancer, &nextchar, 10);
    if((*nextchar != '.') || (octet & maskcheck) || (octet == 0))
    {
        return false;
    }
    ipAddress[0] = (TUint8)octet;


    advancer = nextchar+1;
    if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t'))
    {
        return false;
    }
    octet = strtoul(advancer, &nextchar, 10);
    if((*nextchar != '.') || (octet & maskcheck))
    {
        return false;
    }
    ipAddress[1] = (TUint8)octet;


    advancer = nextchar+1;
    if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t'))
    {
        return false;
    }
    octet = strtoul(advancer, &nextchar, 10);
    if((*nextchar != '.') || (octet & maskcheck))
    {
        return false;
    }
    ipAddress[2] = (TUint8)octet;
    

    advancer = nextchar+1;
    if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t'))
    {
        return false;
    }
    octet = strtoul(advancer, &nextchar, 10);
    if((*nextchar) || (octet & maskcheck) || (octet == 0))
    {
        return false;
    }
    ipAddress[3] = (TUint8)octet;
    
    return true;

}

void NetworkAddress::SetInetAddr()
{
  TBuf<32> ip;// = _L("10.11.29.43");
  Str2TBuf(ip,remoteHost);

//	TBuf8<32> aIP;
//	TBuf<32> IP;
//	Str2TBuf1V1(aIP,remoteHost);
//	CnvUtfConverter::ConvertToUnicodeFromUtf8(IP,aIP);
    Address.Input(ip);

  TBuf<32> ap;
  Address.Output(ap);
  Address.SetPort(remotePort);
}
// End of File

⌨️ 快捷键说明

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