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

📄 networkinterfaces.c

📁 wifi 无线网络路由协议OLSR linux下C代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * OLSR Basic Multicast Forwarding (BMF) plugin. * Copyright (c) 2005 - 2007, Thales Communications, Huizen, The Netherlands. * Written by Erik Tromp. * All rights reserved. * * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met: * * * Redistributions of source code must retain the above copyright  *   notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright  *   notice, this list of conditions and the following disclaimer in  *   the documentation and/or other materials provided with the  *   distribution. * * Neither the name of Thales, BMF nor the names of its  *   contributors may be used to endorse or promote products derived  *   from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  * OF THE POSSIBILITY OF SUCH DAMAGE. *//* ------------------------------------------------------------------------- * File       : NetworkInterfaces.c * Description: Functions to open and close sockets * Created    : 29 Jun 2006 * * ------------------------------------------------------------------------- */#include "NetworkInterfaces.h"/* System includes */#include <stddef.h> /* NULL */#include <syslog.h> /* syslog() */#include <string.h> /* strerror(), strchr(), strcmp() */#include <errno.h> /* errno */#include <unistd.h> /* close() */#include <sys/ioctl.h> /* ioctl() */#include <fcntl.h> /* fcntl() */#include <assert.h> /* assert() */#include <net/if.h> /* socket(), ifreq, if_indextoname(), if_nametoindex() */#include <netinet/in.h> /* htons() */#include <linux/if_ether.h> /* ETH_P_IP */#include <linux/if_packet.h> /* packet_mreq, PACKET_MR_PROMISC, PACKET_ADD_MEMBERSHIP */#include <linux/if_tun.h> /* IFF_TAP */#include <netinet/ip.h> /* struct ip */#include <netinet/udp.h> /* SOL_UDP *//* OLSRD includes */#include "olsr.h" /* olsr_printf() */#include "defs.h" /* olsr_cnf */#include "local_hna_set.h" /* add_local_hna4_entry() */#include "link_set.h" /* get_link_set() */#include "lq_route.h" /* MIN_LINK_QUALITY */#include "tc_set.h" /* olsr_lookup_tc_entry(), olsr_tc_lookup_dst() *//* Plugin includes */#include "Packet.h" /* IFHWADDRLEN */#include "Bmf.h" /* PLUGIN_NAME, MainAddressOf() */#include "Address.h" /* IsMulticast() *//* List of network interface objects used by BMF plugin */struct TBmfInterface* BmfInterfaces = NULL;struct TBmfInterface* LastBmfInterface = NULL;/* Highest-numbered open socket file descriptor. To be used as first * parameter in calls to select(...). */int HighestSkfd = -1;/* Set of socket file descriptors */fd_set InputSet;/* File descriptor of EtherTunTap interface */int EtherTunTapFd = -1;/* Network interface name of EtherTunTap interface. May be overruled by * setting the plugin parameter "BmfInterface". */char EtherTunTapIfName[IFNAMSIZ] = "bmf0";/* The underlying mechanism to forward multicast packets. Either: * - BM_BROADCAST: BMF uses the IP local broadcast as destination address * - BM_UNICAST_PROMISCUOUS: BMF uses the IP address of the best neighbor as *   destination address. The other neighbors listen promiscuously. */enum TBmfMechanism BmfMechanism = BM_BROADCAST;#define ETHERTUNTAPIPNOTSET 0/* The IP address of the BMF network interface in host byte order. * May be overruled by setting the plugin parameter "BmfInterfaceIp". */u_int32_t EtherTunTapIp = ETHERTUNTAPIPNOTSET;/* 255.255.255.255 in host byte order. May be overruled by * setting the plugin parameter "BmfInterfaceIp". */u_int32_t EtherTunTapIpMask = 0xFFFFFFFF;/* The IP broadcast address of the BMF network interface in host byte order. * May be overruled by setting the plugin parameter "BmfinterfaceIp". */u_int32_t EtherTunTapIpBroadcast = ETHERTUNTAPIPNOTSET;/* Whether or not the configuration has overruled the default IP * configuration of the EtherTunTap interface */int TunTapIpOverruled = 0;/* Whether or not to capture packets on the OLSR-enabled * interfaces (in promiscuous mode). May be overruled by setting the plugin * parameter "CapturePacketsOnOlsrInterfaces" to "yes". */int CapturePacketsOnOlsrInterfaces = 0;/* ------------------------------------------------------------------------- * Function   : SetBmfInterfaceName * Description: Overrule the default network interface name ("bmf0") of the *              EtherTunTap interface * Input      : ifname - network interface name (e.g. "mybmf0") *              data - not used *              addon - not used * Output     : none * Return     : success (0) or fail (1) * Data Used  : EtherTunTapIfName * ------------------------------------------------------------------------- */int SetBmfInterfaceName(const char* ifname, void* data __attribute__((unused)), set_plugin_parameter_addon addon  __attribute__((unused))){  strncpy(EtherTunTapIfName, ifname, IFNAMSIZ - 1);  EtherTunTapIfName[IFNAMSIZ - 1] = '\0'; /* Ensures null termination */  return 0;} /* SetBmfInterfaceName *//* ------------------------------------------------------------------------- * Function   : SetBmfInterfaceIp * Description: Overrule the default IP address and prefix length *              ("10.255.255.253/30") of the EtherTunTap interface * Input      : ip - IP address string, followed by '/' and prefix length *              data - not used *              addon - not used * Output     : none * Return     : success (0) or fail (1) * Data Used  : EtherTunTapIp, EtherTunTapIpMask, EtherTunTapIpBroadcast, *              TunTapIpOverruled * ------------------------------------------------------------------------- */int SetBmfInterfaceIp(const char* ip, void* data __attribute__((unused)), set_plugin_parameter_addon addon  __attribute__((unused))){#define IPV4_MAX_ADDRLEN 16#define IPV4_MAX_PREFIXLEN 32  char* slashAt;  char ipAddr[IPV4_MAX_ADDRLEN];  struct in_addr sinaddr;  int prefixLen;  int i;  /* Inspired by function str2prefix_ipv4 as found in Quagga source   * file lib/prefix.c */  /* Find slash inside string. */  slashAt = strchr(ip, '/');  /* String doesn't contain slash. */  if (slashAt == NULL || slashAt - ip >= IPV4_MAX_ADDRLEN)  {    /* No prefix length specified, or IP address too long */    return 1;  }  strncpy(ipAddr, ip, slashAt - ip);  *(ipAddr + (slashAt - ip)) = '\0';  if (inet_aton(ipAddr, &sinaddr) == 0)  {    /* Invalid address passed */    return 1;  }  EtherTunTapIp = ntohl(sinaddr.s_addr);  /* Get prefix length. */  prefixLen = atoi(++slashAt);  if (prefixLen <= 0 || prefixLen > IPV4_MAX_PREFIXLEN)  {    return 1;  }  /* Compose IP subnet mask in host byte order */  EtherTunTapIpMask = 0;  for (i = 0; i < prefixLen; i++)  {    EtherTunTapIpMask |= (1 << (IPV4_MAX_PREFIXLEN - 1 - i));  }  /* Compose IP broadcast address in host byte order */  EtherTunTapIpBroadcast = EtherTunTapIp;  for (i = prefixLen; i < IPV4_MAX_PREFIXLEN; i++)  {    EtherTunTapIpBroadcast |= (1 << (IPV4_MAX_PREFIXLEN - 1 - i));  }  TunTapIpOverruled = 1;  return 0;} /* SetBmfInterfaceIp *//* ------------------------------------------------------------------------- * Function   : SetCapturePacketsOnOlsrInterfaces * Description: Overrule the default setting, enabling or disabling the *              capturing of packets on OLSR-enabled interfaces. * Input      : enable - either "yes" or "no" *              data - not used *              addon - not used * Output     : none * Return     : success (0) or fail (1) * Data Used  : none * ------------------------------------------------------------------------- */int SetCapturePacketsOnOlsrInterfaces(const char* enable, void* data __attribute__((unused)), set_plugin_parameter_addon addon  __attribute__((unused))){  if (strcmp(enable, "yes") == 0)  {    CapturePacketsOnOlsrInterfaces = 1;    return 0;  }  else if (strcmp(enable, "no") == 0)  {    CapturePacketsOnOlsrInterfaces = 0;    return 0;  }  /* Value not recognized */  return 1;} /* SetCapturePacketsOnOlsrInterfaces *//* ------------------------------------------------------------------------- * Function   : SetBmfMechanism * Description: Overrule the default BMF mechanism to either BM_BROADCAST or *              BM_UNICAST_PROMISCUOUS. * Input      : mechanism - either "Broadcast" or "UnicastPromiscuous" *              data - not used *              addon - not used * Output     : none * Return     : success (0) or fail (1) * Data Used  : none * ------------------------------------------------------------------------- */int SetBmfMechanism(const char* mechanism, void* data __attribute__((unused)), set_plugin_parameter_addon addon  __attribute__((unused))){  if (strcmp(mechanism, "Broadcast") == 0)  {    BmfMechanism = BM_BROADCAST;    return 0;  }  else if (strcmp(mechanism, "UnicastPromiscuous") == 0)  {    BmfMechanism = BM_UNICAST_PROMISCUOUS;    return 0;  }  /* Value not recognized */  return 1;} /* SetBmfMechanism *//* ------------------------------------------------------------------------- * Function   : AddDescriptorToInputSet * Description: Add a socket descriptor to the global set of socket file descriptors * Input      : skfd - socket file descriptor * Output     : none * Return     : none * Data Used  : HighestSkfd, InputSet * Notes      : Keeps track of the highest-numbered descriptor * ------------------------------------------------------------------------- */static void AddDescriptorToInputSet(int skfd){  /* Keep the highest-numbered descriptor */  if (skfd > HighestSkfd)  {    HighestSkfd = skfd;  }  /* Add descriptor to input set */  FD_SET(skfd, &InputSet);} /* AddDescriptorToInputSet *//* To save the state of the IP spoof filter for the EtherTunTap interface */static char EthTapSpoofState = '1';/* ------------------------------------------------------------------------- * Function   : DeactivateSpoofFilter * Description: Deactivates the Linux anti-spoofing filter for the tuntap *              interface * Input      : none * Output     : none * Return     : fail (0) or success (1) * Data Used  : EtherTunTapIfName, EthTapSpoofState * Notes      : Saves the current filter state for later restoring * ------------------------------------------------------------------------- */int DeactivateSpoofFilter(void){  FILE* procSpoof;  char procFile[FILENAME_MAX];  /* Generate the procfile name */  sprintf(procFile, "/proc/sys/net/ipv4/conf/%s/rp_filter", EtherTunTapIfName);  /* Open procfile for reading */  procSpoof = fopen(procFile, "r");  if (procSpoof == NULL)  {    fprintf(      stderr,      "WARNING! Could not open the %s file to check/disable the IP spoof filter!\n"      "Are you using the procfile filesystem?\n"      "Does your system support IPv4?\n"      "I will continue (in 3 sec) - but you should manually ensure that IP spoof\n"      "filtering is disabled!\n\n",      procFile);          sleep(3);    return 0;  }  EthTapSpoofState = fgetc(procSpoof);  fclose(procSpoof);  /* Open procfile for writing */  procSpoof = fopen(procFile, "w");  if (procSpoof == NULL)  {    fprintf(stderr, "Could not open %s for writing!\n", procFile);    fprintf(      stderr,      "I will continue (in 3 sec) - but you should manually ensure that IP"      " spoof filtering is disabled!\n\n");    sleep(3);    return 0;  }  syslog(LOG_INFO, "Writing \"0\" to %s", procFile);  fputs("0", procSpoof);  fclose(procSpoof);  return 1;} /* DeactivateSpoofFilter *//* ------------------------------------------------------------------------- * Function   : RestoreSpoofFilter * Description: Restores the Linux anti-spoofing filter setting for the tuntap *              interface * Input      : none * Output     : none * Return     : none * Data Used  : EtherTunTapIfName, EthTapSpoofState * ------------------------------------------------------------------------- */void RestoreSpoofFilter(void){  FILE* procSpoof;  char procFile[FILENAME_MAX];  /* Generate the procfile name */  sprintf(procFile, "/proc/sys/net/ipv4/conf/%s/rp_filter", EtherTunTapIfName);  /* Open procfile for writing */  procSpoof = fopen(procFile, "w");  if (procSpoof == NULL)  {    fprintf(stderr, "Could not open %s for writing!\nSettings not restored!\n", procFile);  }  else  {    syslog(LOG_INFO, "Resetting %s to %c\n", procFile, EthTapSpoofState);    fputc(EthTapSpoofState, procSpoof);    fclose(procSpoof);  }} /* RestoreSpoofFilter */#ifndef USING_THALES_LINK_COST_ROUTING/* ------------------------------------------------------------------------- * Function   : CalcEtx * Description: Calculate the Expected Transmission Count (ETX) value, based on *              link loss fraction and inverse link loss fraction * Input      : loss - link loss fraction *              neigh_loss - inverse link loss fraction  * Output     : none * Return     : the ETX value * Data Used  : none * ------------------------------------------------------------------------- */static float CalcEtx(float loss, float neigh_loss) {  if (loss < MIN_LINK_QUALITY || neigh_loss < MIN_LINK_QUALITY)  {    return INFINITE_ETX;

⌨️ 快捷键说明

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