1 /** \file mad.c \brief General ALC stuff
2 *
3 * $Author: peltotal $ $Date: 2007/02/28 08:58:00 $ $Revision: 1.27 $
4 *
5 * MAD-ALCLIB: Implementation of ALC/LCT protocols, Compact No-Code FEC,
6 * Simple XOR FEC, Reed-Solomon FEC, and RLC Congestion Control protocol.
7 * Copyright (c) 2003-2007 TUT - Tampere University of Technology
8 * main authors/contacts: jani.peltotalo@tut.fi and sami.peltotalo@tut.fi
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * In addition, as a special exception, TUT - Tampere University of Technology
25 * gives permission to link the code of this program with the OpenSSL library (or
26 * with modified versions of OpenSSL that use the same license as OpenSSL), and
27 * distribute linked combinations including the two. You must obey the GNU
28 * General Public License in all respects for all of the code used other than
29 * OpenSSL. If you modify this file, you may extend this exception to your version
30 * of the file, but you are not obligated to do so. If you do not wish to do so,
31 * delete this exception statement from your version.
32 */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <time.h>
37 #include <string.h>
38
39 #ifdef _MSC_VER
40 #include <winsock2.h>
41 #include <ws2tcpip.h>
42 #else
43 #include <sys/time.h>
44 #endif
45
46 #include "mad.h"
47
48 /* Initialize global variable */
49
50 BOOL lib_init = FALSE;
51
52 #ifdef _MSC_VER
53 double time_factor; /**< local global variable for timer */
54 unsigned long long start_time; /**< local global variable for timer */
55 #endif
56
57 #ifdef _MSC_VER
58 /**
59 * This function initializes timer.
60 */
61
sec_init(void)62 void sec_init(void) {
63 unsigned long long perf_cnt;
64 QueryPerformanceFrequency((LARGE_INTEGER*)&perf_cnt);
65 time_factor = 1.0/perf_cnt;
66 QueryPerformanceCounter((LARGE_INTEGER*)&start_time);
67 }
68 #endif
69
alc_init(void)70 void alc_init(void) {
71
72 #ifdef _MSC_VER
73 sec_init(); /* Initialize timer */
74 #endif
75 lib_init = TRUE;
76 }
77
78 #ifdef _MSC_VER
sec(void)79 double sec(void) {
80 long long cur_time;
81 double time_span;
82 QueryPerformanceCounter((LARGE_INTEGER*)&cur_time);
83
84 time_span = (cur_time - start_time) * time_factor;
85 return time_span;
86 }
87 #else
sec(void)88 double sec(void) {
89
90 struct timeval tv;
91 gettimeofday(&tv, 0);
92 return tv.tv_sec + tv.tv_usec / 1000000.0;
93 }
94 #endif
95
increase_ipv6_address(struct in6_addr * ipv6)96 int increase_ipv6_address(struct in6_addr *ipv6) {
97
98 if(ipv6->s6_addr[15] != 0xFF) {
99 ipv6->s6_addr[15]++;
100 }
101 else if(ipv6->s6_addr[14] != 0xFF) {
102 ipv6->s6_addr[15] = 0;
103 ipv6->s6_addr[14]++;
104 }
105 else if(ipv6->s6_addr[13] != 0xFF) {
106 ipv6->s6_addr[13]++;
107 }
108 else if(ipv6->s6_addr[12] != 0xFF) {
109 ipv6->s6_addr[13] = 0;
110 ipv6->s6_addr[12]++;
111 }
112 else if(ipv6->s6_addr[11] != 0xFF) {
113 ipv6->s6_addr[11]++;
114 }
115 else if(ipv6->s6_addr[10] != 0xFF) {
116 ipv6->s6_addr[11] = 0;
117 ipv6->s6_addr[10]++;
118 }
119 else if(ipv6->s6_addr[9] != 0xFF) {
120 ipv6->s6_addr[9]++;
121 }
122 else if(ipv6->s6_addr[8] != 0xFF) {
123 ipv6->s6_addr[9] = 0;
124 ipv6->s6_addr[8]++;
125 }
126 else if(ipv6->s6_addr[7] != 0xFF) {
127 ipv6->s6_addr[7]++;
128 }
129 else if(ipv6->s6_addr[6] != 0xFF) {
130 ipv6->s6_addr[7] = 0;
131 ipv6->s6_addr[6]++;
132 }
133 else if(ipv6->s6_addr[5] != 0xFF) {
134 ipv6->s6_addr[5]++;
135 }
136 else if(ipv6->s6_addr[4] != 0xFF) {
137 ipv6->s6_addr[5] = 0;
138 ipv6->s6_addr[4]++;
139 }
140 else if(ipv6->s6_addr[3] != 0xFF) {
141 ipv6->s6_addr[3]++;
142 }
143 else if(ipv6->s6_addr[2] != 0xFF) {
144 ipv6->s6_addr[3] = 0;
145 ipv6->s6_addr[2]++;
146 }
147 else if(ipv6->s6_addr[1] != 0xFF) {
148 ipv6->s6_addr[1]++;
149 }
150 else if(ipv6->s6_addr[0] != 0xFF) {
151 ipv6->s6_addr[1] = 0;
152 ipv6->s6_addr[0]++;
153 }
154 else {
155 return -1;
156 }
157
158 return 0;
159 }
160
randomloss(double lossprob)161 int randomloss(double lossprob) {
162
163 int loss = 0;
164
165 double msb;
166 double lsb;
167
168 double tmp;
169
170 if(lossprob == 0.0) {
171 return loss;
172 }
173
174 msb = (double)(rand()%100);
175 lsb = (double)(rand()%10);
176
177 tmp = msb + (double)(lsb/(double)10);
178
179 if(tmp < lossprob) {
180 loss = 1;
181 }
182
183 return loss;
184 }
185
186
187 #ifdef _MSC_VER
188
lldiv(long long num,long long denom)189 lldiv_t lldiv(long long num, long long denom) {
190 lldiv_t r;
191
192 if (num > 0 && denom < 0) {
193 num = -num;
194 denom = -denom;
195 }
196
197 r.quot = num / denom;
198 r.rem = num % denom;
199
200 if (num < 0 && denom > 0) {
201 if (r.rem > 0) {
202 r.quot++;
203 r.rem -= denom;
204 }
205 }
206
207 return r;
208 }
209
210 #endif
211