📄 tcpip.c
字号:
/***********************************************************************
* MODULE: tcp_ip.c
* Description: TCPIP Min Stack
* Runtime Env: ARM7TDMI - KS32C50100
* Company:
* Change History:
* 03-28-02 Create (Yadong Wang)
***********************************************************************/
//#include "common_types.h"
#include "snds.h"
//#include "main.h"
#include "mac.h"
U8 dev_addr1[] = {0x00, 0x40, 0x95, 0x36, 0x35, 0x34};
#define EthUdpPortHwTest 0x0007
#define EthUdpPortLookBack 0x0008
//static void ARP_Process(U8 *buf, U16 size);
//static void IP_Process(U8 *buf, U16 size);
//static void IP_ICMP(U8 *buf, U16 size);
//static void WriteIP(U8 *sptr, U8 * dptr, U16 size);
static U8 TxBuf[1500], *RxBuf;
static U8 IpAddr[4] = {192,168,100,5};
static U8 IPDAddr[4]={192,168,100,45};
/***********************************************************************
* Call Back by Ethernet Rx Interrupt
***********************************************************************/
void ReceivePackets(U8 *buf, U16 size)
{
if (buf[12] == 0x08 && buf[13] == 0x06)
ARP_Process(buf, size);
else if (buf[12] == 0x08 && buf[13] == 0x00)
IP_Process(buf, size);
//else
//EthData[EthNoArpIpCnt]++;
}
/***********************************************************************
* ARP Rx Message processing
***********************************************************************/
void ARP_Process(U8 *buf, U16 size)
{
U8 error = 0, i, *sptr, *dptr;
//EthData[EthArpCnt]++;
sptr = &buf[14];
if (sptr[0] != 0 || sptr[1] != 0x01)
error = 1;
if (!error && (sptr[2] != 0x08 || sptr[3] != 0x00))
error = 1;
if (!error && sptr[4] != 0x06)
error = 1;
if (!error && sptr[5] != 0x04)
error = 1;
if (!error && (sptr[6] != 0x00 || sptr[7] != 0x01))
error = 1;
if (!error && (sptr[24]!=IpAddr[0] || sptr[25]!=IpAddr[1] ||
sptr[26]!=IpAddr[2] || sptr[27]!=IpAddr[3]))
error = 1;
if(error)
{
//EthData[EthArpErrCnt]++;
return;
}
sptr = buf;
dptr = TxBuf;
for(i=0; i<6; i++)
*dptr++ = sptr[i];
for(i=0; i<6; i++)
*dptr++ = dev_addr1[i];
*dptr++ = 0x08;
*dptr++ = 0x06;
*dptr++ = 0x00;
*dptr++ = 0x01;
*dptr++ = 0x08;
*dptr++ = 0x00;
*dptr++ = 0x06;
*dptr++ = 0x04;
*dptr++ = 0x00;
*dptr++ = 0x02;
for(i=0; i<6; i++)
*dptr++ = dev_addr1[i];
for(i=0; i<4; i++)
*dptr++ = IpAddr[i];
for(i=0; i<6; i++)
*dptr++ = sptr[i+6];
for(i=0; i<4; i++)
*dptr++ = sptr[i+28];
DrvEthWrite(TxBuf, 42);
}
/***********************************************************************
* IP Rx Message processing
***********************************************************************/
void IP_ICMP1(U8 *buf, U16 size)
{
U8 y, *ptr;
U16 x0, x, i;
ptr = &TxBuf[14+20];
// Copy the Data
ptr[0] = 0x00; // Type
ptr[1] = 0x00; // Code
ptr[2] = 0x00; // CheckSumHi
ptr[3] = 0x00; // CheckSumLo
for(i=4; i<(size-20); i++)
ptr[i] = buf[14+20+i];
// Data Checksum
for(x=0, y=0, i=0; i<(size-20); i+=2)
{
x0 = x;
x += (ptr[i]<<8) + ptr[i+1];
if (x<x0)
y++;
}
x += y;
x = 0xFFFF - x;
ptr[2] = x>>8;
ptr[3] = x;
WriteIP(buf, TxBuf, size);
}
void IP_Process(U8 *buf, U16 size)
{
U8 *ptr;
U16 Frag, ReqSize;
ptr = &buf[14];
// Check the Data Length
ReqSize = (ptr[2] << 8) | ptr[3];
if (size < ReqSize)
{
// Print("Error: IP Length\n");
return;
}
// Check the Fragment, will be support later
Frag = (ptr[6] << 8) | ptr[7];
if (Frag & 0x3FFF)
{
//EthData[EthIpFragCnt]++;
return;
}
// Check Protocol
if (ptr[9] == 0x01)
{
//EthData[EthIpIcmpCnt]++;
if(ptr[20]==0)
{
i_printf("IP %d.%d.%d.%d \n\r",ptr[12],ptr[13],ptr[14],ptr[15]);
IP_ICMP(buf, ReqSize);
}
else
IP_ICMP1(buf, ReqSize);
}
else if (ptr[9] == 0x11)
{
//EthData[EthIpUdpCnt]++;
}
else
{
//EthData[EthIpOtherCnt]++;
}
}
Delay1(int time)
{
int j,i;
for(i=0;i<time;i++)
{
j=1;
}
}
/***********************************************************************
* ICMP Rx Message processing
***********************************************************************/
void IP_ICMP(U8 *buf, U16 size)
{
U8 y, *ptr;
U16 x0, x, i;
ptr = &TxBuf[14+20];
// Copy the Data
ptr[0] = 0x08; // Type
ptr[1] = 0x00; // Code
ptr[2] = 0x00; // CheckSumHi
ptr[3] = 0x00; // CheckSumLo
for(i=4; i<(size-20); i++)
ptr[i] = buf[14+20+i];
// Data Checksum
for(x=0, y=0, i=0; i<(size-20); i+=2)
{
x0 = x;
x += (ptr[i]<<8) + ptr[i+1];
if (x<x0)
y++;
}
x += y;
x = 0xFFFF - x;
ptr[2] = x>>8;
ptr[3] = x;
WriteIP(buf, TxBuf, size);
Delay1(10000);
Delay1(10000);
Delay1(10000);
}
/***********************************************************************
* IP Tx Routines
***********************************************************************/
void WriteIP(U8 *sptr, U8 *dptr, U16 size)
{
U8 y, *dptr0;
U16 x0, x, i;
dptr0 = dptr;
for (i=0; i<6; i++)
dptr[i] = sptr[i+6];
for (i=0; i<6; i++)
dptr[6+i] = dev_addr1[i];
// Ethernet Type
dptr[12] = sptr[12];
dptr[13] = sptr[13];
// IP version, header length, TOS
dptr[14] = sptr[14];
dptr[15] = sptr[15];
// IP length
dptr[16] = size >> 8;
dptr[17] = size & 0xFF;
// Datagram ID
dptr[18] = sptr[18];
dptr[19] = sptr[19];
// Fragment Offset
dptr[20] = 0x00; //sptr[i];
dptr[21] = 0x00; //sptr[i];
dptr[22] = 128;
dptr[23] = sptr[23];
dptr[14+10] = 0x00;
dptr[14+11] = 0x00;
for (i=0; i<4; i++)
dptr[14+16+i] =sptr[14+12+i];
for (i=0; i<4; i++)
dptr[14+12+i] = IpAddr[i];
x=0;
y=0;
for(i=0; i<20; i+=2)
{
x0 = x;
x += (dptr[14+i]<<8) + dptr[14+i+1];
if (x < x0)
y++;
}
x +=y;
x = 0xFFFF - x;
dptr[14+10] = x >> 8;
dptr[14+11] = x;
if (DrvEthWrite(dptr0, size + 14) == 0)
i_printf("Error: Send Packet\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -