home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / prand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  2.5 KB  |  84 lines

  1. /*
  2.  *  $Id: prand.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  prand.c - pseudo-random number generation
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../include/config.h"
  36. #endif
  37. #include "../include/libnet.h"
  38.  
  39. int
  40. seed_prand()
  41. {
  42.     struct timeval seed;
  43.  
  44.     if (gettimeofday(&seed, NULL) == -1)
  45.     {
  46.         perror("seed_rand: cannot gettimeofday");
  47.         return (-1);
  48.     }
  49.  
  50.     /*
  51.      *  More entropy then just seeding with time(2).
  52.      */
  53.     srandom((unsigned)(seed.tv_sec ^ seed.tv_usec));
  54.     return (1);
  55. }
  56.  
  57.  
  58. u_long
  59. get_prand(int mod)
  60. {
  61.     u_long n;
  62.  
  63.     n = random();
  64.  
  65.     switch (mod)
  66.     {
  67.         case PR2:
  68.             return (n % 0x2);           /* 0 - 1 */
  69.         case PR8:
  70.             return (n % 0xff);          /* 0 - 255 */
  71.         case PR16:
  72.             return (n % 0x7fff);        /* 0 - 32767 */
  73.         case PRu16:
  74.             return (n % 0xffff);        /* 0 - 65535 */
  75.         case PR32:
  76.             return (n % 0x7fffffff);    /* 0 - 2147483647 */
  77.         case PRu32:
  78.             return (n);                 /* 0 - 4294967295 */
  79.     }
  80.     return (0);                         /* NOTTREACHED */
  81. }
  82.  
  83. /* EOF */
  84.