📄 net_udp.c
字号:
/************************************************************************
*
* NET_UDP.c
*
* The 'NET_UDP' module implements the UDP layer of the IP-protocols
* and and it provides services to send and receive
* UDP datagrams via service access points, which are linked
* with the 'port'-field of the UDP-header.
*
*
* ######################################################################
*
* 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_ip.h"
#include "net_udp.h"
/************************************************************************
* Definitions
************************************************************************/
/************************************************************************
* Public variables
************************************************************************/
/************************************************************************
* Static variables
************************************************************************/
/* Global state of UDP */
static UINT32 NET_UDP_state = UDP_STATE_CLOSED ;
/* The IP SP-handle, returned by 'IP-open' */
static UINT32 ip_sp_hd ;
/* UDP SAP table */
static t_udp_sap_context sap_context[UDP_SAP_COUNT] ;
/* UDP SAP allocation variable, keeping a simple counter
to be used for allocating unique 'port' numbers
in UDP-'open' */
static UINT16 udp_sap_next ;
/************************************************************************
* Static function prototypes
************************************************************************/
/************************************************************************
*
* NET_UDP_receive
* Description :
* -------------
* This function is registered in the IP-module and linked with the
* IP-protocol = '17', to let the IP call us back with a reference
* to the received frame, containing an UDP-frame.
* In this function the UDP-header will be validated and
* the any opened UDP-SAP will be checked to call a user registered
* receive handler.
*
*
*
* Parameters :
* ------------
* 'src_ip_adr': sender's IP-address (BE-format)
* 'src_mac_adr': sender's MAC-address
* 'length': length of received ethernet frame
* 'data': pointer for received ethernet frame (in driver's space)
*
*
* Return values :
* ---------------
* 'OK'
*
************************************************************************/
static
UINT32 NET_UDP_receive( UINT32 src_ip_adr,
t_mac_addr *src_mac_adr,
UINT32 length,
UINT8 *data ) ;
/************************************************************************
* Implementation : Public functions
************************************************************************/
/************************************************************************
*
* NET_UDP_init
* Description :
* -------------
* Initialize the UDP module.
*
*
* Parameters :
* ------------
* -
*
*
* Return values :
* ---------------
* 'OK'(=0), successfull initialization
*
************************************************************************/
UINT32 NET_UDP_init( void )
{
UINT32 rcode ;
int i ;
/* initialize UDP SAP table */
for ( i = 0; i <IP_SAP_COUNT ; i++)
{
sap_context[i].sap_state = UDP_SAP_STATE_CLOSED ;
sap_context[i].sap = UDP_SAP_UNDEFINED ;
sap_context[i].usr_receive = NULL ;
}
/* first unique SAP to be used at 'open' */
udp_sap_next = UDP_SAP_RESERVED_UPPER + 1 ;
/* register our receive handler */
IF_ERROR( (rcode),
(NET_IP_open( IP_SAP_UDP,
NET_UDP_receive,
&ip_sp_hd) ) )
/* UDP-module has now been initialized and opened for service */
NET_UDP_state = UDP_STATE_OPEN ;
return(OK) ;
}
/************************************************************************
*
* NET_UDP_open
* Description :
* -------------
* Allocate a UDP-SAP and register user context.
*
*
* Parameters :
* ------------
* 'sap_id', IN, value of UDP-'port' (our) to bind for.
* If the value is 'undefined'(=0), the
* UDP-module allocates a unique port.
* 'usr_receive', IN, user-receive function to be registered
* 'sp_hd', OUT, handle of UDP to be used by user by call
* of 'send' or 'close'
*
* Return values :
* ---------------
* 'ERROR_NET_UDP_FATAL_STATE' A fatal state has been detected in UDP.
* 'ERROR_NET_UDP_NOT_INITIALIZED' UDP-'init' has not been called.
* 'OK'(=0),
*
*
************************************************************************/
UINT32 NET_UDP_open( UINT16 sap_id, /* port in BE-format */
t_udp_usr_receive usr_receive,
UINT32 *sp_hd )
{
UINT32 rcode = OK ;
int i ;
switch (NET_UDP_state)
{
case UDP_STATE_CLOSED:
rcode = ERROR_NET_UDP_NOT_INITIALIZED ;
break;
case UDP_STATE_OPEN:
/* Validate or find unique sap */
if ( sap_id == UDP_SAP_UNDEFINED )
{
sap_id = CPU_TO_BE16( udp_sap_next ) ;
udp_sap_next++ ;
if (udp_sap_next == 0)
{
udp_sap_next = UDP_SAP_RESERVED_UPPER + 1 ;
}
}
for ( i = 0; i <UDP_SAP_COUNT ; i++)
{
if ( (sap_context[i].sap_state == UDP_STATE_OPEN) &&
(sap_context[i].sap == sap_id) )
{
/* we have found an unuseable SAP */
return( ERROR_NET_UDP_SAP_INUSE ) ;
}
}
/* allocate a free UDP SAP table entry */
for ( i = 0; i <UDP_SAP_COUNT ; i++)
{
if ( sap_context[i].sap_state == UDP_SAP_STATE_CLOSED )
{
/* save SAP context in allocated entry */
sap_context[i].sap_state = UDP_SAP_STATE_OPEN ;
sap_context[i].sap = sap_id ;
sap_context[i].usr_receive = usr_receive ;
*sp_hd = i ;
return( OK ) ;
}
}
rcode = ERROR_NET_UDP_NO_FREE_SAP ;
break;
default:
/* we should never arrive here */
rcode = ERROR_NET_UDP_FATAL_STATE ;
break;
}
return( rcode ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -