/**
 * Initializes the udp receive service
 *@param addr IP address of the server
 *@param port  Port the server will send to
 *@return -1 on error, 0 on success
 */
int init_udp_conn(const char *addr, const char *port){
  return init_udp_client(addr, port);
}
Example #2
0
// main function
int main(int argc, char *argv[])
{
  char ch, *p, *argv0, *faddr, *taddr, buf[BUFSIZ];
  int s, fid, tid, from, to, pipe_nr;
  uint16_t rulenum;

  int usage_type;

  float time, dummy[PARAMETERS_UNUSED], bandwidth, delay, lossrate;
  FILE *fd;
  timer_handle *timer;
  int loop_count=0;

  int direction=DIRECTION_BOTH;

  struct timeval tp_begin, tp_end;
  
  int i, my_id;
  in_addr_t IP_addresses[MAX_NODES];
  char IP_char_addresses[MAX_NODES*IP_ADDR_SIZE];
  int node_number;

  int j, offset_num, rule_count, next_hop_id, rule_num;
  float time_period, next_time;
  qomet_param param_table[MAX_RULE_NUM];
  qomet_param param_over_read;
  unsigned int over_read;
  char broadcast_address[IP_ADDR_SIZE];

  usage_type = 1;
  
  // initializing the param_table
  memset(param_table, 0, sizeof(qomet_param)*MAX_RULE_NUM);
  memset(&param_over_read, 0, sizeof(qomet_param));
  over_read = 0;
  rule_count = 0;
  next_time = 0.0;
  next_hop_id = 0;
  rule_num = -1;

  // init variables to invalid values
  argv0 = argv[0];
  fd = NULL;

  faddr = taddr = NULL;
  fid = tid = pipe_nr = -1;
  rulenum = 65535;

  my_id = -1;
  node_number = -1;
  time_period = -1;
  strncpy(broadcast_address, "255.255.255.255", IP_ADDR_SIZE);

    // -----------------codes are added by ntrtrung---------------------//
  //---------------------------------Init UDP client--------------//
  int server_fd_do_action;
  int server_do_action_connected;
  server_do_action_connected = 0;
  server_fd_do_action = init_udp_client();
  if(server_fd_do_action >0)
	server_do_action_connected = 1;
  else
	printf("\nCannot Init UDP Client\n");
  //----------------------------------------------------------------------//
  
  if(argc<2)
    {
      WARNING("No arguments provided");
      usage(argv0);
      exit(1);
    }

  // parse command-line options
  while((ch = getopt(argc, argv, "q:f:F:t:T:r:p:d:i:s:m:b:")) != -1)
    {
      switch(ch)
	{
	  //QOMET output file
	case 'q':
	  if((fd = fopen(optarg, "r")) == NULL) 
	    {
	      WARNING("Could not open QOMET output file '%s'", optarg);
	      exit(1);
	    }
	  break;

	  /////////////////////////////////////////////
	  // Usage (1) parameters
	  /////////////////////////////////////////////
	  // from_node_id
	case 'f':
	  fid = strtol(optarg, &p, 10);
	  if((*optarg == '\0') || (*p != '\0'))
	    {
	      WARNING("Invalid from_node_id '%s'", optarg);
	      exit(1);
	    }
	  break;

	  // IP address of from_node
	case 'F':
	  faddr = optarg;
	  break;

	  // to_node_id
	case 't':
	  tid = strtol(optarg, &p, 10);
	  if((*optarg == '\0') || (*p != '\0'))
	    {
	      WARNING("Invalid to_node_id '%s'", optarg);
	      exit(1);
	    }
	  break;

	  // IP address of to_node
	case 'T':
	  taddr = optarg;
	  break;

	  // rule number for dummynet configuration
	case 'r':
	  rulenum = strtol(optarg, &p, 10);
	  if((*optarg == '\0') || (*p != '\0'))
	    {
	      WARNING("Invalid rule_number '%s'", optarg);
	      exit(1);
	    }
	  break;

	  // pipe number for dummynet configuration
	case 'p':
	  pipe_nr = strtol(optarg, &p, 10);
	  if((*optarg == '\0') || (*p != '\0'))
	    {
	      WARNING("Invalid pipe_number '%s'", optarg);
	      exit(1);
	    }
	  break;

	  // direction option for dummynet configuration
	case 'd':
	  if(strcmp(optarg, "in")==0)
	    {
	      direction = DIRECTION_IN;
	    }
	  else if(strcmp(optarg, "out")==0)
	    {
	      direction = DIRECTION_OUT;
	    }
	  else
	    {
	      WARNING("Invalid direction '%s'", optarg);
	      exit(1);
	    }
	  break;

	  /////////////////////////////////////////////
	  // Usage (2) parameters
	  /////////////////////////////////////////////
	  // current node ID
	case 'i':
	  my_id = strtol(optarg, NULL, 10);
	  break;

	  // settings file
	case 's':
	  usage_type = 2;
	  if((node_number = read_settings(optarg, IP_addresses, MAX_NODES)) < 1)
	    {
	      WARNING("Settings file '%s' is invalid", optarg);
	      exit(1);
	    }
	  for(i = 0; i < node_number; i++)
	    snprintf(IP_char_addresses + i * IP_ADDR_SIZE, IP_ADDR_SIZE, 
		     "%hu.%hu.%hu.%hu",
		     *(((uint8_t *)&IP_addresses[i]) + 0),
		     *(((uint8_t *)&IP_addresses[i]) + 1),
		     *(((uint8_t *)&IP_addresses[i]) + 2),
		     *(((uint8_t *)&IP_addresses[i]) + 3));
	  break;

	  // time interval between settings
	case 'm':
	  // check if conversion was performed (we assume a time
	  // period of 0 is also invalid)
	  if ((time_period = strtod(optarg,NULL)) == 0)
	    {
	      WARNING("Invalid time period");
	      exit(1);
	    }
	  break;

	case 'b':
	  strncpy(broadcast_address, optarg, IP_ADDR_SIZE);
	  break;

	  // help output
	case '?':
	default:
	  usage(argv0);
	  exit(1);
	}
    }

  if((my_id<FIRST_NODE_ID) || (my_id>=node_number+FIRST_NODE_ID))
  {
    WARNING("Invalid ID '%d'. Valid range is [%d, %d]", my_id,
	    FIRST_NODE_ID, node_number+FIRST_NODE_ID-1);
    exit(1);
  }

  // update argument-related counter and pointer
  argc -= optind;
  argv += optind;

  // check that all the required arguments were provided
  if(fd == NULL)
    {
      WARNING("No QOMET data file was provided");
      usage(argv0);
      exit(1);
    }
  
  if((usage_type == 1) &&
     ((fid == -1) || (faddr == NULL) ||
      (tid == -1) || (taddr == NULL) || (pipe_nr == -1) || (rulenum == 65535)))
    {
      WARNING("Insufficient arguments were provided for usage (1)");
      usage(argv0);
      fclose(fd);
      exit(1);
    }
  else if ((my_id == -1) || (node_number == -1) || (time_period == -1))
    {
      WARNING("Insufficient arguments were provided for usage (2)");
      usage(argv0);
      fclose(fd);
      exit(1);
    }


  // initialize timer
  DEBUG("Initialize timer...");
  if((timer = timer_init()) == NULL)
    {
      WARNING("Could not initialize timer");
      exit(1);
    }

  // open control socket
  DEBUG("Open control socket...");
  if((s = get_socket()) < 0)
    {
      WARNING("Could not open control socket (requires root priviledges)\n");
      exit(1);
    }

  // add pipe to dummynet in normal manner
  if(usage_type==1)
    {
      // get rule
      //get_rule(s, 123);

      INFO("Add rule #%d with pipe #%d from %s to %s", 
	   rulenum, pipe_nr, faddr, taddr);
      if(add_rule(s, rulenum, pipe_nr, faddr, taddr, direction) < 0)
	{
	  WARNING("Could not add rule #%d with pipe #%d from %s to %s", 
		  rulenum, pipe_nr, faddr, taddr);
	  exit(1);
	}

      // get rule
      //get_rule(s, 123);

      //exit(2);
    }
  else // usage (2) => sets of rules must be added
    {
      // add rule & pipe for unicast traffic _to_ j
      for(j = FIRST_NODE_ID; j < node_number + FIRST_NODE_ID; j++)
	{
	  if(j == my_id)
	    continue;

	  offset_num = j; 

	  INFO("Node %d: Add rule #%d with pipe #%d to destination %s", 
	       my_id, MIN_PIPE_ID_OUT+offset_num, MIN_PIPE_ID_OUT+offset_num, 
	       IP_char_addresses+(j-FIRST_NODE_ID)*IP_ADDR_SIZE);
	  if(add_rule(s, MIN_PIPE_ID_OUT+offset_num, MIN_PIPE_ID_OUT+offset_num,
		      "any", IP_char_addresses+(j-FIRST_NODE_ID)*IP_ADDR_SIZE, 
		      DIRECTION_OUT) < 0)
	    {
	      WARNING("Node %d: Could not add rule #%d with pipe #%d to \
destination %s", my_id, MIN_PIPE_ID_OUT + offset_num, 
		      MIN_PIPE_ID_OUT + offset_num, 
		      IP_char_addresses + (j-FIRST_NODE_ID) * IP_ADDR_SIZE);
	      exit(1);
	    }
	}// end for loop


      // add rule & pipe for broadcast traffic _from_ j
      for(j = FIRST_NODE_ID; j < node_number + FIRST_NODE_ID; j++)
	{
	  if(j == my_id)
	    continue;

	  offset_num = j; 

	  INFO("Node %d: Add rule #%d with pipe #%d to destination %s",
	       my_id, MIN_PIPE_ID_IN_BCAST + offset_num, 
	       MIN_PIPE_ID_IN_BCAST + offset_num, broadcast_address);
	  if(add_rule(s, MIN_PIPE_ID_IN_BCAST + offset_num, 
		      MIN_PIPE_ID_IN_BCAST + offset_num, 
		      IP_char_addresses+(j-FIRST_NODE_ID)*IP_ADDR_SIZE,
		      broadcast_address, DIRECTION_IN) < 0)
	    {
	      WARNING("Node %d: Could not add rule #%d with pipe #%d from %s to \
destination %s", my_id, MIN_PIPE_ID_IN_BCAST + offset_num, 
		      MIN_PIPE_ID_IN_BCAST + offset_num, 
		      IP_char_addresses+(j-FIRST_NODE_ID)*IP_ADDR_SIZE, broadcast_address);
	      exit(1);
	    }
	}
    }

  // get the time at the beginning of the experiment
  gettimeofday(&tp_begin, NULL);

  // do this only for usage (1)
  if(usage_type==1)
    {
#ifdef OLSR_ROUTING
      // get the next hop ID to find correct configuration lines
      if( (next_hop_id = get_next_hop_id(tid, direction)) == ERROR )
	{
	  WARNING("Time=%.2f: Couldn't locate the next hop for destination node %i, direction=%i", time, tid, direction);
	  exit(1);
	} 
      else
	INFO("Time=%.2f: Next_hop=%i for destination=%i", time, 
	     next_hop_id, tid);
#else
      next_hop_id = tid;
#endif
    }

  // read input data from QOMET output file
  INFO("Reading QOMET data from file...");
  
  
  //--------codes are added by ntrtrung--------//
  next_time = 0;
  float this_time,last_time_for_do_action,last_time_for_broad_envi;
  last_time_for_do_action=-1;
  last_time_for_broad_envi=-1;
  //--------codes are added by ntrtrung--------//
  
  while(fgets(buf, BUFSIZ, fd) != NULL)
    {
      if(sscanf(buf, "%f %d %f %f %f %d %f %f %f %f %f %f "
		"%f %f %f %f %f %f %f", &time, &from, &dummy[0],
		&dummy[1], &dummy[2], &to, &dummy[3], &dummy[4],
		&dummy[5],  &dummy[6], &dummy[7], &dummy[8],
		&dummy[9], &dummy[10], &dummy[11], &bandwidth, 
		&lossrate, &delay, &dummy[12]) != PARAMETERS_TOTAL)
	{
	  INFO("Skipped non-parametric line");
	  continue;
	}
	//--------codes are added by ntrtrung--------//
     this_time = time;
	 if(server_do_action_connected == 1)
	 {
		if(this_time != last_time_for_do_action && server_fd_do_action > 0)
		{
			if( SendDataToServer(&this_time,server_fd_do_action,DO_ACTION_SERVER_PORT) == -1)
			{
				server_do_action_connected = 0;
			}
			else
		 	   printf("\ntime is sent to do action server:%f \n",this_time);
			if( SendDataToServer(&this_time,server_fd_do_action,BROAD_ENVI_SERVER_PORT) == -1)
			{
				server_do_action_connected = 0;
			}
			else
			   printf("\ntime is sent to broad envi server:%f \n",this_time);

			last_time_for_do_action = this_time;
		}
	 }
	
	 //--------codes are added by ntrtrung--------//
	 
	 
      if(usage_type==1)
	{
	  // check whether the from_node and to_node from the file
	  // match the user selected ones
	  if((from == fid) && (to == next_hop_id))
	    {
	      
#ifdef PREDEFINED_EXPERIMENT
	      bandwidth=BANDWIDTH;
	      delay=DELAY;
	      lossrate=PACKET_LOSS_RATE;
#endif
	      // print current configuration info
	      INFO("* Wireconf configuration (time=%.2f s): bandwidth=%.2fbit/s \
loss_rate=%.4f delay=%.4f ms", time, bandwidth, lossrate, delay);
	      
	      // if this is the first operation we reset the timer
	      // Note: it is assumed time always equals 0.0 for the first line
	      if(time == 0.0)
		timer_reset(timer);
	      else 
		{
		  // wait for for the next timer event
		  if(timer_wait(timer, time * 1000000) < 0)
		    {
		      WARNING("Timer deadline missed at time=%.2f s", time);
		      //exit(1); // NOT NEEDED ANYMORE!!!!
		    }
		}
	      
#ifdef OLSR_ROUTING
	      // get the next hop ID to find correct configuration lines
	      if((next_hop_id = get_next_hop_id(tid, direction)) == ERROR)
		{
		  WARNING("Time=%.2f: Couldn't locate the next hop for destination node %i,direction=%i", time, tid, direction);
		  exit(1);
		}
	      else
		INFO("Time=%.2f: Next_hop=%i for destination=%i", time, next_hop_id, tid);
#endif

	      // prepare adjusted values:
	      // 1. if packet size is known bandwidth could be adjusted:
	      //   multiplication factor 1.0778 was computed for 400 byte datagrams
	      //   because of 28 bytes header (428/400=1.07)
	      //   however we consider bandwidth at Ethernet level, therefore we
	      //   don't multiply here but when plotting results
	      //   Note: ip_dummynet.h says that bandwidth is in bytes/tick
	      //         but we seem to obtain correct values using bits/second
	      bandwidth = (int)round(bandwidth);// * 2.56);
	      
	      // 2. no adjustment necessary for delay expressed in ms
	      delay = (int) round(delay);//(delay / 2);
	      
	      // 3. loss rate probability must be extended to 
	      //    2^31-1 (=0x7fffffff) range, equivalent to 100% loss
	      lossrate = (int)round(lossrate * 0x7fffffff);
	      
	      // do configure pipe
	      configure_pipe(s, pipe_nr, bandwidth, delay, lossrate);
	      
	      // increase loop counter
	      loop_count++;
	      
#ifdef PREDEFINED_EXPERIMENT
	      if(loop_count>LOOP_COUNT)
		break;
#endif
	    }
	}
      else // usage (2) => manage multiple rules
	{
	  // add rules regarding next deadline to parameter table
	  if(time == next_time)