| CSpoofSocket:: Send() |

Public Function | Declared in: SpoofSocket.h

## Declaration

    virtual BOOL Send(
        LPCSTR lpDestinationAddress,
        const char* buf,
        int bufLength,
        unsigned short usDestinationPort=0);

## Description

Send data to a socket

## Function Body

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

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

        m_TargetAddress.sin_family=AF_INET;
        m_TargetAddress.sin_addr.s_addr=inet_addr(lpDestinationAddress);
        m_TargetAddress.sin_port=htons(usDestinationPort);

        //packet send status ?
        int iResult;

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

            if (m_Options)
                ucHeaderLength+=m_IPOptions->GetBufferLength();

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

            //Set the address
            SetIPHeaderAddress(lpHead,m_SourceAddress,lpDestinationAddress);

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

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

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

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

            //Do we need to copy options ?
            if (m_Options)
                memcpy(newBuf+IpHeaderLength,m_IPOptions->GetBuffer(),m_IPOptions->GetBufferLength());

            //Only if not null
            if (buf)
                memcpy(newBuf+ucHeaderLength,buf,bufLength);

            //Calculate the checksum
            lpHead->CheckSum=CalculateChecksum((unsigned short*)newBuf,iTotalLength);

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

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

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

            if (iResult==SOCKET_ERROR)
                SetLastError("Send - Raw");

            //Dispose of the buffer
            delete [] newBuf;

            //Dispose the header
            delete lpHead;
        }
        else
        {
            iResult=!SOCKET_ERROR;

            //Insert options
            //if (m_Options)
                //if (setsockopt(GetHandle(),IPPROTO_IP,IP_OPTIONS,m_IPOptions->GetBuffer(),m_IPOptions->GetBufferLength())==SOCKET_ERROR)
                    //Error
                    //iResult=SOCKET_ERROR;
                //else
                //  ;
            //else
                //No options
                //iResult=setsockopt(GetHandle(),IPPROTO_IP,IP_OPTIONS,NULL,0);

            //Check if we had an error
            if (iResult!=SOCKET_ERROR)
                //Use regular send !!!
                iResult=sendto(GetHandle(),(const char*)buf,bufLength,0,(sockaddr*)&m_TargetAddress,sizeof(m_TargetAddress));
        }

        if (iResult==SOCKET_ERROR)
            //Set the error
            SetLastError("Send");

        return iResult!=SOCKET_ERROR;
    }
    ERROR_HANDLER_RETURN("Send",FALSE)

## See Also

Class Overview | CSpoofBase | Hierarchy Chart

This web site was generated using Surveyor V4.50.811.1. Click here for more information. | Site content copyright © 2001 Barak Weichselbaum. See the About page for additional notices. This page last updated: 27 Jun 2001.