📄 ip.lst
字号:
C51 COMPILER V7.07 IP 01/14/2009 14:46:35 PAGE 1
C51 COMPILER V7.07, COMPILATION OF MODULE IP
OBJECT MODULE PLACED IN .\IP.obj
COMPILER INVOKED BY: D:\KEIL\C51\BIN\C51.EXE ..\TCPIP\IP.c BROWSE DEBUG OBJECTEXTEND PRINT(.\IP.lst) OBJECT(.\IP.obj)
stmt level source
1 /*
2 * Copyright (c) 2003 Electric Application Laboratory of NAN KAI University
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * Author: Li Zhanglin <wzzlin@nankai.edu.cn>
28 *
29 */
30
31 #include "..\GloblDef\GloblDef.h"
32 #include "..\TCPIP\TCPIPmem.h"
33 #include "..\TCPIP\IP.h"
34 #include "..\TCPIP\icmp.h"
35 #include "..\TCPIP\Netif.h"
36 #include "..\TCPIP\TCP.h"
37
38 /* Check sum calulation. data in buff, size, InSum is initial sum */
39 WORD CheckSum(WORD DT_XDATA * buff,WORD size,DWORD InSum) REENTRANT_SIG
40 {
41 1 /* TO DO:in packet memory high part of short is in low memory. add all data in
42 1 form of 16 bits get a result of 32 bits, add high 16 bits to low 16 bits two
43 1 times. get a 16 bits result then complement it. */
44 1
45 1 DWORD cksum = InSum;
46 1
47 1 /* sum all word except the last odd byte(if size is a odd num) */
48 1 WORD DT_XDATA * EndBuf = buff + size/2;
49 1 while(buff < EndBuf)
50 1 {
51 2 /* net order is equeal as host order in mirochip, so no need to change */
52 2 cksum += *(buff++);
53 2 }
54 1
55 1 /**((WORD xdata *)CheckSumInParam) = size;
C51 COMPILER V7.07 IP 01/14/2009 14:46:35 PAGE 2
56 1 *((WORD xdata *)(CheckSumInParam+2)) = buff;
57 1 asmAddCheckSum();
58 1 cksum = CheckSumOutParm;
59 1 */
60 1
61 1 /* if has last odd byte. use this byte as the high part of 16 bits, and add. */
62 1 if((size & 0x0001) != 0)
63 1 cksum += (*buff) & 0xff00;
64 1
65 1 cksum = (cksum >> 16) + (cksum & 0xffff);
66 1 cksum += (cksum >>16);
67 1 return (WORD)(~cksum);
68 1 }
69
70 /* IP input process */
71 void IPInput(struct SMemHead DT_XDATA *MemHead) REENTRANT_MUL
72 {
73 1 struct SIPHead DT_XDATA *pIPHead;
74 1 struct SNetIf DT_XDATA *pNetIf; /* for search netif list */
75 1
76 1 pIPHead = (struct SIPHead DT_XDATA *)(MemHead->pStart);
77 1
78 1 /* check ip version */
79 1 if(IP_VERSION(pIPHead) != IP_VERSION_4)
80 1 {
81 2 MemFree(MemHead);
82 2 return;
83 2 }
84 1
85 1 /* if checksum is ok */
86 1 if(CheckSum((WORD DT_XDATA *)pIPHead,(WORD)IP_HEAD_LEN(pIPHead),0) != 0)
87 1 {
88 2 MemFree(MemHead);
89 2 return;
90 2 }
91 1
92 1 /* ip packet with options is not supported */
93 1 if(IP_HEAD_LEN(pIPHead) != IP_HEAD_MIN_LEN)
94 1 {
95 2 MemFree(MemHead);
96 2 return;
97 2 }
98 1
99 1 /* ip packet fragmented is not supported */
100 1 if((pIPHead->FragmentFlag_Offset & IP_FRAGMENT_OFFSET_MASK)!= 0)
101 1 {
102 2 MemFree(MemHead);
103 2 return;
104 2 }
105 1
106 1
107 1 /* if this packet for us. check all the netif. if a host
108 1 has tow device(tow ip). This packet may come from one device
109 1 but send for the IP of the other deviec. In this case we should
110 1 not drop or forward this packet */
111 1
112 1 /* if this packet is not for us. forward it */
113 1 if((pNetIf = NetIfFindIP(pIPHead->IPDest)) == NULL)
114 1 {
115 2 #ifdef IP_ENABLE_FORWARD /* if act as a router */
/* We should decrease the IPHead->ttl */
if(pIPHead->LifeLength != 0)
C51 COMPILER V7.07 IP 01/14/2009 14:46:35 PAGE 3
{
pIPHead->LifeLength--;
/* recaculate IP head checksum. there is a easy method
to recaculate, leave for later version improvment */
CheckSum((WORD DT_XDATA *)pIPHead,(WORD)IP_HEAD_LEN(pIPHead),0);
/* find a rout( a interface ) */
if((pNetIf = NetIfFindRout(pIPHead->IPDest)) != NULL)
{
/* forward. send it through this interface. if return FALSE, we
do not care, the soure of the packet will deel with it. */
pNetIf->output(MemHead,pNetIf,pIPHead->IPDest);
}
}
#endif
134 2
135 2 MemFree(MemHead);
136 2 return;
137 2 }
138 1 else
139 1 {
140 2 /* MemHead->pStart set to point uper layer */
141 2 MemHead->pStart += sizeof(struct SIPHead);
142 2
143 2 /* pass to the uper layer */
144 2 switch(pIPHead->Protocol)
145 2 {
146 3 case IP_PROTOCOL_TCP:
147 3 TCPInput(MemHead);
148 3 break;
149 3 #if ICMP_EN
150 3 case IP_PROTOCOL_ICMP:
151 3 ICMPInput(MemHead);
152 3 break;
153 3 #endif
154 3 default:
155 3 MemFree(MemHead);
156 3 }
157 2 }
158 1 }
159
160 /* out put a ip packet,NOTE:MemHead->pStart point to IPHead.
161 IPScr IPDest Protocol TotalLen is already filled at uper layer.
162 To do so TCPCheckSum is easy to generate and pass augument to
163 IPOutput is easyer.
164 return :
165 TURE: send the packt successful. */
166 BOOL IPOutput(struct SMemHead DT_XDATA * MemHead) REENTRANT_SIG
167 {
168 1 struct SNetIf DT_XDATA *pNetIf;
169 1 struct SIPHead DT_XDATA *pIPHead;
170 1 WORD tCheckSum;
171 1
172 1 pIPHead = (struct SIPHead DT_XDATA *)(MemHead->pStart);
173 1
174 1 /* found a rout */
175 1 if((pNetIf = NetIfFindRout(pIPHead->IPDest)) != NULL)
176 1 {
177 2 /* fill IP head */
178 2 pIPHead->CheckSum = 0;
179 2 pIPHead->FragmentFlag_Offset = 0;
C51 COMPILER V7.07 IP 01/14/2009 14:46:35 PAGE 4
180 2 pIPHead->FragmentID = 0;
181 2 pIPHead->LifeLength = IP_INITIAL_LIFE;
182 2 pIPHead->ServeType = 0;
183 2 pIPHead->Ver_HeadLen = (IP_VERSION_4 << 4) + IP_HEAD_MIN_LEN/4;
184 2
185 2 /* checksum */
186 2 tCheckSum = CheckSum((WORD DT_XDATA *)pIPHead,(WORD)IP_HEAD_LEN(pIPHead),0);
187 2 pIPHead->CheckSum = htons(tCheckSum);
188 2
189 2 /* output it */
190 2 return pNetIf->output(MemHead,pNetIf,pIPHead->IPDest);
191 2 }
192 1 else
193 1 return FALSE;
194 1 /* 'MemHead' freeing is at tcp model when it is acked */
195 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 935 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 6
IDATA SIZE = ---- ----
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 + -