Example #1
0
File: yam.c Project: ivucica/linux
static void yam_arbitrate(struct net_device *dev)
{
	struct yam_port *yp = netdev_priv(dev);

	if (yp->magic != YAM_MAGIC || yp->tx_state != TX_OFF ||
	    skb_queue_empty(&yp->send_queue))
		return;
	/* tx_state is TX_OFF and there is data to send */

	if (yp->dupmode) {
		/* Full duplex mode, don't wait */
		yam_start_tx(dev, yp);
		return;
	}
	if (yp->dcd) {
		/* DCD on, wait slotime ... */
		yp->slotcnt = yp->slot / 10;
		return;
	}
	/* Is slottime passed ? */
	if ((--yp->slotcnt) > 0)
		return;

	yp->slotcnt = yp->slot / 10;

	/* is random > persist ? */
	if ((random_num() % 256) > yp->pers)
		return;

	yam_start_tx(dev, yp);
}
Example #2
0
// !randkick
void
scriptcmd_randkick (NetServer *s)
{
  if (strcasecmp (CMD[2], s->nick) == 0)
    {
      SEND_TEXT(DEST, "\002!randkick\002 can only be used inside channels.");
      return;
    }
  int i = CHANNEL_INDEX (CMD[2]);
  if (i == -1)
    return;
  struct Channel::user_type *list[C_USERS_MAX];
  int user_num, list_num = 0;

  // build non-ops list
  for (user_num = 0; user_num < CHANNELS[i]->user_num; user_num++)
    if ( ! (CHANNELS[i]->users[user_num]->is_op) )
      list[list_num++] = CHANNELS[i]->users[user_num];

  // if there's at least one
  if (list_num != 0)
    {
      int rand_user = random_num (list_num);
      CHANNELS[i]->irc_kick (list[rand_user]->nick, RANDKICK_REASON);
    }
  else
    SEND_TEXT (DEST, "There's no one to kick.");
}
Example #3
0
static void
setup_blind(mp_int *n, mp_int *b, mp_int *bi)
{
    random_num(b, mp_count_bits(n));
    mp_mod(b, n, b);
    mp_invmod(b, n, bi);
}
Example #4
0
void generate_descending_set(vector<int> &v, unsigned int n){
	int inc = RANGE/n;
	int start = RANGE/2;
	for(unsigned i =0;i<n;i++){
		start -= random_num(1,inc);
		v.push_back(start);
	}
}
int randomized_partition(double *Address, int start, int end)
{
	int random_position;

	random_position=random_num(start,end);
	Exchange(&Address[random_position],&Address[end]);
	return partition(Address,start,end);
}
int main(int argc, char **argv){
    if(argc != 7){
        printf("wrong input!\n");
        exit(EXIT_FAILURE);
    }

    file = argv[1];
    sscanf(argv[2], "%d", &size);
    sscanf(argv[3], "%lf", &p_max);
    sscanf(argv[4], "%lf", &p_min);
    sscanf(argv[5], "%lf", &v_max);
    sscanf(argv[6], "%lf", &v_min);

    double **testcase = new double * [size];
    srand (time(NULL));

    for(int i=0; i<size; i++){
        testcase[i] = new double[4];
        testcase[i][0] = random_num(p_max, p_min);
        testcase[i][1] = random_num(p_max, p_min);
        testcase[i][2] = random_num(v_max, v_min);
        testcase[i][3] = random_num(v_max, v_min);
    }

    FILE *fp = fopen(file, "w");
    if(fp == NULL){
        printf("open file fail\n");
        exit(EXIT_FAILURE);
    }

    fprintf(fp, "%d\n", size);
    for(int i=0; i<size; i++){
        fprintf(fp, "%.15lf", testcase[i][0]);
        for(int j=1; j<4; j++){
            fprintf(fp, " %.15lf", testcase[i][j]);
        }
        fprintf(fp, "\n");

        delete [] testcase[i];
    }

    fclose(fp);
    delete [] testcase;
}
Example #7
0
int print_math(int num) {
    
    char c[] = {'+','-','*','/'};
    char *pc;
    int x;
    int y;
    int r;

    r = random_num(4);
    user_answer = 0;
    pc = c;
    pc += r;
    switch(r){
    case 0:
    	x = random_num(50);
    	y = random_num(50);
        answer = x + y;
        break;
    case 1:
    	x = random_num(100);
    	y = random_num(50);
        answer = x - y;
        break;
    case 2:
    	x = random_num(10);
    	y = random_num(10);
        answer = x * y;
        break;
    case 3:
    	x = random_num(100);
    	y = random_num(10);
        if(y == 0) y++;
        answer = x / y;
        x = answer * y;
        break;
    }
    printf("第%d题: ", num);
    printf("%d%c%d=", x , *pc , y );
    return 0;
}
Example #8
0
int main()
{	
	set_gpio('G',9,1);
	open_driver(PSAM1,O_RDWR);							//打开PSAM小卡设备

	if(Drivers[PSAM1].fd==-1)
	{
		printf("open device error\n");
		return;
	}

	coldreset();
	random_num();

	close(Drivers[PSAM1].fd);
}
Example #9
0
void generate_random_set(vector<int> &v, unsigned int n){
	for(unsigned i =0;i<n;i++)
		v.push_back(random_num(-RANGE/2,RANGE/2));
}
Example #10
0
static int
ltm_rsa_generate_key(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb)
{
    mp_int el, p, q, n, d, dmp1, dmq1, iqmp, t1, t2, t3;
    int counter, ret, bitsp;

    if (bits < 789)
	return -1;

    bitsp = (bits + 1) / 2;

    ret = -1;

    mp_init_multi(&el, &p, &q, &n, &d,
		  &dmp1, &dmq1, &iqmp,
		  &t1, &t2, &t3, NULL);

    BN2mpz(&el, e);

    /* generate p and q so that p != q and bits(pq) ~ bits */
    counter = 0;
    do {
	BN_GENCB_call(cb, 2, counter++);
	CHECK(random_num(&p, bitsp), 0);
	CHECK(mp_find_prime(&p), MP_YES);

	mp_sub_d(&p, 1, &t1);
	mp_gcd(&t1, &el, &t2);
    } while(mp_cmp_d(&t2, 1) != 0);

    BN_GENCB_call(cb, 3, 0);

    counter = 0;
    do {
	BN_GENCB_call(cb, 2, counter++);
	CHECK(random_num(&q, bits - bitsp), 0);
	CHECK(mp_find_prime(&q), MP_YES);

	if (mp_cmp(&p, &q) == 0) /* don't let p and q be the same */
	    continue;

	mp_sub_d(&q, 1, &t1);
	mp_gcd(&t1, &el, &t2);
    } while(mp_cmp_d(&t2, 1) != 0);

    /* make p > q */
    if (mp_cmp(&p, &q) < 0) {
	mp_int c;
	c = p;
	p = q;
	q = c;
    }

    BN_GENCB_call(cb, 3, 1);

    /* calculate n,  		n = p * q */
    mp_mul(&p, &q, &n);

    /* calculate d, 		d = 1/e mod (p - 1)(q - 1) */
    mp_sub_d(&p, 1, &t1);
    mp_sub_d(&q, 1, &t2);
    mp_mul(&t1, &t2, &t3);
    mp_invmod(&el, &t3, &d);

    /* calculate dmp1		dmp1 = d mod (p-1) */
    mp_mod(&d, &t1, &dmp1);
    /* calculate dmq1		dmq1 = d mod (q-1) */
    mp_mod(&d, &t2, &dmq1);
    /* calculate iqmp 		iqmp = 1/q mod p */
    mp_invmod(&q, &p, &iqmp);

    /* fill in RSA key */

    rsa->e = mpz2BN(&el);
    rsa->p = mpz2BN(&p);
    rsa->q = mpz2BN(&q);
    rsa->n = mpz2BN(&n);
    rsa->d = mpz2BN(&d);
    rsa->dmp1 = mpz2BN(&dmp1);
    rsa->dmq1 = mpz2BN(&dmq1);
    rsa->iqmp = mpz2BN(&iqmp);

    ret = 1;

out:
    mp_clear_multi(&el, &p, &q, &n, &d,
		   &dmp1, &dmq1, &iqmp,
		   &t1, &t2, &t3, NULL);

    return ret;
}
Example #11
0
static int
gmp_rsa_generate_key(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb)
{
    mpz_t el, p, q, n, d, dmp1, dmq1, iqmp, t1, t2, t3;
    int counter, ret;

    if (bits < 789)
	return -1;

    ret = -1;

    mpz_init(el);
    mpz_init(p);
    mpz_init(q);
    mpz_init(n);
    mpz_init(d);
    mpz_init(dmp1);
    mpz_init(dmq1);
    mpz_init(iqmp);
    mpz_init(t1);
    mpz_init(t2);
    mpz_init(t3);

    BN2mpz(el, e);

    /* generate p and q so that p != q and bits(pq) ~ bits */

    counter = 0;
    do {
	BN_GENCB_call(cb, 2, counter++);
	random_num(p, bits / 2 + 1);
	mpz_nextprime(p, p);

	mpz_sub_ui(t1, p, 1);
	mpz_gcd(t2, t1, el);
    } while(mpz_cmp_ui(t2, 1) != 0);

    BN_GENCB_call(cb, 3, 0);

    counter = 0;
    do {
	BN_GENCB_call(cb, 2, counter++);
	random_num(q, bits / 2 + 1);
	mpz_nextprime(q, q);

	mpz_sub_ui(t1, q, 1);
	mpz_gcd(t2, t1, el);
    } while(mpz_cmp_ui(t2, 1) != 0);

    /* make p > q */
    if (mpz_cmp(p, q) < 0)
	mpz_swap(p, q);

    BN_GENCB_call(cb, 3, 1);

    /* calculate n,  		n = p * q */
    mpz_mul(n, p, q);

    /* calculate d, 		d = 1/e mod (p - 1)(q - 1) */
    mpz_sub_ui(t1, p, 1);
    mpz_sub_ui(t2, q, 1);
    mpz_mul(t3, t1, t2);
    mpz_invert(d, el, t3);

    /* calculate dmp1		dmp1 = d mod (p-1) */
    mpz_mod(dmp1, d, t1);
    /* calculate dmq1		dmq1 = d mod (q-1) */
    mpz_mod(dmq1, d, t2);
    /* calculate iqmp 		iqmp = 1/q mod p */
    mpz_invert(iqmp, q, p);

    /* fill in RSA key */

    rsa->e = mpz2BN(el);
    rsa->p = mpz2BN(p);
    rsa->q = mpz2BN(q);
    rsa->n = mpz2BN(n);
    rsa->d = mpz2BN(d);
    rsa->dmp1 = mpz2BN(dmp1);
    rsa->dmq1 = mpz2BN(dmq1);
    rsa->iqmp = mpz2BN(iqmp);

    ret = 1;

    mpz_clear(el);
    mpz_clear(p);
    mpz_clear(q);
    mpz_clear(n);
    mpz_clear(d);
    mpz_clear(dmp1);
    mpz_clear(dmq1);
    mpz_clear(iqmp);
    mpz_clear(t1);
    mpz_clear(t2);
    mpz_clear(t3);

    return ret;
}
Example #12
0
int32_t main(int32_t argc, char **argv)
{
  int32_t ret_val = HM_OK;
  SOCKADDR_IN addr;

  int32_t cmd_opt;
  /* Randomly select a group for subscription */
  int32_t subs_group = random_num(0, 4);
  int32_t node[2] = {random_num(1,4), random_num(1,4)};

  int32_t pct_type = 0x75010001;
  int32_t pid = 0x00000034;

  extern char *optarg;

  while((cmd_opt = getopt(argc, argv, "l:")) != -1)
  {
    switch(cmd_opt)
    {
    case 'l':
      TRACE_INFO(("Location Index: %s", optarg));
      location_index = atoi(optarg);
      break;
    default:
      printf("\nUsage: %s -l <location_number>", argv[0]);
      break;
    }
  }

  if(location_index == 0)
  {
    TRACE_ERROR(("Did not find Location Index"));
    goto EXIT_LABEL;
  }

  pid = pid | (location_index << 24);
  TRACE_INFO(("PID Assigned: 0x%x", pid));

  /***************************************************************************/
  /* Setup address                               */
  /***************************************************************************/
  memset(&addr, 0, sizeof(SOCKADDR));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(4999);
  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);

  /***************************************************************************/
  /* Open Socket                                 */
  /***************************************************************************/
  sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if(sock_fd < 0)
  {
    TRACE_PERROR(("Error opening socket"));
    goto EXIT_LABEL;
  }

  /***************************************************************************/
  /* Send connect request                             */
  /***************************************************************************/
  ret_val = connect(sock_fd, (SOCKADDR *)&addr, (socklen_t)sizeof(SOCKADDR));
  if(ret_val < 0)
  {
    TRACE_PERROR(("Error connecting to HM."));
    goto EXIT_LABEL;
  }

  /***************************************************************************/
  /* Connected! Go ahead, send an init                     */
  /***************************************************************************/
  init_msg.hdr.msg_id = 1;
  init_msg.hdr.msg_len = sizeof(init_msg);
  init_msg.hdr.msg_type = HM_MSG_TYPE_INIT;
  init_msg.hdr.request = TRUE;
  init_msg.hdr.response_ok = FALSE;

  init_msg.hardware_num = 0;
  init_msg.index = location_index;
  init_msg.keepalive_period = 1000; /* ms */
  init_msg.location_status = TRUE;
  init_msg.service_group_index = location_index;

  TRACE_INFO(("Sending INIT message!"));
  ret_val = send(sock_fd, (char *)&init_msg, sizeof(init_msg), 0);
  if(ret_val != sizeof(init_msg))
  {
    TRACE_PERROR(("Error sending complete message on socket!"));
    goto EXIT_LABEL;
  }
  TRACE_INFO(("INIT Message sent."));

  ret_val = recv(sock_fd, (char *)&init_msg, sizeof(init_msg), 0);
  if(ret_val != sizeof(init_msg))
  {
    TRACE_WARN(("Partial Message Received!"));
    goto EXIT_LABEL;
  }
  TRACE_INFO(("Message response received"));
  if(init_msg.hdr.response_ok == TRUE)
  {
    TRACE_INFO(("Hardware Index is %d", init_msg.hardware_num));
  }

  //TRACE_INFO(("Send Keepalive"));
  //TODO: LATER

  //Send Process UP Notification
  proc_msg.hdr.msg_id = 1;
  proc_msg.hdr.msg_len = sizeof(proc_msg);
  proc_msg.hdr.msg_type = HM_MSG_TYPE_PROCESS_CREATE;
  proc_msg.hdr.request = TRUE;
  proc_msg.hdr.response_ok = FALSE;

  proc_msg.if_offset = 0;
  snprintf(proc_msg.name, sizeof(proc_msg.name), "TEST");
  proc_msg.pid = pid;
  proc_msg.proc_type = pct_type;

  TRACE_INFO(("Sending PROCESS_CREATED message!"));
  ret_val = send(sock_fd, (char *)&proc_msg, sizeof(proc_msg), 0);
  if(ret_val != sizeof(proc_msg))
  {
    TRACE_PERROR(("Error sending complete message on socket!"));
  }
  TRACE_INFO(("PROCESS_CREATED Message sent."));

  ret_val = recv(sock_fd, (char *)&proc_msg, sizeof(proc_msg), 0);
  if(ret_val != sizeof(proc_msg))
  {
    TRACE_WARN(("Partial Message Received!"));
  }
  TRACE_INFO(("Message response received"));

  if(proc_msg.hdr.response_ok == TRUE)
  {
    TRACE_INFO(("Process Create Notification OK"));
  }

  //Send REGISTER for Group
