![]() |
Send() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Public Function |
Declared in: SpoofSocket.h |
virtual int Send( IP aDestinationAddress, const char* pBuffer, unsigned long ulBufferLength, unsigned short usDestinationPort=0);
Send data to a socket , can be used with all sub sockets (protocols) Return value: Positive - The number of bytes received. Negative - Error
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())
![]() |
Site content copyright © 2003 Komodia LTD.. See the About page for additional notices. This page last updated: 24 Feb 2003. |