void * drop_open(int argc, char *argv[]) { struct drop_data *data; if (argc != 3) return (NULL); if ((data = calloc(1, sizeof(*data))) == NULL) return (NULL); data->rnd = rand_open(); if (strcasecmp(argv[1], "first") == 0) data->which = DROP_FIRST; else if (strcasecmp(argv[1], "last") == 0) data->which = DROP_LAST; else if (strcasecmp(argv[1], "random") == 0) data->which = DROP_RANDOM; else return (drop_close(data)); if ((data->percent = atoi(argv[2])) <= 0 || data->percent > 100) return (drop_close(data)); return (data); }
void * delay_open(int argc, char *argv[]) { struct delay_data *data; uint64_t usec; if (argc != 3) return (NULL); if ((data = malloc(sizeof(*data))) == NULL) return (NULL); data->rnd = rand_open(); if (strcasecmp(argv[1], "first") == 0) data->which = DELAY_FIRST; else if (strcasecmp(argv[1], "last") == 0) data->which = DELAY_LAST; else if (strcasecmp(argv[1], "random") == 0) data->which = DELAY_RANDOM; else return (delay_close(data)); if ((usec = atoi(argv[2])) == 0) return (delay_close(data)); usec *= 1000; data->tv.tv_sec = usec / 1000000; data->tv.tv_usec = usec % 1000000; return (data); }
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)); }
/* * assume: the timer object is initialized successfully. */ TiAloha * aloha_open( TiAloha * mac, TiFrameTxRxInterface * rxtx, uint8 chn, uint16 panid, uint16 address, TiTimer * timer, TiFunEventHandler listener, void * lisowner, uint8 option ) { void * provider; // assert( the rxtx driver has already been opened ); // assert( mac->timer is already opened but not start yet ); rtl_assert((rxtx != NULL) && (timer != NULL)); mac->state = ALOHA_STATE_IDLE; mac->rxtx = rxtx; mac->timer = timer; mac->listener = listener; mac->lisowner = lisowner; mac->retry = 0; mac->backoff = CONFIG_ALOHA_MIN_BACKOFF; mac->panto = panid; mac->shortaddrto = FRAME154_BROADCAST_ADDRESS; mac->panfrom = panid; mac->shortaddrfrom = address; mac->seqid = 0; mac->sendoption = 0x00; mac->sendfailed = 0; mac->option = option; mac->txbuf = frame_open( &(mac->txbuf_memory[0]), FRAME_HOPESIZE(CONFIG_ALOHA_MAX_FRAME_SIZE), 0, 0, 0 ); hal_assert( mac->timer != NULL ); // initialize the frame transceiver component // attention enable ACK support for aloha protocol. the current implementation depends // on low level transceiver component to provide ACK mechanism. provider = rxtx->provider; rxtx->setchannel( provider, chn ); rxtx->setpanid( provider, panid ); rxtx->setshortaddress( provider, address ); rxtx->enable_addrdecode( provider ); rxtx->enable_autoack( provider ); ieee802frame154_open( &(mac->desc) ); // initialize the random number generator with a random seed. you can replace // the seed with other values. // // @attention: generally, the random generator can be initialized when the whole // application started. if so, you needn't call rand_open() to initialize it // again here. rand_open( 0x3212 ); return mac; }
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; }
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); }