⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nodes.c

📁 wifi 无线网络路由协议OLSR linux下C代码
💻 C
📖 第 1 页 / 共 2 页
字号:
    }  memcpy(&tmp_hna->net, net, ipsize);  memcpy(&tmp_hna->mask, mask, ipsize);  tmp_hna->next = &inserted->hna;  tmp_hna->prev = &inserted->hna;  inserted->hna.next = tmp_hna;  inserted->hna.prev = tmp_hna;  update_nodes_list(inserted);  return 2;}/** *Add the MPR mpr to the node nodes selected MPRs. *Nodes are NOT added if they are not yet registered! * *@param node the node that has chosen an MPR *@param mpr the MPR choosen by node *@return negative if node already registered or node not found */intadd_mpr(union olsr_ip_addr *node, union olsr_ip_addr *mpr, struct timeval *tmp_timer){  struct node *tmp_nodes;  struct mpr *mprs;  struct mpr *tmp_mpr;  for(tmp_nodes = nodes.next;      tmp_nodes != &nodes;      tmp_nodes = tmp_nodes->next)    {      if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)	{	  for(mprs = tmp_nodes->mpr.next;	      mprs != &tmp_nodes->mpr;	      mprs = mprs->next)	    {	      if(memcmp(&mprs->addr, mpr, ipsize) == 0)		  return 0;	    }	  //printf("Adding MPR %s to ", ip_to_string(mpr));	  //printf("%s\n", ip_to_string(node));	  /* Add mpr */	  if((tmp_mpr = malloc(sizeof(struct mpr))) == 0)	    {	      fprintf(stderr, "OUT OF MEMORY\n");	      exit(1);	    }	  memcpy(&tmp_mpr->addr, mpr, ipsize);	  gettimeofday(&now, (struct timezone *)NULL);	  timeradd(&now, tmp_timer, &tmp_mpr->timer);	  /* queue */	  tmp_nodes->mpr.next->prev = tmp_mpr;	  tmp_mpr->next = tmp_nodes->mpr.next;	  tmp_nodes->mpr.next = tmp_mpr;	  tmp_mpr->prev = &tmp_nodes->mpr;	  update_nodes_list(tmp_nodes);	  return 1; 	}    }  return 1;}intremove_node(struct node *node){  struct hna *tmp_hna, *tmp_hna2;  struct mid *tmp_mid, *tmp_mid2;  struct mpr *tmp_mpr, *tmp_mpr2;  printf("Remove node %s\n", ip_to_string(&node->addr));  tmp_hna = node->hna.next;  while(tmp_hna != &node->hna)    {      tmp_hna2 = tmp_hna;      tmp_hna = tmp_hna->next;      free(tmp_hna2);    }  tmp_mpr = node->mpr.next;  while(tmp_mpr != &node->mpr)    {      tmp_mpr2 = tmp_mpr;      tmp_mpr = tmp_mpr->next;      free(tmp_mpr2);    }  tmp_mid = node->mid.next;  while(tmp_mid != &node->mid)    {      tmp_mid2 = tmp_mid;      tmp_mid = tmp_mid->next;      free(tmp_mid2);    }    /* Gemove form GUI */  remove_nodes_list(&node->addr);    /* Dequeue */  node->prev->next = node->next;  node->next->prev = node->prev;    free(node);  return 1;  }/* * Remove based on address */intremove_node_addr(union olsr_ip_addr *node){  struct node *tmp_nodes;  struct hna *tmp_hna, *tmp_hna2;  struct mid *tmp_mid, *tmp_mid2;  struct mpr *tmp_mpr, *tmp_mpr2;  printf("Remove node %s\n", ip_to_string(node));  tmp_nodes = nodes.next;  while(tmp_nodes != &nodes)    {      if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)	{	  printf("(2)Deleting node %s\n", ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr));	  tmp_hna = tmp_nodes->hna.next;	  while(tmp_hna != &tmp_nodes->hna)	    {	      tmp_hna2 = tmp_hna;	      tmp_hna = tmp_hna->next;	      free(tmp_hna2);	    }	  tmp_mpr = tmp_nodes->mpr.next;	  while(tmp_mpr != &tmp_nodes->mpr)	    {	      tmp_mpr2 = tmp_mpr;	      tmp_mpr = tmp_mpr->next;	      free(tmp_mpr2);	    }	  tmp_mid = tmp_nodes->mid.next;	  while(tmp_mid != &tmp_nodes->mid)	    {	      tmp_mid2 = tmp_mid;	      tmp_mid = tmp_mid->next;	      free(tmp_mid2);	    }	  /* Gemove form GUI */	  remove_nodes_list(&tmp_nodes->addr);	  /* Dequeue */	  tmp_nodes->prev->next = tmp_nodes->next;	  tmp_nodes->next->prev = tmp_nodes->prev;	  free(tmp_nodes);	  return 1;	}      tmp_nodes = tmp_nodes->next;    }  return 0;}struct node *find_node(char *ip){  struct node *tmp_nodes;  for(tmp_nodes = nodes.next;      tmp_nodes != &nodes;      tmp_nodes = tmp_nodes->next)    {      if(strcmp(ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr), ip) == 0)	return tmp_nodes;    }  return NULL;}struct node *find_node_t(union olsr_ip_addr *ip){  struct node *tmp_nodes;  for(tmp_nodes = nodes.next;      tmp_nodes != &nodes;      tmp_nodes = tmp_nodes->next)    {      if(memcmp(&tmp_nodes->addr, ip, ipsize) == 0)	return tmp_nodes;    }  return 0;}/* *Remove timed out nodes */ginttime_out_nodes(gpointer data){  struct node *tmp_nodes;  struct node *node_to_delete;  /* Wait before starting timing out */  if(timeouts)    {      timeouts--;      //printf("Waiting...\n");      return 1;    }  //printf("Timing out nodes...\n");  gettimeofday(&now, (struct timezone *)NULL);  tmp_nodes = nodes.next;  while(tmp_nodes != &nodes)    {      //printf("%s: %6d < %6d\n", ip_to_string(&tmp_nodes->addr), tmp_nodes->timer.tv_sec, now.tv_sec);      if(timercmp(&tmp_nodes->timer,&now,<))	{	  printf("Node %s timed out...\n", ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr));	  node_to_delete = tmp_nodes;	  tmp_nodes = tmp_nodes->next;	  remove_nodes_list(&node_to_delete->addr);	  remove_node(node_to_delete);	}       else	tmp_nodes = tmp_nodes->next;    }  return 1;}/** *Timeout MPRs for a given node. Only called when user *is to see the registered MPRs of the node. *@param node the node whom MPRs should be timed out *@return negative if node not found */inttime_out_mprs(union olsr_ip_addr *node){  struct node *tmp_nodes;  struct mpr *mpr_to_delete;  struct mpr *tmp_mpr;  gettimeofday(&now, (struct timezone *)NULL);  /* W A R N I N G !   *   * THIS ALGORITHM HAS NOT BEEN TESTED PROPERLY!!!!!!   * -Andreas   */  for(tmp_nodes = nodes.next;      tmp_nodes != &nodes;      tmp_nodes = tmp_nodes->next)    {      if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)	{	  tmp_mpr = tmp_nodes->mpr.next;	 	  while(tmp_mpr != &tmp_nodes->mpr)	    {	      if(timercmp(&tmp_mpr->timer,&now,<))		{		  printf("MPR %s OF NODE ", ip_to_string((union olsr_ip_addr *)&tmp_mpr->addr));		  printf("%s TIMIED OUT ", ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr));fflush(stdout);		  mpr_to_delete = tmp_mpr;		  tmp_mpr = tmp_mpr->next;		  /* Dequeue */		  mpr_to_delete->next->prev = mpr_to_delete->prev;		  mpr_to_delete->prev->next = mpr_to_delete->next;		  /* Delete */		  free(mpr_to_delete);		}	      else		tmp_mpr = tmp_mpr->next;	    }	  return 1;	}    }  return 0;}voidinit_timer(olsr_u32_t time_value, struct timeval *hold_timer){   olsr_u16_t  time_value_sec=0;  olsr_u16_t  time_value_msec=0;  time_value_sec=time_value/1000;  time_value_msec=time_value-(time_value_sec*1000);  hold_timer->tv_sec=time_value_sec;  hold_timer->tv_usec=time_value_msec*1000;   }/** *Function that converts a mantissa/exponent 8bit value back *to double as described in RFC3626: * * value = C*(1+a/16)*2^b [in seconds] * *  where a is the integer represented by the four highest bits of the *  field and b the integer represented by the four lowest bits of the *  field. * *@param me the 8 bit mantissa/exponen value * *@return a double value */doubleme_to_double(olsr_u8_t me){  int a = me>>4;  int b = me - a*16;  return (double)(VTIME_SCALE_FACTOR*(1+(double)a/16)*(double)pow(2,b));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -