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

📄 ifnet.c

📁 wifi 无线网络路由协议OLSR linux下C代码
💻 C
📖 第 1 页 / 共 2 页
字号:
                                  0, NULL);    }  else    {      olsr_remove_scheduler_event(&olsr_output_lq_hello, Int,                                  IntConf->cnf->hello_params.emission_interval,                                  0, NULL);      olsr_remove_scheduler_event(&olsr_output_lq_tc, Int,                                  IntConf->cnf->tc_params.emission_interval,                                  0, NULL);    }  olsr_remove_scheduler_event(&generate_mid, Int,                              IntConf->cnf->mid_params.emission_interval,                              0, NULL);  olsr_remove_scheduler_event(&generate_hna, Int,                              IntConf->cnf->hna_params.emission_interval,                              0, NULL);  net_remove_buffer(Int);  IntConf->configured = 0;  IntConf->interf = NULL;  closesocket(Int->olsr_socket);  remove_olsr_socket(Int->olsr_socket, &olsr_input);  free(Int->int_name);  free(Int);  if (ifnet == NULL && !olsr_cnf->allow_no_interfaces)  {    OLSR_PRINTF(1, "No more active interfaces - exiting.\n");    olsr_cnf->exit_value = EXIT_FAILURE;    CallSignalHandler();  }}int add_hemu_if(struct olsr_if *iface){  struct interface *ifp;  union olsr_ip_addr null_addr;  olsr_u32_t addr[4];  if(!iface->host_emul)    return -1;  ifp = olsr_malloc(sizeof (struct interface), "Interface update 2");  memset(ifp, 0, sizeof (struct interface));  iface->configured = OLSR_TRUE;  iface->interf = ifp;  ifp->is_hcif = OLSR_TRUE;  ifp->int_name = olsr_malloc(strlen("hcif01") + 1, "Interface update 3");  ifp->int_metric = 0;  strcpy(ifp->int_name, "hcif01");  OLSR_PRINTF(1, "Adding %s(host emulation):\n", ifp->int_name);  OLSR_PRINTF(1, "       Address:%s\n", olsr_ip_to_string(&iface->hemu_ip));  OLSR_PRINTF(1, "       NB! This is a emulated interface\n       that does not exist in the kernel!\n");  ifp->int_next = ifnet;  ifnet = ifp;  memset(&null_addr, 0, olsr_cnf->ipsize);  if(COMP_IP(&null_addr, &olsr_cnf->main_addr))    {      COPY_IP(&olsr_cnf->main_addr, &iface->hemu_ip);      OLSR_PRINTF(1, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));    }  ifp->int_mtu = OLSR_DEFAULT_MTU;  ifp->int_mtu -= (olsr_cnf->ip_version == AF_INET6) ?    UDP_IPV6_HDRSIZE : UDP_IPV4_HDRSIZE;  /* Set up buffer */  net_add_buffer(ifp);  if(olsr_cnf->ip_version == AF_INET)    {      struct sockaddr_in sin;      memset(&sin, 0, sizeof(sin));      sin.sin_family = AF_INET;      sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);      sin.sin_port = htons(10150);      /* IP version 4 */      ifp->ip_addr.v4 = iface->hemu_ip.v4;      memcpy(&((struct sockaddr_in *)&ifp->int_addr)->sin_addr, &iface->hemu_ip, olsr_cnf->ipsize);            /*       *We create one socket for each interface and bind       *the socket to it. This to ensure that we can control       *on what interface the message is transmitted       */            ifp->olsr_socket = gethemusocket(&sin);            if (ifp->olsr_socket < 0)	{	  fprintf(stderr, "Could not initialize socket... exiting!\n\n");	  exit(1);	}    }  else    {      /* IP version 6 */      memcpy(&ifp->ip_addr, &iface->hemu_ip, olsr_cnf->ipsize);#if 0            /*       *We create one socket for each interface and bind       *the socket to it. This to ensure that we can control       *on what interface the message is transmitted       */            ifp->olsr_socket = gethcsocket6(&addrsock6, bufspace, ifp->int_name);            join_mcast(ifp, ifp->olsr_socket);            if (ifp->olsr_socket < 0)	{	  fprintf(stderr, "Could not initialize socket... exiting!\n\n");	  exit(1);	}      #endif    }  /* Send IP as first 4/16 bytes on socket */  memcpy(addr, iface->hemu_ip.v6.s6_addr, olsr_cnf->ipsize);  addr[0] = htonl(addr[0]);  addr[1] = htonl(addr[1]);  addr[2] = htonl(addr[2]);  addr[3] = htonl(addr[3]);  if(send(ifp->olsr_socket, (char *)addr, olsr_cnf->ipsize, 0) != (int)olsr_cnf->ipsize)    {      fprintf(stderr, "Error sending IP!");    }      /* Register socket */  add_olsr_socket(ifp->olsr_socket, &olsr_input_hostemu);  if (olsr_cnf->lq_level == 0)    {      olsr_register_scheduler_event(&generate_hello,                                     ifp,                                     iface->cnf->hello_params.emission_interval,                                     0,                                     NULL);      olsr_register_scheduler_event(&generate_tc,                                     ifp,                                     iface->cnf->tc_params.emission_interval,                                    0,                                     NULL);    }  else    {      olsr_register_scheduler_event(&olsr_output_lq_hello,                                     ifp,                                     iface->cnf->hello_params.emission_interval,                                     0,                                     NULL);      olsr_register_scheduler_event(&olsr_output_lq_tc,                                     ifp,                                     iface->cnf->tc_params.emission_interval,                                    0,                                     NULL);    }  olsr_register_scheduler_event(&generate_mid, 				ifp, 				iface->cnf->mid_params.emission_interval,				0, 				NULL);  olsr_register_scheduler_event(&generate_hna, 				ifp, 				iface->cnf->hna_params.emission_interval,				0, 				NULL);  /* Recalculate max jitter */  if((olsr_cnf->max_jitter == 0) ||      ((iface->cnf->hello_params.emission_interval / 4) < olsr_cnf->max_jitter))    olsr_cnf->max_jitter = iface->cnf->hello_params.emission_interval / 4;  /* Recalculate max topology hold time */  if(olsr_cnf->max_tc_vtime < iface->cnf->tc_params.emission_interval)    olsr_cnf->max_tc_vtime = iface->cnf->tc_params.emission_interval;  ifp->hello_etime = iface->cnf->hello_params.emission_interval;  ifp->valtimes.hello = double_to_me(iface->cnf->hello_params.validity_time);  ifp->valtimes.tc = double_to_me(iface->cnf->tc_params.validity_time);  ifp->valtimes.mid = double_to_me(iface->cnf->mid_params.validity_time);  ifp->valtimes.hna = double_to_me(iface->cnf->hna_params.validity_time);  return 1;}int chk_if_changed(struct olsr_if *IntConf){  struct interface *Int;  struct InterfaceInfo Info;  int Res;  int IsWlan;  union olsr_ip_addr OldVal, NewVal;  struct sockaddr_in *AddrIn;  if (olsr_cnf->ip_version == AF_INET6)  {    fprintf(stderr, "IPv6 not supported by chk_if_changed()!\n");    return 0;  }#ifdef DEBUG  OLSR_PRINTF(3, "Checking if %s is set down or changed\n", IntConf->name);#endif  Int = IntConf->interf;  if (GetIntInfo(&Info, IntConf->name) < 0)  {    RemoveInterface(IntConf);    return 1;  }  Res = 0;  IsWlan = IsWireless(IntConf->name);  if (IsWlan < 0)    IsWlan = 1;  if (Int->is_wireless != IsWlan)  {    OLSR_PRINTF(1, "\tLAN/WLAN change: %d -> %d.\n", Int->is_wireless, IsWlan);    Int->is_wireless = IsWlan;    if (IntConf->cnf->weight.fixed)      Int->int_metric = IntConf->cnf->weight.value;    else      Int->int_metric = Info.Metric;    Res = 1;  }  if (Int->int_mtu != Info.Mtu)  {    OLSR_PRINTF(1, "\tMTU change: %d -> %d.\n", (int)Int->int_mtu,                Info.Mtu);    Int->int_mtu = Info.Mtu;    net_remove_buffer(Int);    net_add_buffer(Int);    Res = 1;  }  OldVal.v4 = ((struct sockaddr_in *)&Int->int_addr)->sin_addr.s_addr;  NewVal.v4 = Info.Addr;#ifdef DEBUG  OLSR_PRINTF(3, "\tAddress: %s\n", olsr_ip_to_string(&NewVal));#endif  if (NewVal.v4 != OldVal.v4)  {    OLSR_PRINTF(1, "\tAddress change.\n");    OLSR_PRINTF(1, "\tOld: %s\n", olsr_ip_to_string(&OldVal));    OLSR_PRINTF(1, "\tNew: %s\n", olsr_ip_to_string(&NewVal));    Int->ip_addr.v4 = NewVal.v4;    AddrIn = (struct sockaddr_in *)&Int->int_addr;    AddrIn->sin_family = AF_INET;    AddrIn->sin_port = 0;    AddrIn->sin_addr.s_addr = NewVal.v4;    if (olsr_cnf->main_addr.v4 == OldVal.v4)    {      OLSR_PRINTF(1, "\tMain address change.\n");      olsr_cnf->main_addr.v4 = NewVal.v4;    }    Res = 1;  }  else    OLSR_PRINTF(3, "\tNo address change.\n");  OldVal.v4 = ((struct sockaddr_in *)&Int->int_netmask)->sin_addr.s_addr;  NewVal.v4 = Info.Mask;#ifdef DEBUG  OLSR_PRINTF(3, "\tNetmask: %s\n", olsr_ip_to_string(&NewVal));#endif  if (NewVal.v4 != OldVal.v4)  {    OLSR_PRINTF(1, "\tNetmask change.\n");    OLSR_PRINTF(1, "\tOld: %s\n", olsr_ip_to_string(&OldVal));    OLSR_PRINTF(1, "\tNew: %s\n", olsr_ip_to_string(&NewVal));    AddrIn = (struct sockaddr_in *)&Int->int_netmask;    AddrIn->sin_family = AF_INET;    AddrIn->sin_port = 0;    AddrIn->sin_addr.s_addr = NewVal.v4;    Res = 1;  }  else    OLSR_PRINTF(3, "\tNo netmask change.\n");  OldVal.v4 = ((struct sockaddr_in *)&Int->int_broadaddr)->sin_addr.s_addr;  NewVal.v4 = Info.Broad;#ifdef DEBUG  OLSR_PRINTF(3, "\tBroadcast address: %s\n", olsr_ip_to_string(&NewVal));#endif  if (NewVal.v4 != OldVal.v4)  {    OLSR_PRINTF(1, "\tBroadcast address change.\n");    OLSR_PRINTF(1, "\tOld: %s\n", olsr_ip_to_string(&OldVal));    OLSR_PRINTF(1, "\tNew: %s\n", olsr_ip_to_string(&NewVal));    AddrIn = (struct sockaddr_in *)&Int->int_broadaddr;    AddrIn->sin_family = AF_INET;    AddrIn->sin_port = 0;    AddrIn->sin_addr.s_addr = NewVal.v4;    Res = 1;  }  else    OLSR_PRINTF(3, "\tNo broadcast address change.\n");  if (Res != 0)    run_ifchg_cbs(Int, IFCHG_IF_UPDATE);  return Res;}int chk_if_up(struct olsr_if *IntConf, int DebugLevel __attribute__((unused))){  struct InterfaceInfo Info;  struct interface *New;  union olsr_ip_addr NullAddr;  unsigned int AddrSockAddr;  int IsWlan;  struct sockaddr_in *AddrIn;    if (olsr_cnf->ip_version == AF_INET6)  {    fprintf(stderr, "IPv6 not supported by chk_if_up()!\n");    return 0;  }  if (GetIntInfo(&Info, IntConf->name) < 0)    return 0;  New = olsr_malloc(sizeof (struct interface), "Interface 1");  New->gen_properties = NULL;  AddrIn = (struct sockaddr_in *)&New->int_addr;  AddrIn->sin_family = AF_INET;  AddrIn->sin_port = 0;  AddrIn->sin_addr.s_addr = Info.Addr;  AddrIn = (struct sockaddr_in *)&New->int_netmask;  AddrIn->sin_family = AF_INET;  AddrIn->sin_port = 0;  AddrIn->sin_addr.s_addr = Info.Mask;  AddrIn = (struct sockaddr_in *)&New->int_broadaddr;  AddrIn->sin_family = AF_INET;  AddrIn->sin_port = 0;  AddrIn->sin_addr.s_addr = Info.Broad;  if (IntConf->cnf->ipv4_broadcast.v4 != 0)    AddrIn->sin_addr.s_addr = IntConf->cnf->ipv4_broadcast.v4;  New->int_flags = 0;  New->is_hcif = OLSR_FALSE;  New->int_mtu = Info.Mtu;  New->int_name = olsr_malloc(strlen (IntConf->name) + 1, "Interface 2");  strcpy(New->int_name, IntConf->name);  IsWlan = IsWireless(IntConf->name);  if (IsWlan < 0)    IsWlan = 1;  New->is_wireless = IsWlan;  if (IntConf->cnf->weight.fixed)    New->int_metric = IntConf->cnf->weight.value;  else    New->int_metric = Info.Metric;  New->olsr_seqnum = random() & 0xffff;  New->ttl_index = 0;      OLSR_PRINTF(1, "\tInterface %s set up for use with index %d\n\n",              IntConf->name, New->if_index);        OLSR_PRINTF(1, "\tMTU: %d\n", New->int_mtu);  OLSR_PRINTF(1, "\tAddress: %s\n", sockaddr_to_string(&New->int_addr));  OLSR_PRINTF(1, "\tNetmask: %s\n", sockaddr_to_string(&New->int_netmask));  OLSR_PRINTF(1, "\tBroadcast address: %s\n",              sockaddr_to_string(&New->int_broadaddr));  New->ip_addr.v4 =    ((struct sockaddr_in *)&New->int_addr)->sin_addr.s_addr;        New->if_index = Info.Index;  OLSR_PRINTF(3, "\tKernel index: %08x\n", New->if_index);  AddrSockAddr = addrsock.sin_addr.s_addr;  addrsock.sin_addr.s_addr = New->ip_addr.v4;  New->olsr_socket = getsocket((struct sockaddr *)&addrsock,                               127 * 1024, New->int_name);        addrsock.sin_addr.s_addr = AddrSockAddr;  if (New->olsr_socket < 0)  {    fprintf(stderr, "Could not initialize socket... exiting!\n\n");    exit(1);  }  add_olsr_socket(New->olsr_socket, &olsr_input);  New->int_next = ifnet;  ifnet = New;  IntConf->interf = New;  IntConf->configured = 1;  memset(&NullAddr, 0, olsr_cnf->ipsize);    if(COMP_IP(&NullAddr, &olsr_cnf->main_addr))  {    COPY_IP(&olsr_cnf->main_addr, &New->ip_addr);    OLSR_PRINTF(1, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));  }  net_add_buffer(New);  if (olsr_cnf->lq_level == 0)  {    olsr_register_scheduler_event(&generate_hello, New,                                  IntConf->cnf->hello_params.emission_interval,                                  0, NULL);    olsr_register_scheduler_event(&generate_tc, New,                                  IntConf->cnf->tc_params.emission_interval,                                  0, NULL);  }  else  {    olsr_register_scheduler_event(&olsr_output_lq_hello, New,                                  IntConf->cnf->hello_params.emission_interval,                                  0, NULL);    olsr_register_scheduler_event(&olsr_output_lq_tc, New,                                  IntConf->cnf->tc_params.emission_interval,                                  0, NULL);  }  olsr_register_scheduler_event(&generate_mid, New,                                IntConf->cnf->mid_params.emission_interval,                                0, NULL);  olsr_register_scheduler_event(&generate_hna, New,                                IntConf->cnf->hna_params.emission_interval,                                0, NULL);  if(olsr_cnf->max_jitter == 0 ||     IntConf->cnf->hello_params.emission_interval / 4 < olsr_cnf->max_jitter)    olsr_cnf->max_jitter = IntConf->cnf->hello_params.emission_interval / 4;  if(olsr_cnf->max_tc_vtime < IntConf->cnf->tc_params.emission_interval)    olsr_cnf->max_tc_vtime = IntConf->cnf->tc_params.emission_interval;  New->hello_etime = IntConf->cnf->hello_params.emission_interval;  New->valtimes.hello = double_to_me(IntConf->cnf->hello_params.validity_time);  New->valtimes.tc = double_to_me(IntConf->cnf->tc_params.validity_time);  New->valtimes.mid = double_to_me(IntConf->cnf->mid_params.validity_time);  New->valtimes.hna = double_to_me(IntConf->cnf->hna_params.validity_time);  run_ifchg_cbs(New, IFCHG_IF_ADD);  return 1;}void check_interface_updates(void *dummy __attribute__((unused))){  struct olsr_if *IntConf;#ifdef DEBUG  OLSR_PRINTF(3, "Checking for updates in the interface set\n");#endif  for(IntConf = olsr_cnf->interfaces; IntConf != NULL; IntConf = IntConf->next)  {    if(IntConf->host_emul)      continue;          if(olsr_cnf->host_emul) /* XXX: TEMPORARY! */      continue;     if(IntConf->configured)          chk_if_changed(IntConf);    else      chk_if_up(IntConf, 3);  }}

⌨️ 快捷键说明

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