]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/LowLevel/RNDISEthernet/Lib/ARP.c
allow overriding of TARGET
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / LowLevel / RNDISEthernet / Lib / ARP.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12   Permission to use, copy, modify, distribute, and sell this
13   software and its documentation for any purpose is hereby granted
14   without fee, provided that the above copyright notice appear in
15   all copies and that both that the copyright notice and this
16   permission notice and warranty disclaimer appear in supporting
17   documentation, and that the name of the author not be used in
18   advertising or publicity pertaining to distribution of the
19   software without specific, written prior permission.
20
21   The author disclaims all warranties with regard to this
22   software, including all implied warranties of merchantability
23   and fitness.  In no event shall the author be liable for any
24   special, indirect or consequential damages or any damages
25   whatsoever resulting from loss of use, data or profits, whether
26   in an action of contract, negligence or other tortious action,
27   arising out of or in connection with the use or performance of
28   this software.
29 */
30
31 /** \file
32  *
33  *  Address Resolution Protocol (ARP) packet handling routines. This protocol handles the
34  *  conversion of physical MAC addresses to protocol IP addresses between the host and the
35  *  device.
36  */
37
38 #include "ARP.h"
39
40 /** Processes an ARP packet inside an Ethernet frame, and writes the appropriate response
41  *  to the output Ethernet frame if the host is requesting the IP or MAC address of the
42  *  virtual server device on the network.
43  *
44  *  \param[in] InDataStart    Pointer to the start of the incoming packet's ARP header
45  *  \param[out] OutDataStart  Pointer to the start of the outgoing packet's ARP header
46  *
47  *  \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise
48  */
49 int16_t ARP_ProcessARPPacket(void* InDataStart,
50                              void* OutDataStart)
51 {
52         DecodeARPHeader(InDataStart);
53
54         ARP_Header_t* ARPHeaderIN  = (ARP_Header_t*)InDataStart;
55         ARP_Header_t* ARPHeaderOUT = (ARP_Header_t*)OutDataStart;
56
57         /* Ensure that the ARP request is a IPv4 request packet */
58         if ((SwapEndian_16(ARPHeaderIN->ProtocolType) == ETHERTYPE_IPV4) &&
59             (SwapEndian_16(ARPHeaderIN->Operation) == ARP_OPERATION_REQUEST))
60         {
61                 /* If the ARP packet is requesting the MAC or IP of the virtual webserver, return the response */
62                 if (IP_COMPARE(&ARPHeaderIN->TPA, &ServerIPAddress) ||
63                     MAC_COMPARE(&ARPHeaderIN->THA, &ServerMACAddress))
64                 {
65                         /* Fill out the ARP response header */
66                         ARPHeaderOUT->HardwareType = ARPHeaderIN->HardwareType;
67                         ARPHeaderOUT->ProtocolType = ARPHeaderIN->ProtocolType;
68                         ARPHeaderOUT->HLEN         = ARPHeaderIN->HLEN;
69                         ARPHeaderOUT->PLEN         = ARPHeaderIN->PLEN;
70                         ARPHeaderOUT->Operation    = SwapEndian_16(ARP_OPERATION_REPLY);
71
72                         /* Copy over the sender MAC/IP to the target fields for the response */
73                         ARPHeaderOUT->THA = ARPHeaderIN->SHA;
74                         ARPHeaderOUT->TPA = ARPHeaderIN->SPA;
75
76                         /* Copy over the new sender MAC/IP - MAC and IP addresses of the virtual webserver */
77                         ARPHeaderOUT->SHA = ServerMACAddress;
78                         ARPHeaderOUT->SPA = ServerIPAddress;
79
80                         /* Return the size of the response so far */
81                         return sizeof(ARP_Header_t);
82                 }
83         }
84
85         return NO_RESPONSE;
86 }
87