1 Star 0 Fork 0

Espressif Systems/libcoap

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
coap_prng.c 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
/*
* coap_prng.c -- random number generation
*
* Copyright (C) 2020 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README
* for terms of use.
*/
#include "coap_internal.h"
#ifdef HAVE_GETRANDOM
#include <sys/random.h>
#else /* !HAVE_GETRANDOM */
#include <stdlib.h>
#endif /* !HAVE_GETRANDOM */
static int
coap_prng_default(void *buf, size_t len) {
#ifdef HAVE_GETRANDOM
return getrandom(buf, len, 0);
#else /* !HAVE_GETRANDOM */
unsigned char *dst = (unsigned char *)buf;
while (len--)
*dst++ = rand() & 0xFF;
return 1;
#endif /* !HAVE_GETRANDOM */
}
static coap_rand_func_t rand_func = coap_prng_default;
void
coap_set_prng(coap_rand_func_t rng) {
rand_func = rng;
}
void
coap_prng_init(unsigned long seed) {
#ifdef HAVE_GETRANDOM
/* No seed to seed the random source if getrandom() is used,
* see dtls_prng(). */
(void)seed;
#else /* !HAVE_GETRANDOM */
srand(seed);
#endif /* !HAVE_GETRANDOM */
}
int
coap_prng(void *buf, size_t len) {
if (!rand_func) {
return 0;
}
rand_func(buf, len);
return 1;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/espressif-systems/libcoap.git
git@gitee.com:espressif-systems/libcoap.git
espressif-systems
libcoap
libcoap
develop

搜索帮助