📄 uip_arp.lst
字号:
C51 COMPILER V9.00 UIP_ARP 02/08/2010 20:58:32 PAGE 1
C51 COMPILER V9.00, COMPILATION OF MODULE UIP_ARP
OBJECT MODULE PLACED IN .\uip_arp.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ..\uip1.0\uip\uip_arp.c LARGE BROWSE INCDIR(..\uip1.0\apps;..\uip1.0\uip;..
-\cp2200;..\cp2200;..\uip1.0\apps\dhcpc;..\uip1.0\apps\httpd) DEBUG OBJECTEXTEND PRINT(.\uip_arp.lst) OBJECT(.\uip_arp.ob
-j)
line level source
1 /**
2 * \addtogroup uip
3 * @{
4 */
5
6 /**
7 * \defgroup uiparp uIP Address Resolution Protocol
8 * @{
9 *
10 * The Address Resolution Protocol ARP is used for mapping between IP
11 * addresses and link level addresses such as the Ethernet MAC
12 * addresses. ARP uses broadcast queries to ask for the link level
13 * address of a known IP address and the host which is configured with
14 * the IP address for which the query was meant, will respond with its
15 * link level address.
16 *
17 * \note This ARP implementation only supports Ethernet.
18 */
19
20 /**
21 * \file
22 * Implementation of the ARP Address Resolution Protocol.
23 * \author Adam Dunkels <adam@dunkels.com>
24 *
25 */
26
27 /*
28 * Copyright (c) 2001-2003, Adam Dunkels.
29 * All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The name of the author may not be used to endorse or promote
40 * products derived from this software without specific prior
41 * written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
49 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
C51 COMPILER V9.00 UIP_ARP 02/08/2010 20:58:32 PAGE 2
54 *
55 * This file is part of the uIP TCP/IP stack.
56 *
57 * $Id: uip_arp.c,v 1.8 2006/06/02 23:36:21 adam Exp $
58 *
59 */
60
61
62 #include "uip_arp.h"
63
64 #include <string.h>
65
66 struct arp_hdr {
67 struct uip_eth_hdr ethhdr;
68 u16_t hwtype;
69 u16_t protocol;
70 u8_t hwlen;
71 u8_t protolen;
72 u16_t opcode;
73 struct uip_eth_addr shwaddr;
74 u16_t sipaddr[2];
75 struct uip_eth_addr dhwaddr;
76 u16_t dipaddr[2];
77 };
78
79 struct ethip_hdr {
80 struct uip_eth_hdr ethhdr;
81 /* IP header. */
82 u8_t vhl,
83 tos,
84 len[2],
85 ipid[2],
86 ipoffset[2],
87 ttl,
88 proto;
89 u16_t ipchksum;
90 u16_t srcipaddr[2],
91 destipaddr[2];
92 };
93
94 #define ARP_REQUEST 1
95 #define ARP_REPLY 2
96
97 #define ARP_HWTYPE_ETH 1
98
99 struct arp_entry {
100 u16_t ipaddr[2];
101 struct uip_eth_addr ethaddr;
102 u8_t time;
103 };
104
105 static const struct uip_eth_addr broadcast_ethaddr =
106 {{0xff,0xff,0xff,0xff,0xff,0xff}};
107 static const u16_t broadcast_ipaddr[2] = {0xffff,0xffff};
108
109 static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
110 static u16_t ipaddr[2];
111 static u8_t i, c;
112
113 static u8_t arptime;
114 static u8_t tmpage;
115
C51 COMPILER V9.00 UIP_ARP 02/08/2010 20:58:32 PAGE 3
116 #define BUF ((struct arp_hdr *)&uip_buf[0])
117 #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
118 /*-----------------------------------------------------------------------------------*/
119 /**
120 * Initialize the ARP module.
121 *
122 */
123 /*-----------------------------------------------------------------------------------*/
124 void
125 uip_arp_init(void)
126 {
127 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
128 2 memset(arp_table[i].ipaddr, 0, 4);
129 2 }
130 1 }
131 /*-----------------------------------------------------------------------------------*/
132 /**
133 * Periodic ARP processing function.
134 *
135 * This function performs periodic timer processing in the ARP module
136 * and should be called at regular intervals. The recommended interval
137 * is 10 seconds between the calls.
138 *
139 */
140 /*-----------------------------------------------------------------------------------*/
141 void
142 uip_arp_timer(void)
143 {
144 1 struct arp_entry *tabptr;
145 1
146 1 ++arptime;
147 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
148 2 tabptr = &arp_table[i];
149 2 if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
150 2 arptime - tabptr->time >= UIP_ARP_MAXAGE) {
151 3 memset(tabptr->ipaddr, 0, 4);
152 3 }
153 2 }
154 1
155 1 }
156 /*-----------------------------------------------------------------------------------*/
157 static void
158 uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
159 {
160 1 register struct arp_entry *tabptr;
161 1 /* Walk through the ARP mapping table and try to find an entry to
162 1 update. If none is found, the IP -> MAC address mapping is
163 1 inserted in the ARP table. */
164 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
165 2
166 2 tabptr = &arp_table[i];
167 2 /* Only check those entries that are actually in use. */
168 2 if(tabptr->ipaddr[0] != 0 &&
169 2 tabptr->ipaddr[1] != 0) {
170 3
171 3 /* Check if the source IP address of the incoming packet matches
172 3 the IP address in this ARP table entry. */
173 3 if(ipaddr[0] == tabptr->ipaddr[0] &&
174 3 ipaddr[1] == tabptr->ipaddr[1]) {
175 4
176 4 /* An old entry found, update this and return. */
177 4 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
C51 COMPILER V9.00 UIP_ARP 02/08/2010 20:58:32 PAGE 4
178 4 tabptr->time = arptime;
179 4
180 4 return;
181 4 }
182 3 }
183 2 }
184 1
185 1 /* If we get here, no existing ARP table entry was found, so we
186 1 create one. */
187 1
188 1 /* First, we try to find an unused entry in the ARP table. */
189 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
190 2 tabptr = &arp_table[i];
191 2 if(tabptr->ipaddr[0] == 0 &&
192 2 tabptr->ipaddr[1] == 0) {
193 3 break;
194 3 }
195 2 }
196 1
197 1 /* If no unused entry is found, we try to find the oldest entry and
198 1 throw it away. */
199 1 if(i == UIP_ARPTAB_SIZE) {
200 2 tmpage = 0;
201 2 c = 0;
202 2 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
203 3 tabptr = &arp_table[i];
204 3 if(arptime - tabptr->time > tmpage) {
205 4 tmpage = arptime - tabptr->time;
206 4 c = i;
207 4 }
208 3 }
209 2 i = c;
210 2 tabptr = &arp_table[i];
211 2 }
212 1
213 1 /* Now, i is the ARP table entry which we will fill with the new
214 1 information. */
215 1 memcpy(tabptr->ipaddr, ipaddr, 4);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -