📄 net_arp.c
字号:
/************************************************************************
*
* 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.
*
*
* ######################################################################
*
* Copyright (c) 1999-2000 MIPS Technologies, Inc. All rights reserved.
*
* Unpublished rights reserved under the Copyright Laws of the United States of
* America.
*
* This document contains information that is proprietary to MIPS Technologies,
* Inc. ("MIPS Technologies"). Any copying, modifying or use of this information
* (in whole or in part) which is not expressly permitted in writing by MIPS
* Technologies or a contractually-authorized third party is strictly
* prohibited. At a minimum, this information is protected under unfair
* competition laws and the expression of the information contained herein is
* protected under federal copyright laws. Violations thereof may result in
* criminal penalties and fines.
* MIPS Technologies or any contractually-authorized third party reserves the
* right to change the information contained in this document to improve
* function, design or otherwise. MIPS Technologies does not assume any
* liability arising out of the application or use of this information. Any
* license under patent rights or any other intellectual property rights owned
* by MIPS Technologies or third parties shall be conveyed by MIPS Technologies
* or any contractually-authorized third party in a separate license agreement
* between the parties.
* The information contained in this document constitutes one or more of the
* following: commercial computer software, commercial computer software
* documentation or other commercial items. If the user of this information, 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 information, 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 information 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 information from MIPS Technologies or any
* contractually-authorized third party.
*
************************************************************************/
/************************************************************************
* 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'
*
************************************************************************/
static
UINT32 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:
*
*
************************************************************************/
static
UINT32 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:
*
*
************************************************************************/
static
UINT32 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:
*
*
************************************************************************/
static
UINT32 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, start, next, count, 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 */
SYSCON_read( SYSCON_BOARD_GET_MILLISEC_ID,
&start,
sizeof(start) ) ;
count = 0 ;
while ( count < 5 )
{
SYSCON_read( SYSCON_BOARD_GET_MILLISEC_ID,
&next,
sizeof(next) ) ;
if (next != start)
{
start = next ;
count++ ;
}
}
(*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 :
* ------------
* -
*
*
* Return values :
* ---------------
* 'OK'(=0)
*
************************************************************************/
UINT32 NET_ARP_poll( void )
{
int i ;
UINT32 time_now ;
UINT32 rcode ;
UINT32 old_ie;
/* get time now (unit is seconds since 1.1.1970) */
rcode = NET_gettime( &time_now ) ;
if (time_now < next_poll_time)
{
return( rcode ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -