Go to the documentation of this file.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 #include "cxutils/networking/ip4address.h"
00041 #include "cxutils/networking/socket.h"
00042 #include <string.h>
00043
00044 using namespace CxUtils;
00045
00051 IP4Address::IP4Address()
00052 {
00053 mString = "0.0.0.0";
00054 memset(mData, 0, sizeof(unsigned char)*4);
00055 }
00056
00057
00063 IP4Address::IP4Address(const char* ipAddress)
00064 {
00065 SetAddress(std::string(ipAddress));
00066 }
00067
00068
00074 IP4Address::IP4Address(const std::string& ipAddress)
00075 {
00076 SetAddress(ipAddress);
00077 }
00078
00079
00085 IP4Address::IP4Address(const IP4Address& ipAddress)
00086 {
00087 mString = ipAddress.mString;
00088 memcpy(mData, ipAddress.mData, sizeof(unsigned char)*4);
00089 }
00090
00091
00097 IP4Address::~IP4Address()
00098 {
00099 }
00100
00101
00112 bool IP4Address::SetAddress(const std::string& ipAddress)
00113 {
00114 int a, b, c, d;
00115 if(ipAddress.empty() == false &&
00116 sscanf(ipAddress.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) == 4)
00117 {
00118 mString = ipAddress;
00119 mData[0] = (unsigned char)a;
00120 mData[1] = (unsigned char)b;
00121 mData[2] = (unsigned char)c;
00122 mData[3] = (unsigned char)d;
00123 return true;
00124 }
00125 else
00126 {
00127
00128 if(ipAddress == "localhost")
00129 {
00130 mString = "127.0.0.1";
00131 mData[0] = (unsigned char)127;
00132 mData[1] = (unsigned char)0;
00133 mData[2] = (unsigned char)0;
00134 mData[3] = (unsigned char)1;
00135 return true;
00136 }
00137 else
00138 {
00139 struct hostent* h;
00140 h = gethostbyname(ipAddress.c_str());
00141 if(h && h->h_addr_list[0] != 0)
00142 {
00143 struct in_addr addr;
00144 memcpy(&addr, h->h_addr_list[0], sizeof(struct in_addr));
00145 return SetAddress(std::string(inet_ntoa(addr)));
00146 }
00147 }
00148 mString = "0.0.0.0";
00149 memset(mData, 0, sizeof(unsigned char)*4);
00150 }
00151 return false;
00152 }
00153
00154
00167 bool IP4Address::SetAddress(const unsigned char a,
00168 const unsigned char b,
00169 const unsigned char c,
00170 const unsigned char d)
00171 {
00172 mData[0] = a;
00173 mData[1] = b;
00174 mData[2] = c;
00175 mData[3] = d;
00176 char buffer[128];
00177 sprintf(buffer, "%d.%d.%d.%d", a, b, c, d);
00178 mString = buffer;
00179 return true;
00180 }
00181
00182
00188 IP4Address& IP4Address::operator=(const IP4Address& ipAddress)
00189 {
00190 mString = ipAddress.mString;
00191 memcpy(mData, ipAddress.mData, sizeof(unsigned char)*4);
00192 return *this;
00193 }
00194
00200 IP4Address& IP4Address::operator=(const std::string& ipAddress)
00201 {
00202 SetAddress(ipAddress);
00203 return *this;
00204 }
00205
00211 bool IP4Address::operator<(const IP4Address& ipAddress) const
00212 {
00213 return mString < ipAddress.mString;
00214 }
00215
00216
00222 bool IP4Address::operator<=(const IP4Address& ipAddress) const
00223 {
00224 return mString <= ipAddress.mString;
00225 }
00226
00227
00233 bool IP4Address::operator==(const IP4Address& ipAddress) const
00234 {
00235 if(memcmp(mData, ipAddress.mData, sizeof(unsigned char)*4) == 0)
00236 {
00237 return true;
00238 }
00239 return false;
00240 }
00241
00247 bool IP4Address::operator!=(const IP4Address& ipAddress) const
00248 {
00249 if(memcmp(mData, ipAddress.mData, sizeof(unsigned char)*4) != 0)
00250 {
00251 return true;
00252 }
00253 return false;
00254 }
00255
00256
00262 void IP4Address::Clear()
00263 {
00264 mString = "0.0.0.0";
00265 memset(mData, 0, sizeof(unsigned char)*4);
00266 }
00267
00268