Annotation of embedaddon/libnet/src/libnet_prand.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  *  $Id: libnet_prand.c,v 1.7 2004/01/28 19:45:00 mike Exp $
                      3:  *
                      4:  *  libnet
                      5:  *  libnet_prand.c - pseudo-random number generation
                      6:  *
                      7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
                      8:  *  All rights reserved.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  *
                     31:  */
                     32:  
                     33: #if (HAVE_CONFIG_H)
                     34: #include "../include/config.h"
                     35: #endif
                     36: #if (!(_WIN32) || (__CYGWIN__)) 
                     37: #include "../include/libnet.h"
                     38: #else
                     39: #include "../include/win32/libnet.h"
                     40: #endif
                     41: int
                     42: libnet_seed_prand(libnet_t *l)
                     43: {
                     44:        #if !(__WIN32__)
                     45:     struct timeval seed;
                     46:        #endif
                     47: 
                     48:     if (l == NULL)
                     49:     { 
                     50:         return (-1);
                     51:     } 
                     52: 
                     53:        #if __WIN32__
                     54:     srand((unsigned)time(NULL));
                     55:        #else
                     56:     if (gettimeofday(&seed, NULL) == -1)
                     57:     {
                     58:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     59:                 "%s(): cannot gettimeofday\n", __func__);
                     60:         return (-1);
                     61:     }
                     62: 
                     63:     /*
                     64:      *  More entropy then just seeding with time(2).
                     65:      */
                     66:     srandom((unsigned)(seed.tv_sec ^ seed.tv_usec));
                     67:        #endif
                     68:     return (1);
                     69: }
                     70: 
                     71: 
1.1.1.2 ! misho      72: uint32_t
1.1       misho      73: libnet_get_prand(int mod)
                     74: {
1.1.1.2 ! misho      75:     uint32_t n;  /* 0 to 4,294,967,295 */
1.1       misho      76: #ifndef _WIN32
                     77:     n = random();
                     78: #else
                     79:        HCRYPTPROV hProv = 0;
                     80:        CryptAcquireContext(&hProv, 
                     81:                0, 0, PROV_RSA_FULL, 
                     82:                CRYPT_VERIFYCONTEXT);
                     83:        
                     84:        CryptGenRandom(hProv, 
                     85:                sizeof(n), (BYTE*)&n);
                     86:        CryptReleaseContext(hProv, 0); 
                     87: #endif
                     88:     switch (mod)
                     89:     {
                     90:         case LIBNET_PR2:
                     91:             return (n % 0x2);           /* 0 - 1 */
                     92:         case LIBNET_PR8:
                     93:             return (n % 0xff);          /* 0 - 255 */
                     94:         case LIBNET_PR16:
                     95:             return (n % 0x7fff);        /* 0 - 32767 */
                     96:         case LIBNET_PRu16:
                     97:             return (n % 0xffff);        /* 0 - 65535 */
                     98:         case LIBNET_PR32:
                     99:             return (n % 0x7fffffff);    /* 0 - 2147483647 */
                    100:         case LIBNET_PRu32:
                    101:             return (n);                 /* 0 - 4294967295 */
                    102:     }
                    103:     return (0);                         /* NOTTREACHED */
                    104: }
                    105: 
                    106: /* EOF */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>