uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Committer:
ban4jp
Date:
Mon Jun 30 16:00:08 2014 +0000
Revision:
3:a2715e9c7737
Parent:
0:685224d2f66d
backported from Contiki 2.7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ban4jp 0:685224d2f66d 1 /**
ban4jp 0:685224d2f66d 2 * \addtogroup uip
ban4jp 0:685224d2f66d 3 * @{
ban4jp 0:685224d2f66d 4 */
ban4jp 0:685224d2f66d 5
ban4jp 0:685224d2f66d 6 /**
ban4jp 0:685224d2f66d 7 * \defgroup uiparp uIP Address Resolution Protocol
ban4jp 0:685224d2f66d 8 * @{
ban4jp 0:685224d2f66d 9 *
ban4jp 0:685224d2f66d 10 * The Address Resolution Protocol ARP is used for mapping between IP
ban4jp 0:685224d2f66d 11 * addresses and link level addresses such as the Ethernet MAC
ban4jp 0:685224d2f66d 12 * addresses. ARP uses broadcast queries to ask for the link level
ban4jp 0:685224d2f66d 13 * address of a known IP address and the host which is configured with
ban4jp 0:685224d2f66d 14 * the IP address for which the query was meant, will respond with its
ban4jp 0:685224d2f66d 15 * link level address.
ban4jp 0:685224d2f66d 16 *
ban4jp 0:685224d2f66d 17 * \note This ARP implementation only supports Ethernet.
ban4jp 0:685224d2f66d 18 */
ban4jp 0:685224d2f66d 19
ban4jp 0:685224d2f66d 20 /**
ban4jp 0:685224d2f66d 21 * \file
ban4jp 0:685224d2f66d 22 * Implementation of the ARP Address Resolution Protocol.
ban4jp 0:685224d2f66d 23 * \author Adam Dunkels <adam@dunkels.com>
ban4jp 0:685224d2f66d 24 *
ban4jp 0:685224d2f66d 25 */
ban4jp 0:685224d2f66d 26
ban4jp 0:685224d2f66d 27 /*
ban4jp 0:685224d2f66d 28 * Copyright (c) 2001-2003, Adam Dunkels.
ban4jp 0:685224d2f66d 29 * All rights reserved.
ban4jp 0:685224d2f66d 30 *
ban4jp 0:685224d2f66d 31 * Redistribution and use in source and binary forms, with or without
ban4jp 0:685224d2f66d 32 * modification, are permitted provided that the following conditions
ban4jp 0:685224d2f66d 33 * are met:
ban4jp 0:685224d2f66d 34 * 1. Redistributions of source code must retain the above copyright
ban4jp 0:685224d2f66d 35 * notice, this list of conditions and the following disclaimer.
ban4jp 0:685224d2f66d 36 * 2. Redistributions in binary form must reproduce the above copyright
ban4jp 0:685224d2f66d 37 * notice, this list of conditions and the following disclaimer in the
ban4jp 0:685224d2f66d 38 * documentation and/or other materials provided with the distribution.
ban4jp 0:685224d2f66d 39 * 3. The name of the author may not be used to endorse or promote
ban4jp 0:685224d2f66d 40 * products derived from this software without specific prior
ban4jp 0:685224d2f66d 41 * written permission.
ban4jp 0:685224d2f66d 42 *
ban4jp 0:685224d2f66d 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
ban4jp 0:685224d2f66d 44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
ban4jp 0:685224d2f66d 45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ban4jp 0:685224d2f66d 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
ban4jp 0:685224d2f66d 47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ban4jp 0:685224d2f66d 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ban4jp 0:685224d2f66d 49 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ban4jp 0:685224d2f66d 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
ban4jp 0:685224d2f66d 51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ban4jp 0:685224d2f66d 52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ban4jp 0:685224d2f66d 53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ban4jp 0:685224d2f66d 54 *
ban4jp 0:685224d2f66d 55 * This file is part of the uIP TCP/IP stack.
ban4jp 0:685224d2f66d 56 *
ban4jp 0:685224d2f66d 57 *
ban4jp 0:685224d2f66d 58 */
ban4jp 0:685224d2f66d 59
ban4jp 0:685224d2f66d 60
ban4jp 0:685224d2f66d 61 #include "uip_arp.h"
ban4jp 0:685224d2f66d 62
ban4jp 0:685224d2f66d 63 #include <string.h>
ban4jp 0:685224d2f66d 64
ban4jp 0:685224d2f66d 65 struct arp_hdr {
ban4jp 0:685224d2f66d 66 struct uip_eth_hdr ethhdr;
ban4jp 3:a2715e9c7737 67 uint16_t hwtype;
ban4jp 3:a2715e9c7737 68 uint16_t protocol;
ban4jp 3:a2715e9c7737 69 uint8_t hwlen;
ban4jp 3:a2715e9c7737 70 uint8_t protolen;
ban4jp 3:a2715e9c7737 71 uint16_t opcode;
ban4jp 0:685224d2f66d 72 struct uip_eth_addr shwaddr;
ban4jp 3:a2715e9c7737 73 uip_ipaddr_t sipaddr;
ban4jp 0:685224d2f66d 74 struct uip_eth_addr dhwaddr;
ban4jp 3:a2715e9c7737 75 uip_ipaddr_t dipaddr;
ban4jp 0:685224d2f66d 76 };
ban4jp 0:685224d2f66d 77
ban4jp 0:685224d2f66d 78 struct ethip_hdr {
ban4jp 0:685224d2f66d 79 struct uip_eth_hdr ethhdr;
ban4jp 0:685224d2f66d 80 /* IP header. */
ban4jp 3:a2715e9c7737 81 uint8_t vhl,
ban4jp 0:685224d2f66d 82 tos,
ban4jp 0:685224d2f66d 83 len[2],
ban4jp 0:685224d2f66d 84 ipid[2],
ban4jp 0:685224d2f66d 85 ipoffset[2],
ban4jp 0:685224d2f66d 86 ttl,
ban4jp 0:685224d2f66d 87 proto;
ban4jp 3:a2715e9c7737 88 uint16_t ipchksum;
ban4jp 3:a2715e9c7737 89 uip_ipaddr_t srcipaddr, destipaddr;
ban4jp 0:685224d2f66d 90 };
ban4jp 0:685224d2f66d 91
ban4jp 0:685224d2f66d 92 #define ARP_REQUEST 1
ban4jp 0:685224d2f66d 93 #define ARP_REPLY 2
ban4jp 0:685224d2f66d 94
ban4jp 0:685224d2f66d 95 #define ARP_HWTYPE_ETH 1
ban4jp 0:685224d2f66d 96
ban4jp 0:685224d2f66d 97 struct arp_entry {
ban4jp 3:a2715e9c7737 98 uip_ipaddr_t ipaddr;
ban4jp 0:685224d2f66d 99 struct uip_eth_addr ethaddr;
ban4jp 3:a2715e9c7737 100 uint8_t time;
ban4jp 0:685224d2f66d 101 };
ban4jp 0:685224d2f66d 102
ban4jp 0:685224d2f66d 103 static const struct uip_eth_addr broadcast_ethaddr =
ban4jp 0:685224d2f66d 104 {{0xff,0xff,0xff,0xff,0xff,0xff}};
ban4jp 3:a2715e9c7737 105 static const uint16_t broadcast_ipaddr[2] = {0xffff,0xffff};
ban4jp 0:685224d2f66d 106
ban4jp 0:685224d2f66d 107 static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
ban4jp 3:a2715e9c7737 108 static uip_ipaddr_t ipaddr;
ban4jp 3:a2715e9c7737 109 static uint8_t i, c;
ban4jp 0:685224d2f66d 110
ban4jp 3:a2715e9c7737 111 static uint8_t arptime;
ban4jp 3:a2715e9c7737 112 static uint8_t tmpage;
ban4jp 0:685224d2f66d 113
ban4jp 0:685224d2f66d 114 #define BUF ((struct arp_hdr *)&uip_buf[0])
ban4jp 0:685224d2f66d 115 #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
ban4jp 3:a2715e9c7737 116
ban4jp 3:a2715e9c7737 117 #define DEBUG 0
ban4jp 3:a2715e9c7737 118 #if DEBUG
ban4jp 3:a2715e9c7737 119 #include <stdio.h>
ban4jp 3:a2715e9c7737 120 #define PRINTF(...) printf(__VA_ARGS__)
ban4jp 3:a2715e9c7737 121 #else
ban4jp 3:a2715e9c7737 122 #define PRINTF(...)
ban4jp 3:a2715e9c7737 123 #endif
ban4jp 3:a2715e9c7737 124
ban4jp 0:685224d2f66d 125 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 126 /**
ban4jp 0:685224d2f66d 127 * Initialize the ARP module.
ban4jp 0:685224d2f66d 128 *
ban4jp 0:685224d2f66d 129 */
ban4jp 0:685224d2f66d 130 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 131 void
ban4jp 0:685224d2f66d 132 uip_arp_init(void)
ban4jp 0:685224d2f66d 133 {
ban4jp 0:685224d2f66d 134 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
ban4jp 3:a2715e9c7737 135 memset(&arp_table[i].ipaddr, 0, 4);
ban4jp 0:685224d2f66d 136 }
ban4jp 0:685224d2f66d 137 }
ban4jp 0:685224d2f66d 138 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 139 /**
ban4jp 0:685224d2f66d 140 * Periodic ARP processing function.
ban4jp 0:685224d2f66d 141 *
ban4jp 0:685224d2f66d 142 * This function performs periodic timer processing in the ARP module
ban4jp 0:685224d2f66d 143 * and should be called at regular intervals. The recommended interval
ban4jp 0:685224d2f66d 144 * is 10 seconds between the calls.
ban4jp 0:685224d2f66d 145 *
ban4jp 0:685224d2f66d 146 */
ban4jp 0:685224d2f66d 147 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 148 void
ban4jp 0:685224d2f66d 149 uip_arp_timer(void)
ban4jp 0:685224d2f66d 150 {
ban4jp 0:685224d2f66d 151 struct arp_entry *tabptr;
ban4jp 0:685224d2f66d 152
ban4jp 0:685224d2f66d 153 ++arptime;
ban4jp 0:685224d2f66d 154 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
ban4jp 0:685224d2f66d 155 tabptr = &arp_table[i];
ban4jp 3:a2715e9c7737 156 if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) &&
ban4jp 0:685224d2f66d 157 arptime - tabptr->time >= UIP_ARP_MAXAGE) {
ban4jp 3:a2715e9c7737 158 memset(&tabptr->ipaddr, 0, 4);
ban4jp 0:685224d2f66d 159 }
ban4jp 0:685224d2f66d 160 }
ban4jp 0:685224d2f66d 161
ban4jp 0:685224d2f66d 162 }
ban4jp 3:a2715e9c7737 163
ban4jp 0:685224d2f66d 164 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 165 static void
ban4jp 3:a2715e9c7737 166 uip_arp_update(uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr)
ban4jp 0:685224d2f66d 167 {
ban4jp 3:a2715e9c7737 168 register struct arp_entry *tabptr = arp_table;
ban4jp 3:a2715e9c7737 169
ban4jp 0:685224d2f66d 170 /* Walk through the ARP mapping table and try to find an entry to
ban4jp 0:685224d2f66d 171 update. If none is found, the IP -> MAC address mapping is
ban4jp 0:685224d2f66d 172 inserted in the ARP table. */
ban4jp 0:685224d2f66d 173 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
ban4jp 3:a2715e9c7737 174 tabptr = &arp_table[i];
ban4jp 0:685224d2f66d 175
ban4jp 0:685224d2f66d 176 /* Only check those entries that are actually in use. */
ban4jp 3:a2715e9c7737 177 if(!uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) {
ban4jp 0:685224d2f66d 178
ban4jp 0:685224d2f66d 179 /* Check if the source IP address of the incoming packet matches
ban4jp 0:685224d2f66d 180 the IP address in this ARP table entry. */
ban4jp 3:a2715e9c7737 181 if(uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr)) {
ban4jp 0:685224d2f66d 182
ban4jp 0:685224d2f66d 183 /* An old entry found, update this and return. */
ban4jp 0:685224d2f66d 184 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
ban4jp 0:685224d2f66d 185 tabptr->time = arptime;
ban4jp 0:685224d2f66d 186
ban4jp 0:685224d2f66d 187 return;
ban4jp 0:685224d2f66d 188 }
ban4jp 0:685224d2f66d 189 }
ban4jp 3:a2715e9c7737 190 tabptr++;
ban4jp 0:685224d2f66d 191 }
ban4jp 0:685224d2f66d 192
ban4jp 0:685224d2f66d 193 /* If we get here, no existing ARP table entry was found, so we
ban4jp 0:685224d2f66d 194 create one. */
ban4jp 0:685224d2f66d 195
ban4jp 0:685224d2f66d 196 /* First, we try to find an unused entry in the ARP table. */
ban4jp 0:685224d2f66d 197 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
ban4jp 0:685224d2f66d 198 tabptr = &arp_table[i];
ban4jp 3:a2715e9c7737 199 if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) {
ban4jp 0:685224d2f66d 200 break;
ban4jp 0:685224d2f66d 201 }
ban4jp 0:685224d2f66d 202 }
ban4jp 0:685224d2f66d 203
ban4jp 0:685224d2f66d 204 /* If no unused entry is found, we try to find the oldest entry and
ban4jp 0:685224d2f66d 205 throw it away. */
ban4jp 0:685224d2f66d 206 if(i == UIP_ARPTAB_SIZE) {
ban4jp 0:685224d2f66d 207 tmpage = 0;
ban4jp 0:685224d2f66d 208 c = 0;
ban4jp 0:685224d2f66d 209 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
ban4jp 0:685224d2f66d 210 tabptr = &arp_table[i];
ban4jp 0:685224d2f66d 211 if(arptime - tabptr->time > tmpage) {
ban4jp 0:685224d2f66d 212 tmpage = arptime - tabptr->time;
ban4jp 0:685224d2f66d 213 c = i;
ban4jp 0:685224d2f66d 214 }
ban4jp 0:685224d2f66d 215 }
ban4jp 0:685224d2f66d 216 i = c;
ban4jp 0:685224d2f66d 217 tabptr = &arp_table[i];
ban4jp 0:685224d2f66d 218 }
ban4jp 0:685224d2f66d 219
ban4jp 0:685224d2f66d 220 /* Now, i is the ARP table entry which we will fill with the new
ban4jp 0:685224d2f66d 221 information. */
ban4jp 3:a2715e9c7737 222 uip_ipaddr_copy(&tabptr->ipaddr, ipaddr);
ban4jp 0:685224d2f66d 223 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
ban4jp 0:685224d2f66d 224 tabptr->time = arptime;
ban4jp 0:685224d2f66d 225 }
ban4jp 0:685224d2f66d 226 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 227 /**
ban4jp 0:685224d2f66d 228 * ARP processing for incoming IP packets
ban4jp 0:685224d2f66d 229 *
ban4jp 0:685224d2f66d 230 * This function should be called by the device driver when an IP
ban4jp 0:685224d2f66d 231 * packet has been received. The function will check if the address is
ban4jp 0:685224d2f66d 232 * in the ARP cache, and if so the ARP cache entry will be
ban4jp 0:685224d2f66d 233 * refreshed. If no ARP cache entry was found, a new one is created.
ban4jp 0:685224d2f66d 234 *
ban4jp 0:685224d2f66d 235 * This function expects an IP packet with a prepended Ethernet header
ban4jp 0:685224d2f66d 236 * in the uip_buf[] buffer, and the length of the packet in the global
ban4jp 0:685224d2f66d 237 * variable uip_len.
ban4jp 0:685224d2f66d 238 */
ban4jp 0:685224d2f66d 239 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 240 #if 0
ban4jp 0:685224d2f66d 241 void
ban4jp 0:685224d2f66d 242 uip_arp_ipin(void)
ban4jp 0:685224d2f66d 243 {
ban4jp 0:685224d2f66d 244 uip_len -= sizeof(struct uip_eth_hdr);
ban4jp 0:685224d2f66d 245
ban4jp 0:685224d2f66d 246 /* Only insert/update an entry if the source IP address of the
ban4jp 0:685224d2f66d 247 incoming IP packet comes from a host on the local network. */
ban4jp 0:685224d2f66d 248 if((IPBUF->srcipaddr[0] & uip_netmask[0]) !=
ban4jp 0:685224d2f66d 249 (uip_hostaddr[0] & uip_netmask[0])) {
ban4jp 0:685224d2f66d 250 return;
ban4jp 0:685224d2f66d 251 }
ban4jp 0:685224d2f66d 252 if((IPBUF->srcipaddr[1] & uip_netmask[1]) !=
ban4jp 0:685224d2f66d 253 (uip_hostaddr[1] & uip_netmask[1])) {
ban4jp 0:685224d2f66d 254 return;
ban4jp 0:685224d2f66d 255 }
ban4jp 0:685224d2f66d 256 uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
ban4jp 0:685224d2f66d 257
ban4jp 0:685224d2f66d 258 return;
ban4jp 0:685224d2f66d 259 }
ban4jp 0:685224d2f66d 260 #endif /* 0 */
ban4jp 0:685224d2f66d 261 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 262 /**
ban4jp 0:685224d2f66d 263 * ARP processing for incoming ARP packets.
ban4jp 0:685224d2f66d 264 *
ban4jp 0:685224d2f66d 265 * This function should be called by the device driver when an ARP
ban4jp 0:685224d2f66d 266 * packet has been received. The function will act differently
ban4jp 0:685224d2f66d 267 * depending on the ARP packet type: if it is a reply for a request
ban4jp 0:685224d2f66d 268 * that we previously sent out, the ARP cache will be filled in with
ban4jp 0:685224d2f66d 269 * the values from the ARP reply. If the incoming ARP packet is an ARP
ban4jp 0:685224d2f66d 270 * request for our IP address, an ARP reply packet is created and put
ban4jp 0:685224d2f66d 271 * into the uip_buf[] buffer.
ban4jp 0:685224d2f66d 272 *
ban4jp 0:685224d2f66d 273 * When the function returns, the value of the global variable uip_len
ban4jp 0:685224d2f66d 274 * indicates whether the device driver should send out a packet or
ban4jp 0:685224d2f66d 275 * not. If uip_len is zero, no packet should be sent. If uip_len is
ban4jp 0:685224d2f66d 276 * non-zero, it contains the length of the outbound packet that is
ban4jp 0:685224d2f66d 277 * present in the uip_buf[] buffer.
ban4jp 0:685224d2f66d 278 *
ban4jp 0:685224d2f66d 279 * This function expects an ARP packet with a prepended Ethernet
ban4jp 0:685224d2f66d 280 * header in the uip_buf[] buffer, and the length of the packet in the
ban4jp 0:685224d2f66d 281 * global variable uip_len.
ban4jp 0:685224d2f66d 282 */
ban4jp 0:685224d2f66d 283 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 284 void
ban4jp 0:685224d2f66d 285 uip_arp_arpin(void)
ban4jp 0:685224d2f66d 286 {
ban4jp 0:685224d2f66d 287
ban4jp 0:685224d2f66d 288 if(uip_len < sizeof(struct arp_hdr)) {
ban4jp 0:685224d2f66d 289 uip_len = 0;
ban4jp 0:685224d2f66d 290 return;
ban4jp 0:685224d2f66d 291 }
ban4jp 0:685224d2f66d 292 uip_len = 0;
ban4jp 0:685224d2f66d 293
ban4jp 0:685224d2f66d 294 switch(BUF->opcode) {
ban4jp 3:a2715e9c7737 295 case UIP_HTONS(ARP_REQUEST):
ban4jp 0:685224d2f66d 296 /* ARP request. If it asked for our address, we send out a
ban4jp 0:685224d2f66d 297 reply. */
ban4jp 3:a2715e9c7737 298 /* if(BUF->dipaddr[0] == uip_hostaddr[0] &&
ban4jp 3:a2715e9c7737 299 BUF->dipaddr[1] == uip_hostaddr[1]) {*/
ban4jp 3:a2715e9c7737 300 PRINTF("uip_arp_arpin: request for %d.%d.%d.%d (we are %d.%d.%d.%d)\n",
ban4jp 3:a2715e9c7737 301 BUF->dipaddr.u8[0], BUF->dipaddr.u8[1],
ban4jp 3:a2715e9c7737 302 BUF->dipaddr.u8[2], BUF->dipaddr.u8[3],
ban4jp 3:a2715e9c7737 303 uip_hostaddr.u8[0], uip_hostaddr.u8[1],
ban4jp 3:a2715e9c7737 304 uip_hostaddr.u8[2], uip_hostaddr.u8[3]);
ban4jp 3:a2715e9c7737 305 if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) {
ban4jp 0:685224d2f66d 306 /* First, we register the one who made the request in our ARP
ban4jp 0:685224d2f66d 307 table, since it is likely that we will do more communication
ban4jp 0:685224d2f66d 308 with this host in the future. */
ban4jp 3:a2715e9c7737 309 uip_arp_update(&BUF->sipaddr, &BUF->shwaddr);
ban4jp 0:685224d2f66d 310
ban4jp 3:a2715e9c7737 311 BUF->opcode = UIP_HTONS(ARP_REPLY);
ban4jp 0:685224d2f66d 312
ban4jp 0:685224d2f66d 313 memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
ban4jp 3:a2715e9c7737 314 memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6);
ban4jp 3:a2715e9c7737 315 memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6);
ban4jp 0:685224d2f66d 316 memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
ban4jp 0:685224d2f66d 317
ban4jp 3:a2715e9c7737 318 uip_ipaddr_copy(&BUF->dipaddr, &BUF->sipaddr);
ban4jp 3:a2715e9c7737 319 uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
ban4jp 0:685224d2f66d 320
ban4jp 3:a2715e9c7737 321 BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
ban4jp 0:685224d2f66d 322 uip_len = sizeof(struct arp_hdr);
ban4jp 0:685224d2f66d 323 }
ban4jp 0:685224d2f66d 324 break;
ban4jp 3:a2715e9c7737 325 case UIP_HTONS(ARP_REPLY):
ban4jp 0:685224d2f66d 326 /* ARP reply. We insert or update the ARP table if it was meant
ban4jp 0:685224d2f66d 327 for us. */
ban4jp 3:a2715e9c7737 328 if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) {
ban4jp 3:a2715e9c7737 329 uip_arp_update(&BUF->sipaddr, &BUF->shwaddr);
ban4jp 0:685224d2f66d 330 }
ban4jp 0:685224d2f66d 331 break;
ban4jp 0:685224d2f66d 332 }
ban4jp 0:685224d2f66d 333
ban4jp 0:685224d2f66d 334 return;
ban4jp 0:685224d2f66d 335 }
ban4jp 0:685224d2f66d 336 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 337 /**
ban4jp 0:685224d2f66d 338 * Prepend Ethernet header to an outbound IP packet and see if we need
ban4jp 0:685224d2f66d 339 * to send out an ARP request.
ban4jp 0:685224d2f66d 340 *
ban4jp 0:685224d2f66d 341 * This function should be called before sending out an IP packet. The
ban4jp 0:685224d2f66d 342 * function checks the destination IP address of the IP packet to see
ban4jp 0:685224d2f66d 343 * what Ethernet MAC address that should be used as a destination MAC
ban4jp 0:685224d2f66d 344 * address on the Ethernet.
ban4jp 0:685224d2f66d 345 *
ban4jp 0:685224d2f66d 346 * If the destination IP address is in the local network (determined
ban4jp 0:685224d2f66d 347 * by logical ANDing of netmask and our IP address), the function
ban4jp 0:685224d2f66d 348 * checks the ARP cache to see if an entry for the destination IP
ban4jp 0:685224d2f66d 349 * address is found. If so, an Ethernet header is prepended and the
ban4jp 0:685224d2f66d 350 * function returns. If no ARP cache entry is found for the
ban4jp 0:685224d2f66d 351 * destination IP address, the packet in the uip_buf[] is replaced by
ban4jp 0:685224d2f66d 352 * an ARP request packet for the IP address. The IP packet is dropped
ban4jp 0:685224d2f66d 353 * and it is assumed that they higher level protocols (e.g., TCP)
ban4jp 0:685224d2f66d 354 * eventually will retransmit the dropped packet.
ban4jp 0:685224d2f66d 355 *
ban4jp 0:685224d2f66d 356 * If the destination IP address is not on the local network, the IP
ban4jp 0:685224d2f66d 357 * address of the default router is used instead.
ban4jp 0:685224d2f66d 358 *
ban4jp 0:685224d2f66d 359 * When the function returns, a packet is present in the uip_buf[]
ban4jp 0:685224d2f66d 360 * buffer, and the length of the packet is in the global variable
ban4jp 0:685224d2f66d 361 * uip_len.
ban4jp 0:685224d2f66d 362 */
ban4jp 0:685224d2f66d 363 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 364 void
ban4jp 0:685224d2f66d 365 uip_arp_out(void)
ban4jp 0:685224d2f66d 366 {
ban4jp 3:a2715e9c7737 367 struct arp_entry *tabptr = arp_table;
ban4jp 0:685224d2f66d 368
ban4jp 0:685224d2f66d 369 /* Find the destination IP address in the ARP table and construct
ban4jp 0:685224d2f66d 370 the Ethernet header. If the destination IP addres isn't on the
ban4jp 0:685224d2f66d 371 local network, we use the default router's IP address instead.
ban4jp 0:685224d2f66d 372
ban4jp 0:685224d2f66d 373 If not ARP table entry is found, we overwrite the original IP
ban4jp 0:685224d2f66d 374 packet with an ARP request for the IP address. */
ban4jp 0:685224d2f66d 375
ban4jp 0:685224d2f66d 376 /* First check if destination is a local broadcast. */
ban4jp 3:a2715e9c7737 377 if(uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr)) {
ban4jp 0:685224d2f66d 378 memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6);
ban4jp 3:a2715e9c7737 379 } else if(IPBUF->destipaddr.u8[0] == 224) {
ban4jp 3:a2715e9c7737 380 /* Multicast. */
ban4jp 3:a2715e9c7737 381 IPBUF->ethhdr.dest.addr[0] = 0x01;
ban4jp 3:a2715e9c7737 382 IPBUF->ethhdr.dest.addr[1] = 0x00;
ban4jp 3:a2715e9c7737 383 IPBUF->ethhdr.dest.addr[2] = 0x5e;
ban4jp 3:a2715e9c7737 384 IPBUF->ethhdr.dest.addr[3] = IPBUF->destipaddr.u8[1];
ban4jp 3:a2715e9c7737 385 IPBUF->ethhdr.dest.addr[4] = IPBUF->destipaddr.u8[2];
ban4jp 3:a2715e9c7737 386 IPBUF->ethhdr.dest.addr[5] = IPBUF->destipaddr.u8[3];
ban4jp 0:685224d2f66d 387 } else {
ban4jp 0:685224d2f66d 388 /* Check if the destination address is on the local network. */
ban4jp 3:a2715e9c7737 389 if(!uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask)) {
ban4jp 0:685224d2f66d 390 /* Destination address was not on the local network, so we need to
ban4jp 0:685224d2f66d 391 use the default router's IP address instead of the destination
ban4jp 0:685224d2f66d 392 address when determining the MAC address. */
ban4jp 3:a2715e9c7737 393 uip_ipaddr_copy(&ipaddr, &uip_draddr);
ban4jp 0:685224d2f66d 394 } else {
ban4jp 0:685224d2f66d 395 /* Else, we use the destination IP address. */
ban4jp 3:a2715e9c7737 396 uip_ipaddr_copy(&ipaddr, &IPBUF->destipaddr);
ban4jp 0:685224d2f66d 397 }
ban4jp 0:685224d2f66d 398 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
ban4jp 3:a2715e9c7737 399 if(uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr)) {
ban4jp 0:685224d2f66d 400 break;
ban4jp 0:685224d2f66d 401 }
ban4jp 3:a2715e9c7737 402 tabptr++;
ban4jp 0:685224d2f66d 403 }
ban4jp 0:685224d2f66d 404
ban4jp 0:685224d2f66d 405 if(i == UIP_ARPTAB_SIZE) {
ban4jp 0:685224d2f66d 406 /* The destination address was not in our ARP table, so we
ban4jp 0:685224d2f66d 407 overwrite the IP packet with an ARP request. */
ban4jp 0:685224d2f66d 408
ban4jp 0:685224d2f66d 409 memset(BUF->ethhdr.dest.addr, 0xff, 6);
ban4jp 0:685224d2f66d 410 memset(BUF->dhwaddr.addr, 0x00, 6);
ban4jp 3:a2715e9c7737 411 memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6);
ban4jp 3:a2715e9c7737 412 memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6);
ban4jp 0:685224d2f66d 413
ban4jp 3:a2715e9c7737 414 uip_ipaddr_copy(&BUF->dipaddr, &ipaddr);
ban4jp 3:a2715e9c7737 415 uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
ban4jp 3:a2715e9c7737 416 BUF->opcode = UIP_HTONS(ARP_REQUEST); /* ARP request. */
ban4jp 3:a2715e9c7737 417 BUF->hwtype = UIP_HTONS(ARP_HWTYPE_ETH);
ban4jp 3:a2715e9c7737 418 BUF->protocol = UIP_HTONS(UIP_ETHTYPE_IP);
ban4jp 0:685224d2f66d 419 BUF->hwlen = 6;
ban4jp 0:685224d2f66d 420 BUF->protolen = 4;
ban4jp 3:a2715e9c7737 421 BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
ban4jp 0:685224d2f66d 422
ban4jp 0:685224d2f66d 423 uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
ban4jp 0:685224d2f66d 424
ban4jp 0:685224d2f66d 425 uip_len = sizeof(struct arp_hdr);
ban4jp 0:685224d2f66d 426 return;
ban4jp 0:685224d2f66d 427 }
ban4jp 0:685224d2f66d 428
ban4jp 0:685224d2f66d 429 /* Build an ethernet header. */
ban4jp 0:685224d2f66d 430 memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
ban4jp 0:685224d2f66d 431 }
ban4jp 3:a2715e9c7737 432 memcpy(IPBUF->ethhdr.src.addr, uip_lladdr.addr, 6);
ban4jp 0:685224d2f66d 433
ban4jp 3:a2715e9c7737 434 IPBUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_IP);
ban4jp 0:685224d2f66d 435
ban4jp 0:685224d2f66d 436 uip_len += sizeof(struct uip_eth_hdr);
ban4jp 0:685224d2f66d 437 }
ban4jp 0:685224d2f66d 438 /*-----------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 439
ban4jp 0:685224d2f66d 440 /** @} */
ban4jp 0:685224d2f66d 441 /** @} */
ban4jp 3:a2715e9c7737 442