Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.

Dependents:   mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more

Library for ENC28J60 Ethernet modules.

/media/uploads/hudakz/enc28j60_module01.jpg

Ported to mbed from Norbert Truchsess's UIPEthernet library for Arduino. Thank you Norbert!

  • Full support for persistent (streaming) TCP/IP and UDP connections Client and Server each, ARP, ICMP, DHCP and DNS.
  • Works with both Mbed OS 2 and Mbed OS 5.

Usage:

  • Import the library into your project.
  • Add #include "UipEthernet.h" to main.cpp
  • Create one instance of the UipEthernet class initialized with the MAC address you'd like to use and SPI pins of the connected Mbed board.

Example programs:

Import programWebSwitch_ENC28J60

HTTP Server serving a simple webpage which enables to remotely turn a digital output on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.

Import programHTTPServer_Echo_ENC28J60

A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.

Import programTcpServer_ENC28J60

Simple TCP/IP Server using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programTcpClient_ENC28J60

Simple TCP/IP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programUdpServer_ENC28J60

Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programUdpClient_ENC28J60

Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programMQTT_Hello_ENC28J60

MQTT Client example program. Ethernet connection is via an ENC28J60 module.

Committer:
hudakz
Date:
Sat Sep 07 17:42:42 2019 +0000
Revision:
15:53715cc81c63
Parent:
8:4acb22344932
Timeout parameter added for the 'connect' function, SPI speed reduced from 20 to 10 Mb/s, debug messages fixed ...

Who changed what in which revision?

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