⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udp.c

📁 用于以太网开发
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************                      
 *
 *       Copyright (C) 2003 Freescale Semiconductor, Inc. 
 *           and 2000-2002 Viola Systems Ltd.
 *       All Rights Reserved
 *
 * File Name      : udp.c
 * Project Name   : Connector_App.mcp
 * Description    : OpenTCP UDP implementation. All functions necessary for 
 *                    UDP packet processing are present here. Note that 
 *                    only a small subset of these functions must be used 
 *                    for "normal" applications that are using the UDP for 
 *                    communciation. For function declarations and lots of 
 *                    other usefull stuff see tcp_ip.h in the OpenTCP folder.
 *
 *	For examples on how to use UDP and write applications that communicate
 *	using UDP see the demo folder.
 *
 * Version : 1.0
 * Date    : 15.7.2002
 *
 ***************************************************************************/

/*
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions 
 *are met:
 *
 *1. Redistributions of source code must retain the above copyright 
 *notice, this list of conditions and the following disclaimer.
 *
 *2. Redistributions in binary form must reproduce the above copyright 
 *notice, this list of conditions and the following disclaimer in the 
 *documentation and/or other materials provided with the distribution.
 *
 *3. The end-user documentation included with the redistribution, if 
 *any, must include the following acknowledgment:
 *	"This product includes software developed by Viola 
 *	Systems (http://www.violasystems.com/)."
 *
 *Alternately, this acknowledgment may appear in the software itself, 
 *if and wherever such third-party acknowledgments normally appear.
 *
 *4. The names "OpenTCP" and "Viola Systems" must not be used to 
 *endorse or promote products derived from this software without prior 
 *written permission. For written permission, please contact 
 *opentcp@opentcp.org.
 *
 *5. Products derived from this software may not be called "OpenTCP", 
 *nor may "OpenTCP" appear in their name, without prior written 
 *permission of the Viola Systems Ltd.
 *
 *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 
 *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 *MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 *IN NO EVENT SHALL VIOLA SYSTEMS LTD. OR ITS CONTRIBUTORS BE LIABLE 
 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 *EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *====================================================================
 *
 *OpenTCP is the unified open source TCP/IP stack available on a series 
 *of 8/16-bit microcontrollers, please see <http://www.opentcp.org>.
 *
 *For more information on how to network-enable your devices, or how to 
 *obtain commercial technical support for OpenTCP, please see 
 *<http://www.violasystems.com/>.
 */

#include <debug.h>
#include <datatypes.h>
#include <ethernet.h>
#if defined(IPv6)
	#include <ipv6.h>
#else
	#include <ip.h>
#endif
#include <tcp_ip.h>
#include <system.h>

/** \brief UDP table holding socket parameters for every UDP socket 
 *
 *	UDP table is an array of ucb structures holding all of the 
 *  necessary information about the state, listener, port numbers
 *	and other info about the UDP sockets opened. Number of UDP sockets
 *	that can be opened at any given time is defined by the NO_OF_UDPSOCKETS
 *	and may be changed in order to change the amount of used RAM memory.
 *	See ucb definition for more information about the structure itself.
 *
 */
struct ucb udp_socket[NO_OF_UDPSOCKETS];

/**	\brief Used for storing field information about the received UDP packet
 *	
 *	Various fields from the received UDP packet are stored in this variable.
 *	See udp_frame definition for struct information.
 */
struct udp_frame received_udp_packet;

/** \brief Initialize UDP socket pool
 *	\ingroup core_initializer
 * 	\author 
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\date 26.07.2002
 *	\return 
 *		\li -1 - Error
 *		\li >0 - Number of UDP sockets initialized
 *	\warning 
 *		\li This function <b>must</b> be invoked before any other
 *			UDP-related function is called
 *
 *	This function initializes UDP socket pool to get everything into
 *	a known state at startup.
 */
INT8 udp_init (void)
{
	UINT8 i;
	struct ucb* soc;
	
	if( NO_OF_UDPSOCKETS < 0 )
		return(-1);
	
	if( NO_OF_UDPSOCKETS == 0 )
		return(0);
	
	UDP_DEBUGOUT("Initializing UDP");
	
	for(i=0; i < NO_OF_UDPSOCKETS; i++) {
		soc = &udp_socket[i];			/* Get Socket	*/
		
		soc->state = UDP_STATE_FREE;
		soc->tos = 0;
		soc->locport = 0;
		soc->opts = UDP_OPT_SEND_CS | UDP_OPT_CHECK_CS;	 
		soc->event_listener = 0;
		
		UDP_DEBUGOUT(".");
		
	}
	
	UDP_DEBUGOUT("\n\rUDP Initialized\n\r");
	
	/* Return number of sockets initialized	*/
	
	return(i+1);

}

/** \brief Allocate a free socket in UDP socket pool
 *  \ingroup udp_app_api
 * 	\author 
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\date 26.07.2002
 *	\param tos type of service for socket. For now nothing implemented so 0.
 *	\param listener pointer to callback function that will be invoked by
 *		the TCP/IP stack to inform socket application of #UDP_DATA_ARRIVAL
 *		event (for now only this, in future maybe others!)
 *	\param opts Options for checksum generation & inspection. Can be one
 *		of the following:
 *		\li #UDP_OPT_NONE
 *		\li #UDP_OPT_SEND_CS
 *		\li #UDP_OPT_CHECK_CS
 *		\li #UDP_OPT_SEND_CS | #UDP_OPT_CHECK_CS
 *	\return 
 *		\li -1 - Error
 *		\li >=0 - Handle to reserved socket
 *
 *	Invoke this function to try to obtain a free socket from UDP socket pool.
 *	Function returns a handle to the free socket that is later used for 
 *	accessing the allocated socket.
 */
