00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef ANON_POLICIES_HXX_
00023 # define ANON_POLICIES_HXX_
00024
00025 # include <cstdlib>
00026 # include <memory>
00027
00028 # include <boost/foreach.hpp>
00029 # include <boost/tuple/tuple.hpp>
00030
00031 # include "anon_policies.hh"
00032
00033 namespace wpl
00034 {
00035
00036 namespace wifi
00037 {
00038
00039 namespace anon
00040 {
00041
00042 namespace internals
00043 {
00044
00045
00046
00047
00048
00049 inline
00050 addr
00051 ppmp_mapping_factory::new_value(const addr& a,
00052 const mac_map& )
00053 {
00054 addr r (a);
00055
00056 if (a != addr::null() and a != addr::broadcast())
00057 {
00058 do
00059 {
00060 for (unsigned i = 3; i < 6; ++i)
00061 r[i] = rand();
00062 }
00063 while (values_.count(r));
00064 }
00065
00066 values_.insert(r);
00067 return r;
00068 }
00069
00070 inline
00071 void
00072 ppmp_mapping_factory::value_loaded(const addr& ,
00073 const addr& a,
00074 const mac_map& )
00075 {
00076 values_.insert(a);
00077 }
00078
00079 }
00080
00081 inline
00082 prefix_preserving_mac_policy::
00083 prefix_preserving_mac_policy(const std::string& mapname):
00084 super_type(mapname)
00085 {
00086 }
00087
00088 namespace internals
00089 {
00090
00091
00092
00093
00094
00095 inline
00096 ppep_mapping_factory::ppep_mapping_factory(): root_ (new node)
00097 {
00098 }
00099
00100 inline
00101 essid
00102 ppep_mapping_factory::new_value(const essid& e,
00103 const essid_map& )
00104 {
00105 using boost::ref;
00106
00107 std::string r;
00108 node* n;
00109
00110 make_tuple(ref(r), ref(n)) = longest_anonymized_prefix(e);
00111
00112 assert(r.size() <= e.size());
00113 for (size_t i = r.size(); i < e.size(); ++i)
00114 {
00115 char c;
00116
00117 make_tuple(ref(c), ref(n)) = n->add_valued_child(e[i]);
00118 r.push_back(c);
00119 }
00120 return r;
00121 }
00122
00123 inline
00124 void
00125 ppep_mapping_factory::value_loaded(const essid& from,
00126 const essid& to,
00127 const essid_map& )
00128 {
00129 if (from.size() != to.size())
00130 throw std::invalid_argument ("Invalid ESSID mapping");
00131
00132 node* n = root_.get();
00133
00134 for (size_t i = 0; i < from.size(); ++i)
00135 {
00136 assert(n);
00137
00138 const char& c = from[i];
00139 node* const t = n->child(c);
00140
00141 n = t? t: n->add_valued_child(c, to[i]);
00142 }
00143 }
00144
00145 inline
00146 std::pair<std::string, ppep_mapping_factory::node*>
00147 ppep_mapping_factory::
00148 longest_anonymized_prefix(const essid& orig_essid)
00149 {
00150 node* n = root_.get();
00151 std::string r;
00152
00153 BOOST_FOREACH(char c, orig_essid)
00154 {
00155 assert(n);
00156
00157 node* t = n->child(c);
00158
00159 if (not t)
00160 break;
00161 {
00162 const valued_node* const v =
00163 dynamic_cast<const valued_node*>(t);
00164
00165 assert(v);
00166 r.push_back(v->value);
00167 }
00168 n = t;
00169 }
00170 return std::make_pair(r, n);
00171 }
00172
00173 inline
00174 ppep_mapping_factory::node*
00175 ppep_mapping_factory::node::child(unsigned char c)
00176 {
00177 return children_[c].get();
00178 }
00179
00180 inline
00181 std::pair<char, ppep_mapping_factory::node*>
00182 ppep_mapping_factory::node::add_valued_child(unsigned char c)
00183 {
00184 assert(not child(c));
00185
00186 char new_c;
00187
00188 do
00189 new_c = rand_same_class(c);
00190 while (children_values_.count(new_c));
00191
00192 children_[c].reset(new valued_node (new_c));
00193 children_values_.insert(new_c);
00194 return std::make_pair(new_c, child(c));
00195 }
00196
00197 inline
00198 ppep_mapping_factory::node*
00199 ppep_mapping_factory::node::add_valued_child(unsigned char c,
00200 unsigned char v)
00201 {
00202 assert(not child(c));
00203 assert(not children_values_.count(v));
00204
00205 children_[c].reset(new valued_node (v));
00206 children_values_.insert(v);
00207 return child(c);
00208 }
00209
00210 inline
00211 ppep_mapping_factory::node::~node()
00212 {
00213 }
00214
00215 inline
00216 unsigned char
00217 ppep_mapping_factory::node::rand_extended()
00218 {
00219 unsigned char r;
00220
00221 do
00222 r = rand();
00223 while (r < 128);
00224 return r;
00225 }
00226
00227 inline
00228 unsigned char
00229 ppep_mapping_factory::node::rand_alnum()
00230 {
00231 unsigned char r;
00232
00233 do
00234 r = rand();
00235 while (r > 127 or not std::isalnum(r));
00236 return r;
00237 }
00238
00239 inline
00240 unsigned char
00241 ppep_mapping_factory::node::rand_print()
00242 {
00243 unsigned char r;
00244
00245 do
00246 r = rand();
00247 while (r > 127 or std::isalnum(r) or not std::isprint(r));
00248 return r;
00249 }
00250
00251 inline
00252 unsigned char
00253 ppep_mapping_factory::node::rand_other()
00254 {
00255 unsigned char r;
00256
00257 do
00258 r = rand();
00259 while (r > 127 or std::isprint(r));
00260 return r;
00261 }
00262
00263 inline
00264 unsigned char
00265 ppep_mapping_factory::node::rand_same_class(unsigned char c)
00266 {
00267 if (c > 127)
00268 return rand_extended();
00269 if (std::isalnum(c))
00270 return rand_alnum();
00271 if (std::isprint(c))
00272 return rand_print();
00273 return rand_other();
00274 }
00275
00276 inline
00277 ppep_mapping_factory::
00278 valued_node::valued_node(char value): value (value)
00279 {
00280 }
00281
00282 }
00283
00284 inline
00285 prefix_preserving_essid_policy::
00286 prefix_preserving_essid_policy(const std::string& mapname):
00287 super_type (mapname)
00288 {
00289 }
00290
00291 namespace internals
00292 {
00293
00294 # ifdef HAVE_OPENSSL
00295
00296
00297
00298
00299
00300 template <class T, size_t L>
00301 xor_policy<T, L>::xor_policy(const uint8_t* mask,
00302 const std::string& seed): mask_ (mask)
00303 {
00304 EVP_DigestInit(&seed_ctx_, EVP_sha1());
00305 EVP_DigestUpdate(&seed_ctx_, seed.data(), seed.size());
00306 }
00307
00308 template <class T, size_t L>
00309 xor_policy<T, L>::xor_policy(const xor_policy& other):
00310 mask_ (other.mask_),
00311 cache_ (other.cache_)
00312 {
00313 EVP_MD_CTX_copy(&seed_ctx_, &other.seed_ctx_);
00314 }
00315
00316 template <class T, size_t L>
00317 xor_policy<T, L>::~xor_policy()
00318 {
00319 EVP_MD_CTX_cleanup(&seed_ctx_);
00320 }
00321
00322 template <class T, size_t L>
00323 xor_policy<T, L>&
00324 xor_policy<T, L>::operator = (const xor_policy& other)
00325 {
00326 if (this == &other)
00327 return *this;
00328
00329 mask_ = other.mask_;
00330 EVP_MD_CTX_cleanup(&seed_ctx_);
00331 EVP_MD_CTX_copy(&seed_ctx_, other.seed_ctx_);
00332 cache_ = other.cache_;
00333 return *this;
00334 }
00335
00336 template <class T, size_t L>
00337 const T&
00338 xor_policy<T, L>::operator [] (const T& x)
00339 {
00340 typedef typename cache_type::iterator iterator;
00341 typedef std::pair<iterator, bool> ins_type;
00342
00343 const ins_type ins_ret = cache_.insert(std::make_pair(x, x));
00344 T& ret = ins_ret.first->second;
00345
00346 if (not ins_ret.second)
00347 return ret;
00348
00349 const size_t x_size = x.size();
00350
00351 for (size_t i = 0; i < x_size; ++i)
00352 {
00353 const uint8_t& mask = mask_[i];
00354
00355 if (not mask)
00356 continue;
00357
00358 uint8_t md[EVP_MAX_MD_SIZE];
00359 unsigned md_len;
00360 EVP_MD_CTX md_ctx;
00361
00362 EVP_MD_CTX_copy(&md_ctx, &seed_ctx_);
00363 for (int j = int (i) - 1; j >= 0; --j)
00364 EVP_DigestUpdate(&md_ctx, &x[j], 1);
00365 EVP_DigestFinal(&md_ctx, md, &md_len);
00366 assert(md_len);
00367 ret[i] = ((x[i] xor md[0]) & mask) | (x[i] & ~mask);
00368 }
00369 return ret;
00370 }
00371
00372 # endif // HAVE_OPENSSL
00373
00374 }
00375
00376 # ifdef HAVE_OPENSSL
00377
00378
00379
00380
00381
00382 inline
00383 xor_mac_policy::xor_mac_policy(const std::string& seed):
00384 super_type (mask_, seed)
00385 {
00386 }
00387
00388 inline
00389 const addr&
00390 xor_mac_policy::operator [] (const addr& x)
00391 {
00392 if (x == addr::broadcast() or x == addr::null())
00393 return x;
00394 return super_type::operator [] (x);
00395 }
00396
00397
00398
00399
00400
00401 inline
00402 xor_essid_policy::xor_essid_policy(const std::string& seed):
00403 super_type (mask_, seed)
00404 {
00405 }
00406
00407 # endif // HAVE_OPENSSL
00408
00409 }
00410
00411 }
00412
00413 }
00414
00415 #endif // ! ANON_POLICIES_HXX_