📄 raw.c
字号:
//--------------------------------------------------------------------------
// Ip Stack
//--------------------------------------------------------------------------
// RAW.C
//
// RAW General Functions
//
// Author: Michael A. Denio
// Copyright 1999 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stkmain.h>
// Private and Public Globals
RAWSTATS raws; // Stats
#define RAW_MSS_DEFAULT 1500 // Default Max seg size (including IP)
//--------------------------------------------------------------------
// RawOutput()
//
// Called when RAW packet should be sent
//--------------------------------------------------------------------
int RawOutput( HANDLE hSock, UINT8 *buf, INT32 size, INT32 *pRetSize)
{
HANDLE hPkt,hFrag,hRoute,hIFTx;
UINT8 *pb;
uint Offset;
INT32 mss;
int error;
//
// Bound the size to something we can handle
//
// We'll try and get the MTU from the route, then the egress IF,
// or finally we just assume a default.
//
if( (hRoute = SockGetRoute( hSock )) )
{
mss = (INT32)RtGetMTU( hRoute );
hIFTx = 0;
}
else if( (hIFTx = SockGetIFTx( hSock )) )
mss = (INT32)IFGetMTU( hIFTx );
else
mss = RAW_MSS_DEFAULT;
mss -= SockGetIpHdrSize(hSock); // Sub off IpHdr (if any)
if( size > mss )
return( EMSGSIZE );
//
// Create the packet
//
if( !(hPkt = SockCreatePacket( hSock, (uint)size, 0 )) )
{
raws.dwSndNoPacket++;
return( ENOBUFS );
}
// Get the frag
hFrag = PktGetFrag( hPkt );
// Get the buffer parameters
pb = FragGetBufParams( hFrag, 0, 0, &Offset );
// Copy the data
mmCopy( (pb + Offset + PktGetSizeNet(hPkt)), buf, (uint)size );
// Indicate the preferred route and IF
PktSetRoute( hPkt, hRoute );
PktSetIFTx( hPkt, hIFTx );
// Count it
raws.dwSndTotal++;
// Send the packet
error=(int)IPTxPacket(hPkt,SockGetOptionFlags(hSock)&FLG_IPTX_SOSUPPORTED);
if( !error )
*pRetSize = size;
return( error );
}
//--------------------------------------------------------------------
// RawInput()
//
// Rx RAW Packet
//--------------------------------------------------------------------
void RawInput( HANDLE hPkt )
{
HANDLE hFrag,hFragCopy,hSock,hSBRx,hIFTx;
uint w,Offset,Valid,RawLen,fBcast = 0;
IPN dwIPSrc,dwIPDst;
UINT8 *pb;
IPHDR *pIpHdr;
if( !(hFrag = PktGetFrag( hPkt )) )
{
DbgPrintf(DBG_ERROR,"RawInput: No frag data");
PktFree( hPkt );
return;
}
// Get the size of the IP Header and pointer to our data
w = PktGetFlags( hPkt );
if( !(w & FLG_PKT_IP_VALID) || (w & FLG_PKT_LLC_VALID) )
{
DbgPrintf(DBG_ERROR,"RawInput: Bad packet");
PktFree( hPkt );
return;
}
// Get the buffer parameters
pb = FragGetBufParams( hFrag, 0, &Valid, &Offset );
// Assign an IP header pointer
pIpHdr = (IPHDR *)(pb + Offset);
// Count the total number of packets in
raws.dwRcvTotal++;
// Get the total length of the message
RawLen = (uint)(HNC16( pIpHdr->TotalLen ));
// Get source and destination
dwIPSrc = RdNet32(&pIpHdr->dwIPSrc);
dwIPDst = RdNet32(&pIpHdr->dwIPDst);
// Set the Frag Parms to hand off to a SB
FragSetAux1( hFrag, 0x10000 );
FragSetAux2( hFrag, dwIPSrc );
FragSetBufParams( hFrag, RawLen, Offset );
// Cache whether we are doing BROADCAST/MULTICAST
if( dwIPDst == INADDR_BROADCAST || IN_MULTICAST( dwIPDst ) ||
(PktGetFlags(hPkt) & (FLG_PKT_MACMCAST|FLG_PKT_MACBCAST)) )
fBcast = 1;
// We have to copy the frag for each socket with a matchng PCB
hSock = 0;
while( (hSock = SockPcbResolveChain( hSock, SOCKPROT_RAW, pIpHdr->Protocol,
dwIPDst, 0, dwIPSrc, 0)) )
{
// Filter Broadcast and Multicast
if( fBcast )
{
// Get the broadcast IF of this socket
hIFTx = SockGetIFTx( hSock );
// If there is a specified BCAST device, and the packet's
// Rx device doesn't match it, then we ignore the packet
if( hIFTx && ( hIFTx != PktGetIFRx(hPkt) ) )
continue;
}
// Get the Linear Receive Buffer
hSBRx = SockGetRx( hSock );
if( SBGetSpace( hSBRx ) < (INT32)RawLen )
raws.dwRcvFull++;
else if( hFragCopy = FragCopy( hFrag ) )
{
// Give the frag to the SB
SBWrite( hSBRx, (INT32)RawLen, 0, hFragCopy );
// Notify the Socket
SockNotify( hSock, SOCK_NOTIFY_RCVDATA );
}
}
PktFree( hPkt );
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -