static void IpId_Init (void) { if ( s_rand ) rand_close(s_rand); s_rand = rand_open(); if ( !s_rand ) FatalError("encode::IpId_Init: rand_open() failed.\n"); rand_get(s_rand, s_id_pool, sizeof(s_id_pool)); }
void * delay_close(void *d) { struct delay_data *data = (struct delay_data *)d; if (data != NULL) { rand_close(data->rnd); free(data); } return (NULL); }
unsigned char random_u8(unsigned int seed) { rand_t *r; int rv; unsigned char rand; r = rand_open(); if (r == NULL) return -1; rv = rand_set(r,&seed,4); if (r == NULL) return -1; rand = rand_uint8(r); r = rand_close(r); return rand; }
static unsigned int Rand_GenRand(unsigned char *pRand, unsigned long ulRandLen) { rand_t *r; int rv; // time_t seed; r = rand_open(); if (r == NULL) return -1; if(g_ulSendlen != 0) { rv = rand_set(r,&g_Seed,g_ulSendlen); if (r == NULL) return -1; } rv = rand_get(r,pRand,ulRandLen); if (r == NULL) return -1; r = rand_close(r); memcpy(g_Seed,pRand,g_ulSendlen); return 0; }
/*lint -e525 -e438*/ int GeneratePublicKey(DH_KEY *params, unsigned char *publicValue, unsigned int pubValueLen) { rand_t *rd; int rv; time_t seed; unsigned int bytesNeeded; unsigned char seedByte[20] = {0}; R_RANDOM_STRUCT randomStruct; bytesNeeded = pubValueLen; randomStruct.bytesNeeded = bytesNeeded; memset ((POINTER)randomStruct.state, 0, sizeof (randomStruct.state)); randomStruct.outputAvailable = 0; time(&seed); rd = rand_open(); if (rd == NULL) return -1; rand_set(rd,&seed,4); if (rd == NULL) return -1; while (1) { bytesNeeded = randomStruct.bytesNeeded; if (bytesNeeded == 0) break; rand_get(rd,seedByte,16); if (rd == NULL) return -1; RandomUpdate(&randomStruct, seedByte, 16); } rd = rand_close(rd); params->priVallen = pubValueLen; rv = SetupDHAgreement(publicValue,params->privateValue,pubValueLen, params,&randomStruct); return rv; }
void clear_screen_bylines() { win_t win; int16_t i, rnd, pos; int8_t *lines = (int8_t *)malloc(g_data.ui.ydim * sizeof(int8_t)); win_init(&win, NULL); win.boxed = 0; memset(lines, 0, g_data.ui.ydim * sizeof(int8_t)); rand_open(); for (i = g_data.ui.ydim; i > 0; i--) { rnd = rand_get() % i; pos = -1; while (rnd > -1) { if (lines[++pos] == 0) rnd--; } win_setdims(&win, pos, 0, 1, g_data.ui.xdim); win_start(&win); win_draw(&win, true, true); win_stop(&win); lines[pos] = 1; usleep(min(50000, (500000 / g_data.ui.ydim))); } rand_close(); win_uninit(&win); free(lines); }
int random_generate(unsigned int seed_ext,unsigned char *buf,unsigned len) { #define SEEDBYTE_NUM 128 unsigned int seed=seed_ext; rand_t *r; int rv; unsigned pos,cnt; unsigned char seedByte[SEEDBYTE_NUM] = {0}; r = rand_open(); if (r == NULL) return -1; rv = rand_set(r,&seed,4); if (r == NULL) return -1; rv = rand_get(r,seedByte,SEEDBYTE_NUM); r = rand_close(r); if(len <= SEEDBYTE_NUM){ memcpy(buf,seedByte,len); } else{ pos = 0; while(len>0){ if(len>=SEEDBYTE_NUM){ cnt = SEEDBYTE_NUM; } else{ cnt = len; } memcpy(&buf[pos],seedByte,cnt); len -= cnt; pos += cnt; //printf("len:%d\n",len); } } #undef SEEDBYTE_NUM return 0; }
static void IpId_Term (void) { if ( s_rand ) rand_close(s_rand); s_rand = NULL; }
int main(int argc, char *argv[]) { struct intf_entry ifent; intf_t *intf; int i, tests; char *cmd; if (argc < 3) usage(); for (tests = 0, i = 1; i < argc - 1; i++) { cmd = argv[i]; if (strcmp(cmd, "all") == 0) tests = ~0; else if (strcmp(cmd, "ping") == 0) tests |= TEST_PING; else if (strcmp(cmd, "ip-opt") == 0) tests |= TEST_IP_OPT; else if (strcmp(cmd, "ip-tracert") == 0) tests |= TEST_IP_TRACERT; else if (strcmp(cmd, "frag") == 0) tests |= TEST_FRAG; else if (strcmp(cmd, "frag-new") == 0) tests |= TEST_FRAG_NEW; else if (strcmp(cmd, "frag-old") == 0) tests |= TEST_FRAG_OLD; else if (strcmp(cmd, "frag-timeout") == 0) tests |= TEST_FRAG_TIMEOUT; else usage(); } if (addr_aton(argv[i], &ctx.dst) < 0) err(1, "invalid host %s", argv[i]); if ((intf = intf_open()) == NULL) err(1, "couldn't open interface handle"); ifent.intf_len = sizeof(ifent); if (intf_get_dst(intf, &ifent, &ctx.dst) < 0) err(1, "couldn't find interface for %s", addr_ntoa(&ctx.dst)); memcpy(&ctx.src, &ifent.intf_addr, sizeof(ctx.src)); ctx.src.addr_bits = IP_ADDR_BITS; intf_close(intf); if ((ctx.ip = ip_open()) == NULL) err(1, "couldn't open raw IP interface"); if ((ctx.pcap = pcap_open(ifent.intf_name)) == NULL) err(1, "couldn't open %s for sniffing", ifent.intf_name); if ((ctx.dloff = pcap_dloff(ctx.pcap)) < 0) err(1, "couldn't determine link layer offset"); ctx.rnd = rand_open(); pkt_init(16); TAILQ_INIT(&ctx.pktq); ping = pkt_new(); ip_pack_hdr(ping->pkt_ip, 0, IP_HDR_LEN + 8 + 24, 666, 0, IP_TTL_DEFAULT, IP_PROTO_ICMP, ctx.src.addr_ip, ctx.dst.addr_ip); icmp_pack_hdr_echo(ping->pkt_icmp, ICMP_ECHO, ICMP_CODE_NONE, 666, 1, "AAAAAAAABBBBBBBBCCCCCCCC", 24); ping->pkt_end = ping->pkt_eth_data + IP_HDR_LEN + 8 + 24; pkt_decorate(ping); if ((tests & TEST_PING) != 0) test_ping(); if ((tests & TEST_IP_OPT) != 0) test_ip_opt(); if ((tests & TEST_IP_TRACERT) != 0) test_ip_tracert(); if ((tests & TEST_FRAG) != 0) test_frag(NULL, 0); if ((tests & TEST_FRAG_NEW) != 0) test_frag("new", 0); if ((tests & TEST_FRAG_OLD) != 0) test_frag("old", 0); if ((tests & TEST_FRAG_TIMEOUT) != 0) test_frag(NULL, 1); rand_close(ctx.rnd); pcap_close(ctx.pcap); ip_close(ctx.ip); exit(0); }