//  TRACE_INFO(("Sending Register for Group %d", subs_group));
  reg_msg = (HM_REGISTER_MSG *)malloc(sizeof(HM_REGISTER_MSG) + 1* sizeof(HM_REGISTER_TLV_CB));
  reg_msg->hdr.msg_id = 1;
  reg_msg->hdr.msg_len = sizeof(HM_REGISTER_MSG) + 2* sizeof(HM_REGISTER_TLV_CB);
  reg_msg->hdr.msg_type = HM_MSG_TYPE_REGISTER;
  reg_msg->hdr.request = TRUE;
  reg_msg->hdr.response_ok = FALSE;

  reg_msg->num_register = 2;
  reg_msg->subscriber_pid = pid;
  reg_msg->type = HM_REG_SUBS_TYPE_PROC;

  tlv = (HM_REGISTER_TLV_CB *)((char *)reg_msg + sizeof(HM_REGISTER_MSG));
  tlv->id = pct_type;

  TRACE_INFO(("Sending PROCESS_REGISTER message!"));
  ret_val = send(sock_fd, (char *)reg_msg, reg_msg->hdr.msg_len, 0);
  if(ret_val != reg_msg->hdr.msg_len)
  {
    TRACE_PERROR(("Error sending complete message on socket!"));
  }


  TRACE_INFO(("Sent Register"));

  //Receive REGISTER Response
  memset((void *)reg_msg, 0, sizeof(reg_msg->hdr.msg_len));

  ret_val = recv(sock_fd, (char *)reg_msg,
          (sizeof(HM_REGISTER_MSG) + 1* sizeof(HM_REGISTER_TLV_CB)), 0);
  if(ret_val != (sizeof(HM_REGISTER_MSG) + 1* sizeof(HM_REGISTER_TLV_CB)))
  {
    TRACE_WARN(("Partial Message Received!"));
  }
  TRACE_INFO(("Register response received"));

  if(reg_msg->hdr.response_ok == TRUE)
  {
    TRACE_INFO(("Register OK"));
  }

  //Send Unregister

  //Receive Unregister Response

  //Send REGISTER for Nodes
//  TRACE_INFO(("Sending Register for Nodes %d, %d", node[0], node[1]));

  //Receive REGISTER Response

  //Send Unregister

  //Receive Unregister Response



  while(1)
  {
    continue;
  }

EXIT_LABEL:
  if (sock_fd != -1)
  {
    close(sock_fd);
    sock_fd = -1;
  }
  return ret_val;
}