gstreamer/ext/sctp/usrsctp/usrsctplib/user_environment.c
Tim-Philipp Müller f4538e24b6 sctp: import internal copy of usrsctp library
There are problems with global shared state and no API stability
guarantees, and we can't rely on distros shipping the fixes we
need. Both firefox and Chrome bundle their own copies too.

Imported from https://github.com/sctplab/usrsctp,
commit 547d3b46c64876c0336b9eef297fda58dbe1adaf
Date: Thu Jul 23 21:49:32 2020 +0200

Fixes https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/issues/870

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1465>
2020-08-14 01:32:45 +01:00

132 lines
3.5 KiB
C

/*-
* Copyright (c) 2009-2010 Brad Penoff
* Copyright (c) 2009-2010 Humaira Kamal
* Copyright (c) 2011-2012 Irene Ruengeler
* Copyright (c) 2011-2012 Michael Tuexen
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* __Userspace__ */
#include <stdlib.h>
#if !defined(_WIN32)
#include <stdint.h>
#include <netinet/sctp_os_userspace.h>
#endif
#include <user_environment.h>
#include <sys/types.h>
/* #include <sys/param.h> defines MIN */
#if !defined(MIN)
#define MIN(arg1,arg2) ((arg1) < (arg2) ? (arg1) : (arg2))
#endif
#include <string.h>
#define uHZ 1000
/* See user_include/user_environment.h for comments about these variables */
int maxsockets = 25600;
int hz = uHZ;
int ip_defttl = 64;
int ipport_firstauto = 49152, ipport_lastauto = 65535;
int nmbclusters = 65536;
/* Source ip_output.c. extern'd in ip_var.h */
u_short ip_id = 0; /*__Userspace__ TODO Should it be initialized to zero? */
/* used in user_include/user_atomic.h in order to make the operations
* defined there truly atomic
*/
userland_mutex_t atomic_mtx;
/* If the entropy device is not loaded, make a token effort to
* provide _some_ kind of randomness. This should only be used
* inside other RNG's, like arc4random(9).
*/
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
void
init_random(void)
{
return;
}
int
read_random(void *buf, int count)
{
memset(buf, 'A', count);
return (count);
}
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
void
init_random(void)
{
return;
}
int
read_random(void *buf, int count)
{
if (count >= 0) {
arc4random_buf(buf, count);
}
return (count);
}
#else
void
init_random(void)
{
struct timeval now;
unsigned int seed;
(void)SCTP_GETTIME_TIMEVAL(&now);
seed = 0;
seed |= (unsigned int)now.tv_sec;
seed |= (unsigned int)now.tv_usec;
#if !defined(_WIN32) &&! defined(__native_client__)
seed |= getpid();
#endif
#if defined(_WIN32) || defined(__native_client__)
srand(seed);
#else
srandom(seed);
#endif
return;
}
int
read_random(void *buf, int count)
{
uint32_t randval;
int size, i;
/* Fill buf[] with random(9) output */
for (i = 0; i < count; i+= (int)sizeof(uint32_t)) {
randval = random();
size = MIN(count - i, (int)sizeof(uint32_t));
memcpy(&((char *)buf)[i], &randval, (size_t)size);
}
return (count);
}
#endif