学习socket raw写一个发送,想测试一下src/des ip都填了127.0.0.1 但是sendto失败了,ip问题吗该怎么填呢?
错误是:sendto: Invalid argument
sendto() error! -1/
部分代码如下:还是哪里写的不对。。
/*********************************************************************************************************
** Function:iphdr_fill
** Description: ip header fill data
*********************************************************************************************************/
void iphdr_fill(struct ip_hdr iphdr , char *buff)
{
iphdr->ip_length = 5;
iphdr->ip_version= 4;
iphdr->ip_tos = 0;
//printf("buff 0 %s OK\n",buff);
iphdr->ip_total_length = htons(sizeof(buff));
//printf("buff 1 %s OK\n",buff);
iphdr->ip_id = 0;
iphdr->ip_flags = 0x40;
iphdr->ip_ttl = 0x40;
iphdr->ip_protocol = 0x11;
iphdr->ip_cksum = 0;
iphdr->ip_source = inet_addr("127.0.0.1");
iphdr->ip_dest = inet_addr("127.0.0.1");
iphdr->ip_cksum = hdr_cksum((unsigned short)buff, 20);
}
/*********************************************************************************************************
** Function:udphdr_fill
** Description: udp header fill data
*********************************************************************************************************/
void udphdr_fill(struct udp_hdr *udphdr, char *buff)
{
udphdr->udp_sport = htons(SRC_PORT);
udphdr->udp_dport = htons(DEST_PORT);
udphdr->udp_ulength = htons(sizeof(buff)-20);
udphdr->udp_chksum = 0;
}
/*********************************************************************************************************
** Function:send_msg
** Description: send msg data
*********************************************************************************************************/
void send_msg(int fd, struct sockaddr_in host_addr)
{
char buff[BUFSIZE];
memset(buff, 'a', sizeof(buff) - 1);
while(1)
{
ip_hdr *iphdr;
iphdr = (ip_hdr)buff;
//printf("buff 3 %s OK\n",buff);
iphdr_fill(iphdr,buff);
udp_hdr udphdr;
udphdr = (udp_hdr)(buff+20);
udphdr_fill(udphdr,buff);
psd_hdr psd;
psd.psd_src = iphdr->ip_source;
psd.psd_dest = iphdr->ip_dest;
psd.psd_memzero = 0;
psd.psd_proto = 0x11;
psd.psd_vlength = udphdr->udp_ulength;
char tmp[sizeof(psd)+ntohs(udphdr->udp_ulength)];
memcpy(tmp, &psd, sizeof(psd));
memcpy(tmp+sizeof(psd), buff+20, sizeof(buff)-20);
udphdr->udp_chksum = hdr_cksum((unsigned short*)tmp, sizeof(tmp));
int res =sendto(fd, buff, sizeof(buff), 0, (struct sockaddr*)&host_addr, sizeof(host_addr));
if(res<0)
{
perror("sendto");
printf("sendto() error! %d/n",res);
}
printf("send %s OK\n",buff);
sleep(1);
}
}
/*********************************************************************************************************
** Function:main
** Description:main
*********************************************************************************************************/
int main(int argc, char *argv[])
{
int sockfd;
int flag = 1;
struct sockaddr_in host_addr;
if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP))<0)
{
printf("socket() error!/n");
exit(1);
}
memset(&host_addr, 0, sizeof(host_addr));
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(DEST_PORT);
host_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, (void *)&flag, sizeof(flag))<0)
{
printf("setsockopt() error!/n");
exit(0);
}
send_msg(sockfd, &host_addr);
}