免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8771 | 回复: 9
打印 上一主题 下一主题

ARP头文件的解释 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-11 16:12 |只看该作者 |倒序浏览
/*        注释ie_minix   */
/*        要了解ARP包,他的结构和一些常量你必须知道是什么意思*/                              
#ifndef _NET_IF_ARP_H_
#define        _NET_IF_ARP_H_

/*
* 地址解释协议.
*
* 查看 RFC 826 对该协议的描述.  ARP 包长度是变化的
* 头部(arphdr)结构定义了固定长度部分.
*/
struct        arphdr {
        u_short        ar_hrd;                /* 格式化的硬件地址 */
#define ARPHRD_ETHER         1        /* 以太网格式 */
#define ARPHRD_IEEE802        6        /* 令牌环网格式 */
#define ARPHRD_FRELAY         15        /* 帧中继硬件格式 */
        u_short        ar_pro;                /* 协议地址格式 */
        u_char        ar_hln;                /* 硬件地址长度*/
        u_char        ar_pln;                /* 协议地址长度 */
        u_short        ar_op;                /* 以下之一: */
#define        ARPOP_REQUEST        1        /* 发出请求解释一IP地址 */
#define        ARPOP_REPLY        2        /* 回应上一个请求 */
#define        ARPOP_REVREQUEST 3        /* 请求一IP地址(给出硬件地址,实际上是逆向地址解释) */
#define        ARPOP_REVREPLY        4        /* 回应逆向地址解释 */
#define ARPOP_INVREQUEST 8         /* 请求对方的ID */
#define ARPOP_INVREPLY        9        /* 回应对方的ID */
/*
* 下面的字段是可变长的,
* 主要是依照上面给出的定义.
*/
#ifdef COMMENT_ONLY
        u_char        ar_sha[];        /* 发送者硬件地址 */
        u_char        ar_spa[];        /* 发送者协议地址 */
        u_char        ar_tha[];        /* 目的方硬件地址 */
        u_char        ar_tpa[];        /* 目的方协议地址*/
#endif
};

/*
* ARP ioctl 请求
*/
struct arpreq {
        struct        sockaddr arp_pa;                /* 协议地址 */
        struct        sockaddr arp_ha;                /* 硬件地址 */
        int        arp_flags;                        /* 标识 */
};
/*  arp_flags 和 at_flags 域的值 */
#define        ATF_INUSE        0x01        /* 入口在使用 */
#define ATF_COM                0x02        /* 完整的入口 (enaddr 有效) */
#define        ATF_PERM        0x04        /* 持久的入口 */
#define        ATF_PUBL        0x08        /* 发布一入口 (回应其他主机) */
#define        ATF_USETRAILERS        0x10        /* 有追踪请求 */

#ifdef _KERNEL
/*
* 该结构是以太网设备驱动程序和ARP程序所共享.
*/
struct        arpcom {
        /*
         *  ifnet 结构必须在此结构的第一个位置.
         */
        struct         ifnet ac_if;       
        u_char        ac_enaddr[6];                /* 以太网硬件地址*/
        int        ac_multicnt;                /* 多播地址列表数 */
        void        *ac_netgraph;                /* PPPoE(ADSL)的指针 */
};

extern u_char        etherbroadcastaddr[6];
#endif

#endif /* !_NET_IF_ARP_H_ */

论坛徽章:
0
2 [报告]
发表于 2003-06-11 18:32 |只看该作者

ARP头文件的解释

