net_appl.c

来自「MIPS YAMON, a famous monitor inc. source」· C语言 代码 · 共 632 行 · 第 1/2 页

C
632
字号
/************************************************************************ * *      net.c * *      The 'NET' module implements the common utility functions, neede *      by the NET-modules. * * ###################################################################### * * 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>#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)  * ************************************************************************/staticUINT32 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 )) )    return( OK ) ; }/************************************************************************ * *                          NET_ping *  Description : *  ------------- *  Poll the net modules * * *  Parameters : *  ------------ *  - * * *  Return values : *  --------------- *  'OK'(=0)  * ************************************************************************/UINT32NET_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") ;                }                printf("%d bytes ICMP-ECHO-REPLY user data received from %d.%d.%d.%d\n",                                                         appl_length,                                                        ((UINT8*)&ipadr)[0],                                                        ((UINT8*)&ipadr)[1],                                                        ((UINT8*)&ipadr)[2],                                                        ((UINT8*)&ipadr)[3] );                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

⌨️ 快捷键说明

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