0

Here is the code of program, which should send a simple 'ping'. I know that i could use IcmpSendEcho2() function, but i decide to do this this way:

struct ICMPh
{
unsigned char typ;
unsigned char kod;
unsigned short suma_spr;
unsigned short id;
unsigned short seq;
};
unsigned short CalcChecksum (char *pBuffer, int nLen)
 { 
  ////Checksum for ICMP is calculated in the same way as for IP header
  //This code was taken from: http://www.netfor2.com/ipsum.htm

  unsigned short nWord;
  unsigned int nSum = 0;
  int i;


  for (i = 0; i < nLen; i = i + 2)
  {
      nWord =((pBuffer [i] << 8)& 0xFF00) + (pBuffer [i + 1] & 0xFF);
      nSum = nSum + (unsigned int)nWord;  
  }


 while (nSum >> 16)
  {
      nSum = (nSum & 0xFFFF) + (nSum >> 16);
  }

  //One's complement the result
  nSum = ~nSum;

  return ((unsigned short) nSum);
 }
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData)==SOCKET_ERROR)
{
    cout<<"ERROR at startup:\n";
    cout<<WSAGetLastError()<<"\n";
    system("pause");
    WSACleanup();
    return -1;
}
SOCKET soc;
int seq=0;

soc=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(soc==INVALID_SOCKET)
{
    cout<<"ERROR: INVALID SOCKET\n";
    cout<<WSAGetLastError()<<"\n";
    system("pause");
    WSACleanup();
    return -1;
}
char* addres=new char;
cin>>addres;

SOCKADDR_IN adr;
adr.sin_addr.S_un.S_addr=inet_addr(addres);
adr.sin_family=AF_INET;
adr.sin_port=rand();

ICMPh icmp;
icmp.kod=0;
icmp.seq=htons(seq++);
icmp.typ=8;
icmp.suma_spr=0;
icmp.id=htons(rand());

int size=32;
char *buffer;

buffer=new char[sizeof(ICMPh)+size];
memcpy_s(buffer,sizeof(ICMPh),&icmp,sizeof(ICMPh));
memset(buffer+sizeof(ICMPh),'x',size);

icmp.suma_spr=htons(CalcChecksum(buffer,sizeof(ICMPh)+size));

memcpy_s(buffer,sizeof(ICMPh),&icmp,sizeof(ICMPh));

if(sendto(soc,buffer,sizeof(ICMPh)+size,0,(SOCKADDR*)&adr,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
{
    cout<<"FAIL at 'sendto':\n";
    cout<<WSAGetLastError()<<"\n";
    system("pause");
    WSACleanup();
    delete []buffer;
    return -1;
}
else
    cout<<"FINE";
WSACleanup();
system("pause");
delete []buffer;
return 0;
} 

And here is the problem. Everything is working fine untill it comes to sendto() function. I get an 10013 Error code form WSAGetLastError() which means WSAEACCES Permission denied
I start this application witch admin privliges, so i have no idea why i cant send data using this socket configuration.

Ufo
  • 225
  • 5
  • 17

1 Answers1

3

For sendto(), WSAEACCES is not related to user permissions. You would have gotten an error on socket() instead if you did not have enough rights to use a RAW socket. Read the documentation. When sendto() reports WSAEACCES, it means your sockaddr_in struct was not filled in correctly. I see three bugs in your code related to that:

1) You are not zeroing out the sockaddr_in before using it. It is important to do so.

2) You are allocating a 1-character buffer to receive the target address from the user. The buffer needs to be 16 characters instead for an IPv4 address (including the null terminator). So you have a buffer overflow.

3) You are not calling htons() when assigning the sin_port field.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770