udp.c
来自「mcf5307实验源代码」· C语言 代码 · 共 467 行 · 第 1/2 页
C
467 行
/*************************************************************************/
/* */
/* CopyrIght (c) 1993 - 1996 Accelerated Technology, Inc. */
/* */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the */
/* subject matter of this material. All manufacturing, reproduction, */
/* use, and sales rights pertaining to this subject matter are governed */
/* by the license agreement. The recipient of this software implicitly */
/* accepts the terms of the license. */
/* */
/*************************************************************************/
/*
*
* Portions of this program were written by: */
/***************************************************************************
* *
* part of: *
* TCP/IP kernel for NCSA Telnet *
* by Tim Krauskopf *
* *
* National Center for Supercomputing Applications *
* 152 Computing Applications Building *
* 605 E. Springfield Ave. *
* Champaign, IL 61820 *
* *
***************************************************************************/
/****************************************************************************/
/* */
/* FILENAME VERSION */
/* */
/* udp.c 3.2 */
/* */
/* DESCRIPTION */
/* */
/* UDP Protocol routines */
/* */
/* AUTHOR */
/* */
/* */
/* DATA STRUCTURES */
/* */
/* */
/* FUNCTIONS */
/* */
/* udpinterpret */
/* neturead */
/* netulisten */
/* netusend */
/* */
/* */
/* DEPENDENCIES */
/* */
/* No other file dependencies */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* Maiqi Qian 04/10/97 Added SNMP control */
/* */
/****************************************************************************/
#include "protocol.h"
#include "socketd.h"
#include "externs.h"
#include "data.h"
#include "target.h"
#include "tcp_errs.h"
#include "netevent.h"
#if SNMP_INCLUDED
#include "snmp_g.h"
#endif
/****************************************************************************/
/* */
/* FUNCTION */
/* */
/* udpinterpret */
/* */
/* DESCRIPTION */
/* */
/* Take an incoming UDP packet and make it available to the user level */
/* routines. Currently keeps the last packet coming in to a port. */
/* */
/* Limitations : */
/* */
/* Can only listen to one UDP port at a time, only saves the last */
/* packet received on that port. Port numbers should be assigned */
/* like TCP ports. */
/* */
/* CALLED BY */
/* ipinterpret */
/* */
/* CALLS */
/* */
/* intswap */
/* tcpcheck */
/* netputevent */
/* dll_update_lists */
/* */
/* NAME DATE REMARKS */
/* */
/* Glen Johnson 03/14/96 Fixed problem in the search of the uportlist */
/* for the destination UDP port. */
/* */
/* */
/****************************************************************************/
int16 udpinterpret (UDPKT *p, uint16 ulen)
{
uint16 hischeck, mycheck;
uint16 i;
struct uport *uptr;
struct pqueue HUGE *buf_ptr;
struct pseudotcp chksum_hdr; /* debug */
/* Increment the number of UDP packets received. */
SNMP_udpInDatagrams_Inc;
/* Was a checksum computed by the foreign host?
If so verfiy the checksum.
*/
if (p->u.check)
{
hischeck = p->u.check;
p->u.check = 0;
memcpy ((void *)chksum_hdr.source, (const void *)p->i.ipsource,
8);
chksum_hdr.z = 0;
chksum_hdr.proto = p->i.protocol;
chksum_hdr.tcplen = intswap (ulen);
mycheck = tcpcheck ((uint16 *)&chksum_hdr, (uint16 *)&p->u, ulen);
if (hischeck != mycheck)
{
/* The Checksum failed log an error and drop the packet. */
NU_Tcp_Log_Error (TCP_UDP_CKSUM, TCP_RECOVERABLE,
__FILE__, __LINE__);
dll_update_lists(&buffer_list, &buffer_freelist);
/* Increment the number of datagrams received with errors. */
SNMP_udpInErrors_Inc;
return (2);
}
p->u.check = hischeck; /* put it back */
}
/*
* did we want this data ? If not, then let it go, no comment
* If we want it, copy the relevent information into our structure
*/
uptr = NU_NULL;
for (i = 0; i < NUPORTS; i++)
{
/* Check to make sure this entry in the uportlist actually points to a
port structure, and check for the destination port matching the local
port. Short circuit evaluation will cause the test to fail
immediately if the pointer to the port structure is NULL.
*/
if ((uportlist[i]) && (p->u.dest == uportlist[i]->in.u.dest))
{
/* When a match is found our search is over. */
uptr = uportlist[i];
break;
}
} /* end for i =0 to NUPORTS */
/* If we did not find a port then we are not waiting for this
so return. */
if (uptr == NU_NULL)
{
/* Drop the packet by placing it back on the buffer_freelist. */
dll_update_lists(&buffer_list, &buffer_freelist);
/* Increment the number of datagrams received for which there was no
port. */
SNMP_udpNoPorts_Inc;
return(-1);
}
ulen -= 8; /* account for header */
if (ulen > UMAXLEN) /* most data that we can accept */
/* SGJ should log an error here. */
ulen = UMAXLEN;
/* If no more datagrams can be accepted then drop this one and return. */
if (uptr->in_dgrams >= UMAXDGRAMS)
{
/* Increment the number of datagrams received with errors. */
SNMP_udpInErrors_Inc;
/* Drop the packet by placing it back on the buffer_freelist. */
dll_update_lists(&buffer_list, &buffer_freelist);
if( (uptr->in_dgrams) && (uptr->RXTask) )
{
/* post that it is here */
netputevent (USERCLASS, UDPDATA, i);
}
return(-1);
}
/* Place the datagram onto this ports datagram list. */
buf_ptr = dll_update_lists(&buffer_list, &uptr->dgram_list);
/* Store the data_ptr and data_len. */
buf_ptr->data_ptr = p->data;
buf_ptr->data_len = ulen;
/* Update the number of buffered datagrams. */
uptr->in_dgrams++;
/* If there is a task pending data on the port, then set an event to
resume that task. */
if(uptr->RXTask)
netputevent (USERCLASS, UDPDATA, i);
return (0);
} /* end udpinterpret */
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* neturead (char *) */
/* */
/* DESCRIPTION */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?