- 论坛徽章:
- 0
|
本帖最后由 shuimu159951 于 2012-02-27 22:56 编辑
源代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<net/if.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netpacket/packet.h>
#include<linux/if_ether.h>
#include<errno.h>
#include"config.h"
int arp_reply(const char *device,struct arp_msg *msg,int n)
{
int res;
int handle;
char eth_addr[MAC_ADDR_LEN];
struct sockaddr_in ip_addr;
struct sockaddr_ll dst_addr;
/*msg结构赋值*/
ip_addr.sin_addr.s_addr=msg->arp_head.sip;
memcpy(eth_addr,msg->arp_head.eth_shost,MAC_ADDR_LEN);
memcpy(msg->eth_head.shost,msg->eth_head.dhost,MAC_ADDR_LEN);
memcpy(msg->eth_head.dhost,eth_addr,MAC_ADDR_LEN);
memcpy(msg->arp_head.eth_shost,msg->arp_head.eth_dhost,MAC_ADDR_LEN);
msg->arp_head.sip=msg->arp_head.dip;
memcpy(msg->arp_head.eth_dhost,eth_addr,MAC_ADDR_LEN);
msg->arp_head.dip=ip_addr.sin_addr.s_addr;
memset(msg->pad,0,1 ;
handle=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP));
dst_addr.sll_family=PF_PACKET;
dst_addr.sll_ifindex=if_nametoindex(device);
if(handle==-1)
{
fprintf(stderr,"Socket cannot be created!\n" ;
return ABNORMAL;
}
/*发送与测*/
printf("%d\n",sizeof(*msg));
res=sendto(handle,msg,ETH_FRAME_MIN_LEN,0,(struct sockaddr *)&dst_addr,sizeof(dst_addr));
if(res==-1)
{
printf("%d\n",errno);
}
return NORMAL;
}
sendto会返回错误代码22:nvalid argument
msg的赋值段和发送于测试段代码放到另一个程序里是可以正常运行的
各位看看我是不是地址结构等参数构造出错了。 |
|