INT8 udp_getsocket (UINT8 tos, INT32 (*listener)(INT8, UINT8, UINT32, UINT16, UINT16, UINT16), UINT8 opts )
{
	INT8 i;
	struct ucb* soc;
	
	if( NO_OF_UDPSOCKETS < 0 )
		return(-1);
	
	if( NO_OF_UDPSOCKETS == 0 )
		return(-1);
	
	
	if(listener == 0) {
		UDP_DEBUGOUT("ERROR:Event listener function for UDP not specified\r\n");
		return(-1);
	}
	
	UDP_DEBUGOUT("Searching for free UDP socket...\r\n");
	
	for(i=0; i < NO_OF_UDPSOCKETS; i++) {
		soc = &udp_socket[i];			/* Get Socket	*/
	
		if(soc->state == UDP_STATE_FREE) {
			/* We found it	*/
			
			UDP_DEBUGOUT("Free socket found\r\n");
			
			soc->state = UDP_STATE_CLOSED;
			soc->tos = tos;
			soc->locport = 0;
			
			soc->opts = 0;
			
			if(opts & UDP_OPT_SEND_CS)
				soc->opts |= UDP_OPT_SEND_CS;
			if(opts & UDP_OPT_CHECK_CS)
				soc->opts |= UDP_OPT_CHECK_CS;
			
			soc->event_listener = listener;

			/* Return handle	*/
			
			return(i);
		}
	
	}
	
	/* We are there so no socket found	*/
	
	UDP_DEBUGOUT("No UDP socket found\r\n");
	return(-1);

}


/** \brief Release a given socket
 *	\ingroup udp_app_api
 * 	\author 
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\date 26.07.2002
 *	\param sochandle handle of UDP socket to be released
 *	\return 
 *		\li -1 - error
 *		\li >=0 - OK (returns handle to release socket)
 *
 *	This function releases UDP socket. This means that the socket entry is
 *	marked as free and all of the ucb fields are initialized to default
 *	values.
 */
INT8 udp_releasesocket (INT8 sochandle)
{
	struct ucb* soc;
	
	if( NO_OF_UDPSOCKETS < 0 )
		return(-1);
	
	if( NO_OF_UDPSOCKETS == 0 )
		return(-1);
	
	if( sochandle > NO_OF_UDPSOCKETS ) {
		UDP_DEBUGOUT("Socket handle non-valid\r\n");
		return(-1);
	}
	
	if( sochandle < 0 ) {
		UDP_DEBUGOUT("Socket handle non-valid\r\n");
		return(-1);
	}
	
	soc = &udp_socket[sochandle];		/* Get referense	*/
	
	soc->state = UDP_STATE_FREE;
	soc->tos = 0;
	soc->locport = 0;
	soc->opts = UDP_OPT_SEND_CS | UDP_OPT_CHECK_CS;	
	soc->event_listener = 0;

	return(sochandle);

}


/** \brief Open a given UDP socket for communication
 *  \ingroup udp_app_api
 * 	\author 
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\date 26.07.2002
 *	\param sochandle handle to socket to be opened
 *	\param locport local port number
 *	\return
 *		\li -1 - Error
 *		\li >=0 - Handle to opened socket
 *
 *	This function binds local port to given UDP socket and opens
 *	the socket (virtually) in order to enable communication.
 */
INT8 udp_open (INT8 sochandle, UINT16 locport)
{
	struct ucb* soc;
	
	if( NO_OF_UDPSOCKETS < 0 )
		return(-1);
	
	if( NO_OF_UDPSOCKETS == 0 )
		return(-1);
	
	if( sochandle > NO_OF_UDPSOCKETS ) {
		UDP_DEBUGOUT("Socket handle non-valid\r\n");
		return(-1);
	}
	
	if( sochandle < 0 ) {
		UDP_DEBUGOUT("Socket handle non-valid\r\n");
		return(-1);
	}
	
	if(locport == 0) {
		locport=udp_getfreeport();
		return(-1);
	}
	
	soc = &udp_socket[sochandle];		/* Get referense	*/

	soc->state = UDP_STATE_OPENED;
	soc->locport = locport;
	
	return(sochandle);

}



/** \brief Close given socket for communication
 *  \ingroup udp_app_api
 * 	\author 
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\date 26.07.2002
 *	\param sochandle handle to socket to be closed
 *	\return 
 *		\li -1 - Error
 *		\li >=0 - handle to closed socket
 *
 *	Closes a given socket in order to disable further communication
 *	over it.
 */
INT8 udp_close (INT8 sochandle)
{
	struct ucb* soc;
	
	if( NO_OF_UDPSOCKETS < 0 )
		return(-1);
	
	if( NO_OF_UDPSOCKETS == 0 )
		return(-1);
	
	if( sochandle > NO_OF_UDPSOCKETS ) {
		UDP_DEBUGOUT("Socket handle non-valid\r\n");
		return(-1);
	}
	
	if( sochandle < 0 ) {
		UDP_DEBUGOUT("Socket handle non-valid\r\n");
		return(-1);
	}
	
	soc = &udp_socket[sochandle];		/* Get referense	*/

	soc->state = UDP_STATE_CLOSED;
	
	return(sochandle);

}


/** \brief Send data to remote host using given UDP socket

⌨️ 快捷键说明

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