Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150483
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-06-26 16:30:02

1. 将arp缓存表中某一IP的MAC地址修改

2.源码
  1. cong@msi:/work/test/tcpip/busy/arp$ cat arp.c
  2. #include "utils.h"

  3. #include <net/if_arp.h>
  4. #include <linux/sockios.h>
  5. //usage: arp 192.168.4.111 78:6a:89:18:31:0c
  6. #define ETH_ALEN 6


  7. int INET_resolve(const char *name, struct sockaddr_in *s_in)
  8. {
  9.     struct hostent *hp;
  10.     /* Grmpf. -FvK */
  11.     s_in->sin_family = AF_INET;
  12.     s_in->sin_port = 0;

  13.     /* Look to see if it's a dotted quad. */
  14.     if (inet_aton(name, &s_in->sin_addr)) {
  15.         return 0;
  16.     }
  17.     
  18.     hp = gethostbyname(name);
  19.     if (hp == NULL) {
  20.         return -1;
  21.     }
  22.     memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  23.     return 0;
  24. }

  25. /* Convert Ethernet address from "XX[:]XX[:]XX[:]XX[:]XX[:]XX" to sockaddr.
  26.  * Return nonzero on error.
  27.  */
  28. int in_ether(const char *bufp, struct sockaddr *sap)
  29. {
  30.     char *ptr;
  31.     int i, j;
  32.     unsigned char val;
  33.     unsigned char c;
  34.     dbmsg();
  35.     sap->sa_family = ARPHRD_ETHER;
  36.     ptr = (char *) sap->sa_data;

  37.     i = ETH_ALEN;
  38.     goto first;
  39.     do {
  40.         /* We might get a semicolon here */
  41.         if (*bufp == ':')
  42.             bufp++;
  43.  first:
  44.         j = val = 0;
  45.         do {
  46.             c = *bufp;
  47.             if (((unsigned char)(c - '0')) <= 9) {
  48.                 c -= '0';
  49.             } else if ((unsigned char)((c|0x20) - 'a') <= 5) {
  50.                 c = (unsigned char)((c|0x20) - 'a') + 10;
  51.             } else {
  52.                 if (j && (c == ':' || c == '\0'))
  53.                     /* One-digit byte: __:X:__ */
  54.                     break;
  55.                 return -1;
  56.             }
  57.             ++bufp;
  58.             val <<= 4;
  59.             val += c;
  60.             j ^= 1;
  61.         } while (j);

  62.         *ptr++ = val;

  63.     } while (--i);

  64.     /* Error if we aren't at end of string */
  65.     return *bufp;
  66. }

  67. int main ( int argc, char *argv[] )
  68. {
  69.     struct arpreq req;
  70.     struct sockaddr sa;
  71.     int ret;
  72.     if(argc != 3)
  73.     {
  74.         dbmsg("usage: ./arp ");
  75.         exit(0);
  76.     }
  77.     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  78.     if(sockfd < 0)
  79.         return -1;
  80.     memset(&req, 0, sizeof(req));
  81.     //a. put host IP to arp_p
  82.     INET_resolve(argv[1], (struct sockaddr_in*)&sa);
  83.     memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));

  84.     //b. put host MAC to arp_ha
  85.     in_ether(argv[2], &req.arp_ha);

  86.     //c. set flag
  87.     req.arp_flags = ATF_PERM | ATF_COM;

  88.     //d. invoke ioctl
  89.     dbmsg("next invoke ioctl");
  90.     ret = ioctl(sockfd, SIOCSARP, &req);
  91.     if(ret < 0)
  92.     {
  93.         dbmsg("error %s", strerror(errno));
  94.         exit(-1);
  95.     }
  96.     return EXIT_SUCCESS;
  97. }

3. 执行结果
  1. cong@msi:/work/test/tcpip/busy/arp$ cat /proc/net/arp | grep "192.168.4.111"   //查看arp缓存中的192.168.4.111的MAC地址
  2. 192.168.4.111 0x1 0x2 78:6a:89:18:31:0b * eth2    -->结尾是0b

  3. cong@msi:/work/test/tcpip/busy/arp$ ping 192.168.4.111   //可以ping通的
  4. PING 192.168.4.111 (192.168.4.111) 56(84) bytes of data.
  5. 64 bytes from 192.168.4.111: icmp_seq=1 ttl=64 time=93.9 ms

  6. cong@msi:/work/test/tcpip/busy/arp$ sudo ./arp 192.168.4.111 78:6a:89:18:31:0c  //修改arp缓存中的192.168.4.111的MAC地址
  7. arp.c:in_ether[38]:
  8. arp.c:main[101]: next invoke ioctl

  9. cong@msi:/work/test/tcpip/busy/arp$ ping 192.168.4.111     //再次ping发现不通了
  10. PING 192.168.4.111 (192.168.4.111) 56(84) bytes of data.
  11. From 192.168.4.62: icmp_seq=1 Redirect Network(New nexthop: 192.168.4.62)
  12. From 192.168.4.62: icmp_seq=2 Redirect Network(New nexthop: 192.168.4.62)
  13. From 192.168.4.62: icmp_seq=3 Redirect Network(New nexthop: 192.168.4.62)

  14. cong@msi:/work/test/tcpip/busy/arp$ cat /proc/net/arp | grep "192.168.4.111"     //查看arp缓存中的192.168.4.111的MAC地址
  15. 192.168.4.111 0x1 0x6 78:6a:89:18:31:0c * eth2  -->将结尾的0b改成了0c
4.
arpset.rar (下载后改名为arpset.rar)

阅读(2798) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~