📄 net_appl.c
字号:
/************************************************************************
*
* net.c
*
* The 'NET' module implements the common utility functions, neede
* by the NET-modules.
*
*
* ######################################################################
*
* 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>
#include <sys_api.h>
#include <shell_api.h>
/* net stuff */
#include <net_api.h>
#include "net.h"
#include "net_mac.h"
#include "net_arp.h"
#include "net_ip.h"
#include "net_icmp.h"
#include "net_tftpc.h"
/************************************************************************
* Definitions
************************************************************************/
/************************************************************************
* Public variables
************************************************************************/
/************************************************************************
* Static variables
************************************************************************/
static bool appl_reply_received ;
static UINT16 appl_reply_sequence ;
static UINT16 appl_reply_length ;
static UINT32 appl_reply_ip ;
static t_mac_addr appl_reply_mac_adr ;
static UINT16 appl_sequence ;
static UINT16 appl_length ;
/************************************************************************
* Static function prototypes
************************************************************************/
/************************************************************************
*
* NET_APPL_echo_reply
* Description :
* -------------
* Initialize the net applications
*
*
* Parameters :
* ------------
* -
*
*
* Return values :
* ---------------
* 'OK'(=0)
*
************************************************************************/
static
UINT32 NET_APPL_echo_reply( UINT32 src_ip_adr, /* BE-format */
t_mac_addr *src_mac_adr,/* MAC */
UINT16 sequence, /* CPU-format */
UINT16 usr_length ) ;
/************************************************************************
* Implementation : Public functions
************************************************************************/
/************************************************************************
*
* NET_APPL_init
* Description :
* -------------
* Initialize the net applications
*
*
* Parameters :
* ------------
* -
*
*
* Return values :
* ---------------
* 'OK'(=0)
*
************************************************************************/
UINT32 NET_APPL_init( void )
{
UINT32 rcode ;
/* register ECHO-REPLY handler */
IF_ERROR( (rcode),
(NET_ICMP_ECHO_open( NET_APPL_echo_reply )) )
}
/************************************************************************
*
* NET_ping
* Description :
* -------------
* Poll the net modules
*
*
* Parameters :
* ------------
* -
*
*
* Return values :
* ---------------
* 'OK'(=0)
*
************************************************************************/
UINT32
NET_ping( UINT32 ipadr, UINT32 data_size )
{
UINT32 dot_count = 0;
UINT32 compl, rcode, time_start, time_now ;
UINT32 retry = 3 ;
#ifdef NET_DEBUG
printf("net_ping\n") ;
#endif
/* clear error context */
net_last_error = OK ;
net_diag_msg[0] = 0 ;
/* check data_size */
if ( data_size > PING_MAX_DATAGRAM_SIZE )
{
data_size = PING_DEFAULT_DATAGRAM_SIZE ;
}
/* Set context before request is called */
appl_reply_received = FALSE ;
appl_sequence = data_size ;
appl_length = data_size ;
/* Request ICMP to 'send' */
compl = NET_ICMP_ECHO_send( ipadr, NULL, appl_sequence, appl_length ) ;
if ( (compl != OK) &&
(compl != ERROR_NET_ARP_MAC_NOT_RESOLUTED) )
{
return( compl ) ;
}
/* Get time right now */
IF_ERROR( (rcode),
(NET_gettime( &time_start)) )
while (1)
{
/* Poll for any reply */
NET_poll() ;
if ( appl_reply_received )
{
if ( ( appl_sequence == appl_reply_sequence ) &&
( appl_length == appl_reply_length ) &&
( ipadr == appl_reply_ip ) )
{
if( dot_count )
{
printf("\n") ;
}
ipadr = BE32_TO_CPU(ipadr) ;
printf("%d bytes ICMP-ECHO-REPLY user data received from %d.%d.%d.%d\n",
appl_length,
(ipadr>>24)&0xff,
(ipadr>>16)&0xff,
(ipadr>> 8)&0xff,
(ipadr )&0xff ) ;
rcode = OK ;
break ;
}
}
/* Get time right now */
IF_ERROR( (rcode),
(NET_gettime( &time_now)) )
if ((time_now - time_start) >= 1)
{
if(shell_print_dot( &dot_count ))
{
rcode = SHELL_ERROR_CONTROL_C_DETECTED ;
break ;
}
/* Do we have any retries left */
if (!retry)
{
rcode = compl ;
if ( compl == ERROR_NET_ARP_MAC_NOT_RESOLUTED )
{
net_last_error = ERROR_NET_ARP_TIME_OUT ;
rcode = net_last_error ;
}
if ( compl == OK )
{
net_last_error = ERROR_NET_PING_TIME_OUT ;
rcode = ERROR_NET_PING_TIME_OUT ;
}
if( dot_count )
{
printf("\n") ;
}
break ;
}
/* retransmit */
time_start = time_now ;
/* Request ICMP to 'send' */
compl = NET_ICMP_ECHO_send( ipadr, NULL, appl_sequence, appl_length ) ;
retry-- ;
}
}
return( rcode ) ;
}
/************************************************************************
*
* TEST_ping
* Description :
* -------------
* Special version of ping for production test:
* non verbose
* without retries
* with variable timeout in milliseconds
*
* The lack of retries makes it important to avoid false timeouts.
* The time algorithm depends on the fact, that the timeout value
* plus the maximum possible timer value will not wrap.
* Timeout is measured in milliseconds.
* The raw timer wraps every 16777216 microsecs ~ 16777 millisec
*
* Parameters :
* ------------
* -
*
*
* Return values :
* ---------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -