NETWORK ATTACKS FRAMEWORK  1.0.0
A NETwork Attacks framework. Making network attacks impact evaluation easier!
NA_timer_queue_aodv.cc
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * Copyright (C) 2001 Uppsala University & Ericsson AB.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * Authors: Erik Nordstr�m, <erik.nordstrom@it.uu.se>
00020  *
00021  *****************************************************************************/
00022 #define NS_PORT
00023 #define OMNETPP
00024 #include <stdlib.h>
00025 #include <signal.h>
00026 
00027 #ifndef _WIN32
00028 #include <sys/time.h>
00029 #endif
00030 
00031 #ifdef NS_PORT
00032 #ifndef OMNETPP
00033 #include "ns/aodv-uu.h"
00034 #else
00035 #include "../NA_aodv_uu_omnet.h"
00036 #endif
00037 #else
00038 #include "NA_timer_queue_aodv.h"
00039 #include "NA_defs_aodv.h"
00040 #include "NA_debug_aodv.h"
00041 #include "NA_list.h"
00042 
00043 static LIST(TQ);
00044 
00045 /* #define DEBUG_TIMER_QUEUE */
00046 
00047 #ifdef DEBUG_TIMER_QUEUE
00048 static void printTQ(list_t * l);
00049 #endif
00050 #endif              /* NS_PORT */
00051 
00052 
00053 #ifdef AODV_USE_STL
00054 int NS_CLASS timer_init(struct timer *t, timeout_func_t f, void *data)
00055 {
00056     if (!t)
00057         return -1;
00058     t->handler = f;
00059     t->data = data;
00060     t->timeout = 0;
00061     t->used = 0;
00062     return 0;
00063 }
00064 
00065 /* Called when a timer should timeout */
00066 void NS_CLASS timer_timeout(const simtime_t &now)
00067 {
00068 
00069     while (!aodvTimerMap.empty())
00070     {
00071         if (aodvTimerMap.begin()->first > now)
00072             return;
00073         struct timer *t = aodvTimerMap.begin()->second;
00074         aodvTimerMap.erase(aodvTimerMap.begin());
00075         t->used = 0;
00076         /* Execute handler function for expired timer... */
00077         if (t->handler)
00078         {
00079             (*this.*t->handler) (t->data);
00080         }
00081 
00082     }
00083 }
00084 
00085 NS_STATIC void NS_CLASS timer_add(struct timer *t)
00086 {
00087     /* Sanity checks: */
00088     if (!t)
00089     {
00090         perror("NULL timer!!!\n");
00091         exit(-1);
00092     }
00093     if (!t->handler)
00094     {
00095         perror("NULL handler!!!\n");
00096         exit(-1);
00097     }
00098 
00099     /* Make sure we remove unexpired timers before adding a new timeout... */
00100     if (t->used)
00101         timer_remove(t);
00102 
00103     t->used = 1;
00104     aodvTimerMap.insert(std::make_pair(t->timeout,t));
00105     return;
00106 }
00107 
00108 int NS_CLASS timer_remove(struct timer *t)
00109 {
00110     if (!t)
00111         return -1;
00112 
00113     t->used = 0;
00114     for (AodvTimerMap::iterator it = aodvTimerMap.begin();it != aodvTimerMap.end();it++)
00115     {
00116         if (it->second == t)
00117         {
00118             aodvTimerMap.erase(it);
00119             return 1;
00120         }
00121     }
00122     return 0;
00123 }
00124 
00125 
00126 int NS_CLASS timer_timeout_now(struct timer *t)
00127 {
00128     if (timer_remove(t)>0)
00129     {
00130         (*this.*t->handler) (t->data);
00131         return 1;
00132     }
00133     return -1;
00134 }
00135 
00136 
00137 void NS_CLASS timer_set_timeout(struct timer *t, long msec)
00138 {
00139     if (t->used)
00140     {
00141         timer_remove(t);
00142     }
00143 
00144 
00145     if (msec < 0)
00146     {
00147         DEBUG(LOG_WARNING, 0, "Negative timeout!!!");
00148         msec=0;
00149     }
00150     double auxtime = ((double)msec)/1000.0;
00151     t->timeout = simTime() + auxtime;
00152     timer_add(t);
00153 }
00154 
00155 simtime_t NS_CLASS timer_age_queue()
00156 {
00157     simtime_t now;
00158     simtime_t remaining;
00159     now = simTime();
00160     timer_timeout(now);
00161     if (aodvTimerMap.empty())
00162         return remaining;
00163     remaining =  aodvTimerMap.begin()->first - now;
00164     return remaining;
00165 }
00166 #else
00167 int NS_CLASS timer_init(struct timer *t, timeout_func_t f, void *data)
00168 {
00169     if (!t)
00170         return -1;
00171 
00172     INIT_LIST_ELM(&t->l);
00173     t->handler = f;
00174     t->data = data;
00175     t->timeout.tv_sec = 0;
00176     t->timeout.tv_usec = 0;
00177     t->used = 0;
00178 
00179     return 0;
00180 }
00181 
00182 /* Called when a timer should timeout */
00183 void NS_CLASS timer_timeout(struct timeval *now)
00184 {
00185     LIST(expTQ);
00186     list_t *pos, *tmp;
00187     list_t *expTQ_ptr;
00188 
00189     expTQ_ptr=&expTQ;
00190 #ifdef DEBUG_TIMER_QUEUE
00191     printf("\n######## timer_timeout: called!!\n");
00192 #endif
00193     /* Remove expired timers from TQ and add them to expTQ */
00194     list_foreach_safe(pos, tmp, &TQ)
00195     {
00196         struct timer *t = (struct timer *) pos;
00197 
00198         if (timeval_diff(&t->timeout, now) > 0)
00199             break;
00200 
00201         list_detach(&t->l);
00202         list_add_tail(expTQ_ptr, &t->l);
00203     }
00204 
00205     /* Execute expired timers in expTQ safely by removing them at the head */
00206     while (!list_empty(expTQ_ptr))
00207     {
00208         struct timer *t = (struct timer *) list_first(expTQ_ptr);
00209         list_detach(&t->l);
00210         t->used = 0;
00211 #ifdef DEBUG_TIMER_QUEUE
00212         printf("removing timer %lu %d\n", pos);
00213 #endif
00214         /* Execute handler function for expired timer... */
00215         if (t->handler)
00216         {
00217 #ifdef NS_PORT
00218             (*this.*t->handler) (t->data);
00219 #else
00220             t->handler(t->data);
00221 #endif
00222         }
00223     }
00224 }
00225 
00226 NS_STATIC void NS_CLASS timer_add(struct timer *t)
00227 {
00228     list_t *pos;
00229     list_t * lista_ptr;
00230     /* Sanity checks: */
00231 
00232     if (!t)
00233     {
00234         perror("NULL timer!!!\n");
00235         exit(-1);
00236     }
00237     if (!t->handler)
00238     {
00239         perror("NULL handler!!!\n");
00240         exit(-1);
00241     }
00242 
00243     /* Make sure we remove unexpired timers before adding a new timeout... */
00244     if (t->used)
00245         timer_remove(t);
00246 
00247     t->used = 1;
00248 
00249 #ifdef DEBUG_TIMER_QUEUE
00250     printf("New timer added!\n");
00251 #endif
00252     lista_ptr = &TQ;
00253     /* Base case when queue is empty: */
00254     if (list_empty(&TQ))
00255     {
00256         list_add(&TQ, &t->l);
00257     }
00258     else
00259     {
00260 
00261         list_foreach(pos, &TQ)
00262         {
00263             struct timer *curr = (struct timer *) pos;
00264             if (timeval_diff(&t->timeout, &curr->timeout) < 0)
00265             {
00266                 break;
00267             }
00268         }
00269         list_add(pos->prev, &t->l);
00270     }
00271 
00272 #ifdef DEBUG_TIMER_QUEUE
00273     printTQ(&TQ);
00274 #endif
00275     return;
00276 }
00277 
00278 int NS_CLASS timer_remove(struct timer *t)
00279 {
00280     int res = 1;
00281 
00282     if (!t)
00283         return -1;
00284 
00285 
00286     if (list_unattached(&t->l))
00287         res = 0;
00288     else
00289         list_detach(&t->l);
00290 
00291     t->used = 0;
00292 
00293     return res;
00294 }
00295 
00296 
00297 int NS_CLASS timer_timeout_now(struct timer *t)
00298 {
00299     if (timer_remove(t))
00300     {
00301 
00302 #ifdef NS_PORT
00303         (*this.*t->handler) (t->data);
00304 #else
00305         t->handler(t->data);
00306 #endif
00307         return 1;
00308     }
00309     return -1;
00310 }
00311 
00312 
00313 void NS_CLASS timer_set_timeout(struct timer *t, long msec)
00314 {
00315     if (t->used)
00316     {
00317         timer_remove(t);
00318     }
00319 
00320     gettimeofday(&t->timeout, NULL);
00321 
00322     if (msec < 0)
00323         DEBUG(LOG_WARNING, 0, "Negative timeout!!!");
00324 
00325     t->timeout.tv_usec += msec * 1000;
00326     t->timeout.tv_sec += t->timeout.tv_usec / 1000000;
00327     t->timeout.tv_usec = t->timeout.tv_usec % 1000000;
00328 
00329     timer_add(t);
00330 }
00331 
00332 #ifndef OMNETPP
00333 long timer_left(struct timer *t)
00334 {
00335     struct timeval now;
00336 
00337     if (!t)
00338         return -1;
00339 
00340     gettimeofday(&now, NULL);
00341 
00342     return timeval_diff(&now, &t->timeout);
00343 }
00344 #endif
00345 
00346 struct timeval *NS_CLASS timer_age_queue()
00347 {
00348     struct timeval now;
00349     struct timer *t;
00350     static struct timeval remaining;
00351 
00352     gettimeofday(&now, NULL);
00353 
00354     fflush(stdout);
00355 
00356     if (list_empty(&TQ))
00357         return NULL;
00358 
00359     timer_timeout(&now);
00360 
00361     /* Check emptyness again since the list might have been updated by a
00362      * timeout */
00363     if (list_empty(&TQ))
00364         return NULL;
00365 
00366     t = (struct timer *) TQ.next;
00367 
00368     remaining.tv_usec = (t->timeout.tv_usec - now.tv_usec);
00369     remaining.tv_sec = (t->timeout.tv_sec - now.tv_sec);
00370 
00371     if (remaining.tv_usec < 0)
00372     {
00373         remaining.tv_usec += 1000000;
00374         remaining.tv_sec -= 1;
00375     }
00376     return (&remaining);
00377 }
00378 
00379 
00380 #ifdef DEBUG_TIMER_QUEUE
00381 void NS_CLASS printTQ(list_t * l)
00382 {
00383     struct timeval now;
00384     int n = 0;
00385     list_t *pos;
00386 
00387     gettimeofday(&now, NULL);
00388 
00389     fprintf(stderr, "================\n");
00390     fprintf(stderr, "%-12s %-4s %lu\n", "left", "n", (unsigned long) l);
00391 
00392     list_foreach(pos, l)
00393     {
00394         struct timer *t = (struct timer *) pos;
00395         fprintf(stderr, "%-12ld %-4d %lu\n", timeval_diff(&t->timeout, &now), n,
00396                 (unsigned long) pos);
00397         n++;
00398     }
00399 }
00400 #endif
00401 #endif
 All Classes Files Functions Variables Typedefs Enumerator Defines