1 //===========================================================================
2 //  @(#) $Name: arts++-1-1-a13 $
3 //  @(#) $Id: ArtsAttribute.cc,v 1.3 2008/04/14 21:17:10 rkoga 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 <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <time.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51 
52 #include "artslocal.h"
53 #include "caida_t.h"
54 }
55 
56 #include <cstring> // memset() and memcpy()
57 #include <string>
58 #ifdef HAVE_IOMANIP
59   #include <iomanip>
60 #else
61   #include <iomanip.h>
62 #endif
63 
64 #include "ArtsAttribute.hh"
65 #include "ArtsPrimitive.hh"
66 
67 static const std::string rcsid = "@(#) $Name: arts++-1-1-a13 $ $Id: ArtsAttribute.cc,v 1.3 2008/04/14 21:17:10 rkoga Exp $";
68 
69 //-------------------------------------------------------------------------
70 //                      ArtsAttribute::ArtsAttribute()
71 //.........................................................................
72 //  constructor
73 //-------------------------------------------------------------------------
ArtsAttribute()74 ArtsAttribute::ArtsAttribute()
75 {
76   this->_identifier = 0;
77   this->_format = 0;
78   this->_length = 0;
79   memset(&(this->_value),0,sizeof(this->_value));
80   this->_value.comment = (std::string *)0;
81   this->_value.ifDescr = (std::string *)0;
82   this->_value.arbitrary = (std::string *)0;
83 
84   #ifndef NDEBUG
85     ++_numObjects;
86   #endif
87 }
88 
ArtsAttribute(const ArtsAttribute & artsAttribute)89 ArtsAttribute::ArtsAttribute(const ArtsAttribute & artsAttribute)
90 {
91   this->_identifier = artsAttribute.Identifier();
92   this->_format = artsAttribute.Format();
93   this->_length = artsAttribute.Length();
94 
95   switch (this->_identifier) {
96     case artsC_ATTR_COMMENT:
97       this->_value.comment = new std::string(artsAttribute.Comment());
98       break;
99     case artsC_ATTR_IFDESCR:
100       this->_value.ifDescr = new std::string(artsAttribute.IfDescr().c_str());
101       break;
102     case artsC_ATTR_CREATION:
103       this->_value.creation = artsAttribute.Creation();
104       break;
105     case artsC_ATTR_PERIOD:
106       memcpy(this->_value.period,artsAttribute.Period(),
107              sizeof(this->_value.period[0]) * 2);
108       break;
109     case artsC_ATTR_HOST:
110       this->_value.host = artsAttribute.Host();
111       break;
112     case artsC_ATTR_IFINDEX:
113       this->_value.ifIndex = artsAttribute.IfIndex();
114       break;
115     case artsC_ATTR_IFIPADDR:
116       this->_value.ifIpAddr = artsAttribute.IfIpAddr();
117       break;
118     case artsC_ATTR_HOSTPAIR:
119       this->_value.hostPair[0] = artsAttribute.HostPair()[0];
120       this->_value.hostPair[1] = artsAttribute.HostPair()[1];
121       break;
122     default:
123       break;
124   }
125 }
126 
127 //-------------------------------------------------------------------------
128 //     ArtsAttribute &
129 //     ArtsAttribute::operator = (const ArtsAttribute & artsAttribute)
130 //.........................................................................
131 //
132 //-------------------------------------------------------------------------
133 ArtsAttribute &
operator =(const ArtsAttribute & artsAttribute)134 ArtsAttribute::operator = (const ArtsAttribute & artsAttribute)
135 {
136   switch (this->_identifier) {
137     case artsC_ATTR_COMMENT:
138       if (this->_value.comment != (std::string *)0) {
139         delete(this->_value.comment);
140         this->_value.comment = (std::string *)0;
141       }
142       break;
143     case artsC_ATTR_IFDESCR:
144       if (this->_value.ifDescr != (std::string *)0) {
145         delete(this->_value.ifDescr);
146         this->_value.ifDescr = (std::string *)0;
147       }
148       break;
149     default:
150       break;
151   }
152 
153   this->_identifier = artsAttribute.Identifier();
154   this->_format = artsAttribute.Format();
155   this->_length = artsAttribute.Length();
156 
157   switch (this->_identifier) {
158     case artsC_ATTR_COMMENT:
159       this->_value.comment = new std::string(artsAttribute.Comment());
160       break;
161     case artsC_ATTR_IFDESCR:
162       this->_value.ifDescr = new std::string(artsAttribute.IfDescr().c_str());
163       break;
164     case artsC_ATTR_CREATION:
165       this->_value.creation = artsAttribute.Creation();
166       break;
167     case artsC_ATTR_PERIOD:
168       memcpy(this->_value.period,artsAttribute.Period(),
169              sizeof(this->_value.period[0]) * 2);
170       break;
171     case artsC_ATTR_HOST:
172       this->_value.host = artsAttribute.Host();
173       break;
174     case artsC_ATTR_IFINDEX:
175       this->_value.ifIndex = artsAttribute.IfIndex();
176       break;
177     case artsC_ATTR_IFIPADDR:
178       this->_value.ifIpAddr = artsAttribute.IfIpAddr();
179       break;
180     case artsC_ATTR_HOSTPAIR:
181       this->_value.hostPair[0] = artsAttribute.HostPair()[0];
182       this->_value.hostPair[1] = artsAttribute.HostPair()[1];
183       break;
184     default:
185       break;
186   }
187 
188   return(*this);
189 }
190 
191 //-------------------------------------------------------------------------
192 //                     ArtsAttribute::~ArtsAttribute()
193 //.........................................................................
194 //
195 //-------------------------------------------------------------------------
~ArtsAttribute()196 ArtsAttribute::~ArtsAttribute()
197 {
198   switch (this->_identifier) {
199     case artsC_ATTR_COMMENT:
200       if (this->_value.comment != (std::string *)0)
201         delete(this->_value.comment);
202       break;
203     case artsC_ATTR_IFDESCR:
204       if (this->_value.ifDescr != (std::string *)0)
205         delete(this->_value.ifDescr);
206       break;
207     default:
208       break;
209   }
210 
211   #ifndef NDEBUG
212     if (_numObjects > 0)
213       --_numObjects;
214   #endif
215 }
216 
217 //----------------------------------------------------------------------------
218 //                uint32_t ArtsAttribute::Identifier() const
219 //............................................................................
220 //
221 //----------------------------------------------------------------------------
Identifier() const222 uint32_t ArtsAttribute::Identifier() const
223 {
224   return(this->_identifier);
225 }
226 
227 //----------------------------------------------------------------------------
228 //         uint32_t ArtsAttribute::Identifier(uint32_t identifier)
229 //............................................................................
230 //
231 //----------------------------------------------------------------------------
Identifier(uint32_t identifier)232 uint32_t ArtsAttribute::Identifier(uint32_t identifier)
233 {
234   this->_identifier = identifier;
235   return(this->_identifier);
236 }
237 
238 //----------------------------------------------------------------------------
239 //                  uint8_t ArtsAttribute::Format() const
240 //............................................................................
241 //
242 //----------------------------------------------------------------------------
Format() const243 uint8_t ArtsAttribute::Format() const
244 {
245   return(this->_format);
246 }
247 
248 //----------------------------------------------------------------------------
249 //                      uint8_t Format(uint8_t format)
250 //............................................................................
251 //
252 //----------------------------------------------------------------------------
Format(uint8_t format)253 uint8_t ArtsAttribute::Format(uint8_t format)
254 {
255   this->_format = format;
256   return(this->_format);
257 }
258 
259 //----------------------------------------------------------------------------
260 //                  uint32_t ArtsAttribute::Length() const
261 //............................................................................
262 //
263 //----------------------------------------------------------------------------
Length() const264 uint32_t ArtsAttribute::Length() const
265 {
266   return(this->_length);
267 }
268 
269 //----------------------------------------------------------------------------
270 //             uint32_t ArtsAttribute::Length(uint32_t length)
271 //............................................................................
272 //
273 //----------------------------------------------------------------------------
Length(uint32_t length)274 uint32_t ArtsAttribute::Length(uint32_t length)
275 {
276   this->_length = length;
277   return(this->_length);
278 }
279 
280 //----------------------------------------------------------------------------
281 //               const std::string ArtsAttribute::Comment() const
282 //............................................................................
283 //
284 //----------------------------------------------------------------------------
Comment() const285 const std::string ArtsAttribute::Comment() const
286 {
287   assert(this->Identifier() == artsC_ATTR_COMMENT);
288   return(*(this->_value.comment));
289 }
290 
291 //----------------------------------------------------------------------------
292 //                 uint32_t ArtsAttribute::Creation() const
293 //............................................................................
294 //
295 //----------------------------------------------------------------------------
Creation() const296 uint32_t ArtsAttribute::Creation() const
297 {
298   assert(this->Identifier() == artsC_ATTR_CREATION);
299   return(this->_value.creation);
300 }
301 
302 //----------------------------------------------------------------------------
303 //           uint32_t ArtsAttribute::Creation(uint32_t creation)
304 //............................................................................
305 //
306 //----------------------------------------------------------------------------
Creation(uint32_t creation)307 uint32_t ArtsAttribute::Creation(uint32_t creation)
308 {
309   assert(this->Identifier() == artsC_ATTR_CREATION);
310   this->_value.creation = creation;
311   return(this->_value.creation);
312 }
313 
314 //----------------------------------------------------------------------------
315 //              const uint32_t *ArtsAttribute::Period() const
316 //............................................................................
317 //
318 //----------------------------------------------------------------------------
Period() const319 const uint32_t *ArtsAttribute::Period() const
320 {
321   assert(this->Identifier() == artsC_ATTR_PERIOD);
322   return(this->_value.period);
323 }
324 
325 //----------------------------------------------------------------------------
326 //  uint32_t *ArtsAttribute::Period(uint32_t startTime, uint32_t endTime)
327 //............................................................................
328 //
329 //----------------------------------------------------------------------------
Period(uint32_t startTime,uint32_t endTime)330 uint32_t *ArtsAttribute::Period(uint32_t startTime, uint32_t endTime)
331 {
332   assert(this->Identifier() == artsC_ATTR_PERIOD);
333   this->_value.period[0] = startTime;
334   this->_value.period[1] = endTime;
335   return(this->_value.period);
336 }
337 
338 //----------------------------------------------------------------------------
339 //                  ipv4addr_t ArtsAttribute::Host() const
340 //............................................................................
341 //
342 //----------------------------------------------------------------------------
Host() const343 ipv4addr_t ArtsAttribute::Host() const
344 {
345   assert(this->Identifier() == artsC_ATTR_HOST);
346   return(this->_value.host);
347 }
348 
349 //----------------------------------------------------------------------------
350 //             ipv4addr_t ArtsAttribute::Host(ipv4addr_t host)
351 //............................................................................
352 //
353 //----------------------------------------------------------------------------
Host(ipv4addr_t host)354 ipv4addr_t ArtsAttribute::Host(ipv4addr_t host)
355 {
356   assert(this->Identifier() == artsC_ATTR_HOST);
357   this->_value.host = host;
358   return(this->_value.host);
359 }
360 
361 //----------------------------------------------------------------------------
362 //     const ipv4addr_t * ArtsAttribute::HostPair() const
363 //............................................................................
364 //
365 //----------------------------------------------------------------------------
HostPair() const366 const ipv4addr_t * ArtsAttribute::HostPair() const
367 {
368   assert(this->Identifier() == artsC_ATTR_HOSTPAIR);
369   return(this->_value.hostPair);
370 }
371 
372 //----------------------------------------------------------------------------
373 //     const ipv4addr_t * ArtsAttribute::HostPair(ipv4addr_t host1,
374 //                                                ipv4addr_t host2)
375 //............................................................................
376 //
377 //----------------------------------------------------------------------------
HostPair(ipv4addr_t host1,ipv4addr_t host2)378 const ipv4addr_t * ArtsAttribute::HostPair(ipv4addr_t host1,
379                                            ipv4addr_t host2)
380 {
381   assert(this->Identifier() == artsC_ATTR_HOSTPAIR);
382   this->_format = artsC_IPV4_ADDRESS_PAIR;
383   this->_value.hostPair[0] = host1;
384   this->_value.hostPair[1] = host2;
385   return(this->_value.hostPair);
386 }
387 
388 //----------------------------------------------------------------------------
389 //              const std::string ArtsAttribute::IfDescr() const
390 //............................................................................
391 //
392 //----------------------------------------------------------------------------
IfDescr() const393 const std::string ArtsAttribute::IfDescr() const
394 {
395   assert(this->Identifier() == artsC_ATTR_IFDESCR);
396   return(*(this->_value.ifDescr));
397 }
398 
399 //----------------------------------------------------------------------------
400 //        std::string ArtsAttribute::IfDescr(const std::string & ifDescr)
401 //............................................................................
402 //
403 //----------------------------------------------------------------------------
IfDescr(const std::string & ifDescr)404 std::string ArtsAttribute::IfDescr(const std::string & ifDescr)
405 {
406   assert(this->Identifier() == artsC_ATTR_IFDESCR);
407 
408   if (this->_value.ifDescr != (std::string *)0) {
409     delete(this->_value.ifDescr);
410     this->_value.ifDescr = (std::string *)0;
411   }
412   this->_value.ifDescr = new std::string(ifDescr.c_str());
413   return(*(this->_value.ifDescr));
414 }
415 
416 
417 //----------------------------------------------------------------------------
418 //                 uint16_t ArtsAttribute::IfIndex() const
419 //............................................................................
420 //
421 //----------------------------------------------------------------------------
IfIndex() const422 uint16_t ArtsAttribute::IfIndex() const
423 {
424   assert(this->Identifier() == artsC_ATTR_IFINDEX);
425   return(this->_value.ifIndex);
426 }
427 
428 //----------------------------------------------------------------------------
429 //            uint16_t ArtsAttribute::IfIndex(uint16_t ifIndex)
430 //............................................................................
431 //
432 //----------------------------------------------------------------------------
IfIndex(uint16_t ifIndex)433 uint16_t ArtsAttribute::IfIndex(uint16_t ifIndex)
434 {
435   assert(this->Identifier() == artsC_ATTR_IFINDEX);
436   this->_value.ifIndex = ifIndex;
437   return(this->_value.ifIndex);
438 }
439 
440 //----------------------------------------------------------------------------
441 //                ipv4addr_t ArtsAttribute::IfIpAddr() const
442 //............................................................................
443 //
444 //----------------------------------------------------------------------------
IfIpAddr() const445 ipv4addr_t ArtsAttribute::IfIpAddr() const
446 {
447   assert(this->Identifier() == artsC_ATTR_IFIPADDR);
448   return(this->_value.ifIpAddr);
449 }
450 
451 //----------------------------------------------------------------------------
452 //          ipv4addr_t ArtsAttribute::IfIpAddr(ipv4addr_t ipAddr)
453 //............................................................................
454 //
455 //----------------------------------------------------------------------------
IfIpAddr(ipv4addr_t ipAddr)456 ipv4addr_t ArtsAttribute::IfIpAddr(ipv4addr_t ipAddr)
457 {
458   assert(this->Identifier() == artsC_ATTR_IFIPADDR);
459   this->_value.ifIpAddr = ipAddr;
460   return(this->_value.ifIpAddr);
461 }
462 
463 //----------------------------------------------------------------------------
464 //                  void *ArtsAttribute::Arbitrary() const
465 //............................................................................
466 //
467 //----------------------------------------------------------------------------
Arbitrary() const468 void *ArtsAttribute::Arbitrary() const
469 {
470   return(this->_value.arbitrary);
471 }
472 
473 //----------------------------------------------------------------------------
474 //             void *ArtsAttribute::Arbitrary(void *arbitrary)
475 //............................................................................
476 //
477 //----------------------------------------------------------------------------
Arbitrary(void * arbitrary)478 void *ArtsAttribute::Arbitrary(void *arbitrary)
479 {
480   this->_value.arbitrary = arbitrary;
481   return(this->_value.arbitrary);
482 }
483 
484 //-------------------------------------------------------------------------
485 //             std::ostream& ArtsAttribute::write(std::ostream& os) const
486 //.........................................................................
487 //
488 //-------------------------------------------------------------------------
write(std::ostream & os) const489 std::ostream& ArtsAttribute::write(std::ostream& os) const
490 {
491   uint32_t     uIntDatum, idAndFormat;
492   uint16_t     uShortDatum;
493 
494   const char*  ptr;
495 
496   idAndFormat = (this->_identifier << 8) | this->_format;
497   uIntDatum = htonl(idAndFormat);
498   os.write((char*)&uIntDatum,sizeof(uIntDatum));
499 
500   uIntDatum = htonl(this->_length);
501   os.write((char*)&uIntDatum,sizeof(uIntDatum));
502 
503   switch (this->_identifier) {
504     case artsC_ATTR_COMMENT:
505       ptr = this->_value.comment->c_str();
506       os.write(ptr,this->_value.comment->length()+1);
507       break;
508     case artsC_ATTR_CREATION:
509       uIntDatum = htonl(this->_value.creation);
510       os.write((char*)&uIntDatum,sizeof(uIntDatum));
511       break;
512     case artsC_ATTR_PERIOD:
513       uIntDatum = htonl(this->_value.period[0]);
514       os.write((char*)&uIntDatum,sizeof(uIntDatum));
515       uIntDatum = htonl(this->_value.period[1]);
516       os.write((char*)&uIntDatum,sizeof(uIntDatum));
517       break;
518     case artsC_ATTR_HOST:
519       os.write((char*)&this->_value.host,sizeof(this->_value.host));
520       break;
521     case artsC_ATTR_IFDESCR:
522       ptr = this->_value.ifDescr->c_str();
523       os.write(ptr,this->_value.ifDescr->length()+1);
524       break;
525     case artsC_ATTR_IFINDEX:
526       uShortDatum = htons(this->_value.ifIndex);
527       os.write((char*)&uShortDatum,sizeof(uShortDatum));
528       break;
529     case artsC_ATTR_IFIPADDR:
530       os.write((char*)&this->_value.ifIpAddr,sizeof(this->_value.ifIpAddr));
531       break;
532     case artsC_ATTR_HOSTPAIR:
533       os.write((char*)&(this->_value.hostPair[0]),sizeof(ipv4addr_t));
534       os.write((char*)&(this->_value.hostPair[1]),sizeof(ipv4addr_t));
535       break;
536     default:
537       break;
538   }
539   return(os);
540 }
541 
542 //-------------------------------------------------------------------------
543 //                  int ArtsAttribute::write(int fd) const
544 //.........................................................................
545 //
546 //-------------------------------------------------------------------------
write(int fd) const547 int ArtsAttribute::write(int fd) const
548 {
549   uint32_t     uIntDatum, idAndFormat;
550   uint16_t     uShortDatum;
551   const char*  ptr;
552   int          rc;
553   int          bytesWritten = 0;
554 
555   idAndFormat = (this->_identifier << 8) | this->_format;
556   uIntDatum = htonl(idAndFormat);
557   if ((rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&uIntDatum,
558                                                 sizeof(uIntDatum))) <
559       (int)sizeof(uIntDatum)) {
560     return(-1);
561   }
562   bytesWritten += rc;
563 
564   uIntDatum = htonl(this->_length);
565   if ((rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&uIntDatum,
566                                                 sizeof(uIntDatum))) <
567       (int)sizeof(uIntDatum)) {
568     return(-1);
569   }
570   bytesWritten += rc;
571 
572   switch (this->_identifier) {
573     case artsC_ATTR_COMMENT:
574       ptr = this->_value.comment->c_str();
575       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,ptr,this->_value.comment->length()+1);
576       if (rc != (int)(this->_value.comment->length()+1)) {
577         return(-1);
578       }
579       bytesWritten += rc;
580       break;
581     case artsC_ATTR_CREATION:
582       uIntDatum = htonl(this->_value.creation);
583       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&uIntDatum,
584                                                sizeof(uIntDatum));
585       if (rc != sizeof(uIntDatum)) {
586         return(-1);
587       }
588       bytesWritten += rc;
589       break;
590     case artsC_ATTR_PERIOD:
591       uIntDatum = htonl(this->_value.period[0]);
592       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&uIntDatum,
593                                                sizeof(uIntDatum));
594       if (rc != sizeof(uIntDatum)) {
595         return(-1);
596       }
597       bytesWritten += rc;
598       uIntDatum = htonl(this->_value.period[1]);
599       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&uIntDatum,
600                                                sizeof(uIntDatum));
601       if (rc != sizeof(uIntDatum)) {
602         return(-1);
603       }
604       bytesWritten += rc;
605       break;
606     case artsC_ATTR_HOST:
607       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&this->_value.host,
608                                                sizeof(this->_value.host));
609       if (rc != sizeof(this->_value.host)) {
610         return(-1);
611       }
612       bytesWritten += rc;
613       break;
614     case artsC_ATTR_IFDESCR:
615       ptr = this->_value.ifDescr->c_str();
616       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,ptr,this->_value.ifDescr->length()+1);
617       if (rc != (int)(this->_value.ifDescr->length()+1)) {
618         return(-1);
619       }
620       bytesWritten += rc;
621       break;
622     case artsC_ATTR_IFINDEX:
623       uShortDatum = htons(this->_value.ifIndex);
624       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&uShortDatum,
625                                                sizeof(uShortDatum));
626       if (rc != sizeof(uShortDatum)) {
627         return(-1);
628       }
629       bytesWritten += rc;
630       break;
631     case artsC_ATTR_IFIPADDR:
632       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&this->_value.ifIpAddr,
633                                                sizeof(this->_value.ifIpAddr));
634       if (rc != sizeof(this->_value.ifIpAddr)) {
635         return(-1);
636       }
637       bytesWritten += rc;
638       break;
639     case artsC_ATTR_HOSTPAIR:
640       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&(this->_value.hostPair[0]),
641                                                sizeof(ipv4addr_t));
642       if (rc != sizeof(ipv4addr_t)) {
643         return(-1);
644       }
645       bytesWritten += rc;
646       rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&(this->_value.hostPair[1]),
647                                                sizeof(ipv4addr_t));
648       if (rc != sizeof(ipv4addr_t)) {
649         return(-1);
650       }
651       bytesWritten += rc;
652       break;
653     default:
654       break;
655   }
656   return(bytesWritten);
657 }
658 
659 //-------------------------------------------------------------------------
660 //                std::istream& ArtsAttribute::read(std::istream& is)
661 //.........................................................................
662 //
663 //-------------------------------------------------------------------------
read(std::istream & is)664 std::istream& ArtsAttribute::read(std::istream& is)
665 {
666   uint32_t  uIntDatum, idAndFormat;
667   char*     ptr;
668 
669   switch (this->_identifier) {
670     case artsC_ATTR_COMMENT:
671       if (this->_value.comment != (std::string *)0) {
672         delete(this->_value.comment);
673         this->_value.comment = (std::string *)0;
674       }
675       break;
676     case artsC_ATTR_IFDESCR:
677       if (this->_value.ifDescr != (std::string *)0) {
678         delete(this->_value.ifDescr);
679         this->_value.ifDescr = (std::string *)0;
680       }
681       break;
682     default:
683       break;
684   }
685 
686   is.read((char*)&uIntDatum,sizeof(uIntDatum));
687   idAndFormat = ntohl(uIntDatum);
688   this->_identifier = idAndFormat >> 8;
689   this->_format = idAndFormat & 0xff;
690 
691   is.read((char*)&uIntDatum,sizeof(uIntDatum));
692   this->_length = ntohl(uIntDatum);
693 
694   switch (this->_identifier) {
695     case artsC_ATTR_COMMENT:
696       ptr = (char *)malloc(this->_length - (sizeof(uint32_t) * 2));
697       assert(ptr);
698       memset(ptr,0,this->_length - (sizeof(uint32_t) * 2));
699       is.read(ptr,this->_length - (sizeof(uint32_t) * 2));
700       this->_value.comment = new std::string(ptr);
701       free(ptr);
702       break;
703     case artsC_ATTR_CREATION:
704       is.read((char*)&uIntDatum,sizeof(uIntDatum));
705       this->_value.creation = ntohl(uIntDatum);
706       break;
707     case artsC_ATTR_PERIOD:
708       is.read((char*)&uIntDatum,sizeof(uIntDatum));
709       this->_value.period[0] = htonl(uIntDatum);
710       is.read((char*)&uIntDatum,sizeof(uIntDatum));
711       this->_value.period[1] = htonl(uIntDatum);
712       break;
713     case artsC_ATTR_HOST:
714       is.read((char*)&this->_value.host,sizeof(this->_value.host));
715       break;
716     case artsC_ATTR_IFDESCR:
717       ptr = (char *)malloc(this->_length - (sizeof(uint32_t) * 2));
718       assert(ptr);
719       memset(ptr,0,this->_length - (sizeof(uint32_t) * 2));
720       is.read(ptr,this->_length - (sizeof(uint32_t) * 2));
721       this->_value.ifDescr = new std::string(ptr);
722       free(ptr);
723       break;
724     case artsC_ATTR_IFINDEX:
725       is.read((char*)&this->_value.ifIndex,sizeof(this->_value.ifIndex));
726       this->_value.ifIndex = ntohs(this->_value.ifIndex);
727       break;
728     case artsC_ATTR_IFIPADDR:
729       is.read((char*)&this->_value.ifIpAddr,sizeof(this->_value.ifIpAddr));
730       break;
731     case artsC_ATTR_HOSTPAIR:
732       is.read((char*)&(this->_value.hostPair[0]),sizeof(ipv4addr_t));
733       is.read((char*)&(this->_value.hostPair[1]),sizeof(ipv4addr_t));
734       break;
735     default:
736       break;
737   }
738 
739   return(is);
740 }
741 
742 //-------------------------------------------------------------------------
743 //                     int ArtsAttribute::read(int fd)
744 //.........................................................................
745 //
746 //-------------------------------------------------------------------------
read(int fd)747 int ArtsAttribute::read(int fd)
748 {
749   uint32_t  uIntDatum, idAndFormat;
750   char*     ptr;
751   int       rc;
752   int       bytesRead = 0;
753 
754   rc = g_ArtsLibInternal_Primitive.FdRead(fd,&uIntDatum,sizeof(uIntDatum));
755   if (rc <= 0)
756     return(rc);
757   bytesRead += rc;
758   idAndFormat = ntohl(uIntDatum);
759   this->_identifier = idAndFormat >> 8;
760   this->_format = idAndFormat & 0xff;
761 
762   rc = g_ArtsLibInternal_Primitive.FdRead(fd,&uIntDatum,sizeof(uIntDatum));
763   if (rc <= 0)
764     return(rc);
765   bytesRead += rc;
766 
767   this->_length = ntohl(uIntDatum);
768 
769   int  commentLen, ifDescrLen;
770 
771   switch (this->_identifier) {
772     case artsC_ATTR_COMMENT:
773       commentLen = this->_length - (sizeof(uint32_t) * 2);
774       ptr = (char *)malloc(commentLen);
775       assert(ptr);
776       memset(ptr,0,commentLen);
777       rc = g_ArtsLibInternal_Primitive.FdRead(fd,ptr,commentLen);
778       if (rc < commentLen)
779         return(rc);
780       bytesRead += rc;
781       if (this->_value.comment)
782         delete(this->_value.comment);
783       this->_value.comment = new std::string(ptr);
784       free(ptr);
785       break;
786     case artsC_ATTR_CREATION:
787       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&uIntDatum,
788                                               sizeof(uIntDatum));
789       if (rc <= 0)
790         return(rc);
791       bytesRead += rc;
792       this->_value.creation = ntohl(uIntDatum);
793       break;
794     case artsC_ATTR_PERIOD:
795       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&uIntDatum,
796                                               sizeof(uIntDatum));
797       if (rc <= 0)
798         return(rc);
799       bytesRead += rc;
800       this->_value.period[0] = htonl(uIntDatum);
801       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&uIntDatum,
802                                               sizeof(uIntDatum));
803       if (rc <= 0)
804         return(rc);
805       bytesRead += rc;
806       this->_value.period[1] = htonl(uIntDatum);
807       break;
808     case artsC_ATTR_HOST:
809       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&this->_value.host,
810                                               sizeof(this->_value.host));
811       if (rc <= 0)
812         return(rc);
813       bytesRead += rc;
814       break;
815     case artsC_ATTR_IFDESCR:
816       ifDescrLen = this->_length - (sizeof(uint32_t) * 2);
817       ptr = (char *)malloc(ifDescrLen);
818       assert(ptr);
819       memset(ptr,0,ifDescrLen);
820       rc = g_ArtsLibInternal_Primitive.FdRead(fd,ptr,ifDescrLen);
821       if (rc < ifDescrLen)
822         return(rc);
823       bytesRead += rc;
824       if (this->_value.ifDescr != (std::string *)0)
825         delete(this->_value.ifDescr);
826       this->_value.ifDescr = new std::string(ptr);
827       free(ptr);
828       break;
829     case artsC_ATTR_IFINDEX:
830       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&this->_value.ifIndex,
831                                               sizeof(this->_value.ifIndex));
832       if (rc <= 0)
833         return(rc);
834       this->_value.ifIndex = ntohs(this->_value.ifIndex);
835       bytesRead += rc;
836       break;
837     case artsC_ATTR_IFIPADDR:
838       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&this->_value.ifIpAddr,
839                                               sizeof(this->_value.ifIpAddr));
840       if (rc <= 0)
841         return(rc);
842       bytesRead += rc;
843       break;
844     case artsC_ATTR_HOSTPAIR:
845       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&(this->_value.hostPair[0]),
846                                               sizeof(ipv4addr_t));
847       if (rc <= 0)
848         return(rc);
849       bytesRead += rc;
850       rc = g_ArtsLibInternal_Primitive.FdRead(fd,&(this->_value.hostPair[1]),
851                                               sizeof(ipv4addr_t));
852       if (rc <= 0)
853         return(rc);
854       bytesRead += rc;
855       break;
856     default:
857       break;
858   }
859 
860   return(bytesRead);
861 }
862 
863 //-------------------------------------------------------------------------
864 //    std::ostream & operator << (std::ostream &os, ArtsAttribute & artsAttribute)
865 //.........................................................................
866 //
867 //-------------------------------------------------------------------------
operator <<(std::ostream & os,ArtsAttribute & artsAttribute)868 std::ostream & operator << (std::ostream &os, ArtsAttribute & artsAttribute)
869 {
870   struct in_addr  inAddr;
871   time_t          timestamp;
872   struct tm      *Tm;
873 
874   using namespace std;
875 
876   os << "ATTRIBUTE" << endl;
877   switch (artsAttribute.Identifier()) {
878     case artsC_ATTR_COMMENT:
879       break;
880     case artsC_ATTR_CREATION:
881       timestamp = artsAttribute.Creation();
882       Tm = localtime(&timestamp);
883       os << setiosflags(ios::internal);
884       os << "\tcreation: " << setfill('0') << setw(2) << Tm->tm_mon+1 << "/"
885          << setw(2) << Tm->tm_mday << "/" << setw(4) << Tm->tm_year+1900
886          << " " << setw(2) << Tm->tm_hour << ":" << setw(2)
887          << Tm->tm_min << ":" << setw(2) << Tm->tm_sec << " ("
888          << hex << (int)(artsAttribute.Creation()) << ")" << dec << endl;
889       os << setfill(' ');
890       break;
891     case artsC_ATTR_PERIOD:
892       os << "\tperiod: " << dec << artsAttribute.Period()[0] << " "
893          << artsAttribute.Period()[1] << endl;
894       break;
895     case artsC_ATTR_HOST:
896       inAddr.s_addr = artsAttribute.Host();
897       os << "\thost: " << inet_ntoa(inAddr) << endl;
898       break;
899     case artsC_ATTR_IFDESCR:
900       os << "\tifDescr: " << artsAttribute.IfDescr() << endl;
901       break;
902     case artsC_ATTR_IFINDEX:
903       os << "\tifIndex: " << artsAttribute.IfIndex() << endl;
904       break;
905     case artsC_ATTR_IFIPADDR:
906       inAddr.s_addr = artsAttribute.IfIpAddr();
907       os << "\tifIpAddr: " << inet_ntoa(inAddr) << endl;
908       break;
909     case artsC_ATTR_HOSTPAIR:
910       inAddr.s_addr = artsAttribute.HostPair()[0];
911       os << "\thostPair: " << inet_ntoa(inAddr);
912       inAddr.s_addr = artsAttribute.HostPair()[1];
913       os << " " << inet_ntoa(inAddr) << endl;
914       break;
915     default:
916       break;
917   }
918   return(os);
919 }
920 
921 uint32_t  ArtsAttribute::_numObjects = 0;
922