Exemplo n.º 1
0
uint32_t UDPRead(HSOCKET hSocket, uint8_t * pbRead, uint16_t cbRead, IPSTATUS * pStatus)
{
    UDPSOCKET *     pSocket     = (UDPSOCKET *) hSocket;

    uint32_t cb = 0;

    if(pSocket != NULL)
    {
        SMGR *  pSMGR = alloca(pSocket->cbRxSMGR);

        if(pSMGR != NULL && (SMGRRead((HSMGR) &pSocket->smgrRxBuff, 0, pSMGR, pSocket->cbRxSMGR) == pSocket->cbRxSMGR))
        {
            cb = UDPPeekSMGR(pSocket, (HSMGR) pSMGR, 0, pbRead, cbRead, pStatus);

            // if this is the whole datagram
            // note, the > part should never happen, cb should always
            // be cb <= pSocket->cbNextDataGram and this condition
            // should be cb == pSocket->cbNextDataGram; but should somehow
            // > happen, this will offer some correction
            if(cb >= pSocket->cbNextDataGram)
            {
                // move the begining to the next datagram
                SMGRMoveEnd((HSMGR) pSMGR, cb + sizeof(uint16_t), SMGRAtBegining);

                // read the next 2 bytes for the datagarm size if it exists
                if(SMGRRead((HSMGR) pSMGR, 0, &pSocket->cbNextDataGram, sizeof(uint16_t)) != sizeof(uint16_t))
                {
                    pSocket->cbNextDataGram = 0;
                }
            }

            // otherwise it is a partial datagram read
            else
            {
                // move the begining to after what we read, but leave room to
                // write the remaining datagram size
                SMGRMoveEnd((HSMGR) pSMGR, cb, SMGRAtBegining);

                // update the remaining datagram size
                pSocket->cbNextDataGram -= cb;
                SMGRWrite((HSMGR) pSMGR, 0, &pSocket->cbNextDataGram, sizeof(uint16_t));
            }

            // save away the table that is stored on the stack
            // this should not fail! It is a fixed size and already allocated
            SMGRWrite((HSMGR) &pSocket->smgrRxBuff, 0, pSMGR, pSocket->cbRxSMGR);
        }
    }

    return(cb);
}
Exemplo n.º 2
0
void UDPDiscard(HSOCKET hSocket)
{
    UDPSOCKET *     pSocket     = (UDPSOCKET *) hSocket;

    if(pSocket != NULL)
    {
        SMGR *  pSMGR = alloca(pSocket->cbRxSMGR);

        if(pSMGR != NULL && (SMGRRead((HSMGR) &pSocket->smgrRxBuff, 0, pSMGR, pSocket->cbRxSMGR) == pSocket->cbRxSMGR))
        {
            uint32_t iEnd = SMGRcbStream(pSMGR);

            SMGRMoveEnd((HSMGR) pSMGR, iEnd, SMGRAtBegining);

            // save away the table that is stored on the stack
            // this should not fail! It is a fixed size and already allocated
            SMGRWrite((HSMGR) &pSocket->smgrRxBuff, 0, pSMGR, pSocket->cbRxSMGR);
        }
    }
}
Exemplo n.º 3
0
/*****************************************************************************
  Function:
	uint32_t TCPRead(SOCKET *  pSocket, void * pv, uint32_t cb, IPSTATUS * pStatus)

  Description:
        Reads bytes out of the receive buffer. We call this receive instead of read because RFC 793 calls it receive.

  Parameters:
	pSocket:        The socket to read the bytes from
        pv:             A buffer to receive the data
        cb:             Max size of the buffer to receive the data
        pStatus:        A pointer to a status variable to recieve the state of the socket as a status

  Returns:
        Number of bytes read

  ***************************************************************************/
