コード例 #1
0
ファイル: gen.cpp プロジェクト: M4573R/pc-code
void test_case()
{
    int a = rand() % MAXA + 1;
    int b = rand() % MAXA + 1;

    if (rand() % 15 == 0) {
        a = rand() % 100 + 1;
        b = rand() % 100 + 1;
    }
    

    u64 lcm = a;
    lcm *= b;
    lcm /= gcd(a, b);

    u64 q = MAXL / lcm;
    u64 L = lcm * (rand_u64() % q + 1);

    if (rand() % 25 == 0)
        L = rand_u64();

    if (rand() % 10 == 0) {
        a = rand() % 1000 + 1;
        b = a * (rand() % 900 + 1);
        L = b * (rand_u64() % 100ULL + 1);
    }

    printf("%d %d %llu\n", a, b, L);
}
コード例 #2
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;
}
コード例 #3
0
ファイル: auth.c プロジェクト: muromec/rehttp
void write_auth(struct request *req, struct mbuf *mb)
{
    int err;
    struct realm *realm;
    uint8_t digest[MD5_SIZE];
    uint64_t cnonce;

    if(!req->auth)
        return;

    realm = req->auth;
    cnonce = rand_u64();

    err = mkdigest(digest, realm, req->meth, req->path, cnonce);

    err |= mbuf_write_str(mb, "Authorization: ");

    err |= mbuf_printf(mb, "Digest username=\"%s\"", realm->user);
    err |= mbuf_printf(mb, ", realm=\"%s\"", realm->realm);
    err |= mbuf_printf(mb, ", nonce=\"%s\"", realm->nonce);
    err |= mbuf_printf(mb, ", uri=\"%s\"", req->path);
    err |= mbuf_printf(mb, ", response=\"%w\"",
            digest, sizeof(digest));

    if (realm->opaque)
        err |= mbuf_printf(mb, ", opaque=\"%s\"", realm->opaque);

    if (realm->qop) {
        err |= mbuf_printf(mb, ", cnonce=\"%016llx\"", cnonce);
	err |= mbuf_write_str(mb, ", qop=auth");
    	err |= mbuf_printf(mb, ", nc=%08x", realm->nc);
    }
    ++realm->nc;
    err |= mbuf_write_str(mb, "\r\n");
}
コード例 #4
0
ファイル: auth.c プロジェクト: AmesianX/restund
static int module_init(void)
{
	auth.nonce_expiry = NONCE_EXPIRY;
	auth.secret = rand_u64();

	conf_get_u32(restund_conf(), "auth_nonce_expiry", &auth.nonce_expiry);

	auth.sharedsecret_length = 0;
	auth.sharedsecret2_length = 0;
    conf_get_str(restund_conf(), "auth_shared", auth.sharedsecret, sizeof(auth.sharedsecret));
    auth.sharedsecret_length = strlen(auth.sharedsecret);
    conf_get_str(restund_conf(), "auth_shared_rollover", auth.sharedsecret2, sizeof(auth.sharedsecret2));
    auth.sharedsecret2_length = strlen(auth.sharedsecret2);
    if (auth.sharedsecret_length > 0 || auth.sharedsecret2_length > 0) {
        restund_debug("auth: module loaded shared secret lengths %d and %d\n", 
                      auth.sharedsecret_length,
                      auth.sharedsecret2_length);
    }

	restund_stun_register_handler(&stun);

	restund_debug("auth: module loaded (nonce_expiry=%us)\n",
		      auth.nonce_expiry);

	return 0;
}
コード例 #5
0
ファイル: auth.c プロジェクト: anchowee/traversal_server
int sip_auth_encode(struct mbuf *mb, struct sip_auth *auth, const char *met,
		    const char *uri)
{
	struct le *le;
	int err = 0;

	if (!mb || !auth || !met || !uri)
		return EINVAL;

	for (le = auth->realml.head; le; le = le->next) {

		const uint64_t cnonce = rand_u64();
		struct realm *realm = le->data;
		uint8_t digest[MD5_SIZE];

		err = mkdigest(digest, realm, met, uri, cnonce);
		if (err)
			break;

		switch (realm->hdr) {

		case SIP_HDR_WWW_AUTHENTICATE:
			err = mbuf_write_str(mb, "Authorization: ");
			break;

		case SIP_HDR_PROXY_AUTHENTICATE:
			err = mbuf_write_str(mb, "Proxy-Authorization: ");
			break;

		default:
			continue;
		}

		err |= mbuf_printf(mb, "Digest username=\"%s\"", realm->user);
		err |= mbuf_printf(mb, ", realm=\"%s\"", realm->realm);
		err |= mbuf_printf(mb, ", nonce=\"%s\"", realm->nonce);
		err |= mbuf_printf(mb, ", uri=\"%s\"", uri);
		err |= mbuf_printf(mb, ", response=\"%w\"",
				   digest, sizeof(digest));

		if (realm->opaque)
			err |= mbuf_printf(mb, ", opaque=\"%s\"",
					   realm->opaque);

		if (realm->qop) {
			err |= mbuf_printf(mb, ", cnonce=\"%016llx\"", cnonce);
			err |= mbuf_write_str(mb, ", qop=auth");
			err |= mbuf_printf(mb, ", nc=%08x", realm->nc);
		}

		++realm->nc;

		err |= mbuf_write_str(mb, "\r\n");
		if (err)
			break;
	}

	return err;
}
コード例 #6
0
ファイル: gen.cpp プロジェクト: lbv/ffa-pc
void test_case()
{
    u64 N = rand_u64() % (MAXN + 1);

    if (rand() % 20 == 0)
        N = rand() % 1000;

    printf("%llu\n", N);
}
コード例 #7
0
ファイル: test-mt.cpp プロジェクト: respu/mersenne-twister
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;
}
コード例 #8
0
static int request(struct sip_request *req, enum sip_transp tp,
		   const struct sa *dst)
{
	struct mbuf *mb = NULL;
	char *branch = NULL;
	int err = ENOMEM;
	struct sa laddr;

	req->provrecv = false;

	branch = mem_alloc(24, NULL);
	mb = mbuf_alloc(1024);

	if (!branch || !mb)
		goto out;

	(void)re_snprintf(branch, 24, "z9hG4bK%016llx", rand_u64());

	err = sip_transp_laddr(req->sip, &laddr, tp, dst);
	if (err)
		goto out;

	err  = mbuf_printf(mb, "%s %s SIP/2.0\r\n", req->met, req->uri);
	err |= mbuf_printf(mb, "Via: SIP/2.0/%s %J;branch=%s;rport\r\n",
			   sip_transp_name(tp), &laddr, branch);
	err |= req->sendh ? req->sendh(tp, &laddr, dst, mb, req->arg) : 0;
	err |= mbuf_write_mem(mb, mbuf_buf(req->mb), mbuf_get_left(req->mb));
	if (err)
		goto out;

	mb->pos = 0;

	if (!req->stateful)
		err = sip_send(req->sip, NULL, tp, dst, mb);
	else
		err = sip_ctrans_request(&req->ct, req->sip, tp, dst, req->met,
					 branch, mb, response_handler, req);
	if (err)
		goto out;

 out:
	mem_deref(branch);
	mem_deref(mb);

	return err;
}
コード例 #9
0
ファイル: ice.c プロジェクト: Issic47/libre
/**
 * Allocate a new ICE Session
 *
 * @param icep    Pointer to allocated ICE Session object
 * @param mode    ICE Mode; Full-mode or Lite-mode
 * @param offerer True if we are SDP offerer, otherwise false
 *
 * @return 0 if success, otherwise errorcode
 */
