00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <string>
00014
00015 #if defined(WIN32)
00016 # include <winsock2.h>
00017 #else
00018 # include <netdb.h>
00019 extern int h_errno;
00020 # include <sys/socket.h>
00021 # include <unistd.h>
00022 # include <sys/utsname.h>
00023 #endif
00024
00025 #include "assa/INETAddress.h"
00026 using namespace ASSA;
00027
00028 string INETAddress::m_fqdn_cache;
00029
00030 void
00031 INETAddress::
00032 init ()
00033 {
00034 ::memset ((char*) &m_address, 0, sizeof(m_address));
00035 }
00036
00037 INETAddress::
00038 INETAddress ()
00039 {
00040
00041 init ();
00042 }
00043
00044 INETAddress::
00045 INETAddress (const char* host_, int port_)
00046 {
00047
00048 init ();
00049 createHostPort (host_, htons (port_));
00050 }
00051
00052 INETAddress::
00053 INETAddress (const char* host_, const char* service_, Protocol protocol_)
00054 {
00055
00056
00057 init ();
00058 createHostPort (host_, getServiceByName (service_,protocol_));
00059 }
00060
00061 INETAddress::
00062 INETAddress (int port_)
00063 {
00064
00065
00066 init ();
00067 createHostPort ("", htons (port_));
00068 }
00069
00070 INETAddress::
00071 INETAddress (SA_IN* address_)
00072 {
00073
00074
00075 init ();
00076 ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
00077 }
00078
00079 INETAddress::
00080 INETAddress (SA* address_)
00081 {
00082
00083
00084 init ();
00085 ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
00086 }
00087
00088 INETAddress::
00089 INETAddress (struct in_addr * haddr_, int port_)
00090 {
00091
00092
00093 init ();
00094 m_address.sin_addr = *haddr_;
00095 m_address.sin_family = AF_INET;
00096 m_address.sin_port = htons(port_);
00097 }
00098
00099 INETAddress::
00100 INETAddress (const char* address_, Protocol protocol_)
00101 {
00102
00103
00104 init ();
00105
00106 string s(address_);
00107 string sPort(s);
00108 int r = 0;
00109 string host;
00110
00111 #ifdef BLOCKED
00112 const u_int HOSTNAMELEN = 64;
00113 char buf[HOSTNAMELEN];
00114 if (gethostname (buf, HOSTNAMELEN) == 0) {
00115 host = buf;
00116 }
00117 #endif
00118
00119 if ( (r = s.find(':')) > 0 ) {
00120 host = s.substr(0, r);
00121 sPort = s.substr(r+1);
00122 }
00123 else if ( (r = s.find('@')) > 0 ) {
00124 sPort = s.substr(0, r);
00125 host = s.substr(r+1);
00126 }
00127
00128 if ( (r = getServiceByName (sPort)) == 0 ) {
00129 return;
00130 }
00131
00132 createHostPort (host.c_str(), r);
00133 }
00134
00135 int
00136 INETAddress::
00137 getServiceByName (string s_, Protocol p_)
00138 {
00139
00140
00141 long l = 0;
00142 struct servent* sp = NULL;
00143
00144 if ((l = strtol (s_.c_str(), (char**) NULL, 10))) {
00145 return htons ((unsigned short int) l);
00146 }
00147
00148 if ((sp = getservbyname (s_.c_str(), (p_==TCP ? "tcp" : "udp")))) {
00149 return sp->s_port;
00150 }
00151
00152 setstate (Address::badbit);
00153 return 0;
00154 }
00155
00156 void
00157 INETAddress::
00158 createHostPort (const char* host_, int port_)
00159 {
00160
00161
00162 struct hostent* hp = 0;
00163
00164 if (strlen (host_) == 0) {
00165 m_address.sin_addr.s_addr = htonl(INADDR_ANY);
00166 goto done;
00167 }
00168
00169 if ((hp = gethostbyname (host_)) == NULL) {
00170 setstate (Address::badbit);
00171 errno = h_errno;
00172 EL((ASSAERR,"gethostbyname (\"%s\") failed\n", host_));
00173 return;
00174 }
00175 memcpy ((char*) &m_address.sin_addr, hp->h_addr_list[0], hp->h_length);
00176
00177 done:
00178 m_address.sin_family = AF_INET;
00179 m_address.sin_port = port_;
00180 }
00181
00182 string
00183 INETAddress::
00184 getHostName ()
00185 {
00186 if (m_address.sin_addr.s_addr == htonl(INADDR_ANY)) {
00187 return ("");
00188 }
00189
00190 struct hostent* hentry;
00191 hentry = gethostbyaddr ((const char*) &m_address.sin_addr,
00192 sizeof(m_address.sin_addr),
00193 AF_INET);
00194 if (hentry == NULL) {
00195 errno = h_errno;
00196 setstate (Address::badbit);
00197 EL((ASSAERR,"gethostbyaddr() failed\n"));
00198 return ("");
00199 }
00200 return hentry->h_name;
00201 }
00202
00203 void
00204 INETAddress::
00205 dump ()
00206 {
00207
00208
00209 Address::dump ();
00210 DL((ADDRESS,"Family - %s\n", ntohs(m_address.sin_family) == AF_INET ?
00211 "AF_INET" : "AF_UNIX"));
00212 DL((ADDRESS,"host - %s\n", getHostName ().c_str()));
00213 DL((ADDRESS,"port - %d\n", getPort ()));
00214 DL((ADDRESS,"address - %s\n", inet_ntoa (m_address.sin_addr)));
00215 }
00216
00217 #if defined(WIN32)
00218 struct utsname
00219 {
00220 char sysname[20];
00221 char nodename[20];
00222 char release[20];
00223 char version[20];
00224 char machine[20];
00225 };
00226 #endif
00227
00228 string
00229 INETAddress::
00230 get_fully_qualified_domain_name (vector<string>& aliases_)
00231 {
00232
00233
00234 if (m_fqdn_cache.length ()) {
00235 return m_fqdn_cache;
00236 }
00237
00238 struct utsname myname;
00239 struct hostent* hptr = NULL;
00240
00241 #if defined(WIN32)
00242 DWORD slen;
00243 slen = sizeof (myname.nodename) - 1;
00244 GetComputerNameA (myname.nodename, &slen);
00245 #else
00246 if (::uname (&myname) < 0) {
00247 EL((ADDRESS,"Hostname is not set!\n"));
00248 return m_fqdn_cache;
00249 }
00250 #endif
00251
00252 if ((hptr = ::gethostbyname (myname.nodename)) == NULL) {
00253 errno = h_errno;
00254 EL((ADDRESS,"gethostbyname (%s) failed\n", myname.nodename));
00255 return m_fqdn_cache;
00256 }
00257 m_fqdn_cache = hptr->h_name;
00258 char** pptr = hptr->h_aliases;
00259 while (*pptr != NULL) {
00260 aliases_.push_back (*pptr);
00261 pptr++;
00262 }
00263
00264 return m_fqdn_cache;
00265 }