📄 ip.lst.svn-base
字号:
C51 COMPILER V7.06 IP 07/24/2007 16:32:51 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE IP
OBJECT MODULE PLACED IN E:\NETBRO~1\TRUNK\NETBRO~1\VW\IP.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE E:\NETBRO~1\TRUNK\NETBRO~1\VW\IP.C DB SB OE
stmt level source
1 /*
2
3 */
4
5 #include "GloblDef.h"
6 #include "TCPIPmem.h"
7 #include "IP.h"
8 #include "icmp.h"
9 #include "Netif.h"
10 #include "TCP.h"
11
12 /* Check sum calulation. data in buff, size, InSum is initial sum */
13 unsigned int CheckSum(unsigned int xdata * buff,unsigned int size,unsigned long InSum) reentrant
14 {
15 1 /* TO DO:in packet memory high part of short is in low memory. add all data in
16 1 form of 16 bits get a result of 32 bits, add high 16 bits to low 16 bits two
17 1 times. get a 16 bits result then complement it. */
18 1
19 1 unsigned long cksum = InSum;
20 1
21 1 /* sum all unsigned int except the last odd unsigned char(if size is a odd num) */
22 1 unsigned int xdata * EndBuf = buff + size/2;
23 1 while(buff < EndBuf)
24 1 {
25 2 /* net order is equeal as host order in mirochip, so no need to change */
26 2 cksum += *(buff++);
27 2 }
28 1
29 1 /**((unsigned int xdata *)CheckSumInParam) = size;
30 1 *((unsigned int xdata *)(CheckSumInParam+2)) = buff;
31 1 asmAddCheckSum();
32 1 cksum = CheckSumOutParm;
33 1 */
34 1
35 1 /* if has last odd unsigned char. use this unsigned char as the high part of 16 bits, and add. */
36 1 if((size & 0x0001) != 0)
37 1 cksum += (*buff) & 0xff00;
38 1
39 1 cksum = (cksum >> 16) + (cksum & 0xffff);
40 1 cksum += (cksum >>16);
41 1 return (unsigned int)(~cksum);
42 1 }
43
44 /* IP input process */
45 void IPInput(struct SMemHead xdata *MemHead) reentrant
46 {
47 1 struct SIPHead xdata *pIPHead;
48 1 struct SNetIf xdata *pNetIf; /* for search netif list */
49 1
50 1 pIPHead = (struct SIPHead xdata *)(MemHead->pStart);
51 1
52 1 /* check ip version */
53 1 if(IP_VERSION(pIPHead) != IP_VERSION_4)
54 1 {
55 2 MemFree(MemHead);
C51 COMPILER V7.06 IP 07/24/2007 16:32:51 PAGE 2
56 2 return;
57 2 }
58 1
59 1 /* if checksum is ok */
60 1 if(CheckSum((unsigned int xdata *)pIPHead,(unsigned int)IP_HEAD_LEN(pIPHead),0) != 0)
61 1 {
62 2 MemFree(MemHead);
63 2 return;
64 2 }
65 1
66 1 /* ip packet with options is not supported */
67 1 if(IP_HEAD_LEN(pIPHead) != IP_HEAD_MIN_LEN)
68 1 {
69 2 MemFree(MemHead);
70 2 return;
71 2 }
72 1
73 1 /* ip packet fragmented is not supported */
74 1 if((pIPHead->FragmentFlag_Offset & IP_FRAGMENT_OFFSET_MASK)!= 0)
75 1 {
76 2 MemFree(MemHead);
77 2 return;
78 2 }
79 1
80 1
81 1 /* if this packet for us. check all the netif. if a host
82 1 has tow device(tow ip). This packet may come from one device
83 1 but send for the IP of the other deviec. In this case we should
84 1 not drop or forward this packet */
85 1
86 1 /* if this packet is not for us. forward it */
87 1 if((pNetIf = NetIfFindIP(pIPHead->IPDest)) == NULL)
88 1 {
89 2 #ifdef IP_ENABLE_FORWARD /* if act as a router */
/* We should decrease the IPHead->ttl */
if(pIPHead->LifeLength != 0)
{
pIPHead->LifeLength--;
/* recaculate IP head checksum. there is a easy method
to recaculate, leave for later version improvment */
CheckSum((unsigned int xdata *)pIPHead,(unsigned int)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
108 2
109 2 MemFree(MemHead);
110 2 return;
111 2 }
112 1 else
113 1 {
114 2 /* MemHead->pStart set to point uper layer */
115 2 MemHead->pStart += sizeof(struct SIPHead);
116 2
117 2 /* pass to the uper layer */
C51 COMPILER V7.06 IP 07/24/2007 16:32:51 PAGE 3
118 2 switch(pIPHead->Protocol)
119 2 {
120 3 case IP_PROTOCOL_TCP:
121 3 //只调试网络驱动和ICMP
122 3 //TCPInput(MemHead);
123 3 break;
124 3 #if ICMP_EN
125 3 case IP_PROTOCOL_ICMP:
126 3 ICMPInput(MemHead);
127 3 break;
128 3 #endif
129 3 default:
130 3 MemFree(MemHead);
131 3 }
132 2 }
133 1 }
134
135 /* out put a ip packet,NOTE:MemHead->pStart point to IPHead.
136 IPScr IPDest Protocol TotalLen is already filled at uper layer.
137 To do so TCPCheckSum is easy to generate and pass augument to
138 IPOutput is easyer.
139 return :
140 TURE: send the packt successful. */
141 unsigned char IPOutput(struct SMemHead xdata * MemHead) reentrant
142 {
143 1 struct SNetIf xdata *pNetIf;
144 1 struct SIPHead xdata *pIPHead;
145 1 unsigned int tCheckSum;
146 1
147 1 pIPHead = (struct SIPHead xdata *)(MemHead->pStart);
148 1
149 1 /* found a rout */
150 1 if((pNetIf = NetIfFindRout(pIPHead->IPDest)) != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -