这是程序代码,它应该发送一个简单的'ping'。我知道我可以使用IcmpSendEcho2()
函数,但我决定这样做:
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;
}
这就是问题所在。一切都很好,直到sendto()
功能。我收到10013错误代码表单WSAGetLastError()
,表示WSAEACCES Permission denied
我启动这个应用程序管理员权限,所以我不知道为什么我不能使用此套接字配置发送数据。
答案 0 :(得分:3)
对于sendto()
,WSAEACCES
与用户权限无关。如果您没有足够的权限使用RAW套接字,则会在socket()
上出现错误。 Read the documentation。当sendto()
报告WSAEACCES
时,表示您的sockaddr_in
结构未正确填写。我看到你的代码中有三个与此相关的错误:
1)在使用之前,您没有将sockaddr_in
归零。这样做非常重要。
2)您正在分配1个字符的缓冲区以从用户接收目标地址。对于IPv4地址(包括空终止符),缓冲区需要为16个字符。所以你有一个缓冲区溢出。
3)分配htons()
字段时,您没有致电sin_port
。