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

📄 swiipaddress.cpp

📁 sloedgy open sip stack source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   aPort = port;
 }
 
 
 SWIipAddress::SWIipAddress ( const char * hostName, int port)
   : ipAddressSet(false)
 {
   setport(port);
   setHostName(hostName);
 }
 
 
 int SWIipAddress::setHostName ( const char * theAddress)
 {
   //
   // Look for semicolon which means the port address is appended
   //
   int colonPos = -1;
 
   const char * cstrAddress = theAddress;
 
   while (*cstrAddress)
   {
     if (*cstrAddress == ':')
     {
       colonPos = cstrAddress - theAddress;
       break;
     }
     cstrAddress++;
   }
 
   //
   // Found the port address so get it
   //
   if (colonPos != -1)
   {
     aPort = atoi(theAddress + (colonPos + 1));
 
     if (aPort)
     {
       strncpy(rawHostName, theAddress, colonPos - 1);
       rawHostName[colonPos] = '\0';
     }
     else
     {
       return -1;
     }
 
   }
   else // No ':' in input string;
   {
     strcpy(rawHostName,theAddress);
   }
 
   ipAddressSet = false;
   return 0;
 }
 
 
 void SWIipAddress::setport( int iPort )
 {
     ipAddressSet = false;
     aPort = iPort;
 }
 
 
 char * SWIipAddress::getHostName( ) const
 {
   char * hostName;
   struct hostent * pxHostEnt = NULL;
 
   initIpAddress();
 
   if ((my_gethostbyaddr_r(
   {
 
   }
   LOCK();
 
   pxHostEnt = gethostbyaddr(ipAddress, IPV4_LENGTH, AF_INET);
 
   if (pxHostEnt)
   {
     hostName = pxHostEnt->h_name;
     UNLOCK();
   }
   else
   {
     UNLOCK();
     hostName = getIpName();
   }
 
   return hostName;
 }
 
 
 char * SWIipAddress::getIpName () const
 {
     char * ipName;
     struct in_addr temp;
 
     initIpAddress();
 
     memcpy((void *)&temp.s_addr, ipAddress, IPV4_LENGTH);
 
     LOCK();
 
     // $$$ This returns a pointer to a static char[]
     ipName = inet_ntoa(temp);
 
     UNLOCK();
 
     return ipName;
 }
 
 
 unsigned int SWIipAddress::getIp4Address () const
 {
     unsigned lTmp;
 
     initIpAddress();
 
     memcpy((void *) &lTmp, ipAddress, IPV4_LENGTH);
 
     return lTmp;
 }
 
 
 void SWIipAddress::getSockAddr (struct sockaddr & socka) const
 {
     short tmp_s = htons (aPort);
     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);
 
     return ;
 }
 
 
 int SWIipAddress::getPort () const
 {
     return aPort;
 }
 
 
 bool operator < ( const SWIipAddress & xAddress, const SWIipAddress & 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.aPort < yAddress.aPort ;
 }
 
 
 bool operator == ( const SWIipAddress & xAddress, const SWIipAddress & yAddress )
 {
     xAddress.initIpAddress();
     yAddress.initIpAddress();
     for (int i = 0 ; i < IPV4_LENGTH ; i++ )
     {
         if ( xAddress.ipAddress[i] != yAddress.ipAddress[i])
         {
             return false;
         }
     }
     return xAddress.aPort == yAddress.aPort ;
 }
 
 
 bool operator != ( const SWIipAddress & xAddress, const SWIipAddress & yAddress )
 {
     return !(xAddress == yAddress);
 }
 
 
 SWIipAddress & SWIipAddress::operator=( const SWIipAddress& x )
 {
     this->aPort = x.aPort;
     memcpy( this->ipAddress, x.ipAddress, sizeof(this->ipAddress) );
     ipAddressSet = x.ipAddressSet;
 
     memcpy(this->rawHostName, x.rawHostName, sizeof(this->rawHostName));
     // xraf rawHostName = x.rawHostName;
     //   strncpy( this->ipAddress, x.ipAddress, IPV4_LENGTH );
     return ( *this );
 }
 
 
 unsigned int SWIipAddress::hashIpPort( )
 {
     unsigned int ipAddressLocal = getIp4Address ();
     int port = getPort ();
     return (hashIpPort( ipAddressLocal, port )) ;
 }
 
 
 unsigned int SWIipAddress::hashIpPort( const char * theAddress, const char * port )
 {
     SWIipAddress net;
 
     net.setHostName ( theAddress );
     net.setport ( atoi(port) );
 
     return ( net.hashIpPort() );
 }
 
 
 unsigned int SWIipAddress::hashIpPort( const unsigned int ipAddress, const int port )
 {
     unsigned int hashKey = 0x0;
     hashKey = (ipAddress << 16) | (port & 0xFF);
 
     return hashKey;
 }
 
 int SWIipAddress::initIpAddress() const
 {
   int status = 0;
 
   if (!ipAddressSet)
   {
     if (is_valid_ip_addr(rawHostName) )
     {
       ipAddressSet = true;
     }
     else
     {
       LOCK();
       struct hostent * pxHostEnt = 0;
       pxHostEnt = gethostbyname(rawHostName);
 
       if (pxHostEnt)
       {
         memcpy((void *)&ipAddress,
                (void *)pxHostEnt->h_addr,
                pxHostEnt->h_length);
         ipAddressSet = true;
       }
       else
       {
         status = -1;
       }
 
       UNLOCK();
     }
   }
   return status;
 }
 
 
 /**
   * 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 SWIipAddress::is_valid_ip_addr(const char * addr) const
 {
 
     unsigned long maskcheck = ~255; // mask to check if 'tween 0 and 255
     const char *advancer = addr;
     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] = (char) 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] = (char) 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] = (char) 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] = (char) octet;
 
     return true;
 
 }
 #endif
 // End of File
#endif

⌨️ 快捷键说明

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