PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/pvrclients/MediaPortal/lib/live555/groupsock/inet.c

https://github.com/macardi/xbmc
C | 468 lines | 257 code | 35 blank | 176 comment | 42 complexity | 48c1a3f35ecff8b736e48b95d39461ca MD5 | raw file
  1. /* Some systems (e.g., SunOS) have header files that erroneously declare
  2. * inet_addr(), inet_ntoa() and gethostbyname() as taking no arguments.
  3. * This confuses C++. To overcome this, we use our own routines,
  4. * implemented in C.
  5. */
  6. #ifndef _NET_COMMON_H
  7. #include "NetCommon.h"
  8. #endif
  9. #include <stdio.h>
  10. #ifdef VXWORKS
  11. #include <inetLib.h>
  12. #endif
  13. unsigned our_inet_addr(cp)
  14. char const* cp;
  15. {
  16. return inet_addr(cp);
  17. }
  18. char *
  19. our_inet_ntoa(in)
  20. struct in_addr in;
  21. {
  22. #ifndef VXWORKS
  23. return inet_ntoa(in);
  24. #else
  25. /* according the man pages of inet_ntoa :
  26. NOTES
  27. The return value from inet_ntoa() points to a buffer which
  28. is overwritten on each call. This buffer is implemented as
  29. thread-specific data in multithreaded applications.
  30. the vxworks version of inet_ntoa allocates a buffer for each
  31. ip address string, and does not reuse the same buffer.
  32. this is merely to simulate the same behaviour (not multithread
  33. safe though):
  34. */
  35. static char result[INET_ADDR_LEN];
  36. inet_ntoa_b(in, result);
  37. return(result);
  38. #endif
  39. }
  40. #if defined(__WIN32__) || defined(_WIN32)
  41. #ifndef IMN_PIM
  42. #define WS_VERSION_CHOICE1 0x202/*MAKEWORD(2,2)*/
  43. #define WS_VERSION_CHOICE2 0x101/*MAKEWORD(1,1)*/
  44. int initializeWinsockIfNecessary(void) {
  45. /* We need to call an initialization routine before
  46. * we can do anything with winsock. (How fucking lame!):
  47. */
  48. static int _haveInitializedWinsock = 0;
  49. WSADATA wsadata;
  50. if (!_haveInitializedWinsock) {
  51. if ((WSAStartup(WS_VERSION_CHOICE1, &wsadata) != 0)
  52. && ((WSAStartup(WS_VERSION_CHOICE2, &wsadata)) != 0)) {
  53. return 0; /* error in initialization */
  54. }
  55. if ((wsadata.wVersion != WS_VERSION_CHOICE1)
  56. && (wsadata.wVersion != WS_VERSION_CHOICE2)) {
  57. WSACleanup();
  58. return 0; /* desired Winsock version was not available */
  59. }
  60. _haveInitializedWinsock = 1;
  61. }
  62. return 1;
  63. }
  64. #else
  65. int initializeWinsockIfNecessary(void) { return 1; }
  66. #endif
  67. #else
  68. #define initializeWinsockIfNecessary() 1
  69. #endif
  70. #ifndef NULL
  71. #define NULL 0
  72. #endif
  73. #if !defined(VXWORKS)
  74. struct hostent* our_gethostbyname(name)
  75. char* name;
  76. {
  77. if (!initializeWinsockIfNecessary()) return NULL;
  78. return (struct hostent*) gethostbyname(name);
  79. }
  80. #endif
  81. #ifndef USE_OUR_RANDOM
  82. /* Use the system-supplied "random()" and "srandom()" functions */
  83. #include <stdlib.h>
  84. long our_random() {
  85. #if defined(__WIN32__) || defined(_WIN32)
  86. return rand();
  87. #else
  88. return random();
  89. #endif
  90. }
  91. void our_srandom(unsigned int x) {
  92. #if defined(__WIN32__) || defined(_WIN32)
  93. srand(x);
  94. #else
  95. srandom(x);
  96. #endif
  97. }
  98. #else
  99. /* Use our own implementation of the "random()" and "srandom()" functions */
  100. /*
  101. * random.c:
  102. *
  103. * An improved random number generation package. In addition to the standard
  104. * rand()/srand() like interface, this package also has a special state info
  105. * interface. The our_initstate() routine is called with a seed, an array of
  106. * bytes, and a count of how many bytes are being passed in; this array is
  107. * then initialized to contain information for random number generation with
  108. * that much state information. Good sizes for the amount of state
  109. * information are 32, 64, 128, and 256 bytes. The state can be switched by
  110. * calling the our_setstate() routine with the same array as was initiallized
  111. * with our_initstate(). By default, the package runs with 128 bytes of state
  112. * information and generates far better random numbers than a linear
  113. * congruential generator. If the amount of state information is less than
  114. * 32 bytes, a simple linear congruential R.N.G. is used.
  115. *
  116. * Internally, the state information is treated as an array of longs; the
  117. * zeroeth element of the array is the type of R.N.G. being used (small
  118. * integer); the remainder of the array is the state information for the
  119. * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of
  120. * state information, which will allow a degree seven polynomial. (Note:
  121. * the zeroeth word of state information also has some other information
  122. * stored in it -- see our_setstate() for details).
  123. *
  124. * The random number generation technique is a linear feedback shift register
  125. * approach, employing trinomials (since there are fewer terms to sum up that
  126. * way). In this approach, the least significant bit of all the numbers in
  127. * the state table will act as a linear feedback shift register, and will
  128. * have period 2^deg - 1 (where deg is the degree of the polynomial being
  129. * used, assuming that the polynomial is irreducible and primitive). The
  130. * higher order bits will have longer periods, since their values are also
  131. * influenced by pseudo-random carries out of the lower bits. The total
  132. * period of the generator is approximately deg*(2**deg - 1); thus doubling
  133. * the amount of state information has a vast influence on the period of the
  134. * generator. Note: the deg*(2**deg - 1) is an approximation only good for
  135. * large deg, when the period of the shift register is the dominant factor.
  136. * With deg equal to seven, the period is actually much longer than the
  137. * 7*(2**7 - 1) predicted by this formula.
  138. */
  139. /*
  140. * For each of the currently supported random number generators, we have a
  141. * break value on the amount of state information (you need at least this
  142. * many bytes of state info to support this random number generator), a degree
  143. * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
  144. * the separation between the two lower order coefficients of the trinomial.
  145. */
  146. #define TYPE_0 0 /* linear congruential */
  147. #define BREAK_0 8
  148. #define DEG_0 0
  149. #define SEP_0 0
  150. #define TYPE_1 1 /* x**7 + x**3 + 1 */
  151. #define BREAK_1 32
  152. #define DEG_1 7
  153. #define SEP_1 3
  154. #define TYPE_2 2 /* x**15 + x + 1 */
  155. #define BREAK_2 64
  156. #define DEG_2 15
  157. #define SEP_2 1
  158. #define TYPE_3 3 /* x**31 + x**3 + 1 */
  159. #define BREAK_3 128
  160. #define DEG_3 31
  161. #define SEP_3 3
  162. #define TYPE_4 4 /* x**63 + x + 1 */
  163. #define BREAK_4 256
  164. #define DEG_4 63
  165. #define SEP_4 1
  166. /*
  167. * Array versions of the above information to make code run faster --
  168. * relies on fact that TYPE_i == i.
  169. */
  170. #define MAX_TYPES 5 /* max number of types above */
  171. static int const degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
  172. static int const seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
  173. /*
  174. * Initially, everything is set up as if from:
  175. *
  176. * our_initstate(1, &randtbl, 128);
  177. *
  178. * Note that this initialization takes advantage of the fact that srandom()
  179. * advances the front and rear pointers 10*rand_deg times, and hence the
  180. * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
  181. * element of the state information, which contains info about the current
  182. * position of the rear pointer is just
  183. *
  184. * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
  185. */
  186. static long randtbl[DEG_3 + 1] = {
  187. TYPE_3,
  188. 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5,
  189. 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
  190. 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88,
  191. 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
  192. 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b,
  193. 0x27fb47b9,
  194. };
  195. /*
  196. * fptr and rptr are two pointers into the state info, a front and a rear
  197. * pointer. These two pointers are always rand_sep places aparts, as they
  198. * cycle cyclically through the state information. (Yes, this does mean we
  199. * could get away with just one pointer, but the code for random() is more
  200. * efficient this way). The pointers are left positioned as they would be
  201. * from the call
  202. *
  203. * our_initstate(1, randtbl, 128);
  204. *
  205. * (The position of the rear pointer, rptr, is really 0 (as explained above
  206. * in the initialization of randtbl) because the state table pointer is set
  207. * to point to randtbl[1] (as explained below).
  208. */
  209. static long* fptr = &randtbl[SEP_3 + 1];
  210. static long* rptr = &randtbl[1];
  211. /*
  212. * The following things are the pointer to the state information table, the
  213. * type of the current generator, the degree of the current polynomial being
  214. * used, and the separation between the two pointers. Note that for efficiency
  215. * of random(), we remember the first location of the state information, not
  216. * the zeroeth. Hence it is valid to access state[-1], which is used to
  217. * store the type of the R.N.G. Also, we remember the last location, since
  218. * this is more efficient than indexing every time to find the address of
  219. * the last element to see if the front and rear pointers have wrapped.
  220. */
  221. static long *state = &randtbl[1];
  222. static int rand_type = TYPE_3;
  223. static int rand_deg = DEG_3;
  224. static int rand_sep = SEP_3;
  225. static long* end_ptr = &randtbl[DEG_3 + 1];
  226. /*
  227. * srandom:
  228. *
  229. * Initialize the random number generator based on the given seed. If the
  230. * type is the trivial no-state-information type, just remember the seed.
  231. * Otherwise, initializes state[] based on the given "seed" via a linear
  232. * congruential generator. Then, the pointers are set to known locations
  233. * that are exactly rand_sep places apart. Lastly, it cycles the state
  234. * information a given number of times to get rid of any initial dependencies
  235. * introduced by the L.C.R.N.G. Note that the initialization of randtbl[]
  236. * for default usage relies on values produced by this routine.
  237. */
  238. long our_random(void); /*forward*/
  239. void
  240. our_srandom(unsigned int x)
  241. {
  242. register int i;
  243. if (rand_type == TYPE_0)
  244. state[0] = x;
  245. else {
  246. state[0] = x;
  247. for (i = 1; i < rand_deg; i++)
  248. state[i] = 1103515245 * state[i - 1] + 12345;
  249. fptr = &state[rand_sep];
  250. rptr = &state[0];
  251. for (i = 0; i < 10 * rand_deg; i++)
  252. (void)our_random();
  253. }
  254. }
  255. /*
  256. * our_initstate:
  257. *
  258. * Initialize the state information in the given array of n bytes for future
  259. * random number generation. Based on the number of bytes we are given, and
  260. * the break values for the different R.N.G.'s, we choose the best (largest)
  261. * one we can and set things up for it. srandom() is then called to
  262. * initialize the state information.
  263. *
  264. * Note that on return from srandom(), we set state[-1] to be the type
  265. * multiplexed with the current value of the rear pointer; this is so
  266. * successive calls to our_initstate() won't lose this information and will be
  267. * able to restart with our_setstate().
  268. *
  269. * Note: the first thing we do is save the current state, if any, just like
  270. * our_setstate() so that it doesn't matter when our_initstate is called.
  271. *
  272. * Returns a pointer to the old state.
  273. */
  274. char *
  275. our_initstate(seed, arg_state, n)
  276. unsigned int seed; /* seed for R.N.G. */
  277. char *arg_state; /* pointer to state array */
  278. int n; /* # bytes of state info */
  279. {
  280. register char *ostate = (char *)(&state[-1]);
  281. if (rand_type == TYPE_0)
  282. state[-1] = rand_type;
  283. else
  284. state[-1] = MAX_TYPES * (rptr - state) + rand_type;
  285. if (n < BREAK_0) {
  286. #ifdef DEBUG
  287. (void)fprintf(stderr,
  288. "random: not enough state (%d bytes); ignored.\n", n);
  289. #endif
  290. return(0);
  291. }
  292. if (n < BREAK_1) {
  293. rand_type = TYPE_0;
  294. rand_deg = DEG_0;
  295. rand_sep = SEP_0;
  296. } else if (n < BREAK_2) {
  297. rand_type = TYPE_1;
  298. rand_deg = DEG_1;
  299. rand_sep = SEP_1;
  300. } else if (n < BREAK_3) {
  301. rand_type = TYPE_2;
  302. rand_deg = DEG_2;
  303. rand_sep = SEP_2;
  304. } else if (n < BREAK_4) {
  305. rand_type = TYPE_3;
  306. rand_deg = DEG_3;
  307. rand_sep = SEP_3;
  308. } else {
  309. rand_type = TYPE_4;
  310. rand_deg = DEG_4;
  311. rand_sep = SEP_4;
  312. }
  313. state = &(((long *)arg_state)[1]); /* first location */
  314. end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */
  315. our_srandom(seed);
  316. if (rand_type == TYPE_0)
  317. state[-1] = rand_type;
  318. else
  319. state[-1] = MAX_TYPES*(rptr - state) + rand_type;
  320. return(ostate);
  321. }
  322. /*
  323. * our_setstate:
  324. *
  325. * Restore the state from the given state array.
  326. *
  327. * Note: it is important that we also remember the locations of the pointers
  328. * in the current state information, and restore the locations of the pointers
  329. * from the old state information. This is done by multiplexing the pointer
  330. * location into the zeroeth word of the state information.
  331. *
  332. * Note that due to the order in which things are done, it is OK to call
  333. * our_setstate() with the same state as the current state.
  334. *
  335. * Returns a pointer to the old state information.
  336. */
  337. char *
  338. our_setstate(arg_state)
  339. char *arg_state;
  340. {
  341. register long *new_state = (long *)arg_state;
  342. register int type = new_state[0] % MAX_TYPES;
  343. register int rear = new_state[0] / MAX_TYPES;
  344. char *ostate = (char *)(&state[-1]);
  345. if (rand_type == TYPE_0)
  346. state[-1] = rand_type;
  347. else
  348. state[-1] = MAX_TYPES * (rptr - state) + rand_type;
  349. switch(type) {
  350. case TYPE_0:
  351. case TYPE_1:
  352. case TYPE_2:
  353. case TYPE_3:
  354. case TYPE_4:
  355. rand_type = type;
  356. rand_deg = degrees[type];
  357. rand_sep = seps[type];
  358. break;
  359. default:
  360. #ifdef DEBUG
  361. (void)fprintf(stderr,
  362. "random: state info corrupted; not changed.\n");
  363. #endif
  364. break;
  365. }
  366. state = &new_state[1];
  367. if (rand_type != TYPE_0) {
  368. rptr = &state[rear];
  369. fptr = &state[(rear + rand_sep) % rand_deg];
  370. }
  371. end_ptr = &state[rand_deg]; /* set end_ptr too */
  372. return(ostate);
  373. }
  374. /*
  375. * random:
  376. *
  377. * If we are using the trivial TYPE_0 R.N.G., just do the old linear
  378. * congruential bit. Otherwise, we do our fancy trinomial stuff, which is
  379. * the same in all the other cases due to all the global variables that have
  380. * been set up. The basic operation is to add the number at the rear pointer
  381. * into the one at the front pointer. Then both pointers are advanced to
  382. * the next location cyclically in the table. The value returned is the sum
  383. * generated, reduced to 31 bits by throwing away the "least random" low bit.
  384. *
  385. * Note: the code takes advantage of the fact that both the front and
  386. * rear pointers can't wrap on the same call by not testing the rear
  387. * pointer if the front one has wrapped.
  388. *
  389. * Returns a 31-bit random number.
  390. */
  391. long
  392. our_random()
  393. {
  394. long i;
  395. if (rand_type == TYPE_0)
  396. i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff;
  397. else {
  398. *fptr += *rptr;
  399. i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */
  400. if (++fptr >= end_ptr) {
  401. fptr = state;
  402. ++rptr;
  403. } else if (++rptr >= end_ptr)
  404. rptr = state;
  405. }
  406. return(i);
  407. }
  408. #endif
  409. u_int32_t our_random32() {
  410. // Return a 32-bit random number.
  411. // Because "our_random()" returns a 31-bit random number, we call it a second
  412. // time, to generate the high bit:
  413. long random1 = our_random();
  414. long random2 = our_random();
  415. return (u_int32_t)((random2<<31) | random1);
  416. }
  417. #ifdef USE_OUR_BZERO
  418. #ifndef __bzero
  419. void
  420. __bzero (to, count)
  421. char *to;
  422. int count;
  423. {
  424. while (count-- > 0)
  425. {
  426. *to++ = 0;
  427. }
  428. }
  429. #endif
  430. #endif