📄 arp1.lst
字号:
C51 COMPILER V7.09 ARP1 06/28/2007 09:51:20 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE ARP1
OBJECT MODULE PLACED IN ARP1.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\ARP1.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\ARP1.lst) OBJECT(ARP1.ob
-j)
line level source
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2002 Jim Brady
3 // Do not use commercially without author's permission
4 // Last revised August 2002
5 // Net ARP.C
6 //
7 // This module handles ARP messages and ARP resolution and manages
8 // the ARP cache. Refer to RFC 826 and RFC 1122
9 //-----------------------------------------------------------------------------
10 //#include "stdio.h"
11
12 #include "stdlib.h"
13 #include "net1.h"
14 #include "eth1.h"
15 #include "serial1.h"
16 #include "ip1.h"
17 #include "arp1.h"
18 #include "utils.h"
19
20
21 extern WAIT wait;
22 extern UCHAR my_hwaddr[];
23 extern UCHAR broadcast_hwaddr[];
24 extern ulong my_ipaddr;
25 extern ulong my_subnet;
26 extern ulong gateway_ipaddr;
27 extern UCHAR debug;
28 ARP_CACHE arp_cache[CACHESIZE];
29 UCHAR waiting_for_arp;
30
31
32 void init_arp(void)
33 {
34 1 memset(arp_cache, 0, sizeof(arp_cache));
35 1 memset(&wait, 0, sizeof(wait));
36 1 waiting_for_arp = FALSE;
37 1 }
38
39
40
41
42 //------------------------------------------------------------------------
43 // This is called every 60 seconds to age the ARP cache
44 // If an entry times out then it is deleted from the cache
45 // See "TCP/IP Illustrated, Volume 1" Sect 4.3
46 //------------------------------------------------------------------------
47
48 //每隔60 s刷新一次ARP表. 超过60s未用的记录删掉.
49 void age_arp_cache(void)
50 {
51 1 UCHAR i;
52 1
53 1 for (i=0; i < CACHESIZE; i++)
54 1 { //如果ARP cahce表的某一记录的ipaddr和timer不为0,此记录的timer减一.如果减一等于0, 此记录丢弃.
C51 COMPILER V7.09 ARP1 06/28/2007 09:51:20 PAGE 2
55 2 if ((arp_cache[i].ipaddr != 0) && (arp_cache[i].timer))
56 2 {
57 3 arp_cache[i].timer--;
58 3 if (arp_cache[i].timer == 0)
59 3 {
60 4 // Timed out so clear out cache entry
61 4 // Do not need to zero hwaddr
62 4 arp_cache[i].ipaddr = 0;
63 4 // if (debug) serial_send("ARP: Aged out a cache entry\r");
64 4 }
65 3 }
66 2 }
67 1 }
68
69
70
71
72 //------------------------------------------------------------------------
73 // This allocates memory for the entire outgoing message,
74 // including eth and ip headers, then builds an outgoing
75 // ARP response message
76 // See "TCP/IP Illustrated, Volume 1" Sect 4.4
77 //------------------------------------------------------------------------
78 //arp_send(arp->source_hwaddr, arp->source_ipaddr, ARP_RESPONSE);
79 // UCHAR * hwaddr, 本端硬件地址;
80 // ulong ipaddr, 对端IP地址;
81 // UCHAR msg_type, 本端ARP类型为: ARP请求还是ARP应答
82
83 void arp_send(UCHAR * hwaddr, ulong ipaddr, UCHAR msg_type)
84 {
85 1 UCHAR * outbuf;
86 1 ARP_HEADER * arp;
87 1
88 1
89 1 // Allocate memory for entire outgoing message including
90 1 // eth header. Always 42 bytes
91 1 outbuf = (UCHAR *)malloc(42);
92 1 if (outbuf == NULL)
93 1 {
94 2 // if (debug) serial_send("ARP: Oops, out of memory\r");
95 2 return;
96 2 }
97 1
98 1 // Allow 14 bytes for the ethernet header
99 1 arp = (ARP_HEADER *)(outbuf + 14); //定位ARP头
100 1
101 1 arp->hardware_type = DIX_ETHERNET; //硬件地址类型: DIX_DIX_ETHERNET
102 1
103 1 arp->protocol_type = IP_PACKET; //上层协议类型: IP_PACKET
104 1
105 1 arp->hwaddr_len = 6; //硬件地址长度: 6
106 1 arp->ipaddr_len = 4; //上层协议长度: 6
107 1 arp->message_type = (uint)msg_type; //ARP类型: 请求还是应答
108 1
109 1 // My hardware address and IP addresses
110 1 memcpy((char *)arp->source_hwaddr, my_hwaddr, 6);
111 1 arp->source_ipaddr = my_ipaddr;
112 1
113 1 // Destination hwaddr and dest IP addr
114 1 if (msg_type == ARP_REQUEST) //如果是ARP请求,向所有主机(EtherADDR = 0)查询指定IP地址的对应硬件地
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -