magic.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * magic.c - PPP Magic Number routines.
  3. *
  4. * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. The name "Carnegie Mellon University" must not be used to
  19. * endorse or promote products derived from this software without
  20. * prior written permission. For permission or any legal
  21. * details, please contact
  22. * Office of Technology Transfer
  23. * Carnegie Mellon University
  24. * 5000 Forbes Avenue
  25. * Pittsburgh, PA 15213-3890
  26. * (412) 268-4387, fax: (412) 268-7395
  27. * tech-transfer@andrew.cmu.edu
  28. *
  29. * 4. Redistributions of any form whatsoever must retain the following
  30. * acknowledgment:
  31. * "This product includes software developed by Computing Services
  32. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  33. *
  34. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  35. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  36. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  37. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  38. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  39. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  40. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  41. */
  42. #define RCSID "$Id: magic.c,v 1.11 2003/06/11 23:56:26 paulus Exp $"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <unistd.h>
  46. #include <sys/types.h>
  47. #include <sys/time.h>
  48. #include "pppd.h"
  49. #include "magic.h"
  50. static const char rcsid[] = RCSID;
  51. extern long mrand48 __P((void));
  52. extern void srand48 __P((long));
  53. /*
  54. * magic_init - Initialize the magic number generator.
  55. *
  56. * Attempts to compute a random number seed which will not repeat.
  57. * The current method uses the current hostid, current process ID
  58. * and current time, currently.
  59. */
  60. void
  61. magic_init()
  62. {
  63. long seed;
  64. struct timeval t;
  65. gettimeofday(&t, NULL);
  66. seed = get_host_seed() ^ t.tv_sec ^ t.tv_usec ^ getpid();
  67. srand48(seed);
  68. }
  69. /*
  70. * magic - Returns the next magic number.
  71. */
  72. u_int32_t
  73. magic()
  74. {
  75. return (u_int32_t) mrand48();
  76. }
  77. /*
  78. * random_bytes - Fill a buffer with random bytes.
  79. */
  80. void
  81. random_bytes(unsigned char *buf, int len)
  82. {
  83. int i;
  84. for (i = 0; i < len; ++i)
  85. buf[i] = mrand48() >> 24;
  86. }
  87. #ifdef NO_DRAND48
  88. /*
  89. * Substitute procedures for those systems which don't have
  90. * drand48 et al.
  91. */
  92. double
  93. drand48()
  94. {
  95. return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
  96. }
  97. long
  98. mrand48()
  99. {
  100. return random();
  101. }
  102. void
  103. srand48(seedval)
  104. long seedval;
  105. {
  106. srandom((int)seedval);
  107. }
  108. #endif