00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef WIFI_TRACES_SIMILARITY_HXX_
00023 # define WIFI_TRACES_SIMILARITY_HXX_
00024
00025 # include "similarity.hh"
00026
00027 # include <boost/tuple/tuple_comparison.hpp>
00028
00029 # include <wipal/wifi/frame/filter/non_noisy_prism.hh>
00030 # include <wipal/wifi/frame/filter/uniquely_identifiable.hh>
00031
00032 namespace wifi
00033 {
00034
00035 namespace traces
00036 {
00037
00039 namespace similarity_helpers
00040 {
00041
00042 template <class I>
00043 void
00044 info(unsigned i, const I& t)
00045 {
00046
00047 (void) i;
00048 (void) t;
00049
00050 # ifdef WP_ENABLE_INFO
00051 std::cerr << "INFO: Trace " << i << " is " << t->file_name()
00052 << std::endl;
00053 # endif // WP_ENABLE_INFO
00054 }
00055
00056 template <class Result, class Intersector, class I1, class I2>
00057 unsigned
00058 score(Result& r, Intersector& i, const I1& lhs, const I2& rhs)
00059 {
00060 unsigned count = 0;
00061
00062 typename Intersector::const_iterator it = i.begin();
00063 while (it != i.end() and
00064 it.rhs_uframe_count() < similarity_score_limit)
00065 {
00066 ++it;
00067 ++count;
00068 }
00069 if (it != i.end() and count > 0)
00070 --count;
00071
00072 r.insert(boost::make_tuple(count, lhs, rhs));
00073 # ifdef WP_ENABLE_INFO
00074 std::cerr << "INFO: Score for " << lhs->file_name()
00075 << " and " << rhs->file_name()
00076 << " is " << count << '.' << std::endl;
00077 # endif // WP_ENABLE_INFO
00078
00079 return count;
00080 }
00081
00082 template <class U, class HT, template <class, class, class> class I,
00083 class C>
00084 std::set< boost::tuple<unsigned, typename C::iterator,
00085 typename C::iterator> >
00086 ignore_prism(C& traces, tool::opt::list& options)
00087 {
00088 using frame::filter::uniquely_identifiable;
00089 using tool::opt::get;
00090
00091 typedef tool::endian::endianness endianness;
00092 typedef typename C::iterator trace_iterator;
00093 typedef typename C::value_type::const_iterator frame_iterator;
00094 typedef uniquely_identifiable<U, frame_iterator, HT> ui;
00095 typedef typename ui::const_iterator ui_iterator;
00096 typedef I<ui_iterator, ui_iterator, tool::bottom> intersector;
00097 typedef typename intersector::const_iterator i_iterator;
00098
00099 typedef std::set< boost::tuple<unsigned, typename C::iterator,
00100 typename C::iterator> >
00101 result;
00102
00103 result r;
00104
00105 if (traces.empty())
00106 return r;
00107
00108 const endianness& end = get<endianness>(options["endianness"]);
00109 addr_mapping& map = get<addr_mapping>(options["mapping"]);
00110
00111 trace_iterator t = traces.begin();
00112 trace_iterator lhs, rhs;
00113
00114 while (lhs = t++, rhs = t, rhs != traces.end())
00115 {
00116 info(1, lhs);
00117
00118 ui lhs_u (lhs->begin(), lhs->end(), map, end);
00119 intersector i (lhs_u.begin(), lhs_u.end(),
00120 lhs_u.begin(), lhs_u.end());
00121 do
00122 {
00123 info(2, rhs);
00124
00125 ui rhs_u (rhs->begin(), rhs->end(), map, end);
00126
00127 i.reset_rhs(rhs_u.begin(), rhs_u.end());
00128 score(r, i, lhs, rhs);
00129 }
00130 while (++rhs != traces.end());
00131 }
00132 return r;
00133 }
00134
00135 template <class U, class HT, template <class, class, class> class I,
00136 class C>
00137 std::set< boost::tuple<unsigned, typename C::iterator,
00138 typename C::iterator> >
00139 filter_prism(C& traces, tool::opt::list& options)
00140 {
00141 using frame::filter::non_noisy_prism;
00142 using frame::filter::uniquely_identifiable;
00143 using tool::opt::get;
00144
00145 typedef tool::endian::endianness endianness;
00146 typedef typename C::iterator trace_iterator;
00147 typedef typename C::value_type::const_iterator frame_iterator;
00148 typedef non_noisy_prism<frame_iterator> nnp;
00149 typedef typename nnp::const_iterator nnp_iterator;
00150 typedef uniquely_identifiable<U, nnp_iterator, HT> ui;
00151 typedef typename ui::const_iterator ui_iterator;
00152 typedef I<ui_iterator, ui_iterator, tool::bottom> intersector;
00153 typedef typename intersector::const_iterator i_iterator;
00154
00155 typedef std::set< boost::tuple<unsigned, typename C::iterator,
00156 typename C::iterator> >
00157 result;
00158
00159 result r;
00160
00161 if (traces.empty())
00162 return r;
00163
00164 const endianness& end = get<endianness>(options["endianness"]);
00165 addr_mapping& map = get<addr_mapping>(options["mapping"]);
00166
00167 trace_iterator t = traces.begin();
00168 trace_iterator lhs, rhs;
00169
00170 while (lhs = t++, rhs = t, rhs != traces.end())
00171 {
00172 info(1, lhs);
00173
00174 nnp lhs_n (lhs->begin(), lhs->end());
00175 ui lhs_u (lhs_n.begin(), lhs_n.end(), map, end);
00176 intersector i (lhs_u.begin(), lhs_u.end(),
00177 lhs_u.begin(), lhs_u.end());
00178 do
00179 {
00180 info(2, rhs);
00181
00182 nnp rhs_n (rhs->begin(), rhs->end());
00183 ui rhs_u (rhs_n.begin(), rhs_n.end(), map, end);
00184
00185 i.reset_rhs(rhs_u.begin(), rhs_u.end());
00186 score(r, i, lhs, rhs);
00187 }
00188 while (++rhs != traces.end());
00189 }
00190 return r;
00191 }
00192
00193 }
00194
00195
00196 template <class U, class P, template <class, class, class> class I,
00197 class C>
00198 std::set< boost::tuple<unsigned, typename C::iterator,
00199 typename C::iterator> >
00200 similarity(C& traces, tool::opt::list& options)
00201 {
00202 return options["ignore_noisy_prism"].get<bool>()?
00203 similarity_helpers::filter_prism<U, P, I>(traces, options):
00204 similarity_helpers::ignore_prism<U, P, I>(traces, options);
00205 }
00206
00207 }
00208
00209 }
00210
00211 #endif // ! WIFI_TRACES_SIMILARITY_HXX_