static int generate_random_uuid(FILE *f) { if (re_fprintf(f, "%08x-%04x-%04x-%04x-%08x%04x", rand_u32(), rand_u16(), rand_u16(), rand_u16(), rand_u32(), rand_u16()) != UUID_LEN) return ENOMEM; return 0; }
int main() { uint32_t errors = 0; int seed = 1; srand(seed); printf("Mersenne Twister -- printing the first %zu numbers w/seed %d\n\n", sizeof(expected)/sizeof(expected[0]), seed); for ( size_t n=0; n<sizeof(expected)/sizeof(expected[0]); ++n ) { uint32_t r = rand_u32(); bool error = r != expected[n]; if ( error ) ++errors; printf("%10u%c%c", r, error? '*' : ' ', n % 5 == 4 ? '\n' : ' '); fflush(stdout); } printf("\nGenerating 64-bit pseudo-random numbers\n\n"); for ( int n=0; n<27; ++n ) printf("%20" PRIu64 "%c", rand_u64(), n % 3 == 2 ? '\n' : ' '); printf("\nFloat values in range [0..1]\n\n"); for ( int n=0; n<40; ++n ) printf("%f%c", randf_cc(), n % 5 == 4 ? '\n' : ' '); printf("\nDouble values in range [0..1]\n\n"); for ( int n=0; n<40; ++n ) printf("%f%c", randd_cc(), n % 5 == 4 ? '\n' : ' '); printf("\nChecking reference numbers for seed 1 (may take some time)\n\n"); srand(1); for ( size_t n=0, target=1, idx=0; n<=0xffffffff; ++n ) { uint32_t r = rand_u32(); if ( n != (target-1) ) continue; bool error = r != doubled_reference_seed1[idx]; if ( error ) ++errors; printf("%11zu %11u%c %c", n, r, error? '*' : ' ', idx % 4 == 3 ? '\n' : ' '); fflush(stdout); target *= 2; ++idx; } printf("\nFound %u incorrect numbers\n\n", errors); return errors > 0; }
static int module_init(void) { auth.nonce_expiry = NONCE_EXPIRY; auth.rand_time = rand_u32(); auth.rand_addr = rand_u32(); conf_get_u32(restund_conf(), "auth_nonce_expiry", &auth.nonce_expiry); restund_stun_register_handler(&stun); restund_debug("auth: module loaded (nonce_expiry=%us)\n", auth.nonce_expiry); return 0; }
/* just keep sending and receiving for a while */ THREAD_FUNC(pipe_child, varg) { pipe_child_arg_t *arg = (pipe_child_arg_t*)varg; int id = arg->id; sock_t fd = arg->sock; int i, n; char buf[CHILD_BUF_MAX]; tid_t tid; tid = thread_tid(); for(i=0; i<CHILD_CALL_MAX; i++) { n = rand_u32(CHILD_BUF_MAX); n = send(fd, buf, n, 0); printf("pipe_child[tid=%d]: send n=%d\n", tid, n); if( n <= 0 ) break; n = recv(fd, buf, sizeof(buf), 0); printf("pipe_child[tid=%d]: recv n=%d\n", tid, n); if( n <= 0 ) break; //usleep((double)rand() * CHILD_SLEEP_RAND / RAND_MAX); } sock_close(fd); free(arg); return 0; }
int main() { uint32_t errors = 0; int seed = 1; srand(seed); printf("Mersenne Twister -- printing the first 200 numbers seed %d\n\n", seed); for ( int n=0; n<200; ++n ) { uint32_t r = rand_u32(); bool error = r != expected[n]; if ( error ) ++errors; printf("%10u%c%c", r, error? '*' : ' ', n % 5 == 4 ? '\n' : ' '); } printf("\nGenerating 64-bit pseudo-random numbers\n\n"); for ( int n=0; n<27; ++n ) printf("%20llu%c", rand_u64(), n % 3 == 2 ? '\n' : ' '); printf("\nFloat values in range [0..1]\n\n"); for ( int n=0; n<40; ++n ) printf("%f%c", randf_cc(), n % 5 == 4 ? '\n' : ' '); printf("\nDouble values in range [0..1]\n\n"); for ( int n=0; n<40; ++n ) printf("%f%c", randd_cc(), n % 5 == 4 ? '\n' : ' '); printf("\nFound %u incorrect numbers\n\n", errors); return errors > 0; }
void make_inplace(network::data *t) { network::init(t, 0, 1); std::vector<edge> edges; edges.reserve(t->matrix->taxons * 2); edge e; e.n0 = 0; e.n1 = 1; edges.push_back(e); for (int i=2;i<t->matrix->taxons;i++) { const int pick = rand_u32() % edges.size(); edge tmp = edges[pick]; network::idx_t neu = network::insert(t, edges[pick].n0, edges[pick].n1, i); edges[pick].n0 = tmp.n0; edges[pick].n1 = neu; edge e1; e1.n0 = i; e1.n1 = neu; edge e2; e2.n0 = neu; e2.n1 = tmp.n1; edges.push_back(e1); edges.push_back(e2); } }
void pipe_select_write(fdselect_t *sel, int fd, int event, void *arg) { int n; char buf[CHILD_BUF_MAX]; tid_t tid = (tid_t)arg; n = rand_u32(CHILD_BUF_MAX); n = send(fd, buf, n, 0); printf("pipe_select_write[tid=%d]: send n=%d\n", tid, n); if( sock_wouldblock(fd, n) ) { return; } fdselect_unset(sel, fd, FDSELECT_WRITE); if( n>0 ) { fdselect_set(sel, fd, FDSELECT_READ, pipe_select_read, arg); } }
int test_sys_rand(void) { char str[64]; uint8_t buf[64]; volatile uint16_t u16 = rand_u16(); volatile uint32_t u32 = rand_u32(); volatile uint64_t u64 = rand_u64(); volatile char ch = rand_char(); (void)u16; (void)u32; (void)u64; (void)ch; rand_str(str, sizeof(str)); rand_bytes(buf, sizeof(buf)); return 0; }
/** * Allocate a new RTP socket * * @param rsp Pointer to returned RTP socket * * @return 0 for success, otherwise errorcode */ int rtp_alloc(struct rtp_sock **rsp) { struct rtp_sock *rs; if (!rsp) return EINVAL; rs = mem_zalloc(sizeof(*rs), destructor); if (!rs) return ENOMEM; sa_init(&rs->rtcp_peer, AF_UNSPEC); rs->enc.seq = rand_u16() & 0x7fff; rs->enc.ssrc = rand_u32(); *rsp = rs; return 0; }
/** * Send a STUN request using a client transaction * * @param ctp Pointer to allocated client transaction (optional) * @param stun STUN Instance * @param proto Transport Protocol * @param sock Socket; UDP (struct udp_sock) or TCP (struct tcp_conn) * @param dst Destination network address * @param presz Number of bytes in preamble, if sending over TURN * @param method STUN Method * @param key Authentication key (optional) * @param keylen Number of bytes in authentication key * @param fp Use STUN Fingerprint attribute * @param resph Response handler * @param arg Response handler argument * @param attrc Number of attributes to encode (variable arguments) * @param ... Variable list of attribute-tuples * Each attribute has 2 arguments, attribute type and value * * @return 0 if success, otherwise errorcode */ int stun_request(struct stun_ctrans **ctp, struct stun *stun, int proto, void *sock, const struct sa *dst, size_t presz, uint16_t method, const uint8_t *key, size_t keylen, bool fp, stun_resp_h *resph, void *arg, uint32_t attrc, ...) { uint8_t tid[STUN_TID_SIZE]; struct mbuf *mb; uint32_t i; va_list ap; int err; if (!stun) return EINVAL; mb = mbuf_alloc(512); if (!mb) return ENOMEM; for (i=0; i<STUN_TID_SIZE; i++) tid[i] = rand_u32(); va_start(ap, attrc); mb->pos = presz; err = stun_msg_vencode(mb, method, STUN_CLASS_REQUEST, tid, NULL, key, keylen, fp, 0x00, attrc, ap); va_end(ap); if (err) goto out; mb->pos = presz; err = stun_ctrans_request(ctp, stun, proto, sock, dst, mb, tid, method, key, keylen, resph, arg); if (err) goto out; out: mem_deref(mb); return err; }
int icem_rcand_add_prflx(struct cand **rcp, struct icem *icem, uint8_t compid, uint32_t prio, const struct sa *addr) { struct cand *rcand; int err; if (!icem || !addr) return EINVAL; rcand = mem_zalloc(sizeof(*rcand), cand_destructor); if (!rcand) return ENOMEM; list_append(&icem->rcandl, &rcand->le, rcand); rcand->type = CAND_TYPE_PRFLX; rcand->compid = compid; rcand->prio = prio; rcand->addr = *addr; err = re_sdprintf(&rcand->foundation, "%08x", rand_u32()); if (err) goto out; icecomp_printf(icem_comp_find(icem, compid), "added PeerReflexive remote candidate" " with priority %u (%J)\n", prio, addr); out: if (err) mem_deref(rcand); else if (rcp) *rcp = rcand; return err; }
int test_rtp(void) { struct rtp_sock *rtp = NULL; struct mbuf *mb = NULL; uint8_t payload[PAYLOAD_SIZE]; int j; int err; memset(payload, 0, sizeof(payload)); mb = mbuf_alloc(RTP_HEADER_SIZE); if (!mb) return ENOMEM; err = rtp_alloc(&rtp); if (err) goto out; for (j=0; j<100; j++) { struct rtp_header hdr, hdr2; memset(&hdr, 0, sizeof(hdr)); hdr.m = rand_u16() & 0x01; hdr.pt = rand_u16() & 0x7f; hdr.ts = rand_u32(); rand_bytes(payload, sizeof(payload)); mb->pos = mb->end = RTP_HEADER_SIZE; err = mbuf_write_mem(mb, payload, sizeof(payload)); if (err) break; mb->pos = 0; err = rtp_encode(rtp, hdr.m, hdr.pt, hdr.ts, mb); if (err) break; mb->pos = 0; err = rtp_decode(rtp, mb, &hdr2); if (err) break; if (hdr.m != hdr2.m) { DEBUG_WARNING("marker bit mismatch (%d != %d)\n", hdr.m, hdr2.m); err = EBADMSG; break; } if (hdr.pt != hdr2.pt) { DEBUG_WARNING("payload type mismatch (%u != %u)\n", hdr.pt, hdr2.pt); err = EBADMSG; break; } if (hdr.ts != hdr2.ts) { DEBUG_WARNING("timestamp mismatch (%lu != %lu)\n", hdr.ts, hdr2.ts); err = EBADMSG; break; } if (hdr2.pad) { DEBUG_WARNING("unexpected padding bit\n"); err = EBADMSG; break; } if (hdr2.ext) { DEBUG_WARNING("unexpected extension bit\n"); err = EBADMSG; break; } if (RTP_HEADER_SIZE != mb->pos || (RTP_HEADER_SIZE + PAYLOAD_SIZE) != mb->end) { DEBUG_WARNING("invalid mbuf size (pos=%u end=%u)\n", mb->pos, mb->end); err = EBADMSG; break; } if (0 != memcmp(mbuf_buf(mb), payload, sizeof(payload))) { DEBUG_WARNING("RTP payload mismatch\n"); err = EBADMSG; break; } } out: mem_deref(rtp); mem_deref(mb); return err; }
void run(network::data *d, tbr::output *out) { int boosts = (d->mtx_characters / 5) + 1; int picks[1024]; for (int i=0;i<boosts;i++) picks[i] = rand_u32() % d->mtx_characters; const int old = out->length; network::data *new_net = network::alloc(d->matrix, true); network::copy(new_net, d); int nw = 10; if (rand_u32()%10 > 5) nw = 0; else nw = 10; // manipulate for (int i=0;i<boosts;i++) { optimize::set_weight(new_net->opt, picks[i], nw); } // set up temporary output structure for tbr, we won't record any of these // networks as final tbr::output tout; tout.best_network = network::alloc(d->matrix, true); tout.length = 1000000; network::copy(tout.best_network, new_net); for (int i=0;i<50;i++) { if (tbr::run(new_net, &tout)) network::copy(new_net, tout.best_network); else break; } network::copy(new_net, tout.best_network); network::free(tout.best_network); // restore for (int i=0;i<boosts;i++) optimize::set_weight(new_net->opt, picks[i], 1); // see if we did any better than previous best // const character::distance_t prestore = optimize::optimize(out->best_network); // run again (no temp out this time) for (int i=0;i<50;i++) { if (tbr::run(new_net, out)) network::copy(new_net, out->best_network); else break; } if (out->length < old) { int d = optimize::optimize(out->best_network); std::cout << "ratchet: found net (ph2) with dist " << d << " recorded(" << out->length << ") previous(" << old << ")" << std::endl; newick::print(new_net); } network::copy(d, new_net); network::free(new_net); }