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