|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* $Id: libnet_prand.c,v 1.7 2004/01/28 19:45:00 mike Exp $
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* libnet
|
|
Packit |
03b34a |
* libnet_prand.c - pseudo-random number generation
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
|
Packit |
03b34a |
* All rights reserved.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
03b34a |
* modification, are permitted provided that the following conditions
|
|
Packit |
03b34a |
* are met:
|
|
Packit |
03b34a |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
03b34a |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
03b34a |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
03b34a |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
03b34a |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
03b34a |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
Packit |
03b34a |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
03b34a |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
03b34a |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
03b34a |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
03b34a |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
03b34a |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
03b34a |
* SUCH DAMAGE.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if (HAVE_CONFIG_H)
|
|
Packit |
03b34a |
#include "../include/config.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#if (!(_WIN32) || (__CYGWIN__))
|
|
Packit |
03b34a |
#include "../include/libnet.h"
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#include "../include/win32/libnet.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
int
|
|
Packit |
03b34a |
libnet_seed_prand(libnet_t *l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
#if !(__WIN32__)
|
|
Packit |
03b34a |
struct timeval seed;
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if __WIN32__
|
|
Packit |
03b34a |
srand((unsigned)time(NULL));
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
if (gettimeofday(&seed, NULL) == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): cannot gettimeofday\n", __func__);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* More entropy then just seeding with time(2).
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
srandom((unsigned)(seed.tv_sec ^ seed.tv_usec));
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
return (1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
uint32_t
|
|
Packit |
03b34a |
libnet_get_prand(int mod)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n; /* 0 to 4,294,967,295 */
|
|
Packit |
03b34a |
#ifndef _WIN32
|
|
Packit |
03b34a |
n = random();
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
HCRYPTPROV hProv = 0;
|
|
Packit |
03b34a |
CryptAcquireContext(&hProv,
|
|
Packit |
03b34a |
0, 0, PROV_RSA_FULL,
|
|
Packit |
03b34a |
CRYPT_VERIFYCONTEXT);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
CryptGenRandom(hProv,
|
|
Packit |
03b34a |
sizeof(n), (BYTE*)&n);
|
|
Packit |
03b34a |
CryptReleaseContext(hProv, 0);
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
switch (mod)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
case LIBNET_PR2:
|
|
Packit |
03b34a |
return (n % 0x2); /* 0 - 1 */
|
|
Packit |
03b34a |
case LIBNET_PR8:
|
|
Packit |
03b34a |
return (n % 0xff); /* 0 - 255 */
|
|
Packit |
03b34a |
case LIBNET_PR16:
|
|
Packit |
03b34a |
return (n % 0x7fff); /* 0 - 32767 */
|
|
Packit |
03b34a |
case LIBNET_PRu16:
|
|
Packit |
03b34a |
return (n % 0xffff); /* 0 - 65535 */
|
|
Packit |
03b34a |
case LIBNET_PR32:
|
|
Packit |
03b34a |
return (n % 0x7fffffff); /* 0 - 2147483647 */
|
|
Packit |
03b34a |
case LIBNET_PRu32:
|
|
Packit |
03b34a |
return (n); /* 0 - 4294967295 */
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (0); /* NOTTREACHED */
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* EOF */
|