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

Last change on this file was 1929, checked in by murphpo, 11 years ago

Adding updated WARPxilnet, required for WARPLab 7 ref design

File size: 4.6 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) {
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      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   node_ip_addr[3] = (unsigned char) sum;
64
65}
66
67
68/*
69 * ip datagram handler:
70 * checks for the source ip address and calls the corr protocol handler
71 * no checksum calc is done to detect any errors
72 */
73
74int xilnet_ip(unsigned char*buf, int len) {
75
76  int iplen = 0;
77  struct xilnet_ip_hdr *iph = (struct xilnet_ip_hdr *) (buf+ETH_HDR_LEN);
78#ifdef _DEBUG_
79  //print("In ip(): \n");
80#endif
81
82  if ((buf+ETH_HDR_LEN)[IP_DADDR_BASE] == node_ip_addr[0] && (buf+ETH_HDR_LEN)[IP_DADDR_BASE+1] == node_ip_addr[1] &&
83      (buf+ETH_HDR_LEN)[IP_DADDR_BASE+2] == node_ip_addr[2] && ((buf+ETH_HDR_LEN)[IP_DADDR_BASE+3] == node_ip_addr[3] || (buf+ETH_HDR_LEN)[IP_DADDR_BASE+3] == 255 )) {
84    // update hw addr table
85    xilnet_eth_update_hw_tbl(buf, ETH_PROTO_IP);
86
87    iplen = (unsigned short)( ((((unsigned short)buf[ETH_HDR_LEN+2]) << 8) & 0xFF00) +
88                                (((unsigned short)buf[ETH_HDR_LEN+3]) & 0x00FF) );
89
90
91    // call corr protocol handler with the ip datagram
92    switch (iph->prot) {
93      case IP_PROTO_UDP:
94    return (xilnet_udp(buf+ETH_HDR_LEN, iplen));
95    break;
96      case IP_PROTO_TCP:
97    return (xilnet_tcp(buf+ETH_HDR_LEN, iplen));
98    break;
99      case IP_PROTO_ICMP:
100    return (xilnet_icmp(buf+ETH_HDR_LEN, iplen));
101    break;
102      default:
103    break;
104      }
105  }
106
107  return 0;
108}
109
110
111/*
112 * xilnet_ip_header: fills in the ip header for proto
113 */
114int ip_id_cnt = 0;
115
116void xilnet_ip_header(unsigned char* buf, int len, int proto, unsigned char *peer_ip_addr) {
117
118   struct xilnet_ip_hdr *iph = (struct xilnet_ip_hdr*) buf;
119
120
121#ifdef _DEBUG_
122   //  print("ip_header(): \n");
123#endif /*  _DEBUG_ */
124
125   buf[0] = IP_VERSION << 4;
126   buf[0] |= IP_HDR_LEN;
127   iph->tos = IP_TOS;
128   iph->total_len = Xil_Htons(len);
129   iph->ident = Xil_Htons(ip_id_cnt++);
130   iph->frag_off = Xil_Htons(IP_FRAG_OFF);
131   iph->ttl = IP_TTL;
132   iph->prot = proto;
133   iph->check_sum = 0;
134   buf[IP_SADDR_BASE]  = node_ip_addr[0];
135   buf[IP_SADDR_BASE+1] = node_ip_addr[1];
136   buf[IP_SADDR_BASE+2] = node_ip_addr[2];
137   buf[IP_SADDR_BASE+3] = node_ip_addr[3];
138   buf[IP_DADDR_BASE]  = peer_ip_addr[0];
139   buf[IP_DADDR_BASE+1] = peer_ip_addr[1];
140   buf[IP_DADDR_BASE+2] = peer_ip_addr[2];
141   buf[IP_DADDR_BASE+3] = peer_ip_addr[3];
142   iph->check_sum = Xil_Htons(xilnet_ip_calc_chksum(buf, IP_HDR_LEN*4));
143
144}
145
146
147/*
148 * xilnet_ip_calc_chksum: compute checksum for ip header
149 */
150
151unsigned short xilnet_ip_calc_chksum(unsigned char* buf, int len) {
152
153   unsigned int sum = 0;
154   unsigned short w16 = 0;
155   int i;
156
157   for (i = 0; i < len; i = i + 2) {
158      w16 = ((buf[i] << 8) & 0xFF00) + (buf[i+1] & 0x00FF);
159      sum = sum + (unsigned int) w16;
160   }
161
162   while (sum >> 16)
163      sum = (sum & 0xFFFF) + (sum >> 16);
164
165   return (unsigned short) (~sum);
166}
Note: See TracBrowser for help on using the repository browser.