int ice_alloc(struct ice **icep, enum ice_mode mode, bool offerer)
{
	struct ice *ice;
	int err = 0;

	if (!icep)
		return EINVAL;

	ice = mem_zalloc(sizeof(*ice), ice_destructor);
	if (!ice)
		return ENOMEM;

	list_init(&ice->ml);

	ice->conf = conf_default;
	ice->lmode = mode;
	ice->tiebrk = rand_u64();

	rand_str(ice->lufrag, sizeof(ice->lufrag));
	rand_str(ice->lpwd, sizeof(ice->lpwd));

	ice_determine_role(ice, offerer);

	if (ICE_MODE_FULL == ice->lmode) {

		err = stun_alloc(&ice->stun, NULL, NULL, NULL);
		if (err)
			goto out;

		/* Update STUN Transport */
		stun_conf(ice->stun)->rto = ice->conf.rto;
		stun_conf(ice->stun)->rc = ice->conf.rc;
	}

 out:
	if (err)
		mem_deref(ice);
	else
		*icep = ice;

	return err;
}
コード例 #10
0
int trice_alloc(struct trice **icemp, const struct trice_conf *conf,
	       bool controlling,
	       const char *lufrag, const char *lpwd)
{
	struct trice *icem;
	int err = 0;

	if (!icemp || !lufrag || !lpwd)
		return EINVAL;

	if (str_len(lufrag) < 4 || str_len(lpwd) < 22) {
		DEBUG_WARNING("alloc: lufrag/lpwd is too short\n");
		return EINVAL;
	}

	icem = mem_zalloc(sizeof(*icem), trice_destructor);
	if (!icem)
		return ENOMEM;

	icem->conf = conf ? *conf : conf_default;
	list_init(&icem->lcandl);
	list_init(&icem->rcandl);
	list_init(&icem->checkl);
	list_init(&icem->validl);

	icem->controlling = controlling;
	icem->tiebrk = rand_u64();


	err |= str_dup(&icem->lufrag, lufrag);
	err |= str_dup(&icem->lpwd, lpwd);
	if (err)
		goto out;

 out:
	if (err)
		mem_deref(icem);
	else
		*icemp = icem;

	return err;
}
コード例 #11
0
ファイル: sys.c プロジェクト: traviscross/libre-test
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;
}
コード例 #12
0
ファイル: random.c プロジェクト: PromyLOPh/pucket
/*	Random boolean value in {0,1}
 */
