?? ip.lst
字號:
C51 COMPILER V7.06 IP 07/29/2006 13:15:57 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE IP
OBJECT MODULE PLACED IN Ip.OBJ
COMPILER INVOKED BY: c:\KEIL\C51\BIN\C51.exe Ip.c DB OE
stmt level source
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2002 Jim Brady
3 // Do not use commercially without author's permission
4 // Last revised August 2002
5 // Net IP.C
6 //
7 // This module is the IP layer
8 // Refer to RFC 791, 1122, and RFC 815 (fragmentation)
9 //-----------------------------------------------------------------------------
10 #include <string.h>
11 #include "C8051f.h"
12 #include "net.h"
13 #include "cksum.h"
14 #include "arp.h"
15 #include "eth.h"
16 #include "icmp.h"
17 #include "udp.h"
18 #include "serial.h"
19 #include "tcp.h"
20 #include "ip.h"
21
22 extern UCHAR idata debug;
23 extern ULONG code my_ipaddr;
24 WAIT xdata wait;
25
26
27 //------------------------------------------------------------------------
28 // This handles outgoing IP datagrams. It adds the 20 byte IP header
29 // and checksum then forwards the IP datagram to the Ethernet layer
30 // for sending. See "TCP/IP Illustrated, Volume 1" Sect 3.2
31 //------------------------------------------------------------------------
32 void ip_send(UCHAR xdata * outbuf, ULONG ipaddr, UCHAR proto_id, UINT len)
33 {
34 1 IP_HEADER xdata * ip;
35 1 UCHAR xdata * hwaddr;
36 1 static UINT ip_ident = 0;
37 1
38 1 ip = (IP_HEADER xdata *)(outbuf + 14);
39 1 ip->ver_len = 0x45; // IPv4 with 20 byte header
40 1 ip->type_of_service = 0;
41 1 ip->total_length = 20 + len;
42 1 ip->identifier = ip_ident++; // sequential identifier
43 1 ip->fragment_info = 0; // not fragmented
44 1 ip->time_to_live = 32; // max hops
45 1 ip->protocol_id = proto_id; // type of payload
46 1 ip->header_cksum = 0;
47 1 ip->source_ipaddr = my_ipaddr;
48 1
49 1 // Outgoing IP address
50 1 ip->dest_ipaddr = ipaddr;
51 1
52 1 // Compute and insert complement of checksum of ip header
53 1 // Outgoing ip header length is always 20 bytes
54 1 ip->header_cksum = ~cksum(outbuf + 14, 20);
55 1
C51 COMPILER V7.06 IP 07/29/2006 13:15:57 PAGE 2
56 1 // Use ARP to get hardware address to send this to
57 1 hwaddr = arp_resolve(ip->dest_ipaddr);
58 1
59 1 // Null means that the ARP resolver did not find the IP address
60 1 // in its cache so had to send an ARP request
61 1 if (hwaddr == NULL)
62 1 {
63 2 // Fill in the destination information so ehrn the ARP response
64 2 // arrives we can identify it and know what to do when we get it
65 2 wait.buf = outbuf;
66 2 wait.ipaddr = ip->dest_ipaddr;
67 2 wait.proto_id = proto_id;
68 2 wait.len = len;
69 2 wait.timer = ARP_TIMEOUT;
70 2 return;
71 2 }
72 1
73 1 eth_send(outbuf, hwaddr, IP_PACKET, 20 + len);
74 1 }
75
76
77
78 //------------------------------------------------------------------------
79 // This handles incoming IP datagrams from the Ethernet layer
80 // See "TCP/IP Illustrated, Volume 1" Sect 3.2
81 //------------------------------------------------------------------------
82 void ip_rcve(UCHAR xdata * inbuf)
83 {
84 1 IP_HEADER xdata * ip;
85 1 UINT idata header_len, payload_len;
86 1
87 1 ip = (IP_HEADER xdata *)(inbuf + 14);
88 1
89 1 // Make sure it is addressed to my IP address
90 1 if (ip->dest_ipaddr != my_ipaddr) return;
91 1
92 1 // Validate checksum of ip header
93 1 header_len = 4 * (0x0F & ip->ver_len);
94 1 payload_len = ip->total_length - header_len;
95 1 if (cksum(inbuf + 14, header_len) != 0xFFFF)
96 1 {
97 2 if (debug) serial_send("IP: Error, cksum bad\r");
98 2 return;
99 2 }
100 1
101 1 // Make sure incoming message is IP version 4
102 1 if ((ip->ver_len >> 4) != 0x04)
103 1 {
104 2 if (debug) serial_send("IP: Error, not IPv4\r");
105 2 return;
106 2 }
107 1
108 1 // Make sure incoming message is not fragmented because
109 1 // we cannot handle fragmented messages
110 1 if ((ip->fragment_info & 0x3FFF) != 0)
111 1 {
112 2 if (debug) serial_send("IP: Error, fragmented msg rcvd\r");
113 2 return;
114 2 }
115 1
116 1 // At this point we have received a valid IP datagram addressed
117 1 // to me. We do not use header options, and do not forward
C51 COMPILER V7.06 IP 07/29/2006 13:15:57 PAGE 3
118 1 // messages, so in the unlikely event there are header options,
119 1 // delete them and shift the data down. The advantage is that
120 1 // layers such as UDP and TCP know where their data starts
121 1 if (header_len > 20)
122 1 {
123 2 if (debug) serial_send("IP: Rcvd header > 20 bytes\r");
124 2
125 2 // Use memmove because of overlap
126 2 memmove(inbuf + 34, inbuf + 14 + header_len, payload_len);
127 2
128 2 // Adjust info to reflect the move
129 2 header_len = 20;
130 2 ip->ver_len = 0x45;
131 2 ip->total_length = 20 + payload_len;
132 2 }
133 1
134 1
135 1 // Look at protocol ID byte and call the appropriate
136 1 // function to handle the received message. See
137 1 // "TCP/IP Illustrated, Volume 1" Sect 1.7 and RFC 791
138 1 // for values for various protocols
139 1 switch (ip->protocol_id)
140 1 {
141 2 case ICMP_TYPE:
142 2 if (debug) serial_send("IP: ICMP pkt rcvd\r");
143 2 icmp_rcve(inbuf, payload_len);
144 2 break;
145 2
146 2 case IGMP_TYPE:
147 2 // We cannot handle IGMP messages
148 2 if (debug) serial_send("IP: Error, IGMP pkt rcvd\r");
149 2 break;
150 2
151 2 case UDP_TYPE:
152 2 if (debug) serial_send("IP: UDP pkt rcvd\r");
153 2 udp_rcve(inbuf, payload_len);
154 2 break;
155 2
156 2 case TCP_TYPE:
157 2 if (debug) serial_send("IP: TCP pkt rcvd\r");
158 2 tcp_rcve(inbuf, payload_len);
159 2 break;
160 2
161 2 default:
162 2 if (debug) serial_send("IP: Unknown IP proto id rcvd\r");
163 2 break;
164 2 }
165 1 }
166
167
168
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 773 ----
CONSTANT SIZE = 222 ----
XDATA SIZE = 10 ----
PDATA SIZE = ---- ----
DATA SIZE = 2 15
IDATA SIZE = ---- 4
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILER V7.06 IP 07/29/2006 13:15:57 PAGE 4
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -