00001 /* Some systems (e.g., SunOS) have header files that erroneously declare 00002 * inet_addr(), inet_ntoa() and gethostbyname() as taking no arguments. 00003 * This confuses C++. To overcome this, we use our own routines, 00004 * implemented in C. 00005 */ 00006 00007 #ifndef _NET_COMMON_H 00008 #include "NetCommon.h" 00009 #endif 00010 00011 #include <stdio.h> 00012 00013 #ifdef VXWORKS 00014 #include <inetLib.h> 00015 #endif 00016 00017 unsigned our_inet_addr(cp) 00018 char const* cp; 00019 { 00020 return inet_addr(cp); 00021 } 00022 00023 char * 00024 our_inet_ntoa(in) 00025 struct in_addr in; 00026 { 00027 #ifndef VXWORKS 00028 return inet_ntoa(in); 00029 #else 00030 /* according the man pages of inet_ntoa : 00031 00032 NOTES 00033 The return value from inet_ntoa() points to a buffer which 00034 is overwritten on each call. This buffer is implemented as 00035 thread-specific data in multithreaded applications. 00036 00037 the vxworks version of inet_ntoa allocates a buffer for each 00038 ip address string, and does not reuse the same buffer. 00039 00040 this is merely to simulate the same behaviour (not multithread 00041 safe though): 00042 */ 00043 static char result[INET_ADDR_LEN]; 00044 inet_ntoa_b(in, result); 00045 return(result); 00046 #endif 00047 } 00048 00049 #if defined(__WIN32__) || defined(_WIN32) 00050 #ifndef IMN_PIM 00051 #define WS_VERSION_CHOICE1 0x202/*MAKEWORD(2,2)*/ 00052 #define WS_VERSION_CHOICE2 0x101/*MAKEWORD(1,1)*/ 00053 int initializeWinsockIfNecessary(void) { 00054 /* We need to call an initialization routine before 00055 * we can do anything with winsock. (How fucking lame!): 00056 */ 00057 static int _haveInitializedWinsock = 0; 00058 WSADATA wsadata; 00059 00060 if (!_haveInitializedWinsock) { 00061 if ((WSAStartup(WS_VERSION_CHOICE1, &wsadata) != 0) 00062 && ((WSAStartup(WS_VERSION_CHOICE2, &wsadata)) != 0)) { 00063 return 0; /* error in initialization */ 00064 } 00065 if ((wsadata.wVersion != WS_VERSION_CHOICE1) 00066 && (wsadata.wVersion != WS_VERSION_CHOICE2)) { 00067 WSACleanup(); 00068 return 0; /* desired Winsock version was not available */ 00069 } 00070 _haveInitializedWinsock = 1; 00071 } 00072 00073 return 1; 00074 } 00075 #else 00076 int initializeWinsockIfNecessary(void) { return 1; } 00077 #endif 00078 #else 00079 #define initializeWinsockIfNecessary() 1 00080 #endif 00081 00082 #ifndef NULL 00083 #define NULL 0 00084 #endif 00085 00086 #if !defined(VXWORKS) 00087 struct hostent* our_gethostbyname(name) 00088 char* name; 00089 { 00090 if (!initializeWinsockIfNecessary()) return NULL; 00091 00092 return (struct hostent*) gethostbyname(name); 00093 } 00094 #endif 00095 00096 #ifdef USE_SYSTEM_RANDOM 00097 #include <stdlib.h> 00098 long our_random() { 00099 #if defined(__WIN32__) || defined(_WIN32) 00100 return rand(); 00101 #else 00102 return random(); 00103 #endif 00104 } 00105 void our_srandom(unsigned int x) { 00106 #if defined(__WIN32__) || defined(_WIN32) 00107 return srand(x); 00108 #else 00109 return srandom(x); 00110 #endif 00111 } 00112 #else 00113 /* 00114 * random.c: 00115 * 00116 * An improved random number generation package. In addition to the standard 00117 * rand()/srand() like interface, this package also has a special state info 00118 * interface. The our_initstate() routine is called with a seed, an array of 00119 * bytes, and a count of how many bytes are being passed in; this array is 00120 * then initialized to contain information for random number generation with 00121 * that much state information. Good sizes for the amount of state 00122 * information are 32, 64, 128, and 256 bytes. The state can be switched by 00123 * calling the our_setstate() routine with the same array as was initiallized 00124 * with our_initstate(). By default, the package runs with 128 bytes of state 00125 * information and generates far better random numbers than a linear 00126 * congruential generator. If the amount of state information is less than 00127 * 32 bytes, a simple linear congruential R.N.G. is used. 00128 * 00129 * Internally, the state information is treated as an array of longs; the 00130 * zeroeth element of the array is the type of R.N.G. being used (small 00131 * integer); the remainder of the array is the state information for the 00132 * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of 00133 * state information, which will allow a degree seven polynomial. (Note: 00134 * the zeroeth word of state information also has some other information 00135 * stored in it -- see our_setstate() for details). 00136 * 00137 * The random number generation technique is a linear feedback shift register 00138 * approach, employing trinomials (since there are fewer terms to sum up that 00139 * way). In this approach, the least significant bit of all the numbers in 00140 * the state table will act as a linear feedback shift register, and will 00141 * have period 2^deg - 1 (where deg is the degree of the polynomial being 00142 * used, assuming that the polynomial is irreducible and primitive). The 00143 * higher order bits will have longer periods, since their values are also 00144 * influenced by pseudo-random carries out of the lower bits. The total 00145 * period of the generator is approximately deg*(2**deg - 1); thus doubling 00146 * the amount of state information has a vast influence on the period of the 00147 * generator. Note: the deg*(2**deg - 1) is an approximation only good for 00148 * large deg, when the period of the shift register is the dominant factor. 00149 * With deg equal to seven, the period is actually much longer than the 00150 * 7*(2**7 - 1) predicted by this formula. 00151 */ 00152 00153 /* 00154 * For each of the currently supported random number generators, we have a 00155 * break value on the amount of state information (you need at least this 00156 * many bytes of state info to support this random number generator), a degree 00157 * for the polynomial (actually a trinomial) that the R.N.G. is based on, and 00158 * the separation between the two lower order coefficients of the trinomial. 00159 */ 00160 #define TYPE_0 0 /* linear congruential */ 00161 #define BREAK_0 8 00162 #define DEG_0 0 00163 #define SEP_0 0 00164 00165 #define TYPE_1 1 /* x**7 + x**3 + 1 */ 00166 #define BREAK_1 32 00167 #define DEG_1 7 00168 #define SEP_1 3 00169 00170 #define TYPE_2 2 /* x**15 + x + 1 */ 00171 #define BREAK_2 64 00172 #define DEG_2 15 00173 #define SEP_2 1 00174 00175 #define TYPE_3 3 /* x**31 + x**3 + 1 */ 00176 #define BREAK_3 128 00177 #define DEG_3 31 00178 #define SEP_3 3 00179 00180 #define TYPE_4 4 /* x**63 + x + 1 */ 00181 #define BREAK_4 256 00182 #define DEG_4 63 00183 #define SEP_4 1 00184 00185 /* 00186 * Array versions of the above information to make code run faster -- 00187 * relies on fact that TYPE_i == i. 00188 */ 00189 #define MAX_TYPES 5 /* max number of types above */ 00190 00191 static int const degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; 00192 static int const seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; 00193 00194 /* 00195 * Initially, everything is set up as if from: 00196 * 00197 * our_initstate(1, &randtbl, 128); 00198 * 00199 * Note that this initialization takes advantage of the fact that srandom() 00200 * advances the front and rear pointers 10*rand_deg times, and hence the 00201 * rear pointer which starts at 0 will also end up at zero; thus the zeroeth 00202 * element of the state information, which contains info about the current 00203 * position of the rear pointer is just 00204 * 00205 * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. 00206 */ 00207 00208 static long randtbl[DEG_3 + 1] = { 00209 TYPE_3, 00210 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, 00211 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 00212 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 00213 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 00214 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, 00215 0x27fb47b9, 00216 }; 00217 00218 /* 00219 * fptr and rptr are two pointers into the state info, a front and a rear 00220 * pointer. These two pointers are always rand_sep places aparts, as they 00221 * cycle cyclically through the state information. (Yes, this does mean we 00222 * could get away with just one pointer, but the code for random() is more 00223 * efficient this way). The pointers are left positioned as they would be 00224 * from the call 00225 * 00226 * our_initstate(1, randtbl, 128); 00227 * 00228 * (The position of the rear pointer, rptr, is really 0 (as explained above 00229 * in the initialization of randtbl) because the state table pointer is set 00230 * to point to randtbl[1] (as explained below). 00231 */ 00232 static long* fptr = &randtbl[SEP_3 + 1]; 00233 static long* rptr = &randtbl[1]; 00234 00235 /* 00236 * The following things are the pointer to the state information table, the 00237 * type of the current generator, the degree of the current polynomial being 00238 * used, and the separation between the two pointers. Note that for efficiency 00239 * of random(), we remember the first location of the state information, not 00240 * the zeroeth. Hence it is valid to access state[-1], which is used to 00241 * store the type of the R.N.G. Also, we remember the last location, since 00242 * this is more efficient than indexing every time to find the address of 00243 * the last element to see if the front and rear pointers have wrapped. 00244 */ 00245 static long *state = &randtbl[1]; 00246 static int rand_type = TYPE_3; 00247 static int rand_deg = DEG_3; 00248 static int rand_sep = SEP_3; 00249 static long* end_ptr = &randtbl[DEG_3 + 1]; 00250 00251 /* 00252 * srandom: 00253 * 00254 * Initialize the random number generator based on the given seed. If the 00255 * type is the trivial no-state-information type, just remember the seed. 00256 * Otherwise, initializes state[] based on the given "seed" via a linear 00257 * congruential generator. Then, the pointers are set to known locations 00258 * that are exactly rand_sep places apart. Lastly, it cycles the state 00259 * information a given number of times to get rid of any initial dependencies 00260 * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] 00261 * for default usage relies on values produced by this routine. 00262 */ 00263 long our_random(void); /*forward*/ 00264 void 00265 our_srandom(unsigned int x) 00266 { 00267 register int i; 00268 00269 if (rand_type == TYPE_0) 00270 state[0] = x; 00271 else { 00272 state[0] = x; 00273 for (i = 1; i < rand_deg; i++) 00274 state[i] = 1103515245 * state[i - 1] + 12345; 00275 fptr = &state[rand_sep]; 00276 rptr = &state[0]; 00277 for (i = 0; i < 10 * rand_deg; i++) 00278 (void)our_random(); 00279 } 00280 } 00281 00282 /* 00283 * our_initstate: 00284 * 00285 * Initialize the state information in the given array of n bytes for future 00286 * random number generation. Based on the number of bytes we are given, and 00287 * the break values for the different R.N.G.'s, we choose the best (largest) 00288 * one we can and set things up for it. srandom() is then called to 00289 * initialize the state information. 00290 * 00291 * Note that on return from srandom(), we set state[-1] to be the type 00292 * multiplexed with the current value of the rear pointer; this is so 00293 * successive calls to our_initstate() won't lose this information and will be 00294 * able to restart with our_setstate(). 00295 * 00296 * Note: the first thing we do is save the current state, if any, just like 00297 * our_setstate() so that it doesn't matter when our_initstate is called. 00298 * 00299 * Returns a pointer to the old state. 00300 */ 00301 char * 00302 our_initstate(seed, arg_state, n) 00303 unsigned int seed; /* seed for R.N.G. */ 00304 char *arg_state; /* pointer to state array */ 00305 int n; /* # bytes of state info */ 00306 { 00307 register char *ostate = (char *)(&state[-1]); 00308 00309 if (rand_type == TYPE_0) 00310 state[-1] = rand_type; 00311 else 00312 state[-1] = MAX_TYPES * (rptr - state) + rand_type; 00313 if (n < BREAK_0) { 00314 #ifdef DEBUG 00315 (void)fprintf(stderr, 00316 "random: not enough state (%d bytes); ignored.\n", n); 00317 #endif 00318 return(0); 00319 } 00320 if (n < BREAK_1) { 00321 rand_type = TYPE_0; 00322 rand_deg = DEG_0; 00323 rand_sep = SEP_0; 00324 } else if (n < BREAK_2) { 00325 rand_type = TYPE_1; 00326 rand_deg = DEG_1; 00327 rand_sep = SEP_1; 00328 } else if (n < BREAK_3) { 00329 rand_type = TYPE_2; 00330 rand_deg = DEG_2; 00331 rand_sep = SEP_2; 00332 } else if (n < BREAK_4) { 00333 rand_type = TYPE_3; 00334 rand_deg = DEG_3; 00335 rand_sep = SEP_3; 00336 } else { 00337 rand_type = TYPE_4; 00338 rand_deg = DEG_4; 00339 rand_sep = SEP_4; 00340 } 00341 state = &(((long *)arg_state)[1]); /* first location */ 00342 end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ 00343 our_srandom(seed); 00344 if (rand_type == TYPE_0) 00345 state[-1] = rand_type; 00346 else 00347 state[-1] = MAX_TYPES*(rptr - state) + rand_type; 00348 return(ostate); 00349 } 00350 00351 /* 00352 * our_setstate: 00353 * 00354 * Restore the state from the given state array. 00355 * 00356 * Note: it is important that we also remember the locations of the pointers 00357 * in the current state information, and restore the locations of the pointers 00358 * from the old state information. This is done by multiplexing the pointer 00359 * location into the zeroeth word of the state information. 00360 * 00361 * Note that due to the order in which things are done, it is OK to call 00362 * our_setstate() with the same state as the current state. 00363 * 00364 * Returns a pointer to the old state information. 00365 */ 00366 char * 00367 our_setstate(arg_state) 00368 char *arg_state; 00369 { 00370 register long *new_state = (long *)arg_state; 00371 register int type = new_state[0] % MAX_TYPES; 00372 register int rear = new_state[0] / MAX_TYPES; 00373 char *ostate = (char *)(&state[-1]); 00374 00375 if (rand_type == TYPE_0) 00376 state[-1] = rand_type; 00377 else 00378 state[-1] = MAX_TYPES * (rptr - state) + rand_type; 00379 switch(type) { 00380 case TYPE_0: 00381 case TYPE_1: 00382 case TYPE_2: 00383 case TYPE_3: 00384 case TYPE_4: 00385 rand_type = type; 00386 rand_deg = degrees[type]; 00387 rand_sep = seps[type]; 00388 break; 00389 default: 00390 #ifdef DEBUG 00391 (void)fprintf(stderr, 00392 "random: state info corrupted; not changed.\n"); 00393 #endif 00394 break; 00395 } 00396 state = &new_state[1]; 00397 if (rand_type != TYPE_0) { 00398 rptr = &state[rear]; 00399 fptr = &state[(rear + rand_sep) % rand_deg]; 00400 } 00401 end_ptr = &state[rand_deg]; /* set end_ptr too */ 00402 return(ostate); 00403 } 00404 00405 /* 00406 * random: 00407 * 00408 * If we are using the trivial TYPE_0 R.N.G., just do the old linear 00409 * congruential bit. Otherwise, we do our fancy trinomial stuff, which is 00410 * the same in all the other cases due to all the global variables that have 00411 * been set up. The basic operation is to add the number at the rear pointer 00412 * into the one at the front pointer. Then both pointers are advanced to 00413 * the next location cyclically in the table. The value returned is the sum 00414 * generated, reduced to 31 bits by throwing away the "least random" low bit. 00415 * 00416 * Note: the code takes advantage of the fact that both the front and 00417 * rear pointers can't wrap on the same call by not testing the rear 00418 * pointer if the front one has wrapped. 00419 * 00420 * Returns a 31-bit random number. 00421 */ 00422 long 00423 our_random() 00424 { 00425 long i; 00426 00427 if (rand_type == TYPE_0) 00428 i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; 00429 else { 00430 *fptr += *rptr; 00431 i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ 00432 if (++fptr >= end_ptr) { 00433 fptr = state; 00434 ++rptr; 00435 } else if (++rptr >= end_ptr) 00436 rptr = state; 00437 } 00438 return(i); 00439 } 00440 #endif 00441 00442 u_int32_t our_random32() { 00443 // Return a 32-bit random number. 00444 // Because "our_random()" returns a 31-bit random number, we call it a second 00445 // time, to generate the high bit: 00446 long random1 = our_random(); 00447 long random2 = our_random(); 00448 return (u_int32_t)((random2<<31) | random1); 00449 } 00450 00451 #ifdef USE_OUR_BZERO 00452 #ifndef __bzero 00453 void 00454 __bzero (to, count) 00455 char *to; 00456 int count; 00457 { 00458 while (count-- > 0) 00459 { 00460 *to++ = 0; 00461 } 00462 } 00463 #endif 00464 #endif
1.5.5