int rand_bool (randctx * const st) {
	return rand_u64 (st) & 1;
}
コード例 #13
0
ファイル: random.c プロジェクト: PromyLOPh/pucket
/*	Generate random double in [0,1]
 */
double rand_d01 (randctx * const st) {
	return (double) rand_u64 (st) / (double) UINT64_MAX;
}
コード例 #14
0
ファイル: msg.c プロジェクト: Issic47/libre
/**
 * Decode a SIP message
 *
 * @param msgp Pointer to allocated SIP Message
 * @param mb   Buffer containing SIP Message
 *
 * @return 0 if success, otherwise errorcode
 */
int sip_msg_decode(struct sip_msg **msgp, struct mbuf *mb)
{
	struct pl x, y, z, e, name;
	const char *p, *v, *cv;
	struct sip_msg *msg;
	bool comsep, quote;
	enum sip_hdrid id = SIP_HDR_NONE;
	uint32_t ws, lf;
	size_t l;
	int err;

	if (!msgp || !mb)
		return EINVAL;

	p = (const char *)mbuf_buf(mb);
	l = mbuf_get_left(mb);

	if (re_regex(p, l, "[^ \t\r\n]+ [^ \t\r\n]+ [^\r\n]*[\r]*[\n]1",
		     &x, &y, &z, NULL, &e) || x.p != (char *)mbuf_buf(mb))
		return (l > STARTLINE_MAX) ? EBADMSG : ENODATA;

	msg = mem_zalloc(sizeof(*msg), destructor);
	if (!msg)
		return ENOMEM;

	err = hash_alloc(&msg->hdrht, HDR_HASH_SIZE);
	if (err)
		goto out;

	msg->tag = rand_u64();
	msg->mb  = mem_ref(mb);
	msg->req = (0 == pl_strcmp(&z, "SIP/2.0"));

	if (msg->req) {

		msg->met = x;
		msg->ruri = y;
		msg->ver = z;

		if (uri_decode(&msg->uri, &y)) {
			err = EBADMSG;
			goto out;
		}
	}
	else {
		msg->ver    = x;
		msg->scode  = pl_u32(&y);
		msg->reason = z;

		if (!msg->scode) {
			err = EBADMSG;
			goto out;
		}
	}

	l -= e.p + e.l - p;
	p = e.p + e.l;

	name.p = v = cv = NULL;
	name.l = ws = lf = 0;
	comsep = false;
	quote = false;

	for (; l > 0; p++, l--) {

		switch (*p) {

		case ' ':
		case '\t':
			lf = 0; /* folding */
			++ws;
			break;

		case '\r':
			++ws;
			break;

		case '\n':
			++ws;

			if (!lf++)
				break;

			++p; --l; /* eoh */

			/*@fallthrough@*/

		default:
			if (lf || (*p == ',' && comsep && !quote)) {

				if (!name.l) {
					err = EBADMSG;
					goto out;
				}

				err = hdr_add(msg, &name, id, cv ? cv : p,
					      cv ? p - cv - ws : 0,
					      true, cv == v && lf);
				if (err)
					goto out;

				if (!lf) { /* comma separated */
					cv = NULL;
					break;
				}

				if (cv != v) {
					err = hdr_add(msg, &name, id,
						      v ? v : p,
						      v ? p - v - ws : 0,
						      false, true);
					if (err)
						goto out;
				}

				if (lf > 1) { /* eoh */
					err = 0;
					goto out;
				}

				comsep = false;
				name.p = NULL;
				cv = v = NULL;
				lf = 0;
			}

			if (!name.p) {
				name.p = p;
				name.l = 0;
				ws = 0;
			}

			if (!name.l) {
				if (*p != ':') {
					ws = 0;
					break;
				}

				name.l = MAX((int)(p - name.p - ws), 0);
				if (!name.l) {
					err = EBADMSG;
					goto out;
				}

				id = hdr_hash(&name);
				comsep = hdr_comma_separated(id);
				break;
			}

			if (!cv) {
				quote = false;
				cv = p;
			}

			if (!v) {
				v = p;
			}

			if (*p == '"')
				quote = !quote;

			ws = 0;
			break;
		}
	}

	err = ENODATA;

 out:
	if (err)
		mem_deref(msg);
	else {
		*msgp = msg;
		mb->pos = mb->end - l;
	}

	return err;
}
コード例 #15
0
ファイル: dialog.c プロジェクト: soramimi/qSIP
/**
 * Allocate a SIP Dialog
 *
 * @param dlgp      Pointer to allocated SIP Dialog
 * @param uri       Target URI
 * @param to_uri    To URI
 * @param from_name From displayname (optional)
 * @param from_uri  From URI
 * @param routev    Route vector
 * @param routec    Route count
 *
 * @return 0 if success, otherwise errorcode
 */
