mad.c

Go to the documentation of this file.
00001 
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <time.h>
00037 #include <string.h>
00038 
00039 #ifdef _MSC_VER
00040 #include <winsock2.h>
00041 #include <ws2tcpip.h>
00042 #else
00043 #include <sys/time.h>
00044 #endif
00045 
00046 #include "mad.h"
00047 
00048 /* Initialize global variable */
00049 
00050 BOOL lib_init = FALSE;
00051 
00052 #ifdef _MSC_VER
00053 double time_factor;                             
00054 unsigned long long start_time;  
00055 #endif
00056 
00057 #ifdef _MSC_VER
00058 
00062 void sec_init(void) {   
00063         unsigned long long perf_cnt;
00064         QueryPerformanceFrequency((LARGE_INTEGER*)&perf_cnt);
00065         time_factor = 1.0/perf_cnt;
00066         QueryPerformanceCounter((LARGE_INTEGER*)&start_time);
00067 }
00068 #endif
00069 
00070 void alc_init(void) {
00071 
00072 #ifdef _MSC_VER
00073         sec_init(); /* Initialize timer */
00074 #endif
00075         lib_init = TRUE;
00076 }
00077 
00078 #ifdef _MSC_VER
00079 double sec(void) {      
00080         long long cur_time;
00081         double time_span;         
00082         QueryPerformanceCounter((LARGE_INTEGER*)&cur_time);
00083 
00084         time_span = (cur_time - start_time) * time_factor;
00085         return time_span;
00086 }       
00087 #else
00088 double sec(void) {
00089 
00090         struct timeval tv;
00091         gettimeofday(&tv, 0);
00092         return tv.tv_sec + tv.tv_usec / 1000000.0;
00093 }
00094 #endif
00095 
00096 int increase_ipv6_address(struct in6_addr *ipv6) { 
00097  
00098   if(ipv6->s6_addr[15] != 0xFF) { 
00099     ipv6->s6_addr[15]++; 
00100   } 
00101   else if(ipv6->s6_addr[14] != 0xFF) { 
00102     ipv6->s6_addr[15] = 0; 
00103     ipv6->s6_addr[14]++; 
00104   } 
00105   else if(ipv6->s6_addr[13] != 0xFF) { 
00106     ipv6->s6_addr[13]++; 
00107   } 
00108   else if(ipv6->s6_addr[12] != 0xFF) { 
00109     ipv6->s6_addr[13] = 0; 
00110     ipv6->s6_addr[12]++; 
00111   } 
00112   else if(ipv6->s6_addr[11] != 0xFF) { 
00113     ipv6->s6_addr[11]++; 
00114   } 
00115   else if(ipv6->s6_addr[10] != 0xFF) { 
00116     ipv6->s6_addr[11] = 0; 
00117     ipv6->s6_addr[10]++; 
00118   } 
00119   else if(ipv6->s6_addr[9] != 0xFF) { 
00120     ipv6->s6_addr[9]++; 
00121   } 
00122   else if(ipv6->s6_addr[8] != 0xFF) { 
00123     ipv6->s6_addr[9] = 0; 
00124     ipv6->s6_addr[8]++; 
00125   } 
00126   else if(ipv6->s6_addr[7] != 0xFF) { 
00127     ipv6->s6_addr[7]++; 
00128   } 
00129   else if(ipv6->s6_addr[6] != 0xFF) { 
00130     ipv6->s6_addr[7] = 0; 
00131     ipv6->s6_addr[6]++; 
00132   } 
00133   else if(ipv6->s6_addr[5] != 0xFF) { 
00134     ipv6->s6_addr[5]++; 
00135   } 
00136   else if(ipv6->s6_addr[4] != 0xFF) { 
00137     ipv6->s6_addr[5] = 0; 
00138     ipv6->s6_addr[4]++; 
00139   } 
00140   else if(ipv6->s6_addr[3] != 0xFF) { 
00141     ipv6->s6_addr[3]++; 
00142   } 
00143   else if(ipv6->s6_addr[2] != 0xFF) { 
00144     ipv6->s6_addr[3] = 0; 
00145     ipv6->s6_addr[2]++; 
00146   } 
00147   else if(ipv6->s6_addr[1] != 0xFF) { 
00148     ipv6->s6_addr[1]++; 
00149   } 
00150   else if(ipv6->s6_addr[0] != 0xFF) { 
00151     ipv6->s6_addr[1] = 0; 
00152     ipv6->s6_addr[0]++; 
00153   } 
00154   else { 
00155     return -1; 
00156   } 
00157    
00158   return 0; 
00159 } 
00160 
00161 int randomloss(double lossprob) {
00162         
00163         int loss = 0;
00164 
00165         double msb;
00166         double lsb;
00167 
00168         double tmp;
00169 
00170         if(lossprob == 0.0) {
00171                 return loss;
00172         }
00173         
00174         msb = (double)(rand()%100);
00175         lsb = (double)(rand()%10);
00176         
00177         tmp = msb + (double)(lsb/(double)10);
00178 
00179         if(tmp < lossprob) {
00180                 loss = 1;
00181         }
00182         
00183         return loss;
00184 }
00185 
00186 
00187 #ifdef _MSC_VER
00188 
00189 lldiv_t lldiv(long long num, long long denom) {
00190   lldiv_t r;
00191 
00192   if (num > 0 && denom < 0) {
00193     num = -num;
00194     denom = -denom;
00195   }
00196 
00197   r.quot = num / denom;
00198   r.rem = num % denom;
00199 
00200   if (num < 0 && denom > 0) {
00201     if (r.rem > 0) {
00202       r.quot++;
00203       r.rem -= denom;
00204     }
00205   }
00206   
00207   return r;
00208 }
00209 
00210 #endif

Generated on Fri Mar 9 20:08:51 2007 for MAD-FCL by  doxygen 1.5.0