00001 #ifndef _CODIFIER_H_
00002 #define _CODIFIER_H_
00003
00004 #include <sys/socket.h>
00005 #include <netinet/in.h>
00006 #include <arpa/inet.h>
00007 #include <string>
00008 #include <map>
00009 #include <iostream>
00010
00011 using namespace std;
00012
00013
00014
00015
00016 struct Nodo;
00017 typedef Nodo* PNodo;
00018
00019 class PrefixPreservingTree{
00020 private:
00021 PNodo root;
00022
00023 void _Codifica(PNodo& n, unsigned long address, int pos, unsigned long& temp, bool b);
00024 void _Svuota(const PNodo& n);
00025
00026 public:
00027 PrefixPreservingTree();
00028 ~PrefixPreservingTree();
00029
00030 struct in_addr Codifica(struct in_addr address);
00031 void Svuota();
00032 };
00033
00034
00035
00036
00037 inline u_int8_t GetByte(u_int32_t a, int byteno)
00038 {
00039
00040 return (a >> (8 * byteno)) & 0xff;
00041 };
00042
00043 struct AddrComp:public binary_function<const struct in_addr, const struct in_addr, bool>
00044 {
00045 bool operator()(const struct in_addr addr1, const struct in_addr addr2) const
00046 {
00047 return (addr1.s_addr <= addr2.s_addr) ? false : true;
00048 }
00049 };
00050
00051 typedef map<struct in_addr, struct in_addr, AddrComp> mappa;
00052
00053 class PublicCodifierA {
00054 class Elem{
00055 public:
00056 mappa mapA;
00057 u_int8_t newnet;
00058 u_int32_t newhost;
00059
00060 Elem() :newnet(0),newhost(0){};
00061 ~Elem(){};
00062 };
00063 private:
00064 u_int8_t curr_net;
00065 Elem V[127];
00066 u_int8_t New_Net();
00067
00068 public:
00069 PublicCodifierA ():curr_net(0){};
00070 ~PublicCodifierA (){};
00071 struct in_addr Codifica (const struct in_addr address);
00072 };
00073
00074 class PrivateCodifierA {
00075 class Elem{
00076 public:
00077 mappa mapA;
00078 u_int32_t newhost;
00079
00080 Elem() :newhost(0){};
00081 ~Elem(){};
00082 };
00083 private:
00084 Elem elem;
00085
00086 public:
00087 PrivateCodifierA (){};
00088 ~PrivateCodifierA (){};
00089 struct in_addr Codifica (const struct in_addr address);
00090 };
00091
00092 class CodifierA {
00093 private:
00094 PublicCodifierA pub_ca;
00095 PrivateCodifierA pri_ca;
00096 public:
00097 CodifierA(){};
00098 ~CodifierA(){};
00099 struct in_addr Codifica(struct in_addr address){
00100 struct in_addr encodedAddress;
00101 int a=GetByte(address.s_addr , 3);
00102 if(a==10)
00103 encodedAddress=pri_ca.Codifica(address);
00104 else
00105 encodedAddress=pub_ca.Codifica(address);
00106 return encodedAddress;
00107 }
00108 };
00109
00110 class PublicCodifierB {
00111 class Elem{
00112 public:
00113 mappa mapB;
00114 u_int16_t newnet;
00115 u_int16_t newhost;
00116
00117 Elem() :newnet(0),newhost(0){};
00118 ~Elem(){};
00119 };
00120 private:
00121 u_int16_t curr_net;
00122 Elem V[64][256];
00123 u_int16_t New_Net();
00124
00125 public:
00126 PublicCodifierB ():curr_net(32767){};
00127 ~PublicCodifierB (){};
00128 struct in_addr Codifica (const struct in_addr IN);
00129 };
00130
00131 class PrivateCodifierB {
00132 class Elem{
00133 public:
00134 mappa mapB;
00135 u_int32_t newhost;
00136
00137 Elem() :newhost(1048576){};
00138 ~Elem(){};
00139 };
00140 private:
00141 Elem V[32];
00142
00143 public:
00144 PrivateCodifierB (){};
00145 ~PrivateCodifierB (){};
00146 struct in_addr Codifica (const struct in_addr address);
00147
00148 };
00149 class CodifierB {
00150 private:
00151 PublicCodifierB pub_cb;
00152 PrivateCodifierB pri_cb;
00153 public:
00154 CodifierB(){};
00155 ~CodifierB(){};
00156 struct in_addr Codifica(struct in_addr address){
00157 struct in_addr encodedAddress;
00158 int a=GetByte(address.s_addr , 3);
00159 int b=GetByte(address.s_addr , 2);
00160 if((a==127)&&(b>=16)&&(b<=31))
00161 encodedAddress=pri_cb.Codifica(address);
00162 else
00163 encodedAddress=pub_cb.Codifica(address);
00164 return encodedAddress;
00165 }
00166 };
00167
00168 typedef class ElemM;
00169 typedef map<u_int8_t, ElemM> mappa1;
00170
00171 class ElemM{
00172 public:
00173 mappa mapC2;
00174 u_int32_t newnet;
00175 u_int8_t newhost;
00176 ElemM():newnet(0),newhost(0){};
00177 ~ElemM(){};
00178 };
00179
00180 class PublicCodifierC {
00181 class Elem{
00182 public:
00183 mappa1 mapC1;
00184 bool flag;
00185 Elem():flag(0){};
00186 ~Elem(){};
00187 };
00188 private:
00189 u_int32_t curr_net;
00190 Elem V[32][256];
00191 u_int32_t New_Net();
00192 public:
00193 PublicCodifierC():curr_net(12582911){};
00194 ~PublicCodifierC(){};
00195 struct in_addr Codifica (const struct in_addr IN);
00196 };
00197
00198 class PrivateCodifierC {
00199 class Elem{
00200 public:
00201 mappa mapC;
00202 u_int16_t newhost;
00203
00204 Elem() :newhost(0){};
00205 ~Elem(){};
00206 };
00207 private:
00208 Elem V[255];
00209
00210 public:
00211 PrivateCodifierC (){};
00212 ~PrivateCodifierC (){};
00213 struct in_addr Codifica (const struct in_addr IN);
00214
00215 };
00216
00217 class CodifierC {
00218 private:
00219 PublicCodifierC pub_cc;
00220 PrivateCodifierC pri_cc;
00221 public:
00222 CodifierC(){};
00223 ~CodifierC(){};
00224 struct in_addr Codifica(struct in_addr address){
00225 struct in_addr encodedAddress;
00226 int a=GetByte(address.s_addr , 3);
00227 int b=GetByte(address.s_addr , 2);
00228
00229 if((a==192)&&(b==168))
00230 encodedAddress=pri_cc.Codifica(address);
00231 else
00232 encodedAddress=pub_cc.Codifica(address);
00233 return encodedAddress;
00234 }
00235 };
00236
00237 class CodifierD{
00238 class Elem{
00239 public:
00240 mappa mapD;
00241 u_int8_t newnet;
00242 u_int32_t newhost;
00243
00244 Elem() :newnet(0),newhost(0){};
00245 ~Elem(){};
00246 };
00247
00248 private:
00249 u_int8_t curr_net;
00250 Elem V[16];
00251 u_int8_t New_Net();
00252
00253 public:
00254 CodifierD():curr_net(223){};
00255 ~CodifierD(){};
00256 struct in_addr Codifica (const struct in_addr IN);
00257 };
00258
00259 class Codifier {
00260 private:
00261 CodifierA A;
00262 CodifierB B;
00263 CodifierC C;
00264 CodifierD D;
00265 PrefixPreservingTree P;
00266 int getClass(const struct in_addr);
00267
00268 public:
00269 struct in_addr Codifica( in_addr address, int prefix);
00270 Codifier(){};
00271 ~Codifier(){};
00272 };
00273
00274 #endif
00275