int sip_dialog_alloc(struct sip_dialog **dlgp,
		     const char *uri, const char *to_uri,
		     const char *from_name, const char *from_uri,
		     const char *routev[], uint32_t routec)
{
	const uint64_t ltag = rand_u64();
	struct sip_dialog *dlg;
	struct sip_addr addr;
	size_t rend = 0;
	struct pl pl;
	uint32_t i;
	int err;

	if (!dlgp || !uri || !to_uri || !from_uri)
		return EINVAL;

	dlg = mem_zalloc(sizeof(*dlg), destructor);
	if (!dlg)
		return ENOMEM;

	dlg->lseq = rand_u16();

	err = str_dup(&dlg->uri, uri);
	if (err)
		goto out;

	err = x64_strdup(&dlg->callid, rand_u64());
	if (err)
		goto out;

	err = x64_strdup(&dlg->ltag, ltag);
	if (err)
		goto out;

	dlg->mb = mbuf_alloc(512);
	if (!dlg->mb) {
		err = ENOMEM;
		goto out;
	}

	for (i=0; i<routec; i++) {
		err |= mbuf_printf(dlg->mb, "Route: <%s;lr>\r\n", routev[i]);
		if (i == 0)
			rend = dlg->mb->pos - 2;
	}
	err |= mbuf_printf(dlg->mb, "To: <%s>\r\n", to_uri);
	dlg->cpos = dlg->mb->pos;
	err |= mbuf_printf(dlg->mb, "From: %s%s%s<%s>;tag=%016llx\r\n",
			   from_name ? "\"" : "", from_name,
			   from_name ? "\" " : "",
			   from_uri, ltag);
	if (err)
		goto out;

	dlg->mb->pos = 0;

	if (rend) {
		pl.p = (const char *)mbuf_buf(dlg->mb) + ROUTE_OFFSET;
		pl.l = rend - ROUTE_OFFSET;
		err = sip_addr_decode(&addr, &pl);
		dlg->route = addr.uri;
	}
	else {
		pl_set_str(&pl, dlg->uri);
		err = uri_decode(&dlg->route, &pl);
	}

 out:
	if (err)
		mem_deref(dlg);
	else
		*dlgp = dlg;

	return err;
}
コード例 #16
0
ファイル: gen.cpp プロジェクト: M4573R/pc-code
void test_case()
{
    u64 N = rand_u64() % MAXN + 1;
    if (rand() % 25 == 0) N = rand() % 1000 + 1;
    printf("%llu\n", N);
}