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

Address.cpp

Go to the documentation of this file.
00001 /*
00002   Address - address of a remote for socket communication
00003  
00004   Module:    $RCSfile: Address.cpp,v $
00005   Date:      $Date: 2002/01/16 13:08:14 $
00006   Version:   $Revision: 1.29 $
00007   ID:        $Id: Address.cpp,v 1.29 2002/01/16 13:08:14 zoran Exp $
00008   Authors:   Zoran Constantinescu <zoranc@acm.org>
00009 */
00010 
00011 #include <stdio.h>    // sprintf()
00012 #include <stdlib.h>
00013 #include <string.h>
00014 
00015 #include "defs.h"
00016 #include "Address.h"
00017 
00018 #ifndef _WIN32
00019 #include <netdb.h>       // for gethostbyname()
00020 #include <netinet/in.h>  // fot ntohl()
00021 #endif  // _WIN32
00022 
00023 uint Address::thishost;
00024 
00025 Address::Address ()
00026 {
00027   setHost ((uint)0);
00028   setPort (0);
00029 }
00030 
00031 
00032 Address::Address (const char *host, int port)
00033 { 
00034   setHost (host);
00035   setPort (port); 
00036 }
00037 
00038 
00039 Address::Address (uint host, int port)
00040 { 
00041   setHost (host);
00042   setPort (port); 
00043 }
00044 
00045 
00046 Address::Address (const Address &other)
00047 {
00048   host = other.host;
00049   port = other.port;
00050 }
00051 
00052 Address::Address (const char *ip_with_port)
00053 {
00054   setIP (ip_with_port);
00055 }
00056 
00057 Address::~Address ()
00058 {
00059 
00060 }
00061 
00062 
00063 Address *Address::getAddress(int port)
00064 {
00065     char hn[81];
00066 
00067     gethostname(hn, 80);
00068     return new Address(hn, port);
00069 }
00070 
00071 void Address::init_thishost()   //WARNING This is implemented as static member!!!!!
00072 {
00073     char hn[81];
00074     struct hostent *th;
00075 
00076     gethostname(hn, 80);
00077     th = gethostbyname(hn);
00078     // error
00079     if (!th) { 
00080       DBUG_PRINT ("err", ("init_thishost() can't get localhost?"));
00081       Address::thishost = (uint) htonl(0x7f000001);
00082     } else {
00083       uint xhost;
00084       memcpy(&xhost, th->h_addr, th->h_length);
00085       Address::thishost = (uint) htonl(xhost);
00086     }
00087 
00088     DBUG_PRINT("d", ("init_thishost() local address is ADDR=%08x", thishost));
00089 }
00090 
00091 
00092 int Address::setHost (const char *host)
00093 {
00094   int err = 0;
00095   struct hostent* h;
00096 
00097   // identify remote host
00098   h = gethostbyname (host);
00099 
00100   if (!h) {
00101     DBUG_PRINT ("err", ("setHost() can't resolve host '%s', using 'localhost' instead", host));
00102     h = gethostbyname ("localhost");
00103     err = 1;
00104 
00105     if ((!h) || (!(h->h_addr)))
00106       { DBUG_PRINT ("err", ("setHost can't get localhost")); exit(1); }
00107 
00108     if (h->h_length != 4)
00109       { DBUG_PRINT ("err", ("setHost IP addr length!=4 ?")); exit(1); }
00110   }
00111 
00112   uint xhost;
00113   memcpy (&xhost, h->h_addr, h->h_length);
00114   setHost (xhost);
00115 
00116   return err;
00117 }
00118 
00119 
00120 void Address::setHost (uint host)
00121 {
00122   if (host == htonl(0x7f000001))
00123     this->host = thishost;
00124   else
00125     this->host = (uint) htonl (host);
00126 
00127 /*  char _host[20];
00128   // we need to call the gethostbyname() system call!!!
00129   // so that we are sure we are using allways either 'localhost' or the ip
00130 
00131   uchar *chost = (uchar *)&host;
00132   sprintf (_host, "%d.%d.%d.%d", 
00133      chost[0], chost[1], chost[2], chost[3]);
00134   setHost (_host);
00135 */
00136 
00137 //DBUG_PRINT("d", ("ADDR=%08x from %08x", this->host, host));
00138 }
00139 
00140 
00141 void Address::setPort (int port)
00142 {
00143   this->port = port;
00144 }
00145 
00146 
00147 uint Address::getHost () const
00148 {
00149   return (uint) ntohl (this->host);
00150 }
00151 
00152 
00153 uint Address::getHost (char *host) const
00154 {
00155   if (host != 0) {
00156     uint   rhost = (uint) ntohl (this->host);
00157     uchar *chost = (uchar *)&rhost;
00158     sprintf (host, "%d.%d.%d.%d", 
00159        chost[0], chost[1], chost[2], chost[3]);
00160   }
00161   return this->host;
00162 }
00163 
00164 
00165 /*
00166 uint Address::getName (char *host) const
00167 {
00168   if (host != 0) {
00169     uint   rhost = (uint) ntohl (this->host);
00170     uchar *chost = (uchar *)&rhost;
00171     sprintf (host, "%d.%d.%d.%d",
00172              chost[0], chost[1], chost[2], chost[3]);
00173     struct hostent *hostent;
00174     hostent = gethostbyaddr (host);
00175     sprintf (host, "%s", hostent->h_name);
00176   }
00177   return this->host;
00178 }
00179 */
00180 
00181 
00182 void Address::setIP(const char *ip_with_port)
00183 {
00184     char *column = strchr(ip_with_port, ':');
00185     if (column == NULL)
00186     {
00187         setHost((uint)0);
00188         setPort(0);
00189         return;
00190     }
00191     int port = atoi(column + 1);
00192     char host[22];
00193     strncpy(host, ip_with_port, column - ip_with_port);
00194     host[column - ip_with_port]= '\0';
00195     setHost(host);
00196     setPort(port);
00197 }
00198 
00199 
00200 int Address::getPort () const
00201 {
00202   return this->port;
00203 }
00204 
00205 
00206 void Address::getIP(char *ip_with_port) const
00207 {
00208     char host[22];   // 255.255.255.255:65535
00209 
00210     getHost(host);
00211     sprintf(ip_with_port, "%s:%d", host, getPort());
00212 }
00213 
00214 
00215 int Address::equals(uint xhost, uint xport)
00216 {
00217     return (port == xport) && (host == xhost);
00218 }
00219 
00220 
00221 int Address::equals(Address &other)
00222 {
00223     return (port == other.port) && (host == other.host);
00224 }
00225 
00226 
00227 int Address::operator== (Address &other)
00228 {
00229     return equals (other);
00230 }
00231 
00232 
00233 int Address::operator!= (Address &other)
00234 {
00235   return !equals (other);
00236 }
00237 
00238 
00239 Address& Address::operator= (const Address &other)
00240 {
00241   host = other.host;
00242   port = other.port;
00243 
00244   return *this;
00245 }
00246 

Generated on Mon Nov 25 12:46:27 2002 for qadpz by doxygen1.2.18