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

📄 iflib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ifLib.c - network interface library *//* Copyright 1984 - 1998 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------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.INCLUDE FILES: ifLib.hSEE ALSO: hostLib,.pG "Network"*/#include "vxWorks.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"/* * 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)/* * Imported global variables */IMPORT struct ifnet 		*ifnet;	/* global pointer to all interfaces */IMPORT struct in_ifaddr 	*in_ifaddr; /* global pointer to addresses *//* 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);/********************************************************************************* 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().** interfaceName, interfaceAddress must be specified.* broadcastAddress is optional. If broadcastAddress is NULL,* in_ifinit() will generate a broadcastAddress by using the interfaceAddress* and the netmask.  subnetMask is optional.  If subnetMask is 0, in_ifinit()* will set a subnetMask as same as a netmask which is generated by the* interfaceAddress.  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));    }/********************************************************************************* ifAddrGet - get the Internet address of a network interface** This routine gets the Internet address of a specified network interface and* copies it to <interfaceAddress>. This pointer should point to a buffer* large enough to contain INET_ADDR_LEN bytes.** RETURNS: OK or ERROR.** SEE ALSO: ifAddrSet(), ifDstAddrSet(), ifDstAddrGet()*/STATUS ifAddrGet    (    char *interfaceName,        /* name of interface, i.e. ei0 */    char *interfaceAddress      /* buffer for Internet address */    )    {    return (ifAddrParamGet (interfaceName, SIOCGIFADDR, interfaceAddress));    }/********************************************************************************* ifBroadcastSet - set the broadcast address for a network interface** This routine assigns a broadcast address for the specified network interface.* The broadcast address must be a string in standard Internet address format* (e.g., 90.0.0.0).** An interface's default broadcast address is its Internet address with a* host part of all ones (e.g., 90.255.255.255).  This conforms to current* ARPA specifications.  However, some older systems use an Internet address* with a host part of all zeros as the broadcast address.** NOTE* VxWorks automatically accepts a host part of all zeros as a broadcast* address, in addition to the default or specified broadcast address.  But* if VxWorks is to broadcast to older systems using a host part of all zeros* as the broadcast address, this routine should be used to change the* broadcast address of the interface.** RETURNS: OK or ERROR.*/STATUS ifBroadcastSet    (    char *interfaceName,        /* name of interface to assign, i.e. ei0 */    char *broadcastAddress      /* broadcast address to assign to interface */    )    {    return (ifIoctl (interfaceName, SIOCSIFBRDADDR, (int)broadcastAddress));    }/********************************************************************************* ifBroadcastGet - get the broadcast address for a network interface** This routine gets the broadcast address for a specified network interface.* The broadcast address is copied to the buffer <broadcastAddress>.** RETURNS: OK or ERROR.** SEE ALSO: ifBroadcastSet()*/STATUS ifBroadcastGet    (    char *interfaceName,        /* name of interface, i.e. ei0 */    char *broadcastAddress      /* buffer for broadcast address */    )    {    return (ifAddrParamGet (interfaceName, SIOCGIFBRDADDR, broadcastAddress));    }/********************************************************************************* ifDstAddrSet - define an address for the other end of a point-to-point link** This routine assigns the Internet address of a machine connected to the* opposite end of a point-to-point network connection, such as a SLIP* connection.  Inherently, point-to-point connection-oriented protocols such as* SLIP require that addresses for both ends of a connection be specified.** RETURNS: OK or ERROR.** SEE ALSO: ifAddrSet(), ifDstAddrGet()*/STATUS ifDstAddrSet    (    char *interfaceName,        /* name of interface to configure, i.e. ei0 */    char *dstAddress            /* Internet address to assign to destination */    )    {    return (ifIoctl (interfaceName, SIOCSIFDSTADDR, (int)dstAddress));    }/********************************************************************************* ifDstAddrGet - get the Internet address of a point-to-point peer** This routine gets the Internet address of a machine connected to the* opposite end of a point-to-point network connection.  The Internet address is* copied to the buffer <dstAddress>.** RETURNS: OK or ERROR.** SEE ALSO: ifDstAddrSet(), ifAddrGet()*/STATUS ifDstAddrGet    (    char *interfaceName,        /* name of interface, i.e. ei0 */    char *dstAddress            /* buffer for destination address */    )    {    return (ifAddrParamGet (interfaceName, SIOCGIFDSTADDR, dstAddress));    }/********************************************************************************* ifAddrParamGet - call ifIoctl to get the Internet address of the interface** ifAddrParamGet may be used to retrieve Internet address of the network* interface in ASCII character string representation (dot-notation* like "192.0.0.3").** RETURNS: OK or ERROR** SEE ALSO: ifAddrGet (), ifDstAddrGet (), ifBroadcastGet ()*/LOCAL STATUS ifAddrParamGet    (    char *interfaceName,        /* name of interface to configure, i.e. ei0 */    int   code,                 /* SIOCG ioctl code */    char *address               /* address retrieved here */    )    {    struct in_addr inetAddrBuf;	/* result */    char netString [INET_ADDR_LEN];    if (address == NULL)	{	(void)errnoSet (EINVAL);	return (ERROR);	}    if (ifIoctl (interfaceName, code, (int)&inetAddrBuf) == OK)	{	inet_ntoa_b (inetAddrBuf, netString);	strncpy (address, netString, INET_ADDR_LEN);	return (OK);	}    return (ERROR);    }/********************************************************************************* ifMaskSet - define a subnet for a network interface** This routine allocates additional bits to the network portion of an* Internet address.  The network portion is specified with a mask that must* contain ones in all positions that are to be interpreted as the network* portion.  This includes all the bits that are normally interpreted as the* network portion for the given class of address, plus the bits to be added.* Note that all bits must be contiguous.  The mask is specified in host byte* order.** In order to correctly interpret the address, a subnet mask should be set* for an interface prior to setting the Internet address of the interface* with the routine ifAddrSet().** RETURNS: OK or ERROR.** SEE ALSO: ifAddrSet()*/STATUS ifMaskSet    (    char *interfaceName,   /* name of interface to set mask for, i.e. ei0 */    int netMask            /* subnet mask (e.g. 0xff000000) */    )    {    return (ifIoctl (interfaceName, SIOCSIFNETMASK, htonl (netMask)));    }/********************************************************************************* ifMaskGet - get the subnet mask for a network interface** This routine gets the subnet mask for a specified network interface.* The subnet mask is copied to the buffer <netMask>.  The subnet mask is* returned in host byte order.** RETURNS: OK or ERROR.** SEE ALSO: ifAddrGet(), ifFlagGet()*/STATUS ifMaskGet    (    char *interfaceName,        /* name of interface, i.e. ei0 */    int  *netMask               /* buffer for subnet mask */    )    {    int status = ifIoctl (interfaceName, SIOCGIFNETMASK, (u_long) netMask);    if (status != ERROR)	*netMask = ntohl (*netMask);    return (status);    }/********************************************************************************* ifFlagChange - change the network interface flags** This routine changes the flags for the specified network interfaces.  If* the parameter <on> is TRUE, the specified flags are turned on; otherwise,* they are turned off.  The routines ifFlagGet() and ifFlagSet() are called* to do the actual work.** RETURNS: OK or ERROR.** SEE ALSO: ifAddrSet(), ifMaskSet(), ifFlagSet(), ifFlagGet()*/

⌨️ 快捷键说明

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