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

📄 ospf_netinet_interface.c

📁 vxworks下ospf协议栈
💻 C
字号:
/* ospf_netinet_interface.c - OSPF Source File for Interface with the Kernel * Layer Components *//* Copyright 2001-2003 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------02b,12may03,asr  Changes to make OSPF virtual stack compatible02a,30aug02,agi  Fixed DIAB compiler warnings01b,12Oct01,aj   Incorporated changes for Synth compatibility.01a,27Jun01,mist written.*//*DESCRIPTIONThis Source Files contains the Wrapper functions written for the lowerlayer functions.When OSPF Initializes, it calls several lower layer functions such as theINADDR_TO_IFP, IFP_TO_IA,in_addmulti etc which are not visible to protectiondomains.Hence in order to make OSPF  work on application domains,each of theabove functions need to be placed inside a Wrapper Function which is exportedso that protection domains can call these functions.*//* includes */#if defined (__OSPF_ROUTER_STACK__) /*only include this file for router stack*/#include <stdio.h>#include <vxWorks.h>#include <netLib.h>#include <net/protosw.h>#include <net/domain.h>#include <net/mbuf.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/in_pcb.h>#include <netinet/ip.h>#include <netinet/ip_var.h>#include <sys/socket.h>#include <net/socketvar.h>#include <errno.h>#include <sys/stat.h>#include <net/if.h>#include <net/route.h>#include <netinet/in_var.h>#include <netinet/in_systm.h>#include "ospf_kernel_interface.h"#if defined (VIRTUAL_STACK)    #include <netinet/vsIp.h>    #include <netinet/vsLib.h>    #include <netinet/vsNetCore.h>#endif /* VIRTUAL_STACK */#define IP_PROT_OSPF    89/* globals */IMPORT OSPF_CALLBACK_IF OspfRegistrationInfo;/* declarations *//* externs */#if !defined (VIRTUAL_STACK)    IMPORT int    _protoSwIndex;    IMPORT struct protosw   inetsw [IP_PROTO_NUM_MAX];    IMPORT u_char ip_protox [IPPROTO_MAX];#endif /* VIRTUAL_STACK *//******************************************************************************** ospfProtocolRegistration ()** Wrapper Function for the* OSPF protosw Entry Initialization** RETURNS : 1 if Initialization was successfull*          -1 if Initialization was not successfull*/int ospf_protocol_registration    (    struct protosw ospf_protocol_switch#if defined (VIRTUAL_STACK)    ,    int vsNumber#endif /* VIRTUAL_STACK */    )    {    FAST struct protosw *   p_ospf_protocol_switch;#if defined (VIRTUAL_STACK)    virtualStackNumTaskIdSet (vsNumber);#endif /* VIRTUAL_STACK */    p_ospf_protocol_switch = &ospf_protocol_switch;    if (_protoSwIndex >= ((sizeof (inetsw)) / (sizeof (inetsw[0]))))        {        printf ("OSPF: _protoSwIndex not correct ... Returning error\r\n");        return (-1) ;        }    p_ospf_protocol_switch = &inetsw [_protoSwIndex];    if (p_ospf_protocol_switch->pr_domain != NULL)        {        return (1);                 /* already initialized */        }    p_ospf_protocol_switch->pr_type       = SOCK_RAW;    p_ospf_protocol_switch->pr_domain     = &inetdomain;    p_ospf_protocol_switch->pr_protocol  = IPPROTO_OSPF;    p_ospf_protocol_switch->pr_flags      = PR_ATOMIC|PR_ADDR;    /* use rip_input to receive packets from IP layer */    p_ospf_protocol_switch->pr_input    = rip_input;    /* pr_output and pr_ctlinput in protosw[] are not used by raw socket */    p_ospf_protocol_switch->pr_output   = 0;    p_ospf_protocol_switch->pr_ctlinput = 0;     p_ospf_protocol_switch->pr_ctloutput = rip_ctloutput;     p_ospf_protocol_switch->pr_usrreq    = rip_usrreq;     p_ospf_protocol_switch->pr_init      =  0 ;     p_ospf_protocol_switch->pr_fasttimo  =  0;     p_ospf_protocol_switch->pr_slowtimo  =  0;     p_ospf_protocol_switch->pr_drain     =  0;     p_ospf_protocol_switch->pr_sysctl    =  0;     ip_protox[IPPROTO_OSPF] = _protoSwIndex;     _protoSwIndex++;     return 1;    }/* * inaddress_to_ifp () * Wrapper Function written for the macro INADDR_TO_IFP * which is defined in netinet/in_var.h * * The INADDR_TO_IFP accesses the global in_ifaddr pointer which cannot be * accessed from the application domain context. * * RETURNS : ifnet* pointer if the address was found *         :  NULL   if the address was not found * */struct ifnet*    inaddress_to_ifp    (    struct in_addr addr    )    {    register struct in_ifaddr *ia;    struct ifnet*  ifp;    int s;    /* asr: need to acquire n/w semaphore before using in_ifaddr */#if defined (VIRTUAL_STACK)    virtualStackNumTaskIdSet (ospf.ospf_vsid);    s = splnet();for    (        ia = _in_ifaddr;    ia != NULL && (IA_SIN(ia)->sin_addr.s_addr != (addr).s_addr);    ia = ia->ia_next    )#else    s = splnet();for    (    ia = in_ifaddr;    ia != NULL && (IA_SIN(ia)->sin_addr.s_addr != (addr).s_addr);    ia = ia->ia_next    )#endif /* VIRTUAL_STACK */    continue;    ifp = (ia == NULL) ? NULL : ia->ia_ifp;    splx(s);    return ifp;    }/* * ospf_kernel_register_with_ip() - Wrapper Function for * ospf_register_with_ip () * * RETURNS: N/A */voidospf_kernel_register_with_ip    (    FP_OSPF_RECEIVE pospfReceive,    FP_OSPF_SEND pospfSend    )    {    OspfRegistrationInfo.pOSpfReceive = pospfReceive;    OspfRegistrationInfo.pOSpfSend    = pospfSend;    OspfRegistrationInfo.ospfEnabled  = 1;    return;    }/* * ospf_kernel_deregister_with_ip () - Wrapper Function for * ospf_deregister_with_ip () * */voidospf_kernel_deregister_with_ip    (    FP_OSPF_RECEIVE pospfReceive,    FP_OSPF_SEND pospfSend    )    {    OspfRegistrationInfo.pOSpfReceive = pospfReceive;    OspfRegistrationInfo.pOSpfSend = pospfSend;    OspfRegistrationInfo.ospfEnabled = 0;    return;    }/* * interface_address_to_ia () - Wrapper Function for the macro IFP_TO_IA declared in * netinet/in_var.h * * * * RETURNS : in_ifaddr* Pointer if the ifnet entry is found. *      NULL       if the ifnet Entry is not found * */struct in_ifaddr*    interface_address_to_ia    (    struct ifnet* ifp    )    {    struct in_ifaddr *ia;    int s;    /* asr: need to acquire n/w semaphore before using in_ifaddr */    #if defined (VIRTUAL_STACK)    virtualStackNumTaskIdSet(ospf.ospf_vsid);    s = splnet();    for    (    ia = _in_ifaddr;    ia != NULL && (ia)->ia_ifp != (ifp);    (ia) = (ia)->ia_next    )  #else  s = splnet();  for    (    ia = in_ifaddr;    ia != NULL && (ia)->ia_ifp != (ifp);    (ia) = (ia)->ia_next    ) #endif /* VIRTUAL_STACK */    {        continue;    }    splx(s);    return ia;    }/******************************************************************************** ospf_check_interface ()**  Wrapper Function to check for the Interface Up Flag in the* ifnet Structure** RETURNS : 1 if the ifnet Entry containing the given IP Address is found*           0 if the ifnet Entry not found for the given IP Address*/int    ospf_check_interface    (    unsigned long ip_address    )    {    struct ifnet *sptr_ifnet;    struct ifaddr* sptr_ifaddr;    struct in_ifaddr * sptr_interface_address;    int s;    sptr_ifnet = NULL;    sptr_ifaddr = NULL;#if defined (VIRTUAL_STACK)    virtualStackNumTaskIdSet (ospf.ospf_vsid);#endif /* VIRTUAL_STACK */    s = splimp ();#if !defined (VIRTUAL_STACK)    for (sptr_ifnet = ifnet; sptr_ifnet; sptr_ifnet = sptr_ifnet->if_next)#else    for (sptr_ifnet = _ifnet; sptr_ifnet; sptr_ifnet = sptr_ifnet->if_next)#endif        {        for        (        sptr_ifaddr = sptr_ifnet->if_addrlist;        sptr_ifaddr;        sptr_ifaddr = sptr_ifaddr->ifa_next        )            {            IFP_TO_IA (sptr_ifnet, sptr_interface_address);            if (sptr_interface_address == NULL)                {                continue;                }            /* check the sin family type for Internet */            if (sptr_interface_address->ia_addr.sin_family != AF_INET)                {                continue;                }            if (sptr_interface_address->ia_addr.sin_addr.s_addr == ip_address)                {                if (sptr_ifnet->if_flags &IFF_UP)                    {                    splx (s);                    return 1;                    }                else                 printf("OSPF: INTERFACE DOWN with address (HEX) %lx\r\n", ip_address);                }            } /* for */        }     /* for */    splx (s);    return 0;    }/******************************************************************************** join_multicast_group ()** Wrapper Function written for the in_addmulti function* which cannot be accessed in the  Application Domain.** RETURNS : mBlk* Pointer if in_addmulti was successfull* NULL  if in_addmulti fails*/struct mBlk* join_multicast_group    (    struct in_addr* multicast_address,    unsigned long ospf_ip_address,    struct inpcb* inpcbptr    )    {    struct mBlk* mBlkptr = NULL;    struct in_addr ip_address;    struct ifnet* vx_ospf_interface;    int s;    ip_address.s_addr = ospf_ip_address;#if defined (VIRTUAL_STACK)    virtualStackNumTaskIdSet (ospf.ospf_vsid);#endif /* VIRTUAL_STACK */    /* asr: need to acquire semaphore */    s = splnet();    INADDR_TO_IFP (ip_address, vx_ospf_interface);    splx(s);    if(NULL == (mBlkptr = in_addmulti (multicast_address, vx_ospf_interface, inpcbptr)))        {#if defined(__MISTRAL_DEBUG__)         printf("OSPF: in_addmulti failed for address (HEX) %lx\r\n",             multicast_address->s_addr);#endif /* (__MISTRAL_DEBUG__) */         return NULL;        }    else        {         return mBlkptr;        }    }/******************************************************************************** ospf_check_multicast_if_bit ()** Wrapper function to est the IFF_MULTICAST_IF flag in the ifnet structure** RETURNS : 1 if the IFF_MULTICAST_IF bit is set for the ifnet entry for the* given IP Address 0 if the IFF_MULTICAST_IF bit is reset for the ifnet entry* for the given Address*/int ospf_check_multicast_if_bit    (    unsigned long ip_address    )    {    struct ifnet* pifnet;    struct in_addr addr;    addr.s_addr = ip_address;    pifnet = inaddress_to_ifp (addr);    if (pifnet && (pifnet->if_flags & IFF_MULTICAST))        return 1;    else        return 0;    }/******************************************************************************** ospf_check_interface_address_for_receive ()** Wrapper function to Check for the* interface address while receving IP packets from the raw socket*** RETURNS : 1 if the Received Interface Address Matches the OSPF Port Address* 0 if the Received Interface Address does not match the OSPF Port Address**/int ospf_check_interface_address_for_receive    (    unsigned long ip_address,    struct ifnet* pifnet    )    {    struct in_ifaddr* interface_address;    int s;#if defined (VIRTUAL_STACK)    virtualStackNumTaskIdSet (ospf.ospf_vsid);#endif /* VIRTUAL_STACK */    /* asr: need to acquire semaphore */    s = splnet();    IFP_TO_IA (pifnet, interface_address);    splx(s);    if (ip_address  == interface_address->ia_addr.sin_addr.s_addr)        return 1;    else        return 0;    }/******************************************************************************** ospf_check_interface_index ()** Wrapper function to check the ifnet index member for the* receiving interface.** RETURNS : 1 if the Received Interface Index matches with the given ifnet** Pointer's Index 0 if the Received Interface Index matches with the given* ifnet* Pointer's Index*/int ospf_check_interface_index    (    struct ifnet* p_ospf_ifnet,    struct ifnet* p_receive_ifnet    )    {    if (p_ospf_ifnet->if_index == p_receive_ifnet->if_index)        return 1;    else        return 0;    }/******************************************************************************** ospf_if_status()- Wrapper Function to check for the Interface UP or DOWN* Flag in the ifnet structure.** RETURNS : 2 if the ifnet Entry Flag is UP.*           1 if the ifnet Entry Flag is DOWN.*           0 if the ifnet Entry does not contain ip_address*/int    ospf_if_status    (    unsigned long   intf,    int         interface_index,    unsigned long   ip_address    )    {    struct ifnet *sptr_ifnet;    struct ifaddr* sptr_ifaddr;    struct in_ifaddr * sptr_interface_address;    int s;    sptr_ifnet = NULL;    sptr_ifaddr = NULL;#if defined (VIRTUAL_STACK)    virtualStackNumTaskIdSet (ospf.ospf_vsid);#endif /* VIRTUAL_STACK */    s = splimp ();#if !defined (VIRTUAL_STACK)    for (sptr_ifnet = ifnet; sptr_ifnet; sptr_ifnet = sptr_ifnet->if_next)#else    for (sptr_ifnet = _ifnet; sptr_ifnet; sptr_ifnet = sptr_ifnet->if_next)#endif        {        if(sptr_ifnet == (struct ifnet*)intf &&               sptr_ifnet->if_index == interface_index)        {            for            (            sptr_ifaddr = sptr_ifnet->if_addrlist;            sptr_ifaddr;            sptr_ifaddr = sptr_ifaddr->ifa_next            )            {                IFP_TO_IA (sptr_ifnet, sptr_interface_address);                if (sptr_interface_address == NULL)                {                continue;                }                /* check the sin family type for Internet */                if (sptr_interface_address->ia_addr.sin_family != AF_INET)                {                    continue;                }                if (sptr_interface_address->ia_addr.sin_addr.s_addr ==                    ip_address)                {                    if (sptr_ifnet->if_flags & IFF_UP)                    {                        splx (s);                        return 2;                    }                    else                    {                        splx(s);                        return 1;                    }                }            } /* for */           } /* if */        }     /* for */    splx (s);    return 0;}#endif /*__OSPF_ROUTER_STACK__*/

⌨️ 快捷键说明

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