Annotation of embedaddon/libnet/src/libnet_prand.c, revision 1.1.1.3
1.1 misho 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: */
1.1.1.3 ! misho 30:
! 31: #include "common.h"
! 32:
! 33: #ifdef _WIN32
! 34: #include <wincrypt.h>
! 35: #include <time.h>
! 36:
1.1 misho 37: #else
1.1.1.3 ! misho 38: #include <sys/time.h> /* gettimeofday() */
1.1 misho 39: #endif
1.1.1.3 ! misho 40:
! 41:
1.1 misho 42: int
43: libnet_seed_prand(libnet_t *l)
44: {
1.1.1.3 ! misho 45: #ifndef WIN32
1.1 misho 46: struct timeval seed;
1.1.1.3 ! misho 47: #endif
1.1 misho 48:
49: if (l == NULL)
1.1.1.3 ! misho 50: {
1.1 misho 51: return (-1);
1.1.1.3 ! misho 52: }
1.1 misho 53:
1.1.1.3 ! misho 54: #ifdef WIN32
1.1 misho 55: srand((unsigned)time(NULL));
1.1.1.3 ! misho 56: #else
1.1 misho 57: if (gettimeofday(&seed, NULL) == -1)
58: {
59: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho 60: "%s(): cannot gettimeofday", __func__);
1.1 misho 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));
1.1.1.3 ! misho 68: #endif
1.1 misho 69: return (1);
70: }
71:
1.1.1.3 ! misho 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: */
1.1.1.2 misho 77: uint32_t
1.1 misho 78: libnet_get_prand(int mod)
79: {
1.1.1.2 misho 80: uint32_t n; /* 0 to 4,294,967,295 */
1.1.1.3 ! misho 81: #ifndef WIN32
1.1 misho 82: n = random();
83: #else
1.1.1.3 ! misho 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);
1.1 misho 91: #endif
92: switch (mod)
93: {
94: case LIBNET_PR2:
1.1.1.3 ! misho 95: return (n & 0x1); /* 0 - 1 */
1.1 misho 96: case LIBNET_PR8:
1.1.1.3 ! misho 97: return (n & 0xff); /* 0 - 255 */
1.1 misho 98: case LIBNET_PR16:
1.1.1.3 ! misho 99: return (n & 0x7fff); /* 0 - 32767 */
1.1 misho 100: case LIBNET_PRu16:
1.1.1.3 ! misho 101: return (n & 0xffff); /* 0 - 65535 */
1.1 misho 102: case LIBNET_PR32:
1.1.1.3 ! misho 103: return (n & 0x7fffffff); /* 0 - 2147483647 */
1.1 misho 104: case LIBNET_PRu32:
105: return (n); /* 0 - 4294967295 */
106: }
107: return (0); /* NOTTREACHED */
108: }
109:
1.1.1.3 ! misho 110: /**
! 111: * Local Variables:
! 112: * indent-tabs-mode: nil
! 113: * c-file-style: "stroustrup"
! 114: * End:
! 115: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>