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

📄 net_arp.c

📁 MIPS YAMON, a famous monitor inc. source, make file and PDF manuals.
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************ * *      NET_ARP.c * *      The 'NET_ARP' module implements the ARP protocol and ARP *      services to provide access to the ARP cache, which contains *      entries of set of IP and ARP addresses. Only IP-addresses *      requested by the user are kept in the cache until they are *      timed-out after a certain period without being requested. * * ###################################################################### * * mips_start_of_legal_notice *  * Copyright (c) 2004 MIPS Technologies, Inc. All rights reserved. * * * Unpublished rights (if any) reserved under the copyright laws of the * United States of America and other countries. * * This code is proprietary to MIPS Technologies, Inc. ("MIPS * Technologies"). Any copying, reproducing, modifying or use of this code * (in whole or in part) that is not expressly permitted in writing by MIPS * Technologies or an authorized third party is strictly prohibited. At a * minimum, this code is protected under unfair competition and copyright * laws. Violations thereof may result in criminal penalties and fines. * * MIPS Technologies reserves the right to change this code to improve * function, design or otherwise. MIPS Technologies does not assume any * liability arising out of the application or use of this code, or of any * error or omission in such code. Any warranties, whether express, * statutory, implied or otherwise, including but not limited to the implied * warranties of merchantability or fitness for a particular purpose, are * excluded. Except as expressly provided in any written license agreement * from MIPS Technologies or an authorized third party, the furnishing of * this code does not give recipient any license to any intellectual * property rights, including any patent rights, that cover this code. * * This code shall not be exported, reexported, transferred, or released, * directly or indirectly, in violation of the law of any country or * international law, regulation, treaty, Executive Order, statute, * amendments or supplements thereto. Should a conflict arise regarding the * export, reexport, transfer, or release of this code, the laws of the * United States of America shall be the governing law. * * This code constitutes one or more of the following: commercial computer * software, commercial computer software documentation or other commercial * items. If the user of this code, or any related documentation of any * kind, including related technical data or manuals, is an agency, * department, or other entity of the United States government * ("Government"), the use, duplication, reproduction, release, * modification, disclosure, or transfer of this code, or any related * documentation of any kind, is restricted in accordance with Federal * Acquisition Regulation 12.212 for civilian agencies and Defense Federal * Acquisition Regulation Supplement 227.7202 for military agencies. The use * of this code by the Government is further restricted in accordance with * the terms of the license agreement(s) and/or applicable contract terms * and conditions covering this code from MIPS Technologies or an authorized * third party. * * * *  * mips_end_of_legal_notice *  * ************************************************************************//************************************************************************ *      Include files ************************************************************************/#include <string.h>#include <stdio.h>#include <sysdefs.h>#include <syserror.h>#include <sysdev.h>#include <syscon_api.h>/* net stuff */#include <net_api.h>#include "net.h"#include "net_mac.h"#include "net_arp.h"#include "net_ip.h"/************************************************************************ *      Definitions ************************************************************************//************************************************************************ *      Public variables ************************************************************************//************************************************************************ *      Static variables ************************************************************************//* System poll function pointer, initialized by 'init' */static UINT32 (*syspoll)(void) = NULL ;/* Global state of ARP */static UINT32 NET_ARP_state = ARP_STATE_CLOSED ;/* The MAC SP-handle, returned by 'MAC-open' */static UINT32 mac_sp_hd ;/* Next poll time (seconds) */static UINT32 next_poll_time ;/* SAP table */static t_arp_sap_context sap_context[ARP_SAP_COUNT] ;/************************************************************************ *      Static function prototypes ************************************************************************//************************************************************************ * *                          NET_ARP_receive *  Description : *  ------------- *  This function gets called from the MAC-module whenever the ARP *  registered SAP (0x806) are addressed in a received frame. * * *  Parameters : *  ------------ *  'src_adr': senders MAC-address *  'length':  length of received ethernet frame *  'data':    pointer for received ethernet frame (in driver's space) * * *  Return values : *  --------------- *  'OK'  * ************************************************************************/staticUINT32 NET_ARP_receive( t_mac_addr *src_adr,                        UINT32     length,                        UINT8      *data    ) ;/************************************************************************ * *                          NET_ARP_alloc_sap *  Description : *  ------------- *  Allocate an ARP SAP and register user context. *  * *  Parameters : *  ------------ *  'ip_addr',          IN,    IP-address, which MAC-address is to be *                             resoluted *  'mac_addr',         INOUT, MAC-address, which may be defined *                             by the caller. At return, the MAC-address *                             may be defined, if the cache contains *                             an entry in state 'resoluted' of the *                             specified IP-address. * * *  Return values : *  --------------- * 'OK' = 0x00:  * * ************************************************************************/staticUINT32 NET_ARP_alloc_sap( UINT32     ip_addr,                          t_mac_addr *mac_addr ) ;/************************************************************************ * *                          NET_ARP_reply *  Description : *  ------------- *  Send a 'reply' with this stations IP-and MAC-addresses to 'requester'. * * *  Parameters : *  ------------ *  'ip_addr',          IN,    IP-address of 'requester' *  'mac_addr',         IN,    MAC-address of 'requester' * * *  Return values : *  --------------- * 'OK' = 0x00: * * ************************************************************************/staticUINT32 NET_ARP_reply( UINT32     ip_addr,                      t_mac_addr *mac_addr ) ;/************************************************************************ * *                          NET_ARP_request *  Description : *  ------------- *  Broadcast a 'request' to resolute the MAC-address of the specified *  'ip_addr'. * * *  Parameters : *  ------------ *  'ip_addr',          IN,    IP-address, which must be MAC-address *                             resoluted.  * * *  Return values : *  --------------- * 'OK' = 0x00: * * ************************************************************************/staticUINT32 NET_ARP_request( UINT32 ip_addr ) ;/************************************************************************ *      Implementation : Public functions ************************************************************************//************************************************************************ * *                          NET_ARP_init *  Description : *  ------------- *  Initialize the ARP module. * * *  Parameters : *  ------------ *  - * * *  Return values : *  --------------- *  'OK'(=0), successfull initialization * ************************************************************************/UINT32 NET_ARP_init( UINT32 (*poll)(void) ){    UINT32 rcode ;    int    i ;    /* initialize system poll function */    syspoll = poll ;    /* initialize ARP SAP table */    for ( i = 0; i < ARP_SAP_COUNT; i++ )    {        sap_context[i].state   = ARP_SAP_STATE_CLOSED ;        sap_context[i].ip_addr = IP_ADDR_UNDEFINED ;        sap_context[i].timeout = 0 ;        memcpy( sap_context[i].mac_addr,                 mac_undefined_adr,                SYS_MAC_ADDR_SIZE ) ;    }    /* We need to create our ARP-SAP in the MAC module, as ARP must       be operational to respond on 'request' */    rcode = NET_MAC_open( MAC_SAP_ARP,                          NET_ARP_receive,                          &mac_sp_hd ) ;    if ( rcode == OK )     {        /* ARP-module is now operational */        NET_ARP_state = ARP_STATE_OPEN ;    }    /* define first poll time */    next_poll_time = 0 ;    return( rcode ) ;}/************************************************************************ * *                          NET_ARP_open *  Description : *  ------------- *  This service is called, whenever *    a) the MAC-address of the specified IP-address should be found *       (either in the cache or an ARP-request is being broadcasted) or *    b) a set of known (MAC,IP)-addresses are to be stored in the cache. * *  Any entries in the ARP-cache are situated in one of the states: *    a) 'closed', i.e. this entry is free and may be allocated, when a *                 new set of (MAC,IP) shall be resoluted *    b) 'allocated', i.e. the IP-address is stored in the entry and an *                    ARP-request has been broadcasted, but the MAC *                    address has not been resoluted by an ARP-reply *    c) 'resoluted', i.e. the MAC-address of the IP-address has been *                    resoluted. * *  In the states, 'allocated' and 'resoluted', an entry is being *  supervised by a timer. By each call of this service ('open'), *  a reference to an entry in one these two states, will reset *  the supversion timer. By expiration, the entry will be deleted *  and the state of the 'entry' will be set to 'closed'. * * *  Parameters : *  ------------ *  'ip_addr',          IN,    IP-address (BE-format), which *                             MAC-address is to be resoluted *  'mac_addr',         INOUT, MAC-address, which may be defined *                             by the caller. At return, the MAC-address *                             may be defined, if the cache contains *                             an entry in state 'resoluted' of the *                             specified IP-address. * * *  Return values : *  --------------- *  'ERROR_NET_ARP_NOT_INITIALIZED' *  'ERROR_NET_ARP_FATAL_STATE' *  'OK'                              MAC-address has been resoluted * ************************************************************************/UINT32 NET_ARP_open( UINT32     ip_addr,                     t_mac_addr *mac_addr ){    UINT32 old_ie, loop ;    UINT32 rcode = OK ;    loop = 3 ;    while (1)    {        switch (NET_ARP_state)        {            case ARP_STATE_CLOSED:                    rcode = ERROR_NET_ARP_NOT_INITIALIZED ;                break;            case ARP_STATE_OPEN:                    /* try allocating a SAP */                    old_ie = sys_disable_int() ;                    rcode = NET_ARP_alloc_sap( ip_addr, mac_addr ) ;                    if(old_ie) sys_enable_int();                break;            default:                    /* we should never arrive here */                    rcode = ERROR_NET_ARP_FATAL_STATE ;                break;        }        loop-- ;        if ( (rcode    == ERROR_NET_ARP_MAC_NOT_RESOLUTED) &&              (syspoll != NULL) &&             (loop     >  0) )        {            /* Wait ~5 msec */            sys_wait_ms(5);            (*syspoll)() ;        }        else        {            break ;        }    }    return( rcode ) ;}/************************************************************************ * *                          NET_ARP_poll *  Description : *  ------------- *  Scans the ARP-cache to check for timer-expired entries, which *  will be removed from the cache. * * *  Parameters : *  ------------ *  - * *

⌨️ 快捷键说明

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