00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #ifdef __GNUG__
00065 # pragma implementation "InetAddress.h"
00066 #endif
00067
00068 #include <EHnet++/InetAddress.h>
00069 #include <cctype>
00070 #include <sys/socket.h>
00071 #include <cstdlib>
00072
00073 namespace strmod {
00074 namespace ehnet {
00075
00076 ::std::string InetAddress::AsString()
00077 {
00078 ::std::string portnum = ToDec(port);
00079 ::std::string temp = hostname;
00080
00081
00082
00083
00084 temp += " port ";
00085 temp += portnum;
00086
00087 return(temp);
00088 }
00089
00090 ::std::string InetAddress::GetHostname(bool forcelookup)
00091 {
00092 if (forcelookup) {
00093 hostname = IaddrToName(inaddr);
00094 return(hostname);
00095 } else {
00096 return(GetHostname());
00097 }
00098 }
00099
00100 const InetAddress &InetAddress::operator =(const InetAddress &b)
00101 {
00102
00103
00104 if (this != &b) {
00105 hostname = b.hostname;
00106 port = b.port;
00107 inaddr = b.inaddr;
00108 }
00109
00110 return(*this);
00111 }
00112
00113 const InetAddress &InetAddress::operator =(const sockaddr_in &iadr)
00114 {
00115
00116
00117 if (&inaddr != &iadr) {
00118 inaddr = iadr;
00119 }
00120
00121
00122 inaddr.sin_family = AF_INET;
00123 memset(inaddr.sin_zero, '\0', sizeof(inaddr.sin_zero));
00124
00125
00126
00127 port = ntohs(iadr.sin_port);
00128 hostname = IaddrToName(inaddr);
00129
00130 return(*this);
00131 }
00132
00133 InetAddress::InetAddress(const ::std::string &h_name, U2Byte prt) :
00134 hostname(h_name), port(prt)
00135 {
00136 const char *c_hostname;
00137
00138 c_hostname = h_name.c_str();
00139 memset(&inaddr, '\0', sizeof(inaddr));
00140 inaddr.sin_family = AF_INET;
00141 inaddr.sin_port = htons(port);
00142 U4Byte saddr = 0;
00143
00144 if (ParseNumeric(c_hostname, saddr)) {
00145
00146
00147 hostname = IaddrToName(inaddr);
00148 inaddr.sin_addr.s_addr = saddr;
00149 } else if (NameToIaddr(c_hostname, saddr)) {
00150
00151
00152
00153 hostname = h_name;
00154 inaddr.sin_addr.s_addr = saddr;
00155 } else {
00156
00157
00158 InvalidateAddress();
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167 InetAddress::InetAddress(U2Byte prt) : port(prt)
00168 {
00169 memset(&inaddr, '\0', sizeof(inaddr));
00170 inaddr.sin_family = AF_INET;
00171 inaddr.sin_port = htons(port);
00172 inaddr.sin_addr.s_addr = INADDR_ANY;
00173 hostname = IaddrToName(inaddr);
00174 }
00175
00176 InetAddress::InetAddress(const InetAddress &b) : hostname(b.hostname),
00177 port(b.port),
00178 inaddr(b.inaddr)
00179 {
00180 }
00181
00182 InetAddress::InetAddress(const sockaddr_in &iadr) : port(0)
00183 {
00184 memset(&inaddr, '\0', sizeof(inaddr));
00185 *this = iadr;
00186 }
00187
00188 void InetAddress::InvalidateAddress()
00189 {
00190 hostname = ::std::string("invalid.host.name");
00191
00192 inaddr.sin_addr.s_addr = INADDR_ANY;
00193 }
00194
00195 bool InetAddress::ParseNumeric(const char *numeric_addr, U4Byte &num)
00196 {
00197 if (isdigit(numeric_addr[0])) {
00198 unsigned long num1, num2, num3, num4;
00199 int i;
00200
00201 i = 0;
00202 if (!AsciiToQInum(numeric_addr, i, num1, true))
00203 return(false);
00204 if (!AsciiToQInum(numeric_addr, i, num2, true))
00205 return(false);
00206 if (!AsciiToQInum(numeric_addr, i, num3, true))
00207 return(false);
00208 if (!AsciiToQInum(numeric_addr, i, num4, false))
00209 return(false);
00210 num = htonl((((((num1 << 8) | num2) << 8) | num3) << 8) | num4);
00211 return(true);
00212 } else {
00213 return(false);
00214 }
00215 }
00216
00217 bool InetAddress::AsciiToQInum(const char *s, int &i,
00218 unsigned long &num, bool endsindot)
00219 {
00220 int mynum;
00221
00222 if (!isdigit(s[i])) {
00223 return(false);
00224 }
00225
00226 mynum = atoi(s + i);
00227 if (mynum < 0 || mynum > 255) {
00228 return(false);
00229 }
00230 while (s[i] != '\0' && isdigit(s[i]))
00231 i++;
00232 if (endsindot) {
00233 if (s[i] != '.') {
00234 return(false);
00235 } else {
00236 i++;
00237 }
00238 } else {
00239 if (s[i] != '\0') {
00240 return(false);
00241 }
00242 }
00243 num = mynum;
00244 return(true);
00245 }
00246
00247 ::std::string InetAddress::ToDec(U2Byte num)
00248 {
00249 U2Byte top = 10000;
00250 ::std::string temp;
00251
00252 while (top > 0 && (num / top) < 1)
00253 top /= 10;
00254 if (top <= 0)
00255 temp += '0';
00256 else {
00257 while (top > 0) {
00258 temp += '0' + ((num / top) % 10);
00259 top /= 10;
00260 }
00261 }
00262 return(temp);
00263 }
00264
00265 }
00266 }