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

Last change on this file was 1749, checked in by chunter, 12 years ago

Updated copyright information and improved handling of broadcast IP address

File size: 4.8 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      mb_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   mb_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] == mb_ip_addr[0] && (buf+ETH_HDR_LEN)[IP_DADDR_BASE+1] == mb_ip_addr[1] &&
83      (buf+ETH_HDR_LEN)[IP_DADDR_BASE+2] == mb_ip_addr[2] && (buf+ETH_HDR_LEN)[IP_DADDR_BASE+3] == mb_ip_addr[3]) {
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  else if ((buf+ETH_HDR_LEN)[IP_DADDR_BASE] == mb_ip_addr[0] && (buf+ETH_HDR_LEN)[IP_DADDR_BASE+1] == mb_ip_addr[1] &&
107      (buf+ETH_HDR_LEN)[IP_DADDR_BASE+2] == mb_ip_addr[2] && (buf+ETH_HDR_LEN)[IP_DADDR_BASE+3] == sync_IP_octet)
108  {
109    return 9999;
110}
111
112  return 0;
113}
114
115
116/*
117 * xilnet_ip_header: fills in the ip header for proto
118 */
119int ip_id_cnt = 0;
120
121void xilnet_ip_header(unsigned char* buf, int len, int proto, unsigned char *peer_ip_addr) {
122
123   struct xilnet_ip_hdr *iph = (struct xilnet_ip_hdr*) buf;
124   unsigned short temp;
125
126#ifdef _DEBUG_
127   //  print("ip_header(): \n");
128#endif /*  _DEBUG_ */
129
130   buf[0] = IP_VERSION << 4;
131   buf[0] |= IP_HDR_LEN;
132   iph->tos = IP_TOS;
133   iph->total_len = len;
134   iph->ident = ip_id_cnt++;
135   iph->frag_off = IP_FRAG_OFF;
136   iph->ttl = IP_TTL;
137   iph->prot = proto;
138   iph->check_sum = 0;
139   buf[IP_SADDR_BASE]  = mb_ip_addr[0];
140   buf[IP_SADDR_BASE+1] = mb_ip_addr[1];
141   buf[IP_SADDR_BASE+2] = mb_ip_addr[2];
142   buf[IP_SADDR_BASE+3] = mb_ip_addr[3];
143   buf[IP_DADDR_BASE]  = peer_ip_addr[0];
144   buf[IP_DADDR_BASE+1] = peer_ip_addr[1];
145   buf[IP_DADDR_BASE+2] = peer_ip_addr[2];
146   buf[IP_DADDR_BASE+3] = peer_ip_addr[3];
147   iph->check_sum = xilnet_ip_calc_chksum(buf, IP_HDR_LEN*4);
148
149}
150
151
152/*
153 * xilnet_ip_calc_chksum: compute checksum for ip header
154 */
155
156unsigned short xilnet_ip_calc_chksum(unsigned char* buf, int len) {
157
158   unsigned int sum = 0;
159   unsigned short w16 = 0;
160   int i;
161
162   for (i = 0; i < len; i = i + 2) {
163      w16 = ((buf[i] << 8) & 0xFF00) + (buf[i+1] & 0x00FF);
164      sum = sum + (unsigned int) w16;
165   }
166
167   while (sum >> 16)
168      sum = (sum & 0xFFFF) + (sum >> 16);
169
170   return (unsigned short) (~sum);
171}
Note: See TracBrowser for help on using the repository browser.