Class CSpoofSocket::
Send()
Base ClassesData ItemsConstructorsDestructorsFunctionsCustom CodeGo to hierarchy chart    Prev page: ResolveDNSNext page: Send    Show member index
Public Function Declared in:
SpoofSocket.h

'Declaration' icon -- Shortcut to top of page. Declaration

virtual int Send(
    IP aDestinationAddress,
    const char* pBuffer,
    unsigned long ulBufferLength,
    unsigned short usDestinationPort=0);

'Description' icon -- Shortcut to top of page. Description

Send data to a socket , can be used with all sub sockets (protocols) Return value: Positive - The number of bytes received. Negative - Error

'Function Body' icon -- Shortcut to top of page. Function Body

try
{
    //Quit if not ok
    if (!CheckSocketValid())
        return GetErrorCode();

    //Is it a valid size
    if (ulBufferLength &&
        !pBuffer)
        return GetErrorCode();

    //Define the target address
    sockaddr_in aTargetAddress;
    memset(&aTargetAddress,
           0,
           sizeof(aTargetAddress));

    aTargetAddress.sin_family=AF_INET;
    aTargetAddress.sin_addr.s_addr=aDestinationAddress;
    aTargetAddress.sin_port=htons(usDestinationPort);

    //packet send status ?
    int iResult;

    //Only if allowing raw headers !!
    if (m_bRaw)
    {
        //Header length
        unsigned char ucHeaderLength;
        ucHeaderLength=IpHeaderLength;

        //Do we have options?
        if (m_bOptions)
            ucHeaderLength+=m_pIPOptions->GetBufferLength();

        //First construct the packet
        LPIpHeader lpHead=ConstructIPHeader(m_ucProtocol,
                                            IpFragFlag_DONT_FRAG,
                                            m_ucTTL,
                                            (unsigned short)GetCurrentProcessId(),
                                            ucHeaderLength);

        //Protect the header
        std::auto_ptr<IpHeader> pProtection(lpHead);

        //Set the address
        SetIPHeaderAddress(lpHead,
                           m_ulSourceAddress,
                           aDestinationAddress);

        //Now add some more options
        int iTotalLength;
        iTotalLength=ucHeaderLength+ulBufferLength;

        //Set the header
        lpHead->usTotalLength=htons(iTotalLength);

        //Need to construct a new packet
        char* pNewBuffer;
        pNewBuffer=new char[iTotalLength];

        //Protect the buffer
        CArray_ptr<char> pBufferProtection(pNewBuffer);

        //Copy two buffers
        memcpy(pNewBuffer,
               lpHead,
               IpHeaderLength);

        //Do we need to copy options ?
        if (m_bOptions)
            memcpy(pNewBuffer+IpHeaderLength,
                   m_pIPOptions->GetBuffer(),
                   m_pIPOptions->GetBufferLength());

        //Only if not null
        if (pBuffer)
            memcpy(pNewBuffer+ucHeaderLength,
                   pBuffer,
                   ulBufferLength);

        //Calculate the checksum
        lpHead->usChecksum=CalculateChecksum((unsigned short*)pNewBuffer,
                                             ucHeaderLength);

        //Alert everyone this is the final header
        FinalIPHeader(lpHead);

        //Recopy the ip
        memcpy(pNewBuffer,
               lpHead,
               IpHeaderLength);

        //Send the data
        iResult=sendto(GetHandle(),
                       (const char*)pNewBuffer,
                       iTotalLength,
                       0,
                       (sockaddr*)&aTargetAddress,
                       sizeof(aTargetAddress));

        //Is all OK
        if (iResult==GetErrorCode())
            //Set the error
            SetLastError("Send - Raw");
    }
    else
    {
        //Set to no error
        iResult=!GetErrorCode();

        //Insert options
        /*if (m_bOptions)
            iResult=setsockopt(GetHandle(),
                               IPPROTO_IP,
                               IP_OPTIONS,
                               m_pIPOptions->GetBuffer(),
                               m_pIPOptions->GetBufferLength());
        else
            //No options
            iResult=setsockopt(GetHandle(),
                               IPPROTO_IP,
                               IP_OPTIONS,
                               NULL,
                               0);*/

        //Check if we had an error
        if (iResult!=GetErrorCode())
            //Use regular send !!!
            iResult=sendto(GetHandle(),
                           (const char*)pBuffer,
                           ulBufferLength,
                           0,
                           (sockaddr*)&aTargetAddress,
                           sizeof(aTargetAddress));

        //Is all OK?
        if (iResult==GetErrorCode())
            //Set the error
            SetLastError("Send");
    }

    //Done
    return iResult;
}
ERROR_HANDLER_RETURN("Send",GetErrorCode())

'See Also' icon -- Shortcut to top of page. See Also

Class Overview Class Overview  |  Public base class CSpoofBase  |  Hierarchy Chart Hierarchy Chart


Get Surveyor!This web site was generated using Surveyor V4.50.811.1.  Click here for more information. Site content copyright © 2003 Komodia LTD.. See the About page for additional notices. This page last updated: 24 Feb 2003.