代码如下,现在源IP填表自己的IP,可以发送.
如果不填自己的IP就会出错,错误:10004
怎么办?, 系统VC7.0+xp,sp2
#include<winsock2.h>
#include<windows.h>
#include<ws2tcpip.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#pragma comment(lib,"ws2_32.lib")
typedef struct iphdr {
unsigned char lenver;
unsigned char tos; // Type of service
unsigned short total_len; // total length of the packet
unsigned short ident; // unique identifier
unsigned short frag_and_flags; // flags
unsigned char ttl;
unsigned char proto; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum
unsigned int sourceIP;
unsigned int destIP;
}IpHeader,*Lpipheader;
typedef struct _udphdr{
USHORT sourceport;
USHORT destport;
USHORT len;
USHORT checksum;
}UdpHeader,*LpUdpHeader;
#define xmalloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))
#define xfree(p) HeapFree(GetProcessHeap(),0,(p))
USHORT checksum(USHORT *, int);
int main()
{
WSADATA wsaData;
SOCKET sockRaw;
struct sockaddr_in dest;
int error;
unsigned int datasize=1400;
int ipflag=1;
char *iphead;
char *data;
unsigned int addr=0;
USHORT seq_no = 0;
if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0){
fprintf(stderr,"WSAStartup failed: %d\n",GetLastError());
getch();
return 0;
}
sockRaw = socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
if (sockRaw == INVALID_SOCKET)
{
fprintf(stderr,"WSASocket() failed: %d\n",WSAGetLastError());
getch();
return 0;
}
error = setsockopt(sockRaw,IPPROTO_IP,IP_HDRINCL,(char*)&ipflag,sizeof(ipflag));
if(error == SOCKET_ERROR) {
fprintf(stderr,"failed to set IP_HDRINCL: %d\n",WSAGetLastError());
getch();
return 0;
}
dest.sin_addr.s_addr = inet_addr("204.168.49.1"); // 目标IP
dest.sin_family = AF_INET;
dest.sin_port=htons(123);
iphead = (char*)xmalloc(datasize);
data = iphead+20;
if (!iphead)
{
fprintf(stderr,"HeapAlloc failed %d\n",GetLastError());
getch();
return 0;
}
memset(iphead,48,datasize);
((Lpipheader)iphead)->lenver=0x45;
((Lpipheader)iphead)->tos=0;
((Lpipheader)iphead)->total_len=datasize;
((Lpipheader)iphead)->ident=(unsigned short)GetProcessId(NULL);
((Lpipheader)iphead)->frag_and_flags=0x40;
((Lpipheader)iphead)->ttl=128;
((Lpipheader)iphead)->proto=IPPROTO_UDP;
((Lpipheader)iphead)->sourceIP=inet_addr("204.168.49.10");// 源IP
((Lpipheader)iphead)->destIP=inet_addr("204.168.49.1"); // 目标IP
((Lpipheader)iphead)->checksum=0;
((Lpipheader)iphead)->checksum=checksum((USHORT*)iphead,20);
((LpUdpHeader)data)->destport = htons(123);
((LpUdpHeader)data)->sourceport = htons(536);
((LpUdpHeader)data)->len = datasize-20;
((LpUdpHeader)data)->checksum = 0;
((LpUdpHeader)data)->checksum = checksum((USHORT*)data,datasize-20);
error = sendto(sockRaw,iphead,datasize,0,(sockaddr *)&dest,sizeof(sockaddr));
if (error == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAETIMEDOUT)
{
printf("Request timed out.\n");
return 0;
getch();
}
printf("sendto failed: %d\n",WSAGetLastError());
_getch();
return 0;
}
if (error < datasize )
{
fprintf(stdout,"Wrote %d bytes\n",error);
getch();
return 0;
}
WSACleanup();
fprintf(stdout,"send %d bytes\n",error);
getch();
return 0;
}
USHORT checksum(USHORT *buffer, int size) {
unsigned long cksum=0;
while(size >1)
{
cksum+=*buffer++;
size -=sizeof(USHORT);
}
if(size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课