00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef PCAP_PKT_HXX_
00023 # define PCAP_PKT_HXX_
00024
00025 # include <cstring>
00026 # include <sstream>
00027
00028 # include "pkt.hh"
00029
00030 namespace wpl
00031 {
00032
00033 namespace pkt
00034 {
00035
00036
00037
00038
00039
00040 inline
00041 metadata::metadata(unsigned id,
00042 unsigned ts_sec,
00043 unsigned ts_usec,
00044 size_t caplen,
00045 size_t len):
00046 id (id),
00047 caplen (caplen),
00048 len (len)
00049 {
00050 ts.tv_sec = ts_sec;
00051 ts.tv_usec = ts_usec;
00052 }
00053
00054 inline
00055 bool
00056 metadata::operator == (const metadata& m) const
00057 {
00058 return (m.ts.tv_sec == ts.tv_sec and m.ts.tv_usec == ts.tv_usec and
00059 m.caplen == caplen and m.len == len);
00060 }
00061
00062 inline
00063 bool
00064 metadata::operator != (const metadata& m) const
00065 {
00066 return not operator == (m);
00067 }
00068
00069
00070
00071
00072
00073
00074 template <class B>
00075 bool
00076 stream<B>::swapped() const
00077 {
00078 return this->exact().swapped_impl();
00079 }
00080
00081 template <class B>
00082 pkt::type
00083 stream<B>::type() const
00084 {
00085 return this->exact().type_impl();
00086 }
00087
00088 template <class B>
00089 void
00090 stream<B>::expect(pkt::type t) const
00091 {
00092 if (type() != t)
00093 {
00094 std::ostringstream os;
00095
00096 os << *this << ": Expected link type " << t << ", got " << type();
00097 throw std::invalid_argument (os.str());
00098 }
00099 }
00100
00101 template <class B>
00102 std::ostream&
00103 stream<B>::print(std::ostream& os) const
00104 {
00105 return this->exact().print_impl(os);
00106 }
00107
00108 template <class B>
00109 std::ostream&
00110 stream<B>::print_impl(std::ostream& os) const
00111 {
00112 return os << typeid (this->exact()).name() << '@' << this;
00113 }
00114
00115 template <class B>
00116 opt::list&
00117 stream<B>::meta()
00118 {
00119 return meta_;
00120 }
00121
00122 template <class B>
00123 const opt::list&
00124 stream<B>::meta() const
00125 {
00126 return meta_;
00127 }
00128
00129 template <class B>
00130 std::ostream&
00131 operator << (std::ostream& os, const stream<B>& s)
00132 {
00133 return s.print(os);
00134 }
00135
00136
00137
00138
00139
00140
00141 template <class Source>
00142 packet<Source>::packet(unsigned index,
00143 unsigned sec,
00144 unsigned usec,
00145 size_t caplen,
00146 size_t len,
00147 const Source* src):
00148 data_ (new shared_data (src, metadata (index, sec, usec, caplen, len)))
00149 {
00150 if (not len and caplen)
00151 len = caplen;
00152
00153 assert(caplen <= max_size);
00154 }
00155
00156 template <class Source>
00157 packet<Source>::packet(const metadata& meta,
00158 const Source* src):
00159 data_ (new shared_data (src, meta))
00160 {
00161 }
00162
00163 template <class Source>
00164 const Source*
00165 packet<Source>::source_ptr() const
00166 {
00167 assert(data_);
00168
00169 return data_->src_;
00170 }
00171
00172 template <class Source>
00173 const Source&
00174 packet<Source>::source() const
00175 {
00176 assert(data_);
00177
00178 if (not data_->src_)
00179 throw std::runtime_error ("Frame not attached to a source");
00180 return *data_->src_;
00181 }
00182
00183 template <class Source>
00184 bool
00185 packet<Source>::swapped() const
00186 {
00187 const Source* const src = source_ptr();
00188
00189 return src ? src->swapped() : false;
00190 }
00191
00192 template <class Source>
00193 long
00194 packet<Source>::use_count() const
00195 {
00196 return data_.use_count();
00197 }
00198
00199 template <class Source>
00200 bool
00201 packet<Source>::unique() const
00202 {
00203 return data_.unique();
00204 }
00205
00206 template <class Source>
00207 const metadata&
00208 packet<Source>::meta() const
00209 {
00210 assert(data_);
00211
00212 return data_->meta_;
00213 }
00214
00215 template <class Source>
00216 metadata&
00217 packet<Source>::meta()
00218 {
00219 assert(data_);
00220
00221 return data_->meta_;
00222 }
00223
00224 template <class Source>
00225 const void*
00226 packet<Source>::bytes() const
00227 {
00228 assert(data_);
00229
00230 return data_->bytes_.begin();
00231 }
00232
00233 template <class Source>
00234 void*
00235 packet<Source>::bytes()
00236 {
00237 assert(data_);
00238
00239 return data_->bytes_.begin();
00240 }
00241
00242 template <class Source>
00243 std::ostream&
00244 packet<Source>::print(std::ostream& os) const
00245 {
00246 const Source* const src = source_ptr();
00247
00248 if (src)
00249 os << *src << ':';
00250 return os << meta().id;
00251 }
00252
00253 template <class Source>
00254 bool
00255 packet<Source>::
00256 operator == (const packet& rhs) const
00257 {
00258 return meta() == rhs.meta() and 0 == memcmp(bytes(), rhs.bytes(),
00259 meta().caplen);
00260 }
00261
00262 template <class Source>
00263 bool
00264 packet<Source>::
00265 operator != (const packet& rhs) const
00266 {
00267 return not (*this == rhs);
00268 }
00269
00270 template <class Source>
00271 template <class B>
00272 packet<Source>::shared_data::shared_data(const stream<B>* src,
00273 const metadata& meta):
00274 meta_ (meta),
00275 src_ (src->exact_ptr())
00276 {
00277 }
00278
00279 template <class Source>
00280 std::ostream&
00281 operator << (std::ostream& os, const packet<Source>& p)
00282 {
00283 return p.print(os);
00284 }
00285
00286 }
00287
00288 }
00289
00290 #endif // ! PCAP_PKT_HXX_