source: edk_user_repository/WARP/sw_services/WARPxilnet_v3_02_a/src/xilnet_ip.c

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

Adding WARPXilnet v 3.02.a library

File size: 6.0 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   : ip.c
18// Date   : 2002, March 20.
19// Author : Sathya Thammanur
20// Company: Xilinx
21// Group  : Emerging Software Technologies
22//
23// Summary:
24// IP layer specific functions
25//
26// $Id: ip.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
36
37/*
38 * initialize xilnet ip address
39 */
40void xilnet_ip_init(unsigned char* addr, unsigned int eth_dev_num) {
41    int k = 0;
42    int j;
43    int sum = 0;
44
45    for (j = 0; j < 3; j++) {
46
47        // parse input for dotted decimal notation of ip address
48        while(addr[k] != '.') {
49            sum = sum * 10 + addr[k] - 48;
50            k++;
51        }
52
53        k++; // move over the dot
54        eth_device[eth_dev_num].node_ip_addr[j] = (unsigned char) sum;
55        sum = 0;
56    }
57
58    // read last byte of ip address
59    while (addr[k] != '\0') {
60        sum = sum * 10 + addr[k] - 48;
61        k++;
62    }
63    eth_device[eth_dev_num].node_ip_addr[3] = (unsigned char) sum;
64}
65
66
67/*
68 * ip datagram handler:
69 * checks for the source ip address and calls the corr protocol handler
70 * no checksum calc is done to detect any errors
71 */
72
73int xilnet_ip(unsigned char*buf, int len, unsigned int eth_dev_num) {
74
75    int iplen = 0;
76    struct xilnet_ip_hdr *iph = (struct xilnet_ip_hdr *) (buf+ETH_HDR_LEN);
77
78#ifdef _DEBUG_
79    xil_printf("BEGIN xilnet_ip(): \n");
80    xil_printf("  Packet IP Address: %d.%d.%d.%d \n", (buf+ETH_HDR_LEN)[IP_DADDR_BASE],   (buf+ETH_HDR_LEN)[IP_DADDR_BASE+1],
81                                                      (buf+ETH_HDR_LEN)[IP_DADDR_BASE+2], (buf+ETH_HDR_LEN)[IP_DADDR_BASE+3]);
82    xil_printf("  Device IP Address: %d.%d.%d.%d \n", eth_device[eth_dev_num].node_ip_addr[0], eth_device[eth_dev_num].node_ip_addr[1],
83                                                      eth_device[eth_dev_num].node_ip_addr[2], eth_device[eth_dev_num].node_ip_addr[3]);
84#endif
85
86    if ((buf+ETH_HDR_LEN)[IP_DADDR_BASE]    == eth_device[eth_dev_num].node_ip_addr[0] &&
87        (buf+ETH_HDR_LEN)[IP_DADDR_BASE+1]  == eth_device[eth_dev_num].node_ip_addr[1] &&
88        (buf+ETH_HDR_LEN)[IP_DADDR_BASE+2]  == eth_device[eth_dev_num].node_ip_addr[2] &&
89        ((buf+ETH_HDR_LEN)[IP_DADDR_BASE+3] == eth_device[eth_dev_num].node_ip_addr[3] || (buf+ETH_HDR_LEN)[IP_DADDR_BASE+3] == 255 )) {
90
91        // update hw addr table
92        xilnet_eth_update_hw_tbl(buf, ETH_PROTO_IP, eth_dev_num);
93
94        iplen = (unsigned short)( ((((unsigned short)buf[ETH_HDR_LEN+2]) << 8) & 0xFF00) +
95                                   (((unsigned short)buf[ETH_HDR_LEN+3]) & 0x00FF) );
96
97#ifdef _DEBUG_
98    xil_printf("  IP protocol: %d \n", iph->prot);
99#endif
100
101    // call corr protocol handler with the ip datagram
102        switch (iph->prot) {
103            case IP_PROTO_UDP:
104                return (xilnet_udp(buf+ETH_HDR_LEN, iplen, eth_dev_num));
105            break;
106            case IP_PROTO_TCP:
107                xil_printf("Error:  Xilnet does not support TCP \n");
108                return 0;
109            break;
110            case IP_PROTO_ICMP:
111                return (xilnet_icmp(buf+ETH_HDR_LEN, iplen, eth_dev_num));
112            break;
113            default:
114                xil_printf("Error:  unknown IP protocol \n");
115                return 0;
116            break;
117        }
118    }
119
120#ifdef _DEBUG_
121    xil_printf("END xilnet_ip(): \n");
122#endif
123
124    return 0;
125}
126
127
128/*
129 * xilnet_ip_header: fills in the ip header for proto
130 */
131int ip_id_cnt = 0;
132
133void xilnet_ip_header(unsigned char* buf, int len, int proto, unsigned char *peer_ip_addr, unsigned int eth_dev_num) {
134
135   struct xilnet_ip_hdr *iph = (struct xilnet_ip_hdr*) buf;
136
137#ifdef _DEBUG_
138   xil_printf("In xilnet_ip_header(): \n");
139#endif
140
141   buf[0]               = IP_VERSION << 4;
142   buf[0]              |= IP_HDR_LEN;
143   iph->tos             = IP_TOS;
144   iph->total_len       = Xil_Htons(len);
145   iph->ident           = Xil_Htons(ip_id_cnt++);
146   iph->frag_off        = Xil_Htons(IP_FRAG_OFF);
147   iph->ttl             = IP_TTL;
148   iph->prot            = proto;
149   iph->check_sum       = 0;
150   buf[IP_SADDR_BASE]   = eth_device[eth_dev_num].node_ip_addr[0];
151   buf[IP_SADDR_BASE+1] = eth_device[eth_dev_num].node_ip_addr[1];
152   buf[IP_SADDR_BASE+2] = eth_device[eth_dev_num].node_ip_addr[2];
153   buf[IP_SADDR_BASE+3] = eth_device[eth_dev_num].node_ip_addr[3];
154   buf[IP_DADDR_BASE]   = peer_ip_addr[0];
155   buf[IP_DADDR_BASE+1] = peer_ip_addr[1];
156   buf[IP_DADDR_BASE+2] = peer_ip_addr[2];
157   buf[IP_DADDR_BASE+3] = peer_ip_addr[3];
158   iph->check_sum = Xil_Htons(xilnet_ip_calc_chksum(buf, IP_HDR_LEN*4));
159
160}
161
162
163/*
164 * xilnet_ip_calc_chksum: compute checksum for ip header
165 */
166
167unsigned short xilnet_ip_calc_chksum(unsigned char* buf, int len) {
168
169   unsigned int sum = 0;
170   unsigned short w16 = 0;
171   int i;
172
173   for (i = 0; i < len; i = i + 2) {
174      w16 = ((buf[i] << 8) & 0xFF00) + (buf[i+1] & 0x00FF);
175      sum = sum + (unsigned int) w16;
176   }
177
178   while (sum >> 16)
179      sum = (sum & 0xFFFF) + (sum >> 16);
180
181   return (unsigned short) (~sum);
182}
Note: See TracBrowser for help on using the repository browser.