Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
363 johboh 1
/*********************************************************************
2
 *
3
 *                  ARP Module for Microchip TCP/IP Stack
4
 *
5
 *********************************************************************
6
 * FileName:        ARP.c
7
 * Dependencies:    string.h
8
 *                  stacktsk.h
9
 *                  helpers.h
10
 *                  arp.h
11
 *                  mac.h
12
 * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
13
 * Complier:        Microchip C18 v3.02 or higher
14
 *                  Microchip C30 v2.01 or higher
15
 * Company:         Microchip Technology, Inc.
16
 *
17
 * Software License Agreement
18
 *
19
 * This software is owned by Microchip Technology Inc. ("Microchip")
20
 * and is supplied to you for use exclusively as described in the
21
 * associated software agreement.  This software is protected by
22
 * software and other intellectual property laws.  Any use in
23
 * violation of the software license may subject the user to criminal
24
 * sanctions as well as civil liability.  Copyright 2006 Microchip
25
 * Technology Inc.  All rights reserved.
26
 *
27
 * This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL
28
 * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
29
 * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
30
 * INFRINGEMENT.  Microchip shall in no event be liable for special,
31
 * incidental, or consequential damages.
32
 *
33
 *
34
 * Author               Date    Comment
35
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36
 * Nilesh Rajbharti     5/1/01  Original        (Rev 1.0)
37
 * Nilesh Rajbharti     2/9/02  Cleanup
38
 * Nilesh Rajbharti     5/22/02 Rev 2.0 (See version.log for detail)
39
 ********************************************************************/
40
#include <string.h>
41
 
42
#include "..\Include\StackTsk.h"
43
#include "..\Include\Helpers.h"
44
#include "..\Include\ARP.h"
45
#include "..\Include\MAC.h"
46
 
47
// ARP Operation codes.
48
#define ARP_OPERATION_REQ       0x01u
49
#define ARP_OPERATION_RESP      0x02u
50
 
51
// ETHERNET packet type as defined by IEEE 802.3
52
#define HW_ETHERNET             (0x0001u)
53
#define ARP_IP                  (0x0800u)
54
 
55
 
56
 
57
// ARP packet
58
typedef struct _ARP_PACKET
59
{
60
    WORD        HardwareType;
61
    WORD        Protocol;
62
    BYTE        MACAddrLen;
63
    BYTE        ProtocolLen;
64
    WORD        Operation;
65
    MAC_ADDR    SenderMACAddr;
66
    IP_ADDR     SenderIPAddr;
67
    MAC_ADDR    TargetMACAddr;
68
    IP_ADDR     TargetIPAddr;
69
} ARP_PACKET;
70
 
71
// Helper function
72
static void SwapARPPacket(ARP_PACKET *p);
73
 
74
 
75
 
76
/*********************************************************************
77
 * Function:        BOOL ARPGet(NODE_INFO* remote, BYTE* opCode)
78
 *
79
 * PreCondition:    ARP packet is ready in MAC buffer.
80
 *
81
 * Input:           remote  - Remote node info
82
 *                  opCode  - Buffer to hold ARP op code.
83
 *
84
 * Output:          TRUE if a valid ARP packet was received.
85
 *                  FALSE otherwise.
86
 *
87
 * Side Effects:    None
88
 *
89
 * Overview:        None
90
 *
91
 * Note:            None
92
 ********************************************************************/
93
BOOL ARPGet(NODE_INFO *remote, BYTE *opCode)
94
{
95
    ARP_PACKET packet;
96
 
97
    MACGetArray((BYTE*)&packet, sizeof(packet));
98
 
99
    MACDiscardRx();
100
 
101
    SwapARPPacket(&packet);
102
 
103
    if ( packet.HardwareType != HW_ETHERNET     ||
104
         packet.MACAddrLen != sizeof(MAC_ADDR)  ||
105
         packet.ProtocolLen != sizeof(IP_ADDR) )
106
    {
107
         return FALSE;
108
    }
109
 
110
    if ( packet.Operation == ARP_OPERATION_RESP )
111
    {  
112
        *opCode = ARP_REPLY;
113
    }
114
    else if ( packet.Operation == ARP_OPERATION_REQ )
115
        *opCode = ARP_REQUEST;
116
    else
117
    {
118
        *opCode = ARP_UNKNOWN;
119
        return FALSE;
120
    }
121
 
122
    if(packet.TargetIPAddr.Val == AppConfig.MyIPAddr.Val)
123
    {
124
        remote->MACAddr     = packet.SenderMACAddr;
125
        remote->IPAddr      = packet.SenderIPAddr;
126
        return TRUE;
127
    }
128
    else
129
        return FALSE;
130
}
131
 