uint32_t TCPRead(HSOCKET hSocket, void * pbRead, uint32_t cbRead, IPSTATUS * pStatus)
{
    TCPSOCKET *     pSocket     = (TCPSOCKET *) hSocket;
    IPSTATUS        status      = ipsSocketNULL;
    uint32_t cb = 0;

    if(pSocket != NULL && pSocket->cbRxSMGR > 0)
    {
        SMGR *  pSMGR = (SMGR*)alloca(pSocket->cbRxSMGR);

        if(SMGRRead((HSMGR) &pSocket->smgrRxTxBuff, 0, pSMGR, pSocket->cbRxSMGR) == pSocket->cbRxSMGR)
        {
            cb = TCPPeekSMGR(pSocket, (HSMGR) pSMGR, 0, (u8*)pbRead, cbRead, &status);

            if(cb > 0)
            {
                // move the begining to after what we read
                SMGRMoveEnd((HSMGR) pSMGR, cb, SMGRAtBegining);

                // apply the read bytes to our base
                pSocket->rcvIRS += cb;

                // and now fix up to the base
                pSocket->rcvNXT -= cb;
                pSocket->rcvUP  -= cb;
//                pSocket->rcvSeqAhead -= cb;
            }

            // save away the table that is stored on the stack
            // this should not fail! It is a fixed size and already allocated
            SMGRWrite((HSMGR) &pSocket->smgrRxTxBuff, 0, pSMGR, pSocket->cbRxSMGR);
        }
        else
        {
            status = ipsFailedToReadStreamBuffer;
        }
    }

    AssignStatusSafely(pStatus, status);
    return(cb);
}
Exemplo n.º 4
0
/*****************************************************************************
  Function:
	void TCPDiscard(SOCKET *  pSocket)

  Description:
        Discards all unread bytes in the sockets receive buffer.

  Parameters:
	hSocket:        The socket to clear the receive buffer from

  Returns:
        None

  ***************************************************************************/
void TCPDiscard(HSOCKET hSocket)
{
    TCPSOCKET *     pSocket     = (TCPSOCKET *) hSocket;

    if(pSocket != NULL && pSocket->cbRxSMGR > 0)
    {
        SMGR *  pSMGR = (SMGR*)alloca(pSocket->cbRxSMGR);

        if(SMGRRead((HSMGR) &pSocket->smgrRxTxBuff, 0, pSMGR, pSocket->cbRxSMGR) == pSocket->cbRxSMGR)
        {
            uint32_t iEnd = SMGRcbStream(pSMGR);

            // clear the stream by moving to the end
            SMGRMoveEnd((HSMGR) pSMGR, iEnd, SMGRAtBegining);

            // pretend like we read it
            // apply the read bytes to our base
            pSocket->rcvIRS += pSocket->rcvNXT;

            // and now fix up to the base
            pSocket->rcvNXT = 0;
            pSocket->rcvUP  = 0;
            
//            if(pSocket->rcvSeqAhead >= pSocket->rcvNXT)
//            {
//                pSocket->rcvSeqAhead -= pSocket->rcvNXT;
//            }
//            else
//            {
//                pSocket->rcvSeqAhead = 0;
//            }

            // save away the table that is stored on the stack
            // this should not fail! It is a fixed size and already allocated
            SMGRWrite((HSMGR) &pSocket->smgrRxTxBuff, 0, pSMGR, pSocket->cbRxSMGR);
        }
    }
}
Exemplo n.º 5
0
static uint32_t UDPProcessRx(IPSTACK * pIpStack, UDPSOCKET * pSocket)
{
    SMGR *      pSMGR = alloca(pSocket->cbRxSMGR);
    uint32_t    cbRet = 0;

    if(pIpStack->cbPayload > 0 && pSMGR != NULL && (SMGRRead((HSMGR) &pSocket->smgrRxBuff, 0, pSMGR, pSocket->cbRxSMGR) == pSocket->cbRxSMGR))
    {
        uint32_t iEnd = SMGRcbStream(pSMGR);

        // put in the size prefex
        uint32_t cbWritten = SMGRWrite((HSMGR) pSMGR, iEnd, &pIpStack->cbPayload, sizeof(uint16_t));

        // put in the data
        cbWritten += SMGRWrite((HSMGR) pSMGR, iEnd + cbWritten, pIpStack->pPayload, pIpStack->cbPayload);

        // check to see that we wrote everything.
        if(cbWritten == (pIpStack->cbPayload + sizeof(uint16_t)))
        {
            if(pSocket->cbNextDataGram == 0)
            {
                pSocket->cbNextDataGram = pIpStack->cbPayload;
            }
            cbRet = pIpStack->cbPayload;
        }

        // we failed to save, so discard the datagram and put the stream back to where it was
        else
        {
            SMGRMoveEnd((HSMGR) pSMGR, iEnd, SMGRAtEnding);
        }

        // save away the table that is stored on the stack
        // this should not fail! It is a fixed size and already allocated
        SMGRWrite((HSMGR) &pSocket->smgrRxBuff, 0, pSMGR, pSocket->cbRxSMGR);
    }

    return(cbRet);
}