Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Net/InetAddress.cpp

Go to the documentation of this file.
00001 // Wolf for J2K Library
00002 // LGPL Licensed.
00003 // http://j2k.sourceforge.net/
00004 // Revision #1: Fred P. - Oct 2nd, 2001
00005 
00006 #ifndef __J2K__NET__INETADDRESS_CPP__
00007 #define __J2K__NET__INETADDRESS_CPP__
00008 
00009 #include <j2k/Net/InetAddress.hpp>
00010 
00011 InetAddress::InetAddress() 
00012 {
00013   char host[81];
00014   memset( host, 0, 81 );
00015   gethostname( host, 80 );
00016   setAddress( host );
00017 }
00018 
00019 #ifdef USE_JSTRING
00020 
00021 InetAddress::InetAddress( const JString& hostname )
00022 {
00023   setAddress( hostname.toString() );
00024 }
00025 
00026 JString InetAddress::getHostAddress() const
00027 {
00028   return JString( inet_ntoa(*ip) );
00029 }
00030 
00031 JString InetAddress::getHostName() const
00032 {
00033   return JString(h->h_name);
00034 }
00035 
00036 void InetAddress::setAddress( const JString& hostname)
00037 {
00038    h = gethostbyname( hostname.toString() );
00039    if ( h == NULL ) 
00040    {
00041       switch( h_errno ) 
00042       {
00043         case HOST_NOT_FOUND:
00044           fprintf( stderr, "Host not found: %s\n", hostname.toString() );
00045           break;
00046        case NO_ADDRESS:
00047          fprintf( stderr, "Host %s has no address\n", hostname.toString() );
00048          break;
00049        case NO_RECOVERY:
00050          fprintf( stderr, "Irrecoverable error while getting address for host %s \n", hostname.toString() );
00051          break;
00052        case TRY_AGAIN:
00053          fprintf( stderr, "Temporary errorcondition, try again. %s \n", hostname.toString() );
00054          break;
00055        default:
00056          fprintf( stderr, "Unknown error occured. %d : %s \n", h_errno, strerror( h_errno ) );
00057       }
00058    }
00059   ip = (in_addr*)( h->h_addr );
00060 }
00061 
00062 #else
00063 
00064 InetAddress::InetAddress( const char *hostname)
00065 {
00066   setAddress(hostname);
00067 }
00068 
00069 const char* InetAddress::getHostAddress() const
00070 {
00071   return inet_ntoa(*ip);
00072 }
00073 
00074 const char* InetAddress::getHostName() const 
00075 {
00076 //  char * ret = (char *) malloc(strlen(h->h_name));   // Memory leak!
00077 //  strcpy(ret,h->h_name);
00078 
00079   // Might memory leak too?
00080   return strdup( h->h_name );  //ret; 
00081 }
00082 
00083 void InetAddress::setAddress( const char* hostname )
00084 {
00085    h = gethostbyname( hostname );
00086    if( h == NULL ) 
00087    {
00088      switch(h_errno) 
00089      {
00090        case HOST_NOT_FOUND:
00091          fprintf( stderr, "Host not found: %s\n", hostname );
00092          break;
00093        case NO_ADDRESS:
00094          fprintf( stderr, "Host %s has no address \n", hostname );
00095          break;
00096        case NO_RECOVERY:
00097          fprintf( stderr, "Irrecoverable error while getting address for host %s \n", hostname );
00098          break;
00099        case TRY_AGAIN:
00100          fprintf( stderr, "Temporary errorcondition, try again %s \n", hostname );
00101          break;
00102        default:
00103          fprintf( stderr, "Unknown error occured. %d : %s \n", h_errno, strerror( h_errno ) );
00104      }
00105 
00106    } // End if
00107 
00108    ip = (in_addr*)( h->h_addr );
00109 }
00110 
00111 
00112 #endif
00113 
00114 InetAddress::InetAddress( const InetAddress& src )
00115 {
00116   setAddress( src.getAddress() );
00117 }
00118 
00119 const InetAddress& InetAddress::operator=( const InetAddress& src )
00120 {
00121   setAddress( src.getAddress() );
00122   return *this;
00123 }
00124 
00125 BOOL InetAddress::equals( const InetAddress& right) const
00126 {
00127   return (right.getAddress2() == ip->s_addr);
00128 }
00129 
00130 BOOL InetAddress::operator==( const InetAddress& right) const
00131 {
00132   return (right.getAddress2() == ip->s_addr);
00133 }
00134 
00135 const char* InetAddress::getAddress() const
00136 {
00137 /*
00138   char ret[5]; // 0..4
00139   memcpy( ret, (char*)ip, 4 ); // 0..3
00140   ret[4] = 0;
00141 
00142   return ret;     // Memory leak ???????
00143 */
00144   return (const char*)ip;
00145 }
00146 
00147 unsigned long InetAddress::getAddress2() const
00148 {
00149   return ip->s_addr;
00150 }
00151 
00152 // It's &  NOT &&
00153 inline BOOL InetAddress::isMulticastAddress() const
00154 {
00155   return ((ntohl(ip->s_addr) & 0x80000000) == 0x80000000);
00156 }
00157 
00158 #ifdef USE_JSTRING
00159 
00160 JString InetAddress::toString() const
00161 {
00162   return JString( inet_ntoa(*ip) );
00163 }
00164 
00165 #else
00166 
00167 const char* InetAddress::toString() const
00168 {
00169   return inet_ntoa( *ip );
00170 }
00171 
00172 #endif
00173 
00174 //
00175 // The following are const methods
00176 //
00177 
00178 #ifdef USE_JSTRING
00179 
00180 const InetAddress* InetAddress::getAllByName( const JString& host ) const
00181 {
00182   return getAllByName( host.toString() );
00183 }
00184 
00185 const InetAddress InetAddress::getByName( const JString& host ) const
00186 {
00187   return InetAddress( host );
00188 }
00189 
00190 #else
00191 
00192 const InetAddress InetAddress::getByName( const char* host ) const
00193 {
00194   return InetAddress( host );
00195 }
00196 
00197 #endif
00198 
00199 const InetAddress* InetAddress::getAllByName( const char* host ) const
00200 {
00201   InetAddress *ret   = NULL;
00202   int n_entries      = 0;
00203   int i              = 0;
00204   struct hostent *he = gethostbyname(host);
00205   struct in_addr *ip = NULL;
00206 
00207   if ( he == NULL ) 
00208   {
00209     switch( h_errno ) 
00210     {
00211       case HOST_NOT_FOUND:
00212         fprintf( stderr, "Host not found: %s \n", host );
00213         break;
00214       case NO_DATA:
00215         fprintf( stderr, "Host \"%s\" has no address \n", host );
00216         break;
00217       case NO_RECOVERY:
00218         fprintf( stderr, "Irrecoverable error while getting address for host %s \n", host );
00219         break;
00220       case TRY_AGAIN:
00221         fprintf( stderr, "Temporary error condition, try again", host );
00222         break;
00223       default:
00224         fprintf( stderr, "Unknown error occured. %d : %s \n", h_errno, strerror( h_errno ) );
00225     }
00226     return NULL;
00227   }
00228   
00229   // BUG ???????? ........................\/
00230   for(i=0;*he->h_addr_list[i] != '\0';i++);      // count number of addresses
00231 
00232   n_entries = i;
00233   ret = new InetAddress[i+1];
00234   
00235   for(i=0;i < n_entries;i++) 
00236   {
00237     ip = (struct in_addr *)he->h_addr_list[i];       // get the address number i
00238     ret[i].setAddress(inet_ntoa(*ip));               // create a new InetAddress object 
00239                                                      // from a struct in_addr
00240   }
00241 
00242   // DELETE ??!? Memory leak
00243   
00244   return ret;
00245 }
00246 
00247 /*
00248 static InetAddress InetAddress::getLocalHost()
00249 {
00250   static InetAddress I;
00251   return I;
00252 }
00253 */
00254 
00255 #endif // __J2K__NET__INETADDRESS_CPP__

Generated on Sun Oct 14 18:46:33 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001