132
 
133
/*********************************************************************
134
 * Function:        BOOL ARPPut(NODE_INFO* more, BYTE opCode)
135
 *
136
 * PreCondition:    None
137
 *
138
 * Input:           remote  - Remote node info
139
 *                  opCode  - ARP op code to send
140
 *
141
 * Output:          TRUE - The ARP packet was generated properly
142
 *                  FALSE - Unable to allocate a TX buffer
143
 *
144
 * Side Effects:    None
145
 *
146
 * Overview:        None
147
 *
148
 * Note:            None
149
 ********************************************************************/
150
BOOL ARPPut(NODE_INFO *remote, BYTE opCode)
151
{
152
    ARP_PACKET packet;
153
    BUFFER MyTxBuffer = MACGetTxBuffer(TRUE);
154
 
155
    // Do not respond if there is no room to generate the ARP reply
156
    if(MyTxBuffer == INVALID_BUFFER)
157
        return FALSE;
158
 
159
    MACSetTxBuffer(MyTxBuffer, 0);
160
 
161
 
162
    packet.HardwareType             = HW_ETHERNET;
163
    packet.Protocol                 = ARP_IP;
164
    packet.MACAddrLen               = sizeof(MAC_ADDR);
165
    packet.ProtocolLen              = sizeof(IP_ADDR);
166
 
167
    if ( opCode == ARP_REQUEST )
168
    {
169
        packet.Operation            = ARP_OPERATION_REQ;
170
        packet.TargetMACAddr.v[0]   = 0xff;
171
        packet.TargetMACAddr.v[1]   = 0xff;
172
        packet.TargetMACAddr.v[2]   = 0xff;
173
        packet.TargetMACAddr.v[3]   = 0xff;
174
        packet.TargetMACAddr.v[4]   = 0xff;
175
        packet.TargetMACAddr.v[5]   = 0xff;
176
    }
177
    else
178
    {
179
        packet.Operation            = ARP_OPERATION_RESP;
180
        packet.TargetMACAddr        = remote->MACAddr;
181
    }
182
 
183
    packet.SenderMACAddr = AppConfig.MyMACAddr;
184
    packet.SenderIPAddr  = AppConfig.MyIPAddr;
185
 
186
    // Check to see if target is on same subnet, if not, find Gateway MAC.
187
    // Once we get Gateway MAC, all access to remote host will go through Gateway.
188
    if((packet.SenderIPAddr.Val ^ remote->IPAddr.Val) & AppConfig.MyMask.Val)
189
    {
190
        packet.TargetIPAddr = AppConfig.MyGateway;
191
    }
192
    else
193
        packet.TargetIPAddr = remote->IPAddr;
194
 
195
    SwapARPPacket(&packet);
196
 
197
    MACPutHeader(&packet.TargetMACAddr, MAC_ARP, sizeof(packet));
198
    MACPutArray((BYTE*)&packet, sizeof(packet));
199
    MACFlush();
200
 
201
    return TRUE;
202
}
203
 
204
 
205
/*********************************************************************
206
 * Function:        static void SwapARPPacket(ARP_PACKET* p)
207
 *
208
 * PreCondition:    None
209
 *
210
 * Input:           p   - ARP packet to be swapped.
211
 *
212
 * Output:          None
213
 *
214
 * Side Effects:    None
215
 *
216
 * Overview:        None
217
 *
218
 * Note:            None
219
 ********************************************************************/
220
static void SwapARPPacket(ARP_PACKET *p)
221
{
222
    p->HardwareType     = swaps(p->HardwareType);
223
    p->Protocol         = swaps(p->Protocol);
224
    p->Operation        = swaps(p->Operation);
225
}