楼上的高手啊,给偶这个新手写个程序吧,感激不尽哦~~~
send_arp
这是程序在Linux的实现(),给写个FreeBSD上的吧,多谢哦~~~

  1. /*
  2. This program sends out one ARP packet with source/target IP
  3. and Ethernet hardware addresses suuplied by the user. It  
  4. compiles and works on Linux and will probably work on any  
  5. Unix that has SOCK_PACKET. volobuev@t1.chem.umn.edu  
  6. */  
  7. /*it do not compile at FreeBSD. :(
  8. */
  9. #include <netdb.h>;
  10. #include <sys/socket.h>;
  11. #include <sys/types.h>;
  12. #include <stdio.h>;
  13. #include <errno.h>;
  14. #include <sys/ioctl.h>;
  15. #include <net/if.h>;
  16. #include <signal.h>;
  17. #include <netinet/ip.h>;
  18. #include <netinet/in.h>;
  19. #include <string.h>;
  20. #include <arpa/inet.h>;
  21. #include <netinet/ip_icmp.h>;
  22. #include <linux/if_ether.h>;
  23. #define ETH_HW_ADDR_LEN 6  
  24. #define IP_ADDR_LEN 4  
  25. #define ARP_FRAME_TYPE 0x0806  
  26. #define ETHER_HW_TYPE 1  
  27. #define IP_PROTO_TYPE 0x0800  
  28. #define OP_ARP_REQUEST 2  
  29. #define OP_ARP_QUEST 1
  30. #define DEFAULT_DEVICE "eth0"  
  31. char usage[] = {"send_arp: sends out custom ARP packet. yuri volobuev
  32.    usage: send_arp src_ip_addr src_hw_addr targ_ip_addr tar_hw_addr number"};  
  33. struct arp_packet  
  34. {  
  35. u_char targ_hw_addr[ETH_HW_ADDR_LEN];  
  36. u_char src_hw_addr[ETH_HW_ADDR_LEN];  
  37. u_short frame_type;  
  38. u_short hw_type;  
  39. u_short prot_type;  
  40. u_char hw_addr_size;  
  41. u_char prot_addr_size;  
  42. u_short op;  
  43. u_char sndr_hw_addr[ETH_HW_ADDR_LEN];  
  44. u_char sndr_ip_addr[IP_ADDR_LEN];  
  45. u_char rcpt_hw_addr[ETH_HW_ADDR_LEN];  
  46. u_char rcpt_ip_addr[IP_ADDR_LEN];  
  47. u_char padding[18];  
  48. };  
  49. void die (char *);  
  50. void get_ip_addr (struct in_addr *, char *);  
  51. void get_hw_addr (char *, char *);  
  52. int main (int argc, char * argv[])  
  53. {  
  54. struct in_addr src_in_addr, targ_in_addr;  
  55. struct arp_packet pkt;  
  56. struct sockaddr sa;  
  57. int sock;  
  58. int j,number;
  59. if (argc != 6)  
  60. die(usage);  
  61. sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_RARP));  
  62. if (sock < 0)  
  63. {  
  64. perror("socket");  
  65. exit(1);  
  66. }  
  67. number=atoi(argv[5]);
  68. pkt.frame_type = htons(ARP_FRAME_TYPE);  
  69. pkt.hw_type = htons(ETHER_HW_TYPE);  
  70. pkt.prot_type = htons(IP_PROTO_TYPE);  
  71. pkt.hw_addr_size = ETH_HW_ADDR_LEN;  
  72. pkt.prot_addr_size = IP_ADDR_LEN;  
  73. pkt.op = htons(OP_ARP_QUEST);  
  74. get_hw_addr(pkt.targ_hw_addr, argv[4]);  
  75. get_hw_addr(pkt.rcpt_hw_addr, argv[4]);  
  76. get_hw_addr(pkt.src_hw_addr, argv[2]);  
  77. get_hw_addr(pkt.sndr_hw_addr, argv[2]);  
  78. get_ip_addr(&src_in_addr, argv[1]);  
  79. get_ip_addr(&targ_in_addr, argv[3]);  
  80. memcpy(pkt.sndr_ip_addr, &src_in_addr, IP_ADDR_LEN);  
  81. memcpy(pkt.rcpt_ip_addr, &targ_in_addr, IP_ADDR_LEN);  
  82. bzero(pkt.padding,18);  
  83. strcpy(sa.sa_data,DEFAULT_DEVICE);  
  84. for (j=0;j<number;j++)
  85. {
  86. if (sendto(sock,&pkt,sizeof(pkt),0,&sa,sizeof(sa)) < 0)  
  87. {  
  88. perror("sendto");  
  89. exit(1);  
  90. }   
  91. }
  92. exit(0);  
  93. }  
  94. void die (char *str)  
  95. {  
  96. fprintf(stderr,"%s\n",str);  
  97. exit(1);  
  98. }  
  99. void get_ip_addr (struct in_addr *in_addr, char *str)  
  100. {  
  101. struct hostent *hostp;  
  102. in_addr->;s_addr = inet_addr(str);  
  103. if(in_addr->;s_addr == -1)
  104. {  
  105. if ((hostp = gethostbyname(str)))  
  106. bcopy(hostp->;h_addr, in_addr, hostp->;h_length);  
  107. else {  
  108. fprintf(stderr, "send_arp: unknown host %s\n", str);  
  109. exit(1);  
  110.      }  
  111. }  
  112. }  
  113. void get_hw_addr (char *buf, char *str)  
  114. {  
  115. int i;  
  116. char c, val;  
  117. for(i = 0; i < ETH_HW_ADDR_LEN; i++)  
  118. {  
  119. if (!(c = tolower(*str++)))  
  120. die("Invalid hardware address");  
  121. if (isdigit(c))  
  122. val = c - '0';  
  123. else if (c >;= 'a' && c <= 'f')  
  124. val = c-'a'+10;  
  125.      else  
  126. die("Invalid hardware address");  
  127. *buf = val << 4;  
  128. if (!(c = tolower(*str++)))  
  129. die("Invalid hardware address");  
  130. if (isdigit(c))  
  131. val = c - '0';  
  132. else if (c >;= 'a' && c <= 'f')  
  133. val = c-'a'+10;  
  134.      else  
  135. die("Invalid hardware address");  
  136. *buf++ |= val;  
  137. if (*str == ':')  
  138. str++;  
  139. }  
  140. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2003-06-11 21:22 |只看该作者

ARP头文件的解释

这位朋友,真对不起,现在我正在对if_ether.c做说明(也就是整个的ARP的实现代码),完成了以后我一定给你写.

论坛徽章:
0
4 [报告]
发表于 2003-06-11 21:27 |只看该作者

ARP头文件的解释

顺便请问一下, "楼上的高手"是什么意思啊,有什么典故吗,

论坛徽章:
0
5 [报告]
发表于 2003-06-11 22:23 |只看该作者

ARP头文件的解释

[quote]原帖由 "xie_minix"]顺便请问一下, "楼上的高手"是什么意思啊,有什么典故吗,[/quote 发表:
     
"楼上的高手"就是指你呀,HoHo~~~

论坛徽章:
0
6 [报告]
发表于 2003-06-11 22:24 |只看该作者

ARP头文件的解释

[quote]原帖由 "xie_minix"]这位朋友,真对不起,现在我正在对if_ether.c做说明(也就是整个的ARP的实现代码),完成了以后我一定给你写.[/quote 发表:
     
非常感谢!     
PS:ports下有个arping,可以看看,也许有用~~~

论坛徽章:
0
7 [报告]
发表于 2003-06-12 10:12 |只看该作者

ARP头文件的解释

可以给我看看你写的ARP实现的原代码吗?

论坛徽章:
0
8 [报告]
发表于 2003-06-12 12:21 |只看该作者

ARP头文件的解释

BSD上写这种程序好像必须用到BPF,不如Linux下方便,我在网上找了快2个月的时间了,都没有找到BPF编程方面的资料。
哪位有共享出来一下三。

论坛徽章:
0
9 [报告]
发表于 2014-12-30 15:55 |只看该作者
楼主您好,请问您有捕获ARP包分析其原MAC地址的代码吗?您的这个讲解我还是没能找出,网上相关资料太少了!感谢!回复 1# xie_minix


   

论坛徽章:
0
10 [报告]
发表于 2015-01-03 22:57 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP