00001 #include <sys/socket.h>
00002 #include <netinet/in.h>
00003 #include <arpa/inet.h>
00004 #include <string>
00005 #include <map>
00006 #include <iostream>
00007
00008 #include "Codifier.h"
00009
00010 using namespace std;
00011
00012
00013 int Codifier::getClass(const struct in_addr address){
00014
00015 u_int8_t a= GetByte(address.s_addr,3);
00016
00017 if (a<=127) return 1;
00018 if((a>=128)&&(a<=191)) return 2;
00019 if((a>=192)&&(a<=223)) return 3;
00020 if((a>=224)&&(a<=239)) return 4;
00021 if((a>=240)&&(a<=254)) return 5;
00022 if((a>=255)) return 6;
00023 else return 6;
00024 }
00025
00026
00027
00028 struct in_addr Codifier::Codifica( struct in_addr address, int prefix){
00029 struct in_addr encodedAddress;
00030
00031
00032 if((address.s_addr!=0x00000000)&&(address.s_addr!=0xffffffff)){
00033
00034
00035 if (prefix!=0) encodedAddress=P.Codifica(address);
00036 else
00037 {
00038 int clas=getClass(address);
00039
00040 if(clas==1) encodedAddress=A.Codifica(address);
00041 if(clas==2) encodedAddress=B.Codifica(address);
00042 if(clas==3) encodedAddress=C.Codifica(address);
00043 if(clas==4) encodedAddress=D.Codifica(address);
00044 if(clas==5 || clas==6) encodedAddress=address;
00045 }
00046 }
00047
00048 return encodedAddress;
00049 }
00050
00051
00052
00053 struct Nodo{
00054 bool bit;
00055 PNodo sin;
00056 PNodo des;
00057 };
00058
00059 inline bool GetBit(unsigned long a, int bitno)
00060 {
00061 return (a >> (bitno))& 1;
00062 };
00063
00064 inline bool randomBit()
00065 {
00066
00067
00068 int iValore = (int)(((float)rand() / (float)RAND_MAX) * 1000.0f);
00069
00070
00071 return ((iValore % 2) != 0) ? true : false;
00072 };
00073
00074 PrefixPreservingTree::PrefixPreservingTree(){
00075 root= new Nodo;
00076 root->des=0;
00077 root->sin=0;
00078 }
00079
00080 PrefixPreservingTree::~PrefixPreservingTree(){
00081 Svuota();
00082 }
00083
00084 void PrefixPreservingTree::_Codifica(PNodo& n, unsigned long address,int pos,unsigned long& temp, bool b)
00085 {
00086
00087
00088 if (pos>27 && pos<32){
00089 if (!n){
00090 n=new Nodo;
00091 n->des=0;
00092 n->sin=0;
00093 n->bit=b;
00094 }
00095
00096
00097 temp=temp | ( b <<(pos));
00098
00099
00100 bool b_succ= GetBit(address,pos-1);
00101 if (b_succ==0)
00102 _Codifica(n->sin, address, pos-1, temp, b_succ);
00103 if (b_succ==1)
00104 _Codifica(n->des, address, pos-1, temp, b_succ);
00105 }
00106
00107
00108
00109
00110
00111 else{
00112 if(!n){
00113 n=new Nodo;
00114 n->des=0;
00115 n->sin=0;
00116 n->bit = randomBit();
00117 }
00118
00119
00120 temp=temp | ( n->bit <<(pos));
00121
00122
00123 if(pos>0){
00124 bool b_succ=GetBit(address,pos-1);
00125 if(b_succ==0)
00126 _Codifica(n->sin,address,pos-1,temp, b_succ);
00127 if(b_succ==1)
00128 _Codifica(n->des,address,pos-1,temp,b_succ);
00129 }
00130 }
00131 }
00132
00133 void PrefixPreservingTree::_Svuota(const PNodo& n){
00134 if(n){
00135 _Svuota(n->sin);
00136 _Svuota(n->des);
00137 delete n;
00138 }
00139 }
00140
00141 void PrefixPreservingTree::Svuota(){
00142 _Svuota(root);
00143 root=0;
00144 }
00145
00146 struct in_addr PrefixPreservingTree::Codifica(struct in_addr address){
00147 int pos=31;
00148 unsigned long temp=0;
00149 struct in_addr encodedAddress;
00150 encodedAddress.s_addr=0;
00151
00152 unsigned long add= address.s_addr;
00153
00154 bool b= GetBit(add,pos);
00155 if (b==0)
00156 _Codifica(root->sin, add, pos, temp,b);
00157 else
00158 _Codifica(root->des, add, pos, temp,b);
00159
00160 encodedAddress.s_addr=temp;
00161 return encodedAddress;
00162 }
00163
00164
00165
00166
00167
00168
00169 u_int8_t PublicCodifierA::New_Net()
00170 {
00171 curr_net += 1;
00172
00173
00174 if (curr_net == 10)
00175 curr_net += 1;
00176
00177
00178
00179
00180 if (curr_net == 128)
00181 curr_net = 1;
00182
00183 return curr_net;
00184 }
00185
00186 struct in_addr PublicCodifierA::Codifica (const struct in_addr address)
00187 {
00188
00189 u_int8_t net = GetByte(address.s_addr, 3);
00190
00191
00192 if (V[net].newnet == 0)
00193 V[net].newnet = New_Net();
00194
00195
00196
00197 struct in_addr encodedAddress;
00198 mappa::iterator i;
00199
00200 i = V[net].mapA.find(address);
00201
00202 if (i == V[net].mapA.end()) {
00203
00204
00205 V[net].newhost += 1;
00206
00207
00208 encodedAddress.s_addr = (V[net].newnet << 24) | V[net].newhost;
00209
00210
00211 V[net].mapA[address] = encodedAddress;
00212 } else {
00213
00214
00215 encodedAddress = (*i).second;
00216 }
00217
00218
00219 return encodedAddress;
00220 }
00221
00222
00223
00224 struct in_addr PrivateCodifierA::Codifica (const struct in_addr address)
00225 {
00226
00227 struct in_addr encodedAddress;
00228 mappa::iterator i;
00229
00230 i = elem.mapA.find(address);
00231
00232 if (i == elem.mapA.end()) {
00233
00234
00235 elem.newhost += 1;
00236
00237
00238 encodedAddress.s_addr = (10 << 24) | elem.newhost;
00239
00240
00241 elem.mapA[address] = encodedAddress;
00242 } else {
00243
00244
00245 encodedAddress = (*i).second;
00246 }
00247
00248
00249 return encodedAddress;
00250 }
00251
00252
00253
00254 u_int16_t PublicCodifierB::New_Net()
00255 {
00256 curr_net += 1;
00257
00258
00259 if((curr_net>=44048)&(curr_net<=44063))
00260 curr_net=44064;
00261
00262
00263
00264
00265 if (curr_net > 49151)
00266 curr_net = 32768;
00267
00268 return curr_net;
00269 }
00270
00271 struct in_addr PublicCodifierB::Codifica (const struct in_addr address)
00272 {
00273
00274 u_int8_t net1=(GetByte(address.s_addr, 3) - 128);
00275 u_int8_t net2=GetByte(address.s_addr, 2);
00276
00277
00278 if (V[net1][net2].newnet == 0)
00279 V[net1][net2].newnet = New_Net();
00280
00281
00282
00283 struct in_addr encodedAddress;
00284 mappa::iterator i;
00285
00286 i = V[net1][net2].mapB.find(address);
00287
00288 if (i == V[net1][net2].mapB.end()) {
00289
00290
00291 V[net1][net2].newhost += 1;
00292
00293
00294 encodedAddress.s_addr = (V[net1][net2].newnet << 16) | V[net1][net2].newhost;
00295
00296
00297 V[net1][net2].mapB[address] = encodedAddress;
00298 } else {
00299
00300
00301 encodedAddress = (*i).second;
00302 }
00303
00304
00305 return encodedAddress;
00306 }
00307
00308
00309 struct in_addr PrivateCodifierB::Codifica (const struct in_addr address)
00310 {
00311
00312 u_int8_t net1=GetByte(address.s_addr, 3);
00313 u_int8_t net2=GetByte(address.s_addr, 2);
00314
00315 struct in_addr encodedAddress;
00316 mappa::iterator i;
00317
00318 i = V[net2].mapB.find(address);
00319
00320 if (i == V[net2].mapB.end()) {
00321
00322
00323 V[net2].newhost += 1;
00324
00325
00326 encodedAddress.s_addr = (net1 << 24) | V[net2].newhost;
00327
00328
00329 V[net2].mapB[address] = encodedAddress;
00330 } else {
00331
00332
00333 encodedAddress = (*i).second;
00334 }
00335
00336
00337 return encodedAddress;
00338 }
00339
00340
00341 u_int32_t PublicCodifierC::New_Net()
00342 {
00343 curr_net += 1;
00344
00345 if((curr_net>=12625920)&(curr_net<=12626175))
00346
00347 curr_net=12626176;
00348
00349
00350
00351
00352 if (curr_net > 14680063)
00353 curr_net = 12582912;
00354
00355 return curr_net;
00356 }
00357
00358
00359 struct in_addr PublicCodifierC::Codifica (const struct in_addr address)
00360 {
00361
00362 u_int8_t net1=(GetByte(address.s_addr, 3)-192);
00363 u_int8_t net2=GetByte(address.s_addr, 2);
00364 u_int8_t net3=GetByte(address.s_addr, 1);
00365
00366 struct in_addr encodedAddress;
00367 mappa1::iterator i;
00368 i=V[net1][net2].mapC1.find(net3);
00369
00370 if(i==V[net1][net2].mapC1.end()){
00371 ElemM Newelem;
00372 Newelem.newnet=New_Net();
00373 Newelem.newhost+=1;
00374
00375 encodedAddress.s_addr=((Newelem.newnet<<8) | (Newelem.newhost));
00376 Newelem.mapC2[address]=encodedAddress;
00377 V[net1][net2].mapC1[net3]=Newelem;
00378 }
00379 else{
00380 mappa::iterator j;
00381 j=V[net1][net2].mapC1[net3].mapC2.find(address);
00382
00383 if(j==V[net1][net2].mapC1[net3].mapC2.end()){
00384
00385 u_int32_t newnet3 = V[net1][net2].mapC1[net3].newnet;
00386 V[net1][net2].mapC1[net3].newhost +=1;
00387
00388 encodedAddress.s_addr=((newnet3 <<8) | (V[net1][net2].mapC1[net3].newhost));
00389 V[net1][net2].mapC1[net3].mapC2[address]=encodedAddress;
00390 }
00391 else
00392
00393
00394 encodedAddress=(*j).second;
00395
00396 }
00397 return encodedAddress;
00398 }
00399
00400
00401 struct in_addr PrivateCodifierC::Codifica (const struct in_addr address)
00402 {
00403
00404 u_int8_t net1=GetByte(address.s_addr, 3);
00405 u_int8_t net2=GetByte(address.s_addr, 2);
00406 u_int8_t net3=GetByte(address.s_addr, 1);
00407
00408 struct in_addr encodedAddress;
00409 mappa::iterator i;
00410
00411 i = V[net3].mapC.find(address);
00412
00413 if (i == V[net3].mapC.end()) {
00414
00415
00416 V[net3].newhost += 1;
00417
00418
00419 encodedAddress.s_addr = ((net1 << 24) | (net2 << 16)| V[net3].newhost);
00420
00421
00422 V[net3].mapC[address] = encodedAddress;
00423 } else {
00424
00425
00426 encodedAddress = (*i).second;
00427 }
00428
00429
00430 return encodedAddress;
00431 }
00432
00433
00434 u_int8_t CodifierD::New_Net()
00435 {
00436 curr_net += 1;
00437
00438 if (curr_net > 247)
00439 curr_net = 1;
00440
00441 return curr_net;
00442 }
00443
00444 struct in_addr CodifierD::Codifica (const struct in_addr address)
00445 {
00446
00447 u_int8_t net = (GetByte(address.s_addr, 3)-224);
00448
00449
00450 if (V[net].newnet == 0)
00451 V[net].newnet = New_Net();
00452
00453
00454
00455 struct in_addr encodedAddress;
00456 mappa::iterator i;
00457
00458 i = V[net].mapD.find(address);
00459
00460 if (i == V[net].mapD.end()) {
00461
00462
00463 V[net].newhost += 1;
00464
00465
00466 encodedAddress.s_addr = ((V[net].newnet << 24) | V[net].newhost);
00467
00468
00469 V[net].mapD[address] = encodedAddress;
00470 } else {
00471
00472
00473 encodedAddress = (*i).second;
00474 }
00475
00476
00477 return encodedAddress;
00478 }
00479
00480
00481