source: edk_user_repository/WARP/sw_services/WARPxilnet_v3_03_a/src/xilnet_arp.c

Last change on this file was 2229, checked in by welsh, 11 years ago

Added WARPxilnet version 3.03.a

File size: 5.5 KB
Line 
1////////////////////////////////////////////////////////////////////////////////
2// Copyright (c) 2004 Xilinx, Inc.  All rights reserved.
3//
4// Xilinx, Inc.
5// XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
6// COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
7// ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
8// STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
9// IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
10// FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
11// XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
12// THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
13// ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
14// FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS FOR A PARTICULAR PURPOSE.
16//
17// File   : arp.c
18// Date   : 2002, March 20.
19// Author : Sathya Thammanur
20// Company: Xilinx
21// Group  : Emerging Software Technologies
22//
23// Summary:
24// ARP layer specific functions
25//
26// $Id: arp.c,v 1.2.8.6 2005/11/15 23:41:10 salindac Exp $
27//
28////////////////////////////////////////////////////////////////////////////////
29
30////////////////////////////////////////////////////////////////////////////////
31// see copyright.txt for Rice University/Mango Communications modifications
32////////////////////////////////////////////////////////////////////////////////
33
34#include <xilnet_xilsock.h>
35#include <string.h>
36
37/*
38 * xilnet_arp: handler for arp packets
39 */
40
41int xilnet_arp(unsigned char *buf, int len, unsigned int eth_dev_num) {
42
43    struct xilnet_arp_pkt* ar = (struct xilnet_arp_pkt*) (buf+ETH_HDR_LEN);
44 
45#ifdef _DEBUG_
46    xil_printf("BEGIN xilnet_arp(): \n");
47#endif
48
49    // Analyse if its a arp request and send a reply if needed
50    if (
51        Xil_Ntohs(ar->hdr.hard_type) == ARP_HARD_TYPE_ETH &&
52        Xil_Ntohs(ar->hdr.prot_type) == ETH_PROTO_IP &&
53        ar->hdr.hard_size            == ARP_HARD_SIZE &&
54        ar->hdr.prot_size            == ARP_PROTO_IP_SIZE &&
55        ar->target_ip[0]             == eth_device[eth_dev_num].node_ip_addr[0] &&
56        ar->target_ip[1]             == eth_device[eth_dev_num].node_ip_addr[1] &&
57        ar->target_ip[2]             == eth_device[eth_dev_num].node_ip_addr[2] &&
58        ar->target_ip[3]             == eth_device[eth_dev_num].node_ip_addr[3]
59        ) {
60        if (Xil_Ntohs(ar->hdr.op) == ARP_REQ) {
61            // update hw addr table
62            xilnet_eth_update_hw_tbl(buf, ETH_PROTO_ARP, eth_dev_num);
63            xilnet_arp_reply( (buf + ETH_HDR_LEN), (len - ETH_HDR_LEN), eth_dev_num);
64        }
65    }
66
67#ifdef _DEBUG_
68    xil_printf("END xilnet_arp(): \n");
69#endif
70
71    return 0;
72}
73
74
75/*
76 * xilnet_arp_reply:
77 */
78
79void xilnet_arp_reply(unsigned char *buf, int len, unsigned int eth_dev_num) {
80
81    int i;
82    struct xilnet_arp_pkt *pkt;
83    struct xilnet_arp_pkt *arp_req = (struct xilnet_arp_pkt*) buf;
84
85#ifdef _DEBUG_ 
86    xil_printf("BEGIN arp_reply(): \n");
87#endif
88
89    pkt = (struct xilnet_arp_pkt*)( ((unsigned char *)eth_device[eth_dev_num].sendbuf) + ETH_HDR_LEN);
90 
91    pkt->hdr.hard_type = Xil_Htons(ARP_HARD_TYPE_ETH);
92    pkt->hdr.prot_type = Xil_Htons(ETH_PROTO_IP);
93    pkt->hdr.hard_size = ARP_HARD_SIZE;
94    pkt->hdr.prot_size = ARP_PROTO_IP_SIZE;
95    pkt->hdr.op        = Xil_Htons(ARP_REPLY);
96 
97    for (i = 0; i < ETH_ADDR_LEN; i++) {
98        pkt->sender_hw[i] = eth_device[eth_dev_num].node_hw_addr[i];
99        pkt->target_hw[i] = arp_req->sender_hw[i];
100    }
101 
102    for (i = 0; i < ARP_PROTO_IP_SIZE ; i++) {
103        pkt->sender_ip[i] = eth_device[eth_dev_num].node_ip_addr[i];
104        pkt->target_ip[i] = arp_req->sender_ip[i];
105    }
106    memset(pkt->pad, 0, ARP_PAD_SIZE);
107
108#ifdef _DEBUG_
109    xil_printf("  ARP packet: \n");
110    print_pkt((unsigned char *)eth_device[eth_dev_num].sendbuf, ETH_MIN_FRAME_LEN);
111#endif
112
113    /* initialize ethernet header */
114    xilnet_eth_send_frame((unsigned char *)eth_device[eth_dev_num].sendbuf,
115                          ETH_MIN_FRAME_LEN, arp_req->sender_ip, pkt->target_hw, ETH_PROTO_ARP, eth_dev_num);
116
117#ifdef _DEBUG_
118    xil_printf("BEGIN arp_reply(): \n");
119#endif
120}
121
122
123/*
124 * xilnet_arp_reply:
125 */
126
127// Currently ARP Announce is not used by the WARPNet framework.  If needed, please uncomment this code.
128#if 0
129 
130void xilnet_arp_announce(unsigned int eth_dev_num) {
131
132    int i;
133    struct xilnet_arp_pkt *pkt;
134    unsigned char bcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
135
136#ifdef _DEBUG_
137    xil_printf("BEGIN xilnet_arp_announce(): \n");
138#endif
139
140    pkt = (struct xilnet_arp_pkt*)(((unsigned char *)eth_device[eth_dev_num].sendbuf) + ETH_HDR_LEN);
141 
142    pkt->hdr.hard_type = ARP_HARD_TYPE_ETH;
143    pkt->hdr.prot_type = ETH_PROTO_IP;
144    pkt->hdr.hard_size = ARP_HARD_SIZE;
145    pkt->hdr.prot_size = ARP_PROTO_IP_SIZE;
146    pkt->hdr.op        = ARP_REPLY;
147 
148    for (i = 0; i < ETH_ADDR_LEN; i++) {
149      pkt->sender_hw[i] = eth_device[eth_dev_num].node_hw_addr[i];
150      pkt->target_hw[i] = 0xFF;
151    }
152 
153    for (i = 0; i < ARP_PROTO_IP_SIZE ; i++) {
154      pkt->sender_ip[i] = eth_device[eth_dev_num].node_ip_addr[i];
155      pkt->target_ip[i] = 0xFF;
156    }
157    memset(pkt->pad, 0, ARP_PAD_SIZE);
158 
159#ifdef _DEBUG_
160    xil_printf("xilnet_arp_announce: Sending arp announce\r\n");
161#endif
162
163    /* initialize ethernet header */
164    xilnet_eth_send_frame((unsigned char *)eth_device[eth_dev_num].sendbuf,
165                          ETH_MIN_FRAME_LEN, NULL, (void *)&bcast_addr, ETH_PROTO_ARP, eth_dev_num);
166
167#ifdef _DEBUG_
168    xil_printf("END xilnet_arp_announce(): \n");
169#endif
170}
171
172
173#endif
Note: See TracBrowser for help on using the repository browser.