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 #include "CeylanIPAddressvFour.h"
00028
00029 #include "CeylanOperators.h"
00030 #include "CeylanRegularExpression.h"
00031
00032
00033 #ifdef CEYLAN_USES_CONFIG_H
00034 #include "CeylanConfig.h"
00035 #endif // CEYLAN_USES_CONFIG_H
00036
00037
00038 #include <sstream>
00039 using std::stringstream ;
00040
00041
00042 using std::string ;
00043
00044
00045 using namespace Ceylan::Network ;
00046
00047
00048
00049 IPAddressvFour::IPAddressvFour(
00050 IPvFourComponent first,
00051 IPvFourComponent second,
00052 IPvFourComponent third,
00053 IPvFourComponent fourth ) :
00054 IPAddress(),
00055 _first ( first ),
00056 _second( second ),
00057 _third ( third ),
00058 _fourth( fourth )
00059 {
00060
00061 validate() ;
00062
00063 }
00064
00065
00066
00067 IPAddressvFour::IPAddressvFour( const string & IPString )
00068 {
00069
00070 stringstream ss ;
00071
00072 ss << IPString ;
00073
00074
00075 Ceylan::Uint16 element ;
00076
00077
00078 char ch ;
00079
00080 ss >> element ;
00081
00082
00083
00084
00085
00086
00087 if ( ss.fail() )
00088 throw IPAddressException(
00089 "IPAddressvFour: unable to construct an IPv4 instance "
00090 "from invalid IP string '" + IPString + "': conversion error" ) ;
00091
00092 if ( element > 255 )
00093 throw IPAddressException(
00094 "IPAddressvFour: unable to construct an IPv4 instance "
00095 "from invalid IP string '" + IPString + "': read too high element: "
00096 + Ceylan::toString( element ) + "." ) ;
00097 else
00098 _first = static_cast<IPvFourComponent>( element ) ;
00099
00100 ss >> ch ;
00101
00102 ss >> element ;
00103
00104 if ( ss.fail() )
00105 throw IPAddressException(
00106 "IPAddressvFour: unable to construct an IPv4 instance "
00107 "from invalid IP string '" + IPString + "': conversion error" ) ;
00108
00109 if ( element > 255 )
00110 throw IPAddressException(
00111 "IPAddressvFour: unable to construct an IPv4 instance "
00112 "from invalid IP string '" + IPString
00113 + "': read too high element: "
00114 + Ceylan::toString( element ) + "." ) ;
00115 else
00116 _second = static_cast<IPvFourComponent>( element ) ;
00117
00118 ss >> ch ;
00119
00120 ss >> element ;
00121
00122 if ( ss.fail() )
00123 throw IPAddressException(
00124 "IPAddressvFour: unable to construct an IPv4 instance "
00125 "from invalid IP string '" + IPString + "': conversion error" ) ;
00126
00127 if ( element > 255 )
00128 throw IPAddressException(
00129 "IPAddressvFour: unable to construct an IPv4 instance "
00130 "from invalid IP string '" + IPString
00131 + "': read too high element: "
00132 + Ceylan::toString( element ) + "." ) ;
00133 else
00134 _third = static_cast<IPvFourComponent>( element ) ;
00135
00136 ss >> ch ;
00137
00138 ss >> element ;
00139
00140 if ( ss.fail() )
00141 throw IPAddressException(
00142 "IPAddressvFour: unable to construct an IPv4 instance "
00143 "from invalid IP string '" + IPString + "': conversion error" ) ;
00144
00145 if ( element > 255 )
00146 throw IPAddressException(
00147 "IPAddressvFour: unable to construct an IPv4 instance "
00148 "from invalid IP string '" + IPString
00149 + "': read too high element: "
00150 + Ceylan::toString( element ) + "." ) ;
00151 else
00152 _fourth = static_cast<IPvFourComponent>( element ) ;
00153
00154
00155 if ( ! ss.eof() )
00156 {
00157
00158 string remainder ;
00159
00160 ss >> remainder ;
00161
00162 throw IPAddressException(
00163 "IPAddressvFour: unable to construct an IPv4 instance "
00164 "from invalid IP string '" + IPString
00165 + "' (too many elements, still having '" + remainder + "')." ) ;
00166
00167 }
00168
00169
00170 try
00171 {
00172
00173 validate() ;
00174
00175 }
00176 catch( const IPAddressException & )
00177 {
00178
00179 throw IPAddressException(
00180 "IPAddressvFour: unable to construct an IPv4 instance "
00181 "from invalid IP string '" + IPString + "'." ) ;
00182
00183 }
00184
00185 }
00186
00187
00188
00189 IPAddressvFour::~IPAddressvFour() throw()
00190 {
00191
00192 }
00193
00194
00195
00196 NetworkAddressType IPAddressvFour::getType() const
00197 {
00198
00199 return Network::IPv4 ;
00200
00201 }
00202
00203
00204
00205 const string IPAddressvFour::toString( Ceylan::VerbosityLevels level ) const
00206 {
00207
00208 return Ceylan::toNumericalString( _first ) + '.'
00209 + Ceylan::toNumericalString( _second ) + '.'
00210 + Ceylan::toNumericalString( _third ) + '.'
00211 + Ceylan::toNumericalString( _fourth ) ;
00212
00213 }
00214
00215
00216
00217 bool IPAddressvFour::IsValidIPv4( const string & ipString )
00218 {
00219
00220 #if CEYLAN_USES_REGEX
00221
00222 const string ipvFourPattern = "^([0-2]{0,1}[0-9]{0,1}[0-9]{1,1}[.]{1,1})"
00223 "{3}([0-2]{0,1}[0-9]{0,1}[0-9]{1,1})$" ;
00224
00225 Ceylan::RegExp target( ipString ) ;
00226
00227 if ( target.matches( ipvFourPattern ) )
00228 {
00229
00230 #endif // CEYLAN_USES_REGEX
00231
00232
00233 try
00234 {
00235 IPAddressvFour testAddress( ipString ) ;
00236 return true ;
00237 }
00238 catch( const IPAddressException & e )
00239 {
00240 return false ;
00241 }
00242
00243
00244 #if CEYLAN_USES_REGEX
00245
00246 }
00247
00248 return false ;
00249
00250 #endif // CEYLAN_USES_REGEX
00251
00252 }
00253
00254
00255
00256 void IPAddressvFour::validate() const
00257 {
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 }
00273