packettracer.cc

00001 /*
00002  * Copyright (c) 2006 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 #include <stdarg.h>
00030 #include "packettracer.h"
00031 #include "sap.h"
00032 
00033 /*-------------------------------------------------------------------------------------------------------
00034         methods for Tracer class
00035 ---------------------------------------------------------------------------------------------------------*/ 
00036 
00037 Tracer::Tracer(int level) : level_(level)
00038 {
00039         next_ = 0;
00040 }
00041 
00042 int Tracer::level()
00043 {
00044         return (level_);
00045 }
00046 
00047 Tracer* Tracer::next()
00048 {
00049         return (next_);
00050 }
00051 
00052 void Tracer::next(Tracer *tr)
00053 {
00054         next_ = tr;
00055 }
00056 
00057 void Tracer::writeTrace(SAP *sap, char *s, ...)
00058 {
00059         va_list ap;
00060         va_start(ap,s);
00061         sap->vWriteTrace(s, ap);
00062         //printf(s,ap);
00063         va_end(ap);
00064 }
00065 
00066 
00067 void Tracer::trace(Packet *p, SAP *sap)
00068 {
00069 //      printf("Tracer::trace(%p,%p) --- this=%p next_=%p\n", p, sap, this, next_);
00070         if(p)
00071                 format(p, sap);
00072         if(next_)
00073                 next_->trace(p, sap);
00074 }
00075 
00076 
00077 
00078 /*-------------------------------------------------------------------------------------------------------
00079         methods for PktTracer class
00080 ---------------------------------------------------------------------------------------------------------*/ 
00081 
00082 PktTracer::PktTracer(void) 
00083 {
00084         tr_ = 0;
00085 //      printf("PktTracer::PktTracer() --- tr_=%p\n", tr_);
00086 //      fflush(stdout);
00087 }
00088 
00089 // add a new tracer (insert sorted by level)
00090 // (a new tracer of the same level is inserted in tail of the already inserted tracer)
00091 void PktTracer::addTracer(Tracer *newTr)
00092 {
00093 //      printf("PktTracer::addTracer(%p) --- newTr->next=%p newTr->level()=%i tr=%p\n", newTr, newTr->next(), newTr->level(), tr_);
00094         
00095         if (newTr->level()<=0)
00096         {
00097                 fprintf(stderr, "Error PktTracer: try to install  a tracer in a level <= 0");
00098                 exit(1);
00099         }
00100         Tracer*pt = tr_;
00101         Tracer*last = tr_;
00102         
00103         if (pt==0)
00104         {
00105                 // first Tracer added
00106                 tr_ = newTr;
00107                 //newTr->next(0);
00108                 return;
00109         }
00110         // insert sorted
00111         bool inserted = FALSE;
00112         while (pt && !inserted)
00113         {
00114                 if (pt->level() > newTr->level())
00115                 {
00116                         // find the position to insert the new tracer
00117                         if (pt==tr_)
00118                         {
00119                                 // insert in head of list
00120                                 newTr->next(tr_);
00121                                 tr_ = newTr;
00122                                 inserted = TRUE;
00123                         }
00124                         else
00125                         {
00126                                 // insert in the middle
00127                                 newTr->next(last->next());
00128                                 last->next(newTr);
00129 //                              newTr->next(pt->next());
00130                                 inserted = TRUE;
00131                         }
00132                 }
00133                 else
00134                 {
00135                         last = pt;
00136                         pt = pt->next();
00137                 }
00138         }
00139         if (!inserted)
00140         {
00141                 // insert in tail
00142                 last->next(newTr);
00143                 newTr->next(0);
00144         }
00145 //      tr_->trace(0,0);
00146 }
00147 
00148 // begin the trace of the packet that is crossing the SAP
00149 void PktTracer::trace(Packet *p, SAP *sap)
00150 {
00151         if (tr_!=0)
00152         {
00153 //              printf("PktTracer::trace(%p,%p) --- tr_=%p\n", p, sap, tr_);
00154                 tr_->trace(p, sap);
00155         }
00156         else
00157         {
00158                 fprintf(stderr,"Error PktTracer: there isn't any tracer installed'");
00159                 exit(1);
00160         }
00161 }

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2