File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_prand.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (8 months, 4 weeks ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

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

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