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

📄 sockint.c

📁 代码在ti的c67系列单片机上实现了完整的TCPIP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// Dequeues a socket from the parent's Ready queue
//--------------------------------------------------------------------
static void SockDeqReady( SOCK *ps )
{
    SOCK *psTemp;

    psTemp = ps->pParent;

    // Zap this flag regardless
    ps->StateFlags &= ~SS_READYQ;

    // Find the socket we're looking for or the end of the list
    while( psTemp->pReady && psTemp->pReady != ps )
        psTemp = psTemp->pReady;

    // If not the end of the list, when dequeue the socket
    if( psTemp->pReady )
    {
        psTemp->pReady = ps->pReady;
        ps->pParent->ConnTotal--;
    }
}

//--------------------------------------------------------------------
// SockValidateRoute()
//
// Attempt to get a valid route for a connected socket
//--------------------------------------------------------------------
HANDLE SockValidateRoute( HANDLE h )
{
    SOCK *ps = (SOCK *)h;

#ifdef _STRONG_CHECKING
    if( ps->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockValidateRoute: HTYPE %04x",ps->Type);
        return(0);
    }
#endif

    // DeRef any held route
    if( ps->hRoute )
        RtDeRef( ps->hRoute );
    ps->hRoute = 0;

    // If the destination is a BCAST, we don't use a route. Otherwise;
    // find the next hop for this destination. Search with cloning.
    if( ps->dwFIP != INADDR_ANY && ps->dwFIP != INADDR_BROADCAST )
        ps->hRoute = IPGetRoute( FLG_RTF_CLONE|FLG_RTF_REPORT, ps->dwFIP, 0 );

    return( ps->hRoute );
}

//--------------------------------------------------------------------
// SockCreatePacket
//
// Creates Ip Packet using Socket info
//--------------------------------------------------------------------
HANDLE SockCreatePacket( HANDLE hSock, uint Payload, uint Reserve )
{
    HANDLE hPkt;
    HANDLE hFrag;
    UINT8  *pb;
    IPHDR  *pIpHdr;
    uint   IPHdrSize,Offset;
    SOCK   *ps = (SOCK *)hSock;

    // Note "ps" can be NULL
    if( !ps )
        IPHdrSize = IPHDR_SIZE;
    else if( ps->IpFlags & IP_HDRINCL )
        IPHdrSize = 0;
    else
        IPHdrSize = IPHDR_SIZE + ps->IpOptSize;

    if( !(hPkt = IFCreatePacket(Payload, IPHdrSize, Reserve)) )
        return( 0 );

    // Get the frag
    hFrag = PktGetFrag( hPkt );

    // Get pointers to packet headers
    pb        = FragGetBufParams( hFrag, 0, 0, 0 );
    Offset    = PktGetSizeLLC( hPkt );

    // Fixup packet frag info
    FragSetBufParams( hFrag, Payload+IPHdrSize+Reserve, Offset );

    // Mark the IP packet as valid
    PktSetFlagsSet( hPkt, FLG_PKT_IP_VALID );

    if( !ps || !(ps->IpFlags & IP_HDRINCL) )
    {
        // Fill in IP Header
        pIpHdr    = (IPHDR *)(pb + Offset);
        pIpHdr->VerLen  = 0x40 + IPHdrSize/4;
        if( ps )
        {
            pIpHdr->Protocol = (UINT8)ps->Protocol;
            pIpHdr->Ttl      = (UINT8)ps->IpTtl;
            pIpHdr->Tos      = (UINT8)ps->IpTos;
            WrNet32( &pIpHdr->dwIPSrc, ps->dwLIP );
            WrNet32( &pIpHdr->dwIPDst, ps->dwFIP );
            if( ps->IpOptSize )
                mmCopy( pIpHdr->Options, ps->IpOptions, ps->IpOptSize );
        }
        else
        {
            pIpHdr->Protocol = 0;
            pIpHdr->Ttl      = SOCK_TTL_DEFAULT;
            pIpHdr->Tos      = SOCK_TOS_DEFAULT;
            WrNet32( &pIpHdr->dwIPSrc, 0 );
            WrNet32( &pIpHdr->dwIPDst, 0 );
        }
    }
    return( hPkt );
}


//
// The following functions are direct sturcture accesses when
// not using _STRONG_CHECKING
//
#ifdef _STRONG_CHECKING

//--------------------------------------------------------------------
// SockGetProtocol()
//
// Returns the socket's Protocol
//--------------------------------------------------------------------
uint SockGetProtocol( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetProtocol: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->Protocol );
}

//--------------------------------------------------------------------
// SockGetIpHdrSize()
//
// Returns the socket's Protocol
//--------------------------------------------------------------------
uint SockGetIpHdrSize( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetIpHdrSize: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }

    if( ((SOCK *)h)->IpFlags & IP_HDRINCL )
        return(0);
    else
        return( IPHDR_SIZE + ((SOCK *)h)->IpOptSize );
}

//--------------------------------------------------------------------
// SockGetTx()
//
// Returns the handle to the socket's Tx SB
//--------------------------------------------------------------------
HANDLE SockGetTx( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetLTB: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->hSBTx );
}

//--------------------------------------------------------------------
// SockGetRx()
//
// Returns the handle to the socket's Rx SB
//--------------------------------------------------------------------
HANDLE SockGetRx( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetLRB: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->hSBRx );
}

//--------------------------------------------------------------------
// SockGetTP()
//
// Returns the handle to the socket's transport protocol
//--------------------------------------------------------------------
HANDLE SockGetTP( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetTP: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->hTP );
}

//--------------------------------------------------------------------
// SockGetLIP()
//
// Returns the socket's Local IP
//--------------------------------------------------------------------
IPN SockGetLIP( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetLIP: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->dwLIP );
}

//--------------------------------------------------------------------
// SockGetFIP()
//
// Returns the socket's foreign IP
//--------------------------------------------------------------------
IPN SockGetFIP( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetFIP: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->dwFIP );
}

//--------------------------------------------------------------------
// SockGetLPort()
//
// Returns the socket's Local Port
//--------------------------------------------------------------------
uint SockGetLPort( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetLPort: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->LPort );
}

//--------------------------------------------------------------------
// SockGetFPort()
//
// Returns the socket's Foreign Port
//--------------------------------------------------------------------
uint SockGetFPort( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetFPort: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->FPort );
}

//--------------------------------------------------------------------
// SockGetRoute()
//
// Returns the socket's cached route
//--------------------------------------------------------------------
HANDLE SockGetRoute( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetRoute: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->hRoute );
}

//--------------------------------------------------------------------
// SockGetIFTx()
//
// Returns the socket's egress IF (used only in special cases)
//--------------------------------------------------------------------
HANDLE SockGetIFTx( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetIFTx: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->hIFTx );
}

//--------------------------------------------------------------------
// SockGetOptionFlags()
//
// Returns the socket's Option Flags
//--------------------------------------------------------------------
uint SockGetOptionFlags( HANDLE h )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockGetOptionFlags: HTYPE %04x",((SOCK *)h)->Type);
        return( 0 );
    }
    return( ((SOCK *)h)->OptionFlags );
}

//--------------------------------------------------------------------
// SockSetError()
//
// Set the socket's ErrorPending value
//--------------------------------------------------------------------
void SockSetError( HANDLE h, int Error )
{
    if( ((SOCK *)h)->Type != HTYPE_SOCK )
    {
        DbgPrintf(DBG_ERROR,"SockSetError: HTYPE %04x",((SOCK *)h)->Type);
        return;
    }
    ((SOCK *)h)->ErrorPending = Error;
}

#endif


⌨️ 快捷键说明

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