1 //===========================================================================
2 // $Name: arts++-1-1-a13 $
3 // $Id: ArtsAsMatrixEntry.cc,v 1.2 2004/04/21 23:51:31 kkeys Exp $
4 //===========================================================================
5 // Copyright Notice
6 //
7 // By accessing this software, arts++, you are duly informed
8 // of and agree to be bound by the conditions described below in this
9 // notice:
10 //
11 // This software product, arts++, is developed by Daniel W. McRobb, and
12 // copyrighted(C) 1998 by the University of California, San Diego
13 // (UCSD), with all rights reserved. UCSD administers the CAIDA grant,
14 // NCR-9711092, under which part of this code was developed.
15 //
16 // There is no charge for arts++ software. You can redistribute it
17 // and/or modify it under the terms of the GNU Lesser General Public
18 // License, Version 2.1, February 1999, which is incorporated by
19 // reference herein.
20 //
21 // arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF
22 // MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use
23 // of it will not infringe on any third party's intellectual
24 // property rights.
25 //
26 // You should have received a copy of the GNU Lesser General Public
27 // License along with arts++. Copies can also be obtained from:
28 //
29 // http://www.gnu.org/copyleft/lesser.html
30 //
31 // or by writing to:
32 //
33 // Free Software Foundation, Inc.
34 // 59 Temple Place, Suite 330
35 // Boston, MA 02111-1307
36 // USA
37 //
38 // Or contact:
39 //
40 // info@caida.org
41 //===========================================================================
42
43 extern "C" {
44 #include <unistd.h>
45 }
46
47 #include <string>
48
49 #include "ArtsAsMatrixEntry.hh"
50 #include "ArtsPrimitive.hh"
51
52 static const std::string rcsid = "@(#) $Name: arts++-1-1-a13 $ $Id: ArtsAsMatrixEntry.cc,v 1.2 2004/04/21 23:51:31 kkeys Exp $";
53
54 //-------------------------------------------------------------------------
55 // ArtsAsMatrixEntry::ArtsAsMatrixEntry()
56 //.........................................................................
57 //
58 //-------------------------------------------------------------------------
ArtsAsMatrixEntry()59 ArtsAsMatrixEntry::ArtsAsMatrixEntry()
60 {
61 this->_descriptor = 0;
62 this->_src = 0;
63 this->_dst = 0;
64 this->_pkts = 0;
65 this->_bytes = 0;
66
67 #ifndef NDEBUG
68 ++ArtsAsMatrixEntry::_numObjects;
69 #endif
70 }
71
72 //----------------------------------------------------------------------------
73 // ArtsAsMatrixEntry::ArtsAsMatrixEntry(const ArtsAsMatrixEntry & asmEntry)
74 //............................................................................
75 //
76 //----------------------------------------------------------------------------
ArtsAsMatrixEntry(const ArtsAsMatrixEntry & asmEntry)77 ArtsAsMatrixEntry::ArtsAsMatrixEntry(const ArtsAsMatrixEntry & asmEntry)
78 {
79 this->_descriptor = asmEntry._descriptor;
80 this->_src = asmEntry._src;
81 this->_dst = asmEntry._dst;
82 this->_pkts = asmEntry._pkts;
83 this->_bytes = asmEntry._bytes;
84
85 #ifndef NDEBUG
86 ++ArtsAsMatrixEntry::_numObjects;
87 #endif
88 }
89
90 //----------------------------------------------------------------------------
91 // ArtsAsMatrixEntry::~ArtsAsMatrixEntry()
92 //............................................................................
93 //
94 //----------------------------------------------------------------------------
~ArtsAsMatrixEntry()95 ArtsAsMatrixEntry::~ArtsAsMatrixEntry()
96 {
97 #ifndef NDEBUG
98 --ArtsAsMatrixEntry::_numObjects;
99 #endif
100 }
101
102 //-------------------------------------------------------------------------
103 // uint16_t ArtsAsMatrixEntry::Src(uint16_t src)
104 //.........................................................................
105 //
106 //-------------------------------------------------------------------------
Src(uint16_t src)107 uint16_t ArtsAsMatrixEntry::Src(uint16_t src)
108 {
109 this->_src = src;
110
111 if (src > 0xff)
112 this->_descriptor = this->_descriptor | 0x01;
113 else
114 this->_descriptor = this->_descriptor & 0xfe;
115
116 return(this->_src);
117 }
118
119
120 //-------------------------------------------------------------------------
121 // uint16_t ArtsAsMatrixEntry::Dst(uint16_t dst)
122 //.........................................................................
123 //
124 //-------------------------------------------------------------------------
Dst(uint16_t dst)125 uint16_t ArtsAsMatrixEntry::Dst(uint16_t dst)
126 {
127 this->_dst = dst;
128 if (dst > 0xff)
129 this->_descriptor = this->_descriptor | 0x02;
130 else
131 this->_descriptor = this->_descriptor & 0xfd;
132
133 return(this->_dst);
134 }
135
136 //-------------------------------------------------------------------------
137 // uint64_t ArtsAsMatrixEntry::Pkts(uint64_t pkts)
138 //.........................................................................
139 //
140 //-------------------------------------------------------------------------
Pkts(uint64_t pkts)141 uint64_t ArtsAsMatrixEntry::Pkts(uint64_t pkts)
142 {
143 this->_pkts = pkts;
144
145 if (pkts > (uint64_t)0xffffffff) {
146 this->_descriptor = (this->_descriptor | 0x1c);
147 } else if (pkts > (uint64_t)0xffff) {
148 this->_descriptor = (this->_descriptor & 0xe3) | (0x0003 << 2);
149 } else if (pkts > (uint64_t)0xff) {
150 this->_descriptor = (this->_descriptor & 0xe3) | (0x0001 << 2);
151 } else {
152 this->_descriptor = (this->_descriptor & 0xe3);
153 }
154 return(this->_pkts);
155 }
156
157 //-------------------------------------------------------------------------
158 // uint64_t ArtsAsMatrixEntry::Bytes(uint64_t bytes)
159 //.........................................................................
160 //
161 //-------------------------------------------------------------------------
Bytes(uint64_t bytes)162 uint64_t ArtsAsMatrixEntry::Bytes(uint64_t bytes)
163 {
164 this->_bytes = bytes;
165
166 if (bytes > (uint64_t)0xffffffff) {
167 this->_descriptor = (this->_descriptor | 0xe0);
168 } else if (bytes > (uint64_t)0xffff) {
169 this->_descriptor = (this->_descriptor & 0x1f) | (0x0003 << 5);
170 } else if (bytes > (uint64_t)0xff) {
171 this->_descriptor = (this->_descriptor & 0x1f) | (0x0001 << 5);
172 } else {
173 this->_descriptor = (this->_descriptor & 0x1f);
174 }
175 return(this->_bytes);
176 }
177
178 //-------------------------------------------------------------------------
179 // uint32_t ArtsAsMatrixEntry::Length(uint8_t version) const
180 //.........................................................................
181 //
182 //-------------------------------------------------------------------------
Length(uint8_t version) const183 uint32_t ArtsAsMatrixEntry::Length(uint8_t version) const
184 {
185 uint32_t length;
186
187 length = (sizeof(this->_descriptor) +
188 (this->_descriptor & 0x01) + 1 +
189 ((this->_descriptor >> 1) & 0x01) + 1 +
190 ((this->_descriptor >> 2) & 0x07) + 1 +
191 ((this->_descriptor >> 5) & 0x07) + 1);
192 return(length);
193 }
194
195 //-------------------------------------------------------------------------
196 // std::istream& ArtsAsMatrixEntry::read(std::istream& is, uint8_t version)
197 //.........................................................................
198 //
199 //-------------------------------------------------------------------------
read(std::istream & is,uint8_t version)200 std::istream& ArtsAsMatrixEntry::read(std::istream& is, uint8_t version)
201 {
202 uint8_t bytesize,
203 pktsize,
204 srcsize,
205 dstsize;
206
207 is.read((char*)&this->_descriptor,sizeof(this->_descriptor));
208
209 srcsize = (this->_descriptor & 0x01) + 1;
210 dstsize = ((this->_descriptor >> 1) & 0x01) + 1;
211 pktsize = ((this->_descriptor >> 2) & 0x07) + 1;
212 bytesize = ((this->_descriptor >> 5) & 0x07) + 1;
213
214 g_ArtsLibInternal_Primitive.ReadUint16(is,this->_src,srcsize);
215 g_ArtsLibInternal_Primitive.ReadUint16(is,this->_dst,dstsize);
216 g_ArtsLibInternal_Primitive.ReadUint64(is,this->_pkts,pktsize);
217 g_ArtsLibInternal_Primitive.ReadUint64(is,this->_bytes,bytesize);
218
219 return(is);
220 }
221
222 //-------------------------------------------------------------------------
223 // int ArtsAsMatrixEntry::read(int fd, uint8_t version)
224 //.........................................................................
225 //
226 //-------------------------------------------------------------------------
read(int fd,uint8_t version)227 int ArtsAsMatrixEntry::read(int fd, uint8_t version)
228 {
229 uint8_t bytesize,
230 pktsize,
231 srcsize,
232 dstsize;
233 int rc;
234 int bytesRead = 0;
235
236 rc = g_ArtsLibInternal_Primitive.FdRead(fd,&this->_descriptor,
237 sizeof(this->_descriptor));
238 if (rc != sizeof(this->_descriptor)) {
239 return(-1);
240 }
241 bytesRead += rc;
242
243 srcsize = (this->_descriptor & 0x01) + 1;
244 dstsize = ((this->_descriptor >> 1) & 0x01) + 1;
245 pktsize = ((this->_descriptor >> 2) & 0x07) + 1;
246 bytesize = ((this->_descriptor >> 5) & 0x07) + 1;
247
248 rc = g_ArtsLibInternal_Primitive.ReadUint16(fd,this->_src,srcsize);
249 if (rc != srcsize) {
250 return(-1);
251 }
252 bytesRead += rc;
253
254 rc = g_ArtsLibInternal_Primitive.ReadUint16(fd,this->_dst,dstsize);
255 if (rc != dstsize) {
256 return(-1);
257 }
258 bytesRead += rc;
259
260 rc = g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_pkts,pktsize);
261 if (rc != pktsize) {
262 return(-1);
263 }
264 bytesRead += rc;
265
266 rc = g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_bytes,bytesize);
267 if (rc != bytesize) {
268 return(-1);
269 }
270 bytesRead += rc;
271
272 return(bytesRead);
273 }
274
275 //-------------------------------------------------------------------------
276 // std::ostream& ArtsAsMatrixEntry::write(std::ostream& os, uint8_t version) const
277 //.........................................................................
278 //
279 //-------------------------------------------------------------------------
write(std::ostream & os,uint8_t version) const280 std::ostream & ArtsAsMatrixEntry::write(std::ostream & os, uint8_t version) const
281 {
282 uint8_t bytesize,
283 pktsize,
284 srcsize,
285 dstsize;
286
287 os.write((char*)&this->_descriptor,sizeof(this->_descriptor));
288
289 srcsize = (this->_descriptor & 0x01) + 1;
290 dstsize = ((this->_descriptor >> 1) & 0x01) + 1;
291 pktsize = ((this->_descriptor >> 2) & 0x07) + 1;
292 bytesize = ((this->_descriptor >> 5) & 0x07) + 1;
293
294 g_ArtsLibInternal_Primitive.WriteUint16(os,this->_src,srcsize);
295 g_ArtsLibInternal_Primitive.WriteUint16(os,this->_dst,dstsize);
296 g_ArtsLibInternal_Primitive.WriteUint64(os,this->_pkts,pktsize);
297 g_ArtsLibInternal_Primitive.WriteUint64(os,this->_bytes,bytesize);
298
299 return(os);
300 }
301
302 //-------------------------------------------------------------------------
303 // int ArtsAsMatrixEntry::write(int fd, uint8_t version) const
304 //.........................................................................
305 //
306 //-------------------------------------------------------------------------
write(int fd,uint8_t version) const307 int ArtsAsMatrixEntry::write(int fd, uint8_t version) const
308 {
309 uint8_t bytesize,
310 pktsize,
311 srcsize,
312 dstsize;
313 int rc;
314 int bytesWritten = 0;
315
316 rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&this->_descriptor,
317 sizeof(this->_descriptor));
318 if (rc != sizeof(this->_descriptor)) {
319 return(-1);
320 }
321 bytesWritten += rc;
322
323 srcsize = (this->_descriptor & 0x01) + 1;
324 dstsize = ((this->_descriptor >> 1) & 0x01) + 1;
325 pktsize = ((this->_descriptor >> 2) & 0x07) + 1;
326 bytesize = ((this->_descriptor >> 5) & 0x07) + 1;
327
328 rc = g_ArtsLibInternal_Primitive.WriteUint16(fd,this->_src,srcsize);
329 if (rc != srcsize) {
330 return(-1);
331 }
332 bytesWritten += rc;
333
334 rc = g_ArtsLibInternal_Primitive.WriteUint16(fd,this->_dst,dstsize);
335 if (rc != dstsize) {
336 return(-1);
337 }
338 bytesWritten += rc;
339
340 rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_pkts,pktsize);
341 if (rc != pktsize) {
342 return(-1);
343 }
344 bytesWritten += rc;
345
346 rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_bytes,bytesize);
347 if (rc != bytesize) {
348 return(-1);
349 }
350 bytesWritten += rc;
351
352 return(bytesWritten);
353 }
354
355 //-------------------------------------------------------------------------
356 // std::ostream & operator << (std::ostream& os,
357 // const ArtsAsMatrixEntry & artsAsMatrixEntry)
358 //.........................................................................
359 //
360 //-------------------------------------------------------------------------
operator <<(std::ostream & os,const ArtsAsMatrixEntry & artsAsMatrixEntry)361 std::ostream & operator << (std::ostream& os,
362 const ArtsAsMatrixEntry & artsAsMatrixEntry)
363 {
364 using namespace std;
365 os << "\tAS MATRIX ENTRY" << endl;
366 os << "\t\tdescriptor: " << (int)artsAsMatrixEntry.Descriptor() << endl;
367 os << "\t\tsrc: " << artsAsMatrixEntry.Src() << endl;
368 os << "\t\tdst: " << artsAsMatrixEntry.Dst() << endl;
369 os << "\t\tpkts: " << artsAsMatrixEntry.Pkts() << endl;
370 os << "\t\tbytes: " << artsAsMatrixEntry.Bytes() << endl;
371
372 return(os);
373 }
374
375 //---------------------------------------------------------------------------
376 // bool ArtsAsMatrixEntryGreaterBytes::
377 // operator () (const ArtsAsMatrixEntry & asEntry1,
378 // const ArtsAsMatrixEntry & asEntry2) const
379 //...........................................................................
380 //
381 //---------------------------------------------------------------------------
382 bool ArtsAsMatrixEntryGreaterBytes::
operator ()(const ArtsAsMatrixEntry & asEntry1,const ArtsAsMatrixEntry & asEntry2) const383 operator () (const ArtsAsMatrixEntry & asEntry1,
384 const ArtsAsMatrixEntry & asEntry2) const
385 {
386 return(asEntry1.Bytes() > asEntry2.Bytes());
387 }
388
389 //---------------------------------------------------------------------------
390 // bool ArtsAsMatrixEntryGreaterPkts::
391 // operator () (const ArtsAsMatrixEntry & asEntry1,
392 // const ArtsAsMatrixEntry & asEntry2) const
393 //...........................................................................
394 //
395 //---------------------------------------------------------------------------
396 bool ArtsAsMatrixEntryGreaterPkts::
operator ()(const ArtsAsMatrixEntry & asEntry1,const ArtsAsMatrixEntry & asEntry2) const397 operator () (const ArtsAsMatrixEntry & asEntry1,
398 const ArtsAsMatrixEntry & asEntry2) const
399 {
400 return(asEntry1.Pkts() > asEntry2.Pkts());
401 }
402
403 uint32_t ArtsAsMatrixEntry::_numObjects = 0;
404