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

📄 iflib.c

📁 VXWORKS源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* ifLib.c - network interface library *//* Copyright 1984 - 2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------03h,10may02,kbw  making man page edits03g,09may02,wsl  fix doc formatting error03f,08jan02,vvv  fixed ifRouteDelete to use correct netmask (SPR #33381)03e,07dec01,rae  merge from synth ver 03r (SPR #70269)03d,05nov01,vvv  fixed compilation warning03c,15oct01,rae  merge from truestack ver 03p, base 03b (ROUTER_STACK)03b,12mar99,c_c  Doc: updated ifAddrGet, ifFlagSet() and ifDeleRoute 		(SPRs #21401, 9146 and 21076).03a,06oct98,ham  changed API for ifAddrAdd() (SPR22267)02a,19mar98,spm  fixed byte ordering in ifRouteDelete() routine; changed to                  use mRouteDelete() to support classless routing (SPR #20548)01z,16apr97,vin  changed SOCK_DGRAM to SOCK_RAW as udp can be scaled out.		 fixed ifRouteDelete().01y,16oct96,dgp  doc: add explanations to ifFlagsSet per SPR 733701x,11jul94,dzb  Fixed setting of errno in ifIoctlCall() (SPR #3188).                 Fixed ntohl transfer in ifMaskGet() (SPR #3334).01w,19oct92,jmm  added ntohl () to ifMaskGet() to make it symetrical w/ifMaskSet01v,02sep93,elh  changed ifunit back to berkeley orig to fix spr (1516).01u,11aug93,jmm  Changed ioctl.h and socket.h to sys/ioctl.h and sys/socket.h01t,05feb93,jag  Changed call to inet_ntoa to inet_ntoa_b in ifInetAddrToStr,		 changed ifRouteDelete accordingly SPR# 181401s,20jan93,jdi  documentation cleanup for 5.1.01r,18jul92,smb  Changed errno.h to errnoLib.h.01q,26may92,rrr  the tree shuffle		  -changed includes to have absolute path from h/01p,10dec91,gae  added includes for ANSI.01o,04oct91,rrr  passed through the ansification filter                  -changed functions to ansi style		  -changed includes to have absolute path from h/		  -fixed #else and #endif		  -changed copyright notice01n,01may91,elh	 added htonl to ifMaskSet because network wants mask                 to be in network byte order.01m,05apr91,jdi	 documentation -- removed header parens and x-ref numbers;	   +elh	 added ifunit() from if.c (elh); doc review by dnw.01l,18jan91,jaa	 documentation.01k,26jun90,hjb  moved ifShow, etc. to netShow.c.01j,18mar90,hjb  de-linted.  minor syntax cleanup.01i,14mar90,jdi  documentation cleanup.01h,07aug89,gae  undid 01e -- fixed if_bp.c.01g,03aug89,hjb  fixed ifShow to work with "bp" interfaces which                 use incompatible "ifp->if_name" convention (unit number is                 for some reason appended to the if_name itself?).01f,30jul89,gae  removed varargs stuff; internal name changes.		 documentation tweaks.  lint.01e,28jul89,hjb  added routines ifAddrGet, ifBroadcastGet, ifDstAddrGet,                 ifAddrParamGet, ifMaskGet, ifFlagChange, ifFlagGet,		 ifMetricSet, ifMetricGet, ifIoctl, ifSetIoctl, ifGetIoctl,		 ifIoctlCall, and modified ifEtherPrint.01d,27jul89,hjb  added routines ifShow, ifFlagSet, ifDstAddrSet, ifAddrPrint,		 ifFlagPrint, ifEtherPrint, ifRouteDelete.01c,18aug88,gae  documentation.01b,30may88,dnw  changed to v4 names.01a,28may88,dnw  extracted from netLib.*//*DESCRIPTIONThis library contains routines to configure the network interface parameters.Generally, each routine corresponds to one of the functions of the UNIXcommand \f3ifconfig\fP.To use this feature, include the following component:INCLUDE_NETWRS_IFLIBINCLUDE FILES: ifLib.hSEE ALSO: hostLib*/#include "vxWorks.h"#include "stdio.h"#include "netinet/in.h"#include "net/if.h"#include "netinet/if_ether.h"#include "sys/ioctl.h"#include "ioLib.h"#include "inetLib.h"#include "string.h"#include "netinet/in_var.h"#include "errnoLib.h"#include "sockLib.h"#include "hostLib.h"#include "routeLib.h"#include "unistd.h"#include "ifLib.h"#include "m2Lib.h"#include "arpLib.h"#include "routeEnhLib.h"#ifdef DEBUG#include "logLib.h"#endif#ifdef VIRTUAL_STACK#include "netinet/vsLib.h"#include "netinet/vsRadix.h" 	/* for rt_tables definition */#include "netinet/vsIp.h" 	/* for _in_ifaddr definition */#endif /* VIRTUAL_STACK *//* * macro to convert interface address into character string representation. * The return value is a_ip_addr. */#define ifInetAddrToStr(ifAddr,a_ip_addr) \((inet_ntoa_b(((struct sockaddr_in *)(ifAddr))->sin_addr,a_ip_addr)),a_ip_addr)#define RT_ARP          0x100                   /* arp route entry *//* * Imported global variables */#ifndef VIRTUAL_STACKIMPORT struct ifnet 		*ifnet;	/* global pointer to all interfaces */IMPORT struct in_ifaddr 	*in_ifaddr; /* global pointer to addresses */#endif /* VIRTUAL_STACK */IMPORT STATUS _arpCmd (int, struct in_addr *, u_char *, int *);/* forward static functions */static STATUS ifAddrParamGet (char *interfaceName, int code, char *address);static STATUS ifIoctl (char *interfaceName, int code, int arg);static STATUS ifIoctlSet (char *interfaceName, int code, int val);static STATUS ifIoctlGet (char *interfaceName, int code, int *val);static STATUS ifIoctlCall (int code, struct ifreq *ifrp);LOCAL int routeNodeDelete ();LOCAL int _routeNodeDelete (struct rtentry * pRt, int * pDeleted);/********************************************************************************* ifUnnumberedSet - configure an interface to be unnumbered** This API sets an interface unnumbered. It sets the IFF_POINTOPOINT flags* and creates a routing entry through the interface using a user-specified* destination IP address. The unnumbered link can then be uniquely referred* to by the destination IP address, <pDstIp>, when adding routes. The* interface is assigned a "borrowed" IP address--borrowed from another* interface on the machine. In RFC 1812 it is also called the router ID.* This address will be used to generate any needed ICMP messages or the like.* Note that ARP is not able to run on an unnumbered link.* * The initialization of the unnumbered device is similar to other* network devices, but it does have a few additional steps and concerns.* ifUnnumberedSet() must come next after ipAttach(). Please note that* the interface using the IP address that the unnumbered interface will* borrow must be brought up first and configured with <ifAddrSet> or * equivalent. This is required to ensure normal network operation for* that IP address/interface.  After ifUnnumberedSet(), one must create* additional routing entries (using mRouteAdd(), routeNetAdd(), etc)* in order to reach other networks, including the network to which* the destination IP address belongs. * * The <pDstMac> field in ifUnnumberedSet() is used to specify the* destination's MAC address. It should be left NULL if the destination* is not an Ethernet device. If the MAC address is not known, then* supply an artificial address. We recommend using "00:00:00:00:00:01"* The destination interface can then be set promiscuous to accept* this artificial address. This is accomplished using the <ifpromisc>* command.** Example:* \ss* ipAttach (1, "fei")* ifUnnumberedSet ("fei1", "120.12.12.12", "140.34.78.94", "00:a0:d0:d8:c8:14")* routeNetAdd ("120.12.0.0","120.12.12.12") <One possible network>* routeNetAdd ("178.45.0.0","120.12.12.12") <Another possible network>* \se* RETURNS: OK, or ERROR if the interface cannot be set.**/#ifdef ROUTER_STACKSTATUS ifUnnumberedSet    (    char *pIfName,	/* Name of interface to configure */     char *pDstIp, 	/* Destination address of the point to point link */    char *pBorrowedIp,	/* The borrowed IP address/router ID */    char *pDstMac	/* Destination MAC address */    )    {    int s, ret;    struct ifnet *ifp;    struct sockaddr_in sock;    if(!(ifp = ifunit (pIfName)))	return (ERROR);    /*     * Make sure that the interface with the "real"     * pBorrowedIp is already brought up.     */    bzero ((char *)&sock, sizeof (struct sockaddr_in));    sock.sin_family = AF_INET;    sock.sin_len = sizeof (struct sockaddr_in);    sock.sin_addr.s_addr = inet_addr (pBorrowedIp);    if (!ifa_ifwithaddr ((struct sockaddr *)&sock))        return (EINVAL);     /* We manually configure the interface to be IFF_UNNUMBERED */    s = splnet ();    ifp->if_flags &= ~IFF_BROADCAST;    ifp->if_flags |= IFF_UNNUMBERED;	/* It's defined as IFF_POINTOPOINT */    splx (s);    ret = ifAddrAdd (pIfName, pBorrowedIp, pDstIp, 0);    if (ret != OK)	return (ret);    /*     * For ethernet devices, we need to complete the route entry     * just created by the above operation. The ATF_INCOMPLETE flag     * will change the routing entry to contain the MAC address     * of the destination's ethernet device.     */    if (pDstMac != (char *)NULL)        ret = arpAdd (pDstIp, pDstMac, ATF_PERM | ATF_INCOMPLETE);    return (ret);    }#endif /* ROUTER_STACK *//********************************************************************************* ifAddrAdd - add an interface address for a network interface** This routine assigns an Internet address to a specified network interface.* The Internet address can be a host name or a standard Internet address* format (e.g., 90.0.0.4).  If a host name is specified, it should already* have been added to the host table with hostAdd().** You must specify both an <interfaceName> and an <interfaceAddress>. A * <broadcastAddress> is optional. If <broadcastAddress> is NULL, in_ifinit() * generates a <broadcastAddress> value based on the <interfaceAddress> value* and the netmask.  A <subnetMask> value is optional.  If <subnetMask> is 0, * in_ifinit() uses a <subnetMask> the same as the netmask that is generated * by the <interfaceAddress>.  The <broadcastAddress> is also <destAddress> in * case of IFF_POINTOPOINT.** RETURNS: OK, or ERROR if the interface cannot be set.** SEE ALSO: ifAddrGet(), ifDstAddrSet(), ifDstAddrGet()*/STATUS ifAddrAdd    (    char *interfaceName,     /* name of interface to configure */    char *interfaceAddress,  /* Internet address to assign to interface */    char *broadcastAddress,  /* broadcast address to assign to interface */    int   subnetMask         /* subnetMask */    )    {    struct ifaliasreq   ifa;    struct sockaddr_in *pSin_iaddr = (struct sockaddr_in *)&ifa.ifra_addr;    struct sockaddr_in *pSin_baddr = (struct sockaddr_in *)&ifa.ifra_broadaddr;    struct sockaddr_in *pSin_snmsk = (struct sockaddr_in *)&ifa.ifra_mask;    int                 so;    int                 status = 0;    bzero ((caddr_t) &ifa, sizeof (ifa));    /* verify Internet address is in correct format */    if ((pSin_iaddr->sin_addr.s_addr =             inet_addr (interfaceAddress)) == ERROR &&        (pSin_iaddr->sin_addr.s_addr =             hostGetByName (interfaceAddress) == ERROR))        {        return (ERROR);        }    /* verify Boradcast address is in correct format */    if (broadcastAddress != NULL &&        (pSin_baddr->sin_addr.s_addr =             inet_addr (broadcastAddress)) == ERROR &&        (pSin_baddr->sin_addr.s_addr =             hostGetByName (broadcastAddress) == ERROR))        {        return (ERROR);        }    strncpy (ifa.ifra_name, interfaceName, sizeof (ifa.ifra_name));    /* for interfaceAddress */    ifa.ifra_addr.sa_len = sizeof (struct sockaddr_in);    ifa.ifra_addr.sa_family = AF_INET;    /* for broadcastAddress */    if (broadcastAddress != NULL)        {        ifa.ifra_broadaddr.sa_len = sizeof (struct sockaddr_in);        ifa.ifra_broadaddr.sa_family = AF_INET;        }    /* for subnetmask */    if (subnetMask != 0)       {       ifa.ifra_mask.sa_len = sizeof (struct sockaddr_in);       ifa.ifra_mask.sa_family = AF_INET;       pSin_snmsk->sin_addr.s_addr = htonl (subnetMask);       }    if ((so = socket (AF_INET, SOCK_RAW, 0)) < 0)        return (ERROR);    status = ioctl (so, SIOCAIFADDR, (int)&ifa);    (void)close (so);    if (status != 0)        {        if (status != ERROR)    /* iosIoctl() can return ERROR */            (void)errnoSet (status);        return (ERROR);        }    return (OK);    }/********************************************************************************* ifAddrSet - set an interface address for a network interface** This routine assigns an Internet address to a specified network interface.* The Internet address can be a host name or a standard Internet address* format (e.g., 90.0.0.4).  If a host name is specified, it should already* have been added to the host table with hostAdd().** A successful call to ifAddrSet() results in the addition of a new route.** The subnet mask used in determining the network portion of the address will* be that set by ifMaskSet(), or the default class mask if ifMaskSet() has not* been called.  It is standard practice to call ifMaskSet() prior to calling* ifAddrSet().** RETURNS: OK, or ERROR if the interface cannot be set.** SEE ALSO: ifAddrGet(), ifDstAddrSet(), ifDstAddrGet()*/STATUS ifAddrSet    (    char *interfaceName,        /* name of interface to configure, i.e. ei0 */    char *interfaceAddress      /* Internet address to assign to interface */    )    {    return (ifIoctl (interfaceName, SIOCSIFADDR, (int)interfaceAddress));    }/********************************************************************************* ifAddrDelete - delete an interface address for a network interface** This routine deletes an Internet address from a specified network interface.* The Internet address can be a host name or a standard Internet address* format (e.g., 90.0.0.4).  If a host name is specified, it should already* have been added to the host table with hostAdd().** RETURNS: OK, or ERROR if the interface cannot be deleted.** SEE ALSO: ifAddrGet(), ifDstAddrSet(), ifDstAddrGet()*/STATUS ifAddrDelete    (    char *interfaceName,        /* name of interface to delete addr from */    char *interfaceAddress      /* Internet address to delete from interface */    )    {    return (ifIoctl (interfaceName, SIOCDIFADDR, (int) interfaceAddress));    }/********************************************************************************* ifAddrGet - get the Internet address of a network interface

⌨️ 快捷键说明

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