📄 net_ip.c
字号:
/************************************************************************ * * NET_IP.c * * The 'NET_IP' module implements the IP-layer services to provide * service access points, which are linked with the 'protocol'-field * of the IP-header. * * ###################################################################### * * 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>/* 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"/************************************************************************ * Definitions ************************************************************************//************************************************************************ * Public variables ************************************************************************//************************************************************************ * Static variables ************************************************************************//* Global state of IP */static UINT32 NET_IP_state = IP_STATE_CLOSED ;/* The MAC SP-handle, returned by 'MAC-open' */static UINT32 mac_sp_hd ;/* SAP table */static t_ip_sap_context sap_context[IP_SAP_COUNT] ;/* Global identification of IP datagrams; implemented as a counter */static UINT32 NET_IP_identification = 0 ;/************************************************************************ * Static function prototypes ************************************************************************//************************************************************************ * * NET_IP_receive * Description : * ------------- * This function is registered in the MAC-module and linked with the * MAC-type = '0x800', to let the MAC call us back with an reference * to the received frame, containing an IP-frame. * In this function the IP-header will be validated and the IP-SAP * table will be searched for any match between the actual value * of the IP-protocol field and a registered user to handle upper * protocol layer. * * * * Parameters : * ------------ * 'src_mac_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_IP_receive( t_mac_addr *src_mac_adr, UINT32 length, UINT8 *data ) ;/************************************************************************ * * NET_IP_alloc_sap * Description : * ------------- * Allocate a IP SAP, register user context and return a sp-handle. * * * Parameters : * ------------ * * 'sap_id', IN, value of IP-'protocol' to bind for * 'ip_usr_receive', IN, user-receive function to be registered * 'ip_sp_hd', OUT, handle of IP to be used by user by call * of 'send' or 'close' * * * Return values : * --------------- * 'ERROR_NET_IP_NO_FREE_SAP', No SAP entry could be allocated * 'OK' = 0x00: * * ************************************************************************/staticUINT32 NET_IP_alloc_sap( UINT8 sap_id, t_ip_usr_receive ip_usr_receive, UINT32 *ip_sp_hd ) ;/************************************************************************ * Implementation : Public functions ************************************************************************//************************************************************************ * * NET_IP_init * Description : * ------------- * Initialize the IP module. * * * Parameters : * ------------ * - * * * Return values : * --------------- * 'OK'(=0), successfull initialization * ************************************************************************/UINT32 NET_IP_init( void ){ int i ; /* initialize IP SAP table */ for ( i = 0; i <IP_SAP_COUNT ; i++) { sap_context[i].ip_sap_state = IP_SAP_STATE_CLOSED ; sap_context[i].ip_sap = IP_SAP_UNDEFINED ; sap_context[i].ip_usr_receive = NULL ; } /* IP-module has now been initialized */ NET_IP_state = IP_STATE_INITED ; return(OK) ;}/************************************************************************ * * NET_IP_open * Description : * ------------- * Allocate a IP-SAP and register user context. * * * Parameters : * ------------ * 'sap_id', IN, value of IP-'protocol' to bind for * 'usr_receive', IN, user-receive function to be registered * 'ip_sp_hd', OUT, handle of IP to be used by user by call * of 'send' or 'close' * * Return values : * --------------- * 'ERROR_NET_IP_FATAL_STATE' A fatal state has been detected in IP. * 'ERROR_NET_IP_NOT_INITIALIZED' IP-'init' has not been called. * 'OK'(=0), * * ************************************************************************/UINT32 NET_IP_open( UINT8 sap_id, t_ip_usr_receive ip_usr_receive, UINT32 *ip_sp_hd ){ UINT32 rcode = OK ; switch (NET_IP_state) { case IP_STATE_CLOSED: rcode = ERROR_NET_IP_NOT_INITIALIZED ; break; case IP_STATE_INITED: /* try allocating a SAP */ rcode = NET_IP_alloc_sap( sap_id, ip_usr_receive, ip_sp_hd ) ; if ( rcode == OK ) { /* By first 'open', we need to create our SAP in MAC */ rcode = NET_MAC_open( MAC_SAP_IP, NET_IP_receive, &mac_sp_hd ) ; if ( rcode == OK ) { NET_IP_state = IP_STATE_OPEN ; } } break; case IP_STATE_OPEN: /* try allocating a SAP */ rcode = NET_IP_alloc_sap( sap_id, ip_usr_receive, ip_sp_hd ) ; break; default: /* we should never arrive here */ rcode = ERROR_NET_IP_FATAL_STATE ; break; } return( rcode ) ;}/************************************************************************ * * NET_IP_close * Description : * ------------- * Close a IP-SAP. * * * Parameters : * ------------ * 'ip_sp_hd' service provider defined handle * * * Return values : * --------------- * 'ERROR_NET_IP_INVALID_HANDLE', invalid handle * 'OK'(=0), SAP has been closed * * ************************************************************************/UINT32 NET_IP_close( UINT32 ip_sp_hd ){ /* validate handle */ IF_UPPER( ERROR_NET_IP_INVALID_HANDLE, ip_sp_hd, IP_SAP_COUNT) /* close SAP */ sap_context[ip_sp_hd].ip_sap_state = IP_SAP_STATE_CLOSED ; sap_context[ip_sp_hd].ip_sap = IP_SAP_UNDEFINED ; sap_context[ip_sp_hd].ip_usr_receive = NULL ; return(OK) ;}/************************************************************************ * * NET_IP_send * Description : * ------------- * Request the IP module to send a frame, linked * to a certain 'SAP' to a specified IP-destination address. * * * Parameters : * ------------ * 'ip_sp_hd', IN, handle to lookup a registered SAP context * 'dst_adr', IN, destination ip address (BE-format) * 'dst_mac_adr', IN, destination MAC address (may be undefined =NULL) * 'length', IN, length of frame to send * 'data', IN, address to start of frame to be send * * * Return values : * --------------- * 'OK'(=0) * ************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -