转自:http://blog.sina.com.cn/s/blog_7d01310d0100sbrc.html
1.IP.C
#define IP_GLOBALS
#include "Config.h"
#include "../include/cfg_net.h"
OS_EVENT *RecIcmpQFlag;
OS_EVENT *RecTcpQFlag;
OS_EVENT *RecUdpQFlag;
OS_EVENT *SendFlag;
Rec_Ptr RECQ[MAX_REC_BUFF];
void *RecIcmpQ[Q_Max_Size];
void *RecUdpQ[Q_Max_Size];
void *RecTcpQ[Q_Max_Size];
uint16 CreateIpHeadCrc(uint8 * Ip)
{
union w CrcTemp;
uint8 i;
CrcTemp.dwords = 0;
for(i=0;i<10;i++)
{
CrcTemp.dwords = CrcTemp.dwords + ((uint32)Ip[2*i]<<8)+(uint32)Ip[2*i+1];
}
while(CrcTemp.words.high>0)
{
CrcTemp.dwords = (uint32)(CrcTemp.words.high + CrcTemp.words.low);
}
CrcTemp.words.low = 0xffff - CrcTemp.words.low; //取反
return(CrcTemp.words.low);
}
uint8 Send_Ip_Frame (
struct _pkst *TxdData,
uint8 * de_ip,
uint8 * so_ip,
uint8 PROTOCOL
)
{
union ip_rc IpHead;
uint8 IpHeadUint8[20];
struct _pkst TxdIpData;
uint8 err,num;
static uint16 FrameIndex=0;
for(num=0;num<MAX_NET_PORT;num++)
{
if(NetPort[num].My_Ip[0]==so_ip[0])
if(NetPort[num].My_Ip[1]==so_ip[1])
if(NetPort[num].My_Ip[2]==so_ip[2])
if(NetPort[num].My_Ip[3]==so_ip[3])
break;
}
if(num>=MAX_NET_PORT)
return (0xff);
IpHead.e_ip.VerandIphLen=0x45; //IP版本和头长度
IpHeadUint8[0]=0x45;
IpHead.e_ip.ServerType=0x00; //服务类型为0
IpHeadUint8[1]=0x00;//服务类型
IpHead.e_ip.TotalLen=(*TxdData).length+20; //IP数据报总长度
IpHeadUint8[2]=(IpHead.e_ip.TotalLen&0xff00)>>8;//IP数据报总长度高字节
IpHeadUint8[3]=IpHead.e_ip.TotalLen&0x00ff;//IP数据报总长度低字节
IpHead.e_ip.FrameIndex=FrameIndex+1;
IpHeadUint8[4]=(FrameIndex&0xff00)>>8;//IP数据报标识高字节
IpHeadUint8[5]=FrameIndex&0x00ff;//IP数据报标识低字节
FrameIndex++;
IpHeadUint8[6]=0x40;//IP数据报标志及分段偏移量
IpHeadUint8[7]=0x00;//IP数据报标志及分段偏移量
IpHead.e_ip.ttl=0x80; //128; //TTL
IpHeadUint8[8]=0x80;//ttl
IpHead.e_ip.NextProtocal=PROTOCOL; //下层协议
IpHeadUint8[9]=PROTOCOL;
IpHead.e_ip.Crc=0;
IpHeadUint8[10]=0;//CRC h
IpHeadUint8[11]=0;//CRC l
IpHead.e_ip.SourceIp[0]=NetPort[num].My_Ip[0]; //填充源IP地址
IpHead.e_ip.SourceIp[1]=NetPort[num].My_Ip[1];
IpHead.e_ip.SourceIp[2]=NetPort[num].My_Ip[2];
IpHead.e_ip.SourceIp[3]=NetPort[num].My_Ip[3];
IpHeadUint8[12]=NetPort[num].My_Ip[0]; //填充源IP地址
IpHeadUint8[13]=NetPort[num].My_Ip[1];
IpHeadUint8[14]=NetPort[num].My_Ip[2];
IpHeadUint8[15]=NetPort[num].My_Ip[3];
IpHead.e_ip.DestId[0]=de_ip[0]; //填充目的IP地址
IpHead.e_ip.DestId[1]=de_ip[1];
IpHead.e_ip.DestId[2]=de_ip[2];
IpHead.e_ip.DestId[3]=de_ip[3];
IpHeadUint8[16]=de_ip[0]; //填充目的IP地址
IpHeadUint8[17]=de_ip[1];
IpHeadUint8[18]=de_ip[2];
IpHeadUint8[19]=de_ip[3];
IpHead.e_ip.Crc=CreateIpHeadCrc(IpHeadUint8); //产生IP头的检验和
IpHeadUint8[10]=(IpHead.e_ip.Crc&0xff00)>>8;//IP数据报总长度高字节
IpHeadUint8[11]=IpHead.e_ip.Crc&0x00ff;//IP数据报总长度低字节
TxdIpData.STPTR=TxdData;
TxdIpData.length=20;
TxdIpData.DAPTR=IpHeadUint8;
OSSemPend(SendFlag,10,&err);//获取发送的权力
if(err==OS_NO_ERR)
{
if(Send_Ip_To_LLC(&TxdIpData,IpHead.e_ip.DestId,num))
{
OSSemPost(SendFlag);
return(1);
}
else
{
OSSemPost(SendFlag);
return(0);
}
}
else
return (0);
}
uint8 IP_PROCESS(uint8 * RecData,uint8 num)
{
static temp=0;
uint8 iii;
uint16 PackedLength;
uint8 TempIP[4];
#ifdef Little_End
uint16 Ltemp;
#endif
TempIP[0]=((eip*)RecData)->SourceIp[0];
TempIP[1]=((eip*)RecData)->SourceIp[1];
TempIP[2]=((eip*)RecData)->SourceIp[2];
TempIP[3]=((eip*)RecData)->SourceIp[3];//保存对方IP
iii=((eip*)RecData)->VerandIphLen&0x0f;
iii=iii*4;//取IP包头的长度
#ifdef Big_End
PackedLength=((eip*)RecData)->TotalLen;
#endif
#ifdef Little_End
PackedLength=((eip*)RecData)->TotalLen;
Ltemp=PackedLength&0x00ff;
PackedLength=(PackedLength&0xff00)>>8;
PackedLength=PackedLength+(Ltemp<<8);
#endif
PackedLength=PackedLength-(uint16)iii;//计算有效数据的长度
temp++;
if(temp>=MAX_REC_BUFF)
temp=1;
switch(((eip*)RecData)->NextProtocal)
{
case 1:
icmp_process(RecData,num);
break;
case 6:
if(((eip*)RecData)->DestId[0]==NetPort[num].My_Ip[0])
if(((eip*)RecData)->DestId[1]==NetPort[num].My_Ip[1])
if(((eip*)RecData)->DestId[2]==NetPort[num].My_Ip[2])
if(((eip*)RecData)->DestId[3]==NetPort[num].My_Ip[3])//目标为本IP
{
RecData=RecData+iii;
RECQ[temp].RecDataPtr=RecData;
RECQ[temp].length=PackedLength; //要传输ip地址
RECQ[temp].ip[0]=TempIP[0];
RECQ[temp].ip[1]=TempIP[1];
RECQ[temp].ip[2]=TempIP[2];
RECQ[temp].ip[3]=TempIP[3];
RECQ[temp].num=num;
RECQ[temp].My_Ip[0]=NetPort[num].My_Ip[0];
RECQ[temp].My_Ip[1]=NetPort[num].My_Ip[1];
RECQ[temp].My_Ip[2]=NetPort[num].My_Ip[2];
RECQ[temp].My_Ip[3]=NetPort[num].My_Ip[3];
iii=OSQPost(RecTcpQFlag,(void *)&RECQ[temp]);
if(iii) iii++;
}
//TCP
break;
case 17:
//要传输ip地址
if(((eip*)RecData)->DestId[0]==NetPort[num].My_Ip[0])
if(((eip*)RecData)->DestId[1]==NetPort[num].My_Ip[1])
if(((eip*)RecData)->DestId[2]==NetPort[num].My_Ip[2])
if(((eip*)RecData)->DestId[3]==NetPort[num].My_Ip[3])//目标为本IP
{
RecData=RecData+iii;
RECQ[temp].RecDataPtr=RecData;
RECQ[temp].length=PackedLength;
RECQ[temp].ip[0]=TempIP[0];
RECQ[temp].ip[1]=TempIP[1];
RECQ[temp].ip[2]=TempIP[2];
RECQ[temp].ip[3]=TempIP[3];
RECQ[temp].num=num;
RECQ[temp].My_Ip[0]=NetPort[num].My_Ip[0];
RECQ[temp].My_Ip[1]=NetPort[num].My_Ip[1];
RECQ[temp].My_Ip[2]=NetPort[num].My_Ip[2];
RECQ[temp].My_Ip[3]=NetPort[num].My_Ip[3];
OSQPost(RecUdpQFlag,(void *)&RECQ[temp]);
}
//UDP
break;
default :
break;
}// end for switch
return 1;
}
2.TCP.C
#define TCP_GLOBALS
#include "config.h"
#include "../include/cfg_net.h"
OS_EVENT *AppSemSend[MAX_TCP_LINKS];
OS_EVENT *AppSemCon[MAX_TCP_LINKS];
OS_EVENT *AppSemDisc[MAX_TCP_LINKS];
struct Socket_Type TcpStatus[MAX_TCP_LINKS];
tcp_send Tcp_Packed;
//=================================================================================
uint16 CheckSumTcp1(uint8 num,uint16 length)
{
uint32 sum=0;
uint16 i;
for (i=0;i<6;i++)
{
sum = sum + ((uint32)TcpStatus[num].TcpDHeadUint8[2*i]<<8)+(uint32)TcpStatus[num].TcpDHeadUint8[2*i+1];
}
length=length-12;
i=0;
while(i<(length/2))
{
sum = sum + ((uint32)TcpStatus[num].TcpHeadUint8[2*i]<<8)+(uint32)TcpStatus[num].TcpHeadUint8[2*i+1];
i++;
}
if(length&0x0001)//长度为奇数个时,要进行该操作
{
//sum = sum + (uint32)(TcpStatus[num].TcpHeadUint8[2*i])<<8;
sum = sum + (uint32)(TcpStatus[num].TcpHeadUint8[2*i])*256;
}
sum = (sum&0x0000ffff) + ((sum>>16)&0x0000ffff);//高16位和低16位相加
if(sum & 0xffff0000)
{//表示有进位
sum++;
if(sum & 0xffff0000)
{//表示有进位
sum++;
}
}
return ( (uint16)(~((sum)&0xffff)) );
}
void Tcp_Initial(void)
{
uint8 i;
for(i=0;i<MAX_TCP_LINKS;i++)
{
TcpStatus[i].Dest_Port=0;
TcpStatus[i].My_Port=0;
TcpStatus[i].State=TCP_STATE_CLOSED;
TcpStatus[i].StaSem=0;
TcpStatus[i].Send_Next=0;
TcpStatus[i].ResendTime=0;
TcpStatus[i].ResendState=0;
TcpStatus[i].RecPassSeq=0;
TcpStatus[i].SenPassSeq=0;
TcpStatus[i].SenFutureSeq=0;
TcpStatus[i].RecPassAck=0;
TcpStatus[i].SenPassAck=0;
TcpStatus[i].RecFutureAck=0;
TcpStatus[i].Snd_Window=MAX_TCP_DATA;
TcpStatus[i].TcpDataQWrPtr=0;
TcpStatus[i].TcpDataQRdPtr=0;
AppSemSend[i]= OSSemCreate(0);
AppSemCon[i]= OSSemCreate(0);
AppSemDisc[i]= OSSemCreate(0);
}
Tcp_Packed.TcpDHead.rev=0;
Tcp_Packed.TcpDHead.NextProtocal=6;
}
void Send_Reset(uint8 num)
{
uint16 i;
struct _pkst TxdData;
i=Tcp_Packed.Tcp.SourcePort;
Tcp_Packed.Tcp.SourcePort=Tcp_Packed.Tcp.DestPort;
Tcp_Packed.Tcp.DestPort=i;
Tcp_Packed.Tcp.offset=0x50;
Tcp_Packed.Tcp.window=TcpStatus[num].Snd_Window;
Tcp_Packed.Tcp.urg=0;
Tcp_Packed.Tcp.Crc=0;
//
Tcp_Packed.TcpDHead.rev=0;
Tcp_Packed.TcpDHead.NextProtocal=6;
Tcp_Packed.TcpDHead.TotalLen=20;
//Tcp_Packed.Tcp.Crc=CheckSumTcp((uint16 EX_RAM *)&Tcp_Packed,Tcp_Packed.TcpDHead.TotalLen+12);
TxdData.STPTR=NULL;
TxdData.length=Tcp_Packed.TcpDHead.TotalLen;
TxdData.DAPTR=(uint8 EX_RAM *)&Tcp_Packed.Tcp;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
//memcpy (&(TcpStatus[num].resend_data),&(Tcp_Packed.Tcp) , Tcp_Packed.TcpDHead.TotalLen);
TcpStatus[num].ResendLength=Tcp_Packed.TcpDHead.TotalLen;
}
void Delete_Socket(uint8 num)
{
uint16 i;
OS_ENTER_CRITICAL();
if((TcpStatus[num].StaSem&0x80)==0x80)
{
TcpStatus[num].State=TCP_STATE_LISTEN;
TcpStatus[num].StaSem=0x82;
}
else
{
TcpStatus[num].State=TCP_STATE_CLOSED;
TcpStatus[num].StaSem=0;
TcpStatus[num].My_Port=0;
TcpStatus[num].My_Ip[0]=0;
TcpStatus[num].My_Ip[1]=0;
TcpStatus[num].My_Ip[2]=0;
TcpStatus[num].My_Ip[3]=0;
}
do
{
i=OSSemAccept(AppSemCon[num]);
}while(i!=0);
do
{
i=OSSemAccept(AppSemSend[num]);
}while(i!=0);
do
{
i=OSSemAccept(AppSemDisc[num]);
}while(i!=0);
TcpStatus[num].RecPassSeq=0;
TcpStatus[num].RecPassAck=0;
TcpStatus[num].SenPassSeq=0;
TcpStatus[num].SenPassAck=0;
TcpStatus[num].RecFutureAck=0;
TcpStatus[num].SenFutureSeq=0;
TcpStatus[num].ResendState=0;//表示该重发缓冲区没有数据
TcpStatus[num].ResendTime=0;
TcpStatus[num].Dest_Port=0;
TcpStatus[num].Dest_Ip[0]=0;
TcpStatus[num].Dest_Ip[1]=0;
TcpStatus[num].Dest_Ip[2]=0;
TcpStatus[num].Dest_Ip[3]=0;
TcpStatus[num].TcpDataQWrPtr=0;
TcpStatus[num].TcpDataQRdPtr=0;
OS_EXIT_CRITICAL();
}
void Tcp_Listen(uint8 num)
{
static uint32 initalseq=32451;
struct _pkst TxdData;
if(Tcp_Packed.Tcp.control&TCP_SYN)
{
TcpStatus[num].State=TCP_STATE_SYN_RCVD;//TCP_STATE_SYN_RCVD;//Tcp_SYN_Rec;
//TcpStatus[num].Dest_Ip[0]=Tcp_Packed.TcpDHead.DestId[0];
//TcpStatus[num].Dest_Ip[1]=Tcp_Packed.TcpDHead.DestId[1];
//TcpStatus[num].Dest_Ip[2]=Tcp_Packed.TcpDHead.DestId[2];
//TcpStatus[num].Dest_Ip[3]=Tcp_Packed.TcpDHead.DestId[3];
TcpStatus[num].Dest_Port=Tcp_Packed.Tcp.SourcePort;//对方端口
//TcpStatus[num].My_Port=Tcp_Packed.Tcp.DestPort;
TcpStatus[num].IRS=Tcp_Packed.Tcp.SeqNum;//对方的初始化顺序号
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;
TcpStatus[num].RecPassAck=0;
TcpStatus[num].SenPassSeq=initalseq;
TcpStatus[num].SenPassAck=TcpStatus[num].RecPassSeq+1;
TcpStatus[num].RecFutureAck=initalseq+1;
TcpStatus[num].SenFutureSeq=initalseq+1;
TcpStatus[num].Rcv_Next=Tcp_Packed.Tcp.SeqNum+1;//对方的顺序号,用于确认
TcpStatus[num].ISS=TcpStatus[num].Send_Next;//我的初始化顺序号
TcpStatus[num].Sent_UnAck=TcpStatus[num].ISS;//我的未确认得序号
TcpStatus[num].Send_Next=TcpStatus[num].ISS+1;//我的顺序号,用于发送
TcpStatus[num].My_Wl1=Tcp_Packed.Tcp.SeqNum;//seq
TcpStatus[num].My_Wl2=TcpStatus[num].Send_Next;
TcpStatus[num].Rcv_Window=Tcp_Packed.Tcp.window;//对方的WINDOW大小
TcpStatus[num].Snd_Window=MAX_TCP_DATA;//通知对方本地最大接收1024字节的包,用于流控
TcpStatus[num].Dest_Max_Seg_Size=MAX_TCP_DATA;//默认为560
if(Tcp_Packed.Tcp.offset>50)
if(Tcp_Packed.Tcp.tcpdata[0]==0x02)
if(Tcp_Packed.Tcp.tcpdata[1]==0x04) //0204为最大segment选项
{
TcpStatus[num].Dest_Max_Seg_Size=Tcp_Packed.Tcp.tcpdata[2]*256+Tcp_Packed.Tcp.tcpdata[3];
}
TcpStatus[num].My_Max_Seg_Size=MAX_TCP_DATA;//本地机可以接受最大的以太网数据包
//
TcpStatus[num].TcpHeadUint8[0]=(TcpStatus[num].My_Port&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[1]=TcpStatus[num].My_Port&0x00ff;
Tcp_Packed.Tcp.SourcePort=TcpStatus[num].My_Port;
TcpStatus[num].TcpHeadUint8[2]=(TcpStatus[num].Dest_Port&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[3]=TcpStatus[num].Dest_Port&0x00ff;
Tcp_Packed.Tcp.DestPort=TcpStatus[num].Dest_Port;
TcpStatus[num].TcpHeadUint8[4]=(TcpStatus[num].SenPassSeq&0xff000000)>>24;
TcpStatus[num].TcpHeadUint8[5]=(TcpStatus[num].SenPassSeq&0x00ff0000)>>16;
TcpStatus[num].TcpHeadUint8[6]=(TcpStatus[num].SenPassSeq&0x0000ff00)>>8;
TcpStatus[num].TcpHeadUint8[7]=(TcpStatus[num].SenPassSeq&0x000000ff);
Tcp_Packed.Tcp.SeqNum=TcpStatus[num].ISS;
TcpStatus[num].TcpHeadUint8[8]=(TcpStatus[num].SenPassAck&0xff000000)>>24;
TcpStatus[num].TcpHeadUint8[9]=(TcpStatus[num].SenPassAck&0x00ff0000)>>16;
TcpStatus[num].TcpHeadUint8[10]=(TcpStatus[num].SenPassAck&0x0000ff00)>>8;
TcpStatus[num].TcpHeadUint8[11]=(TcpStatus[num].SenPassAck&0x000000ff);
Tcp_Packed.Tcp.AckNum=TcpStatus[num].Rcv_Next;
TcpStatus[num].TcpHeadUint8[12]=0x70;
Tcp_Packed.Tcp.offset=0x70;
TcpStatus[num].TcpHeadUint8[13]=0x12;
Tcp_Packed.Tcp.control=0x12; //syn+ack
TcpStatus[num].TcpHeadUint8[14]=(TcpStatus[num].Snd_Window&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[15]=TcpStatus[num].Snd_Window&0x00ff;
Tcp_Packed.Tcp.window=TcpStatus[num].Snd_Window;
TcpStatus[num].TcpHeadUint8[16]=0;
TcpStatus[num].TcpHeadUint8[17]=0;
Tcp_Packed.Tcp.Crc=0;
TcpStatus[num].TcpHeadUint8[18]=0;
TcpStatus[num].TcpHeadUint8[19]=0;
Tcp_Packed.Tcp.urg=0;
//
TcpStatus[num].TcpHeadUint8[20]=0X02;
Tcp_Packed.Tcp.tcpdata[0]=0X02;
TcpStatus[num].TcpHeadUint8[21]=0X04;
Tcp_Packed.Tcp.tcpdata[1]=0X04;
TcpStatus[num].TcpHeadUint8[22]=MAX_TCP_DATA/256;
Tcp_Packed.Tcp.tcpdata[2]=MAX_TCP_DATA/256;
TcpStatus[num].TcpHeadUint8[23]=MAX_TCP_DATA%6;
Tcp_Packed.Tcp.tcpdata[3]=MAX_TCP_DATA%6;
TcpStatus[num].TcpHeadUint8[24]=0X01;
Tcp_Packed.Tcp.tcpdata[4]=0X01;
TcpStatus[num].TcpHeadUint8[25]=0X01;
Tcp_Packed.Tcp.tcpdata[5]=0X01;
TcpStatus[num].TcpHeadUint8[26]=0X01;
Tcp_Packed.Tcp.tcpdata[6]=0X01;
TcpStatus[num].TcpHeadUint8[27]=0X01;
Tcp_Packed.Tcp.tcpdata[7]=0X01;
//
TcpStatus[num].TcpDHeadUint8[0]=0;
Tcp_Packed.TcpDHead.rev=0;
TcpStatus[num].TcpDHeadUint8[1]=6;
Tcp_Packed.TcpDHead.NextProtocal=6;
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=28;
Tcp_Packed.TcpDHead.TotalLen=28;
TcpStatus[num].TcpDHeadUint8[4]=TcpStatus[num].My_Ip[0];
TcpStatus[num].TcpDHeadUint8[5]=TcpStatus[num].My_Ip[1];
TcpStatus[num].TcpDHeadUint8[6]=TcpStatus[num].My_Ip[2];
TcpStatus[num].TcpDHeadUint8[7]=TcpStatus[num].My_Ip[3];
TcpStatus[num].TcpDHeadUint8[8]=TcpStatus[num].Dest_Ip[0];
TcpStatus[num].TcpDHeadUint8[9]=TcpStatus[num].Dest_Ip[1];
TcpStatus[num].TcpDHeadUint8[10]=TcpStatus[num].Dest_Ip[2];
TcpStatus[num].TcpDHeadUint8[11]=TcpStatus[num].Dest_Ip[3];
//Tcp_Packed.Tcp.Crc=CheckSumTcp((uint16 EX_RAM *)&Tcp_Packed,Tcp_Packed.TcpDHead.TotalLen+12);
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,40);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
//memcpy (&(TcpStatus[num].resend_data),&(Tcp_Packed.Tcp) , Tcp_Packed.TcpDHead.TotalLen);
TcpStatus[num].ResendLength=Tcp_Packed.TcpDHead.TotalLen;
TcpStatus[num].TcpDataQWrPtr=0;
TcpStatus[num].TcpDataQRdPtr=0;
TxdData.STPTR=NULL;
TxdData.length=Tcp_Packed.TcpDHead.TotalLen;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
else if(Tcp_Packed.Tcp.control&TCP_RST)
{;}
}
void Tcp_SYN_Rec(uint8 num)
{struct _pkst TxdData;
if(Tcp_Packed.Tcp.control&(TCP_RST))//reset//处理reset,对方不接受请求
{
Delete_Socket(num);
//Send_Reset(num);
}
else if(Tcp_Packed.Tcp.control&(TCP_SYN))//收到同步信号
{
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;//确认
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;//对方的初始化顺序号
TcpStatus[num].SenPassSeq=TcpStatus[num].RecPassAck;
TcpStatus[num].SenPassAck=TcpStatus[num].RecPassSeq;//我的顺序号,用于发送
TcpHeadHandle(num ,0x5000+TCP_RST);//headlength 0x20,
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.TcpDHead.TotalLen=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
Delete_Socket(num);
//Tcp_Listen(num) ;
//Send_Reset(num);
}
else if(Tcp_Packed.Tcp.control&TCP_ACK)//表示这是一个3次握手的确认表明连接建立
{
if((TcpStatus[num].SenPassSeq<=Tcp_Packed.Tcp.AckNum)
&&(Tcp_Packed.Tcp.AckNum<=TcpStatus[num].RecFutureAck))
{
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;//确认
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;//确认
TcpStatus[num].State=TCP_STATE_ESTABLISHED;//tcp_syn_established;
if((TcpStatus[num].StaSem&0x02)==0x02)
OSSemPost(AppSemCon[num]);
//TcpConnected=1;
}
}
}
uint8 Tcp_SYN_Sent(uint8 num)
{
static uint32 initalseq=32451;
struct _pkst TxdData;
if((Tcp_Packed.Tcp.control&TCP_SYN)&&(Tcp_Packed.Tcp.control&TCP_ACK))
{
if(TcpStatus[num].RecFutureAck<=Tcp_Packed.Tcp.AckNum)
{
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;//确认
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;//对方的初始化顺序号
TcpStatus[num].SenPassSeq=TcpStatus[num].RecPassAck;
TcpStatus[num].SenPassAck=TcpStatus[num].RecPassSeq+1;//我的顺序号,用于发送
TcpStatus[num].My_Wl1=TcpStatus[num].RecPassSeq;//seq
TcpStatus[num].Rcv_Window=Tcp_Packed.Tcp.window;//对方的WINDOW大小
TcpStatus[num].Dest_Max_Seg_Size=MAX_TCP_DATA;//默认为560
if(Tcp_Packed.Tcp.offset>20)
if(Tcp_Packed.Tcp.tcpdata[0]==0x02)
if(Tcp_Packed.Tcp.tcpdata[1]==0x04) //0204为最大segment选项
{
TcpStatus[num].Dest_Max_Seg_Size=Tcp_Packed.Tcp.tcpdata[2]*256+Tcp_Packed.Tcp.tcpdata[3];
}
//
TcpHeadHandle(num ,0x5000+TCP_ACK);
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
TcpStatus[num].State=TCP_STATE_ESTABLISHED;
if((TcpStatus[num].StaSem&0x02)==0x02)
OSSemPost(AppSemCon[num]);
return (0);
}
return (0);
}
else if(Tcp_Packed.Tcp.control&TCP_SYN)
{
TcpStatus[num].State=TCP_STATE_SYN_RCVD;//TCP_STATE_SYN_RCVD;//Tcp_SYN_Rec;
//TcpStatus[num].Dest_Ip[0]=Tcp_Packed.TcpDHead.DestId[0];
//TcpStatus[num].Dest_Ip[1]=Tcp_Packed.TcpDHead.DestId[1];
//TcpStatus[num].Dest_Ip[2]=Tcp_Packed.TcpDHead.DestId[2];
//TcpStatus[num].Dest_Ip[3]=Tcp_Packed.TcpDHead.DestId[3];
TcpStatus[num].Dest_Port=Tcp_Packed.Tcp.SourcePort;//对方端口
//TcpStatus[num].My_Port=Tcp_Packed.Tcp.DestPort;
TcpStatus[num].IRS=Tcp_Packed.Tcp.SeqNum;//对方的初始化顺序号
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;
TcpStatus[num].RecPassAck=0;
TcpStatus[num].SenPassSeq=initalseq;
TcpStatus[num].SenPassAck=TcpStatus[num].RecPassSeq+1;
TcpStatus[num].RecFutureAck=initalseq+1;
TcpStatus[num].SenFutureSeq=initalseq+1;
TcpStatus[num].Rcv_Next=Tcp_Packed.Tcp.SeqNum+1;//对方的顺序号,用于确认
TcpStatus[num].ISS=TcpStatus[num].Send_Next;//我的初始化顺序号
TcpStatus[num].Sent_UnAck=TcpStatus[num].ISS;//我的未确认得序号
TcpStatus[num].Send_Next=TcpStatus[num].ISS+1;//我的顺序号,用于发送
TcpStatus[num].My_Wl1=Tcp_Packed.Tcp.SeqNum;//seq
TcpStatus[num].My_Wl2=TcpStatus[num].Send_Next;
TcpStatus[num].Rcv_Window=Tcp_Packed.Tcp.window;//对方的WINDOW大小
TcpStatus[num].Snd_Window=MAX_TCP_DATA;//通知对方本地最大接收1024字节的包,用于流控
TcpStatus[num].Dest_Max_Seg_Size=MAX_TCP_DATA;//默认为560
if(Tcp_Packed.Tcp.offset>50)
if(Tcp_Packed.Tcp.tcpdata[0]==0x02)
if(Tcp_Packed.Tcp.tcpdata[1]==0x04) //0204为最大segment选项
{
TcpStatus[num].Dest_Max_Seg_Size=Tcp_Packed.Tcp.tcpdata[2]*256+Tcp_Packed.Tcp.tcpdata[3];
}
TcpStatus[num].My_Max_Seg_Size=MAX_TCP_DATA;//本地机可以接受最大的以太网数据包
//
TcpStatus[num].TcpHeadUint8[0]=(TcpStatus[num].My_Port&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[1]=TcpStatus[num].My_Port&0x00ff;
Tcp_Packed.Tcp.SourcePort=TcpStatus[num].My_Port;
TcpStatus[num].TcpHeadUint8[2]=(TcpStatus[num].Dest_Port&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[3]=TcpStatus[num].Dest_Port&0x00ff;
Tcp_Packed.Tcp.DestPort=TcpStatus[num].Dest_Port;
TcpStatus[num].TcpHeadUint8[4]=(TcpStatus[num].SenPassSeq&0xff000000)>>24;
TcpStatus[num].TcpHeadUint8[5]=(TcpStatus[num].SenPassSeq&0x00ff0000)>>16;
TcpStatus[num].TcpHeadUint8[6]=(TcpStatus[num].SenPassSeq&0x0000ff00)>>8;
TcpStatus[num].TcpHeadUint8[7]=(TcpStatus[num].SenPassSeq&0x000000ff);
Tcp_Packed.Tcp.SeqNum=TcpStatus[num].ISS;
TcpStatus[num].TcpHeadUint8[8]=(TcpStatus[num].SenPassAck&0xff000000)>>24;
TcpStatus[num].TcpHeadUint8[9]=(TcpStatus[num].SenPassAck&0x00ff0000)>>16;
TcpStatus[num].TcpHeadUint8[10]=(TcpStatus[num].SenPassAck&0x0000ff00)>>8;
TcpStatus[num].TcpHeadUint8[11]=(TcpStatus[num].SenPassAck&0x000000ff);
Tcp_Packed.Tcp.AckNum=TcpStatus[num].Rcv_Next;
TcpStatus[num].TcpHeadUint8[12]=0x70;
Tcp_Packed.Tcp.offset=0x70;
TcpStatus[num].TcpHeadUint8[13]=0x12;
Tcp_Packed.Tcp.control=0x12; //syn+ack
TcpStatus[num].TcpHeadUint8[14]=(TcpStatus[num].Snd_Window&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[15]=TcpStatus[num].Snd_Window&0x00ff;
Tcp_Packed.Tcp.window=TcpStatus[num].Snd_Window;
TcpStatus[num].TcpHeadUint8[16]=0;
TcpStatus[num].TcpHeadUint8[17]=0;
Tcp_Packed.Tcp.Crc=0;
TcpStatus[num].TcpHeadUint8[18]=0;
TcpStatus[num].TcpHeadUint8[19]=0;
Tcp_Packed.Tcp.urg=0;
//
TcpStatus[num].TcpHeadUint8[20]=0X02;
Tcp_Packed.Tcp.tcpdata[0]=0X02;
TcpStatus[num].TcpHeadUint8[21]=0X04;
Tcp_Packed.Tcp.tcpdata[1]=0X04;
TcpStatus[num].TcpHeadUint8[22]=MAX_TCP_DATA/256;
Tcp_Packed.Tcp.tcpdata[2]=MAX_TCP_DATA/256;
TcpStatus[num].TcpHeadUint8[23]=MAX_TCP_DATA%6;
Tcp_Packed.Tcp.tcpdata[3]=MAX_TCP_DATA%6;
TcpStatus[num].TcpHeadUint8[24]=0X01;
Tcp_Packed.Tcp.tcpdata[4]=0X01;
TcpStatus[num].TcpHeadUint8[25]=0X01;
Tcp_Packed.Tcp.tcpdata[5]=0X01;
TcpStatus[num].TcpHeadUint8[26]=0X01;
Tcp_Packed.Tcp.tcpdata[6]=0X01;
TcpStatus[num].TcpHeadUint8[27]=0X01;
Tcp_Packed.Tcp.tcpdata[7]=0X01;
//
TcpStatus[num].TcpDHeadUint8[0]=0;
Tcp_Packed.TcpDHead.rev=0;
TcpStatus[num].TcpDHeadUint8[1]=6;
Tcp_Packed.TcpDHead.NextProtocal=6;
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=28;
Tcp_Packed.TcpDHead.TotalLen=28;
TcpStatus[num].TcpDHeadUint8[4]=TcpStatus[num].My_Ip[0];
TcpStatus[num].TcpDHeadUint8[5]=TcpStatus[num].My_Ip[1];
TcpStatus[num].TcpDHeadUint8[6]=TcpStatus[num].My_Ip[2];
TcpStatus[num].TcpDHeadUint8[7]=TcpStatus[num].My_Ip[3];
TcpStatus[num].TcpDHeadUint8[8]=TcpStatus[num].Dest_Ip[0];
TcpStatus[num].TcpDHeadUint8[9]=TcpStatus[num].Dest_Ip[1];
TcpStatus[num].TcpDHeadUint8[10]=TcpStatus[num].Dest_Ip[2];
TcpStatus[num].TcpDHeadUint8[11]=TcpStatus[num].Dest_Ip[3];
//Tcp_Packed.Tcp.Crc=CheckSumTcp((uint16 EX_RAM *)&Tcp_Packed,Tcp_Packed.TcpDHead.TotalLen+12);
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,40);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
//memcpy (&(TcpStatus[num].resend_data),&(Tcp_Packed.Tcp) , Tcp_Packed.TcpDHead.TotalLen);
TcpStatus[num].ResendLength=Tcp_Packed.TcpDHead.TotalLen;
TxdData.STPTR=NULL;
TxdData.length=Tcp_Packed.TcpDHead.TotalLen;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
else if(Tcp_Packed.Tcp.control&TCP_RST)
{
Delete_Socket(num);
}
else if((Tcp_Packed.Tcp.control&TCP_ACK)==TCP_ACK)
{
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;//确认
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;//对方的初始化顺序号
TcpStatus[num].SenPassSeq=TcpStatus[num].RecPassAck;
TcpStatus[num].SenPassAck=TcpStatus[num].RecPassSeq;//我的顺序号,用于发送
TcpHeadHandle(num ,0x5000+TCP_RST);//headlength 0x20,
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.TcpDHead.TotalLen=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
return 0;
}
void Tcp_Last_Ack(uint8 num)
{
if(Tcp_Packed.Tcp.control&(TCP_RST+TCP_SYN+TCP_ACK))
{//对方不接受请求,关闭本地连接
Delete_Socket(num);
}
}
uint8 Tcp_Established(uint8 num)
{//
uint16 i;
uint16 temp;
// uint16 tcplength;
struct _pkst TxdData;
if(Tcp_Packed.Tcp.control&(TCP_RST+TCP_SYN))
{
Delete_Socket(num);
return (0);
}
if(Tcp_Packed.Tcp.control&(TCP_FIN))
{
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;
//Tcp_Packed.Tcp.control=TCP_ACK;
TcpStatus[num].SenPassSeq=TcpStatus[num].RecPassAck;
TcpStatus[num].SenPassAck=TcpStatus[num].RecPassSeq+1;
TcpStatus[num].SenFutureSeq=TcpStatus[num].SenPassSeq;
TcpStatus[num].RecFutureAck=TcpStatus[num].SenPassSeq+1;
TcpHeadHandle(num ,0x5000+TCP_ACK);//headlength 0x20,
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.TcpDHead.TotalLen=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
//Tcp_Packed.Tcp.control=TCP_FIN+TCP_ACK;
TcpHeadHandle(num ,0x5000+TCP_FIN+TCP_ACK);//headlength 0x20,
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.TcpDHead.TotalLen=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+20
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
//Tcp_Close_Wait(num);
TcpStatus[num].State=TCP_STATE_LAST_ACK;
return(0);
}
temp=Tcp_Packed.Tcp.offset>>4;
temp=temp*4; //IP首部占20字节
temp=Tcp_Packed.TcpDHead.TotalLen-temp;//数据报总长度减去TCP和IP首部长度,得到数据长度
if((temp>0)&&(TcpStatus[num].Snd_Window>=temp))
{
if(Tcp_Packed.Tcp.SeqNum>=TcpStatus[num].RecFutureAck)//??
{
for(i=0;i<temp;i++)
{
TcpStatus[num].TcpDataQ[TcpStatus[num].TcpDataQWrPtr]=Tcp_Packed.Tcp.tcpdata[i];
if(TcpStatus[num].TcpDataQWrPtr>=(MAX_TCP_DATA-1))
TcpStatus[num].TcpDataQWrPtr=0;
else
TcpStatus[num].TcpDataQWrPtr++;
}
TcpStatus[num].Snd_Window=TcpStatus[num].Snd_Window-temp;
TcpStatus[num].SenPassAck=TcpStatus[num].SenPassAck+temp;
}
if(Tcp_Packed.Tcp.control&TCP_ACK)
{//表示这是一个3次握手的确认
if((TcpStatus[num].SenPassSeq<=Tcp_Packed.Tcp.AckNum)
&&(Tcp_Packed.Tcp.AckNum<=TcpStatus[num].RecFutureAck))
{
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;//确认
//if(Resend_Buff.TcpFrame.SeqNum<TCP1024.Sent_UnAck);
}
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;
TcpStatus[num].SenPassSeq=Tcp_Packed.Tcp.AckNum;
TcpStatus[num].SenFutureSeq=TcpStatus[num].SenFutureSeq;
TcpStatus[num].RecFutureAck=TcpStatus[num].SenPassSeq;
TcpHeadHandle(num ,0x5010);//headlength 0x20 , ack
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.TcpDHead.TotalLen=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
return (1);
}
else if(((Tcp_Packed.Tcp.control&TCP_ACK)==TCP_ACK)&&(temp>0))
{
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;
TcpStatus[num].SenPassSeq=Tcp_Packed.Tcp.AckNum;
TcpStatus[num].SenFutureSeq=TcpStatus[num].SenFutureSeq;
TcpStatus[num].RecFutureAck=TcpStatus[num].SenPassSeq;
TcpHeadHandle(num ,0x5010);//headlength 0x20 , ack
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.TcpDHead.TotalLen=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
else if(((Tcp_Packed.Tcp.control&TCP_ACK)==TCP_ACK)&&(temp==0))
{//表示这是一个3次握手的确认
if((TcpStatus[num].SenPassSeq<=Tcp_Packed.Tcp.AckNum)
&&(Tcp_Packed.Tcp.AckNum<=TcpStatus[num].RecFutureAck))
{
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;//确认
if((TcpStatus[num].StaSem&0x01)==0x01)
{
TcpStatus[num].StaSem=TcpStatus[num].StaSem&0xFE;
OSSemPost(AppSemSend[num]);
}
return (2);
}
return (3);
}
return (4);
}
void TCP_FIN_WAIT1(uint8 num)
{
if(Tcp_Packed.Tcp.control&(TCP_ACK))
{
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;
TcpStatus[num].RecFutureAck=TcpStatus[num].RecPassAck;
TcpStatus[num].State=TCP_STATE_FIN_WAIT2;
}
else
OSSemPost(AppSemDisc[num]);
}
void TCP_FIN_WAIT2(uint8 num)
{
struct _pkst TxdData;
if(Tcp_Packed.Tcp.control&(TCP_FIN))
{
TcpStatus[num].RecPassSeq=Tcp_Packed.Tcp.SeqNum;
TcpStatus[num].RecPassAck=Tcp_Packed.Tcp.AckNum;
TcpStatus[num].SenPassSeq=TcpStatus[num].RecPassAck;
TcpStatus[num].SenPassAck=TcpStatus[num].RecPassSeq+1;
TcpHeadHandle(num ,0x5000+TCP_ACK);//headlength 0x20,
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
Tcp_Packed.TcpDHead.TotalLen=20;
Tcp_Packed.Tcp.Crc=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(Tcp_Packed.Tcp.Crc&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=Tcp_Packed.Tcp.Crc&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
OSSemPost(AppSemDisc[num]);
}
uint8 Process_Tcp1(Rec_Ptr * Tcp_Rec)
{
uint8 i,j;
uint8 * temp_ptr;
struct _pkst TxdData;
if((*Tcp_Rec).length<=(MAX_TCP_DATA+20))
{
OS_ENTER_CRITICAL();
memcpy (&(Tcp_Packed.Tcp), (*Tcp_Rec).RecDataPtr, (*Tcp_Rec).length);
OS_EXIT_CRITICAL();
}
else
{
return 0;
}
temp_ptr=(uint8 *)&(Tcp_Packed.Tcp);
Tcp_Packed.Tcp.SourcePort=Char2ToInt16(temp_ptr);
temp_ptr=temp_ptr+2;
Tcp_Packed.Tcp.DestPort=Char2ToInt16(temp_ptr);
temp_ptr=temp_ptr+2;
Tcp_Packed.Tcp.SeqNum=Char4ToInt32(temp_ptr);
temp_ptr=temp_ptr+4;
Tcp_Packed.Tcp.AckNum=Char4ToInt32(temp_ptr);
temp_ptr=temp_ptr+4;
temp_ptr=temp_ptr+2;
Tcp_Packed.Tcp.window=Char2ToInt16(temp_ptr);
temp_ptr=temp_ptr+4;
Tcp_Packed.Tcp.urg=Char2ToInt16(temp_ptr);
i=0;
j=MAX_TCP_LINKS;
Tcp_Packed.TcpDHead.TotalLen=(*Tcp_Rec).length;
for(i=0;i<MAX_TCP_LINKS;i++)
{
if(TcpStatus[i].State>=TCP_STATE_SYN_RCVD)//已经建立的连接
{
if(TcpStatus[i].Dest_Ip[0]==((Rec_Ptr*)Tcp_Rec)->ip[0])
if(TcpStatus[i].Dest_Ip[1]==((Rec_Ptr*)Tcp_Rec)->ip[1])
if(TcpStatus[i].Dest_Ip[2]==((Rec_Ptr*)Tcp_Rec)->ip[2])
if(TcpStatus[i].Dest_Ip[3]==((Rec_Ptr*)Tcp_Rec)->ip[3])
if(TcpStatus[i].My_Ip[0]==((Rec_Ptr*)Tcp_Rec)->My_Ip[0])
if(TcpStatus[i].My_Ip[1]==((Rec_Ptr*)Tcp_Rec)->My_Ip[1])
if(TcpStatus[i].My_Ip[2]==((Rec_Ptr*)Tcp_Rec)->My_Ip[2])
if(TcpStatus[i].My_Ip[3]==((Rec_Ptr*)Tcp_Rec)->My_Ip[3])
if(Tcp_Packed.Tcp.DestPort==TcpStatus[i].My_Port)
if(Tcp_Packed.Tcp.SourcePort==TcpStatus[i].Dest_Port)
{
break;
}
}
}
if(i<MAX_TCP_LINKS)
{
switch (TcpStatus[i].State)
{
case 1: //Tcp_Listen(i);
break;
case 2: Tcp_SYN_Rec(i);
break;
case 3: Tcp_SYN_Sent(i);
break;
case 4: Tcp_Established(i);
break;
case 5: TCP_FIN_WAIT1(i);
break;
case 6: TCP_FIN_WAIT2(i);
break;
case 8: //Tcp_Close_Wait();
break;
case 9: Tcp_Last_Ack(i);//关闭
break;
}
return (2);
}
else
{
for(i=0;i<MAX_TCP_LINKS;i++)
{
if(TcpStatus[i].My_Ip[0]==((Rec_Ptr*)Tcp_Rec)->My_Ip[0])
if(TcpStatus[i].My_Ip[1]==((Rec_Ptr*)Tcp_Rec)->My_Ip[1])
if(TcpStatus[i].My_Ip[2]==((Rec_Ptr*)Tcp_Rec)->My_Ip[2])
if(TcpStatus[i].My_Ip[3]==((Rec_Ptr*)Tcp_Rec)->My_Ip[3])
if(Tcp_Packed.Tcp.DestPort==TcpStatus[i].My_Port)
if(TcpStatus[i].State==TCP_STATE_LISTEN)
{
break;
}
}
if(i<MAX_TCP_LINKS)
{
TcpStatus[i].Dest_Ip[0]=((Rec_Ptr*)Tcp_Rec)->ip[0];
TcpStatus[i].Dest_Ip[1]=((Rec_Ptr*)Tcp_Rec)->ip[1];
TcpStatus[i].Dest_Ip[2]=((Rec_Ptr*)Tcp_Rec)->ip[2];
TcpStatus[i].Dest_Ip[3]=((Rec_Ptr*)Tcp_Rec)->ip[3];
Tcp_Listen(i);
return (2);
}
else
{
Tcp_Packed.TcpDHead.TotalLen=0x14;
Tcp_Packed.TcpDHead.rev=0;
Tcp_Packed.TcpDHead.NextProtocal=6;
Tcp_Packed.TcpDHead.SourceIp[0]=((Rec_Ptr*)Tcp_Rec)->My_Ip[0];
Tcp_Packed.TcpDHead.SourceIp[1]=((Rec_Ptr*)Tcp_Rec)->My_Ip[1];
Tcp_Packed.TcpDHead.SourceIp[2]=((Rec_Ptr*)Tcp_Rec)->My_Ip[2];
Tcp_Packed.TcpDHead.SourceIp[3]=((Rec_Ptr*)Tcp_Rec)->My_Ip[3];
Tcp_Packed.TcpDHead.DestId[0]=((Rec_Ptr*)Tcp_Rec)->ip[0];
Tcp_Packed.TcpDHead.DestId[1]=((Rec_Ptr*)Tcp_Rec)->ip[1];
Tcp_Packed.TcpDHead.DestId[2]=((Rec_Ptr*)Tcp_Rec)->ip[2];
Tcp_Packed.TcpDHead.DestId[3]=((Rec_Ptr*)Tcp_Rec)->ip[3];
#ifdef Little_End
Tcp_Packed.TcpDHead.TotalLen=0x1400;
Tcp_Packed.Tcp.SourcePort=swap_int16(Tcp_Packed.Tcp.SourcePort);
Tcp_Packed.Tcp.DestPort=swap_int16(Tcp_Packed.Tcp.DestPort);
#endif
Tcp_Packed.Tcp.Crc=Tcp_Packed.Tcp.DestPort;
Tcp_Packed.Tcp.DestPort=Tcp_Packed.Tcp.SourcePort;
Tcp_Packed.Tcp.SourcePort=Tcp_Packed.Tcp.Crc;
Tcp_Packed.Tcp.Crc=0;
Tcp_Packed.Tcp.AckNum=Tcp_Packed.Tcp.SeqNum+1;
#ifdef Little_End
Tcp_Packed.Tcp.AckNum=swap_int32(Tcp_Packed.Tcp.AckNum);
#endif
Tcp_Packed.Tcp.SeqNum=0;
Tcp_Packed.Tcp.offset=0x50;
Tcp_Packed.Tcp.control=0x14;
Tcp_Packed.Tcp.window=0;
Tcp_Packed.Tcp.urg=0;
Tcp_Packed.Tcp.Crc=CheckSumUdp((uint8 *)&Tcp_Packed,32);
#ifdef Little_End
Tcp_Packed.Tcp.Crc=swap_int16(Tcp_Packed.Tcp.Crc);
#endif
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=(uint8 *)&Tcp_Packed.Tcp;
Send_Ip_Frame
(
&TxdData,
Tcp_Packed.TcpDHead.DestId,
Tcp_Packed.TcpDHead.SourceIp,
6
);
return (1);
}
}
}
void TcpHeadHandle(uint8 num ,uint16 Tcp_headlength_Control)
{
TcpStatus[num].TcpHeadUint8[0]=(TcpStatus[num].My_Port&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[1]=TcpStatus[num].My_Port&0x00ff;
TcpStatus[num].TcpHeadUint8[2]=(TcpStatus[num].Dest_Port&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[3]=TcpStatus[num].Dest_Port&0x00ff;
TcpStatus[num].TcpHeadUint8[4]=(TcpStatus[num].SenPassSeq&0xff000000)>>24;
TcpStatus[num].TcpHeadUint8[5]=(TcpStatus[num].SenPassSeq&0x00ff0000)>>16;
TcpStatus[num].TcpHeadUint8[6]=(TcpStatus[num].SenPassSeq&0x0000ff00)>>8;
TcpStatus[num].TcpHeadUint8[7]=(TcpStatus[num].SenPassSeq&0x000000ff);
TcpStatus[num].TcpHeadUint8[8]=(TcpStatus[num].SenPassAck&0xff000000)>>24;
TcpStatus[num].TcpHeadUint8[9]=(TcpStatus[num].SenPassAck&0x00ff0000)>>16;
TcpStatus[num].TcpHeadUint8[10]=(TcpStatus[num].SenPassAck&0x0000ff00)>>8;
TcpStatus[num].TcpHeadUint8[11]=(TcpStatus[num].SenPassAck&0x000000ff);
TcpStatus[num].TcpHeadUint8[12]=(Tcp_headlength_Control&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[13]=Tcp_headlength_Control&0x00ff;
TcpStatus[num].TcpHeadUint8[14]=(TcpStatus[num].Snd_Window&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[15]=TcpStatus[num].Snd_Window&0x00ff;
TcpStatus[num].TcpHeadUint8[16]=0;
TcpStatus[num].TcpHeadUint8[17]=0;
TcpStatus[num].TcpHeadUint8[18]=0;
TcpStatus[num].TcpHeadUint8[19]=0;
TcpStatus[num].TcpDHeadUint8[0]=0;
TcpStatus[num].TcpDHeadUint8[1]=6;
TcpStatus[num].TcpDHeadUint8[4]=TcpStatus[num].My_Ip[0];
TcpStatus[num].TcpDHeadUint8[5]=TcpStatus[num].My_Ip[1];
TcpStatus[num].TcpDHeadUint8[6]=TcpStatus[num].My_Ip[2];
TcpStatus[num].TcpDHeadUint8[7]=TcpStatus[num].My_Ip[3];
TcpStatus[num].TcpDHeadUint8[8]=TcpStatus[num].Dest_Ip[0];
TcpStatus[num].TcpDHeadUint8[9]=TcpStatus[num].Dest_Ip[1];
TcpStatus[num].TcpDHeadUint8[10]=TcpStatus[num].Dest_Ip[2];
TcpStatus[num].TcpDHeadUint8[11]=TcpStatus[num].Dest_Ip[3];
}
3.UDP.C
#define UDP_GLOBALS
#include "config.h"
#include "../include/cfg_net.h"
//=================================================================================
uint16 CheckSumUdp(uint8 *check,uint16 length)
{
uint32 sum=0;
uint16 i;
for (i=0;i<(length)/2;i++)
{
sum = sum + ((uint32)check[2*i]<<8)+(uint32)check[2*i+1];
}
if(length&0x0001)//长度为奇数个时,要进行该操作
{
sum = sum + ((uint32)check[2*i]<<8);
}
sum = (sum&0xffff) + ((sum>>16)&0xffff);//高16位和低16位相加
if(sum & 0xffff0000)
{//表示有进位
sum++;
}
return ( (uint16)(~((sum)&0xffff)) );
}
uint8 Udp_Process(Rec_Ptr * Udp_Rec)
{
static uint8 UDP_REC_BUFF_NUM=0;
uint8 i,j;
OS_ENTER_CRITICAL();
memcpy (&(UdpRecBuff[UDP_REC_BUFF_NUM]), (*Udp_Rec).RecDataPtr, (*Udp_Rec).length);
OS_EXIT_CRITICAL();
#ifdef Little_End
UdpRecBuff[UDP_REC_BUFF_NUM].DestPort=swap_int16(UdpRecBuff[UDP_REC_BUFF_NUM].DestPort);
#endif
i=0;
do
{
if(UdpStatus[i].My_Port==UdpRecBuff[UDP_REC_BUFF_NUM].DestPort)//有可能组播
{
break;
}
i++;
}
while(i<MAX_UDP_LINKS);
if(i>=MAX_UDP_LINKS)
{
Icmp_Send(3,3,((Rec_Ptr*)Udp_Rec)->My_Ip,((Rec_Ptr*)Udp_Rec)->ip, (*Udp_Rec).RecDataPtr-20 );
return 0;
}
OS_ENTER_CRITICAL();
j=0;
do
{
if(UdpStatus[i].info[j].num==UdpMaxRec)
break;
j++;
}
while(j<UdpMaxRec);
if(j>=UdpMaxRec)
{
OS_EXIT_CRITICAL();
return (1);
}
UdpStatus[i].info[j].De_Port=UdpRecBuff[UDP_REC_BUFF_NUM].SourcePort;
#ifdef Little_End
UdpStatus[i].info[j].De_Port=swap_int16(UdpStatus[i].info[j].De_Port);
#endif
UdpStatus[i].info[j].De_Ip[0]=(*Udp_Rec).ip[0];
UdpStatus[i].info[j].De_Ip[1]=(*Udp_Rec).ip[1];
UdpStatus[i].info[j].De_Ip[2]=(*Udp_Rec).ip[2];
UdpStatus[i].info[j].De_Ip[3]=(*Udp_Rec).ip[3];
UdpStatus[i].info[j].num=UDP_REC_BUFF_NUM;
UdpStatus[i].info[j].len=(*Udp_Rec).length;
OS_EXIT_CRITICAL();
UDP_REC_BUFF_NUM++;
if(UDP_REC_BUFF_NUM>=UdpMaxRec)
UDP_REC_BUFF_NUM=0;
OSSemPost(UdpStatus[i].UdpSemRec);
return (2);
}
void Udp_Initial(void)
{
uint8 i,j;
for(i=0;i<MAX_UDP_LINKS;i++)
{
UdpStatus[i].My_Port=0;
UdpStatus[i].My_Ip[0]=0xff;
UdpStatus[i].My_Ip[1]=0xff;
UdpStatus[i].My_Ip[2]=0xff;
UdpStatus[i].My_Ip[3]=0xff;
UdpStatus[i].Rec_Sta=0;
UdpStatus[i].UdpSemRec=OSSemCreate(0);
for(j=0;j<UdpMaxRec;j++)
{
UdpStatus[i].info[j].De_Port=0;
UdpStatus[i].info[j].num=UdpMaxRec;
UdpStatus[i].info[j].len=0;
}
}
}
4.ARP.C
#define ARP_GLOBALS
#include "config.h"
#include "../include/cfg_net.h"
typedef struct {
uint16 HardwareType;
uint16 ProtocalType;
uint8 HardwareLen;
uint8 ProtocalLen;
uint16 Operation;
uint8 SourceMacId[6];
uint8 SourceIp[4];
uint8 DestMacId[6];
uint8 DestId[4];
} arp ;
uint8 ARP_REP_MAC[6]={0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
uint8 Ping_Ip_Address[4];
uint8 Ping_IP_TTL;
//uint8 My_Ip_Address[4]=MY_IP;
//uint8 My_Gateway_Ip_Address[4]=MY_GATEWAY;
//uint8 MY_IP_MARK[4]=IP_MARK_SETTING;
//struct a_t ARP_TERM[MAX_ARP_TERM];
//===========================================================
uint8 Arp_Answer(uint8 * ARP_REC_PTR,uint8 num)
{
struct _pkst TxdArp;
uint8 SEND_ARP_MAC[6];
uint8 i;
//如果目标IP地址是本机IP
if (((arp*)ARP_REC_PTR)->DestId[0]==NetPort[num].My_Ip[0])
if (((arp*)ARP_REC_PTR)->DestId[1]==NetPort[num].My_Ip[1])
if (((arp*)ARP_REC_PTR)->DestId[2]==NetPort[num].My_Ip[2])
if (((arp*)ARP_REC_PTR)->DestId[3]==NetPort[num].My_Ip[3])
{ //表示是要解析本地IP的请求
for(i=0;i<4;i++) //复制对方IP地址,填充源地址
{
SEND_ARP_MAC[i]=((arp*)ARP_REC_PTR)->SourceIp[i];
((arp*)ARP_REC_PTR)->SourceIp[i]=NetPort[num].My_Ip[i];
((arp*)ARP_REC_PTR)->DestId[i]=SEND_ARP_MAC[i];
}
for(i=0;i<6;i++)
{ //复制对方物理地址或网关地址
SEND_ARP_MAC[i]=((arp*)ARP_REC_PTR)->SourceMacId[i];
((arp*)ARP_REC_PTR)->SourceMacId[i]=NetPort[num].My_Mac[i];
((arp*)ARP_REC_PTR)->DestMacId[i]=SEND_ARP_MAC[i];
}
#ifdef Big_End
((arp*)ARP_REC_PTR)->Operation=0x0002; //表明数据帧为ARP应答
#endif
#ifdef Little_End
((arp*)ARP_REC_PTR)->Operation=0x0200; //表明数据帧为ARP应答
#endif
TxdArp.STPTR=NULL;
TxdArp.length=0x60;
TxdArp.DAPTR=ARP_REC_PTR;
Send_ethernet_Frame(&TxdArp,SEND_ARP_MAC,ARP_PACKED,num);//发送ARP应答帧
//如果发送方属于本网段
if((((arp*)ARP_REC_PTR)->SourceIp[0]&NetPort[num].My_Ip_Mark[0])==(NetPort[num].My_Ip[0]&NetPort[num].My_Ip_Mark[0]))
if((((arp*)ARP_REC_PTR)->SourceIp[1]&NetPort[num].My_Ip_Mark[1])==(NetPort[num].My_Ip[1]&NetPort[num].My_Ip_Mark[1]))
if((((arp*)ARP_REC_PTR)->SourceIp[2]&NetPort[num].My_Ip_Mark[2])==(NetPort[num].My_Ip[2]&NetPort[num].My_Ip_Mark[2]))
if((((arp*)ARP_REC_PTR)->SourceIp[3]&NetPort[num].My_Ip_Mark[3])==(NetPort[num].My_Ip[3]&NetPort[num].My_Ip_Mark[3]))
{
//查找有否属于该IP的对应MAC表
for(i=0;i<MAX_ARP_TERM;i++)
{
if(NetPort[num].ARP_TERM[i].IP_NUM[2]==((arp*)ARP_REC_PTR)->SourceIp[2])
if(NetPort[num].ARP_TERM[i].IP_NUM[3]==((arp*)ARP_REC_PTR)->SourceIp[3])
if(NetPort[num].ARP_TERM[i].TTL>0)
{//有则刷新
NetPort[num].ARP_TERM[i].TTL=100;
return(0);
}
}
//查找有否空的MAC表项
for(i=0;i<MAX_ARP_TERM;i++)
{
if(NetPort[num].ARP_TERM[i].TTL==0)
{//有则保存
NetPort[num].ARP_TERM[i].IP_NUM[0]=((arp*)ARP_REC_PTR)->SourceIp[0];
NetPort[num].ARP_TERM[i].IP_NUM[1]=((arp*)ARP_REC_PTR)->SourceIp[1];
NetPort[num].ARP_TERM[i].IP_NUM[2]=((arp*)ARP_REC_PTR)->SourceIp[2];
NetPort[num].ARP_TERM[i].IP_NUM[3]=((arp*)ARP_REC_PTR)->SourceIp[3];
NetPort[num].ARP_TERM[i].MAC_NUM[0]=((arp*)ARP_REC_PTR)->SourceMacId[0];
NetPort[num].ARP_TERM[i].MAC_NUM[1]=((arp*)ARP_REC_PTR)->SourceMacId[1];
NetPort[num].ARP_TERM[i].MAC_NUM[2]=((arp*)ARP_REC_PTR)->SourceMacId[2];
NetPort[num].ARP_TERM[i].MAC_NUM[3]=((arp*)ARP_REC_PTR)->SourceMacId[3];
NetPort[num].ARP_TERM[i].MAC_NUM[4]=((arp*)ARP_REC_PTR)->SourceMacId[4];
NetPort[num].ARP_TERM[i].MAC_NUM[5]=((arp*)ARP_REC_PTR)->SourceMacId[5];
NetPort[num].ARP_TERM[i].TTL=100;
return(2);
}
}//FOR
//MAC表已经满
return(4);
}//IF ARP
//不属于同一网段的
return (3);
}
//目标IP不是本机
return (1);
}
uint8 REC_ARP_REQ(uint8 * ARP_REC_REQ_PTR,uint8 num)
{
uint8 i;
//======================================================================
if(((arp*)ARP_REC_REQ_PTR)->SourceIp[0]==NetPort[num].My_Gateway[0])
if(((arp*)ARP_REC_REQ_PTR)->SourceIp[1]==NetPort[num].My_Gateway[1])
if(((arp*)ARP_REC_REQ_PTR)->SourceIp[2]==NetPort[num].My_Gateway[2])
if(((arp*)ARP_REC_REQ_PTR)->SourceIp[3]==NetPort[num].My_Gateway[3])
{ //表示是网关对ARP请求的回答.
for (i=0;i<6;i++)
{
NetPort[num].My_Gateway_Mac[i]=((arp*)ARP_REC_REQ_PTR)->SourceMacId[i];
}
NetPort[num].Gateway_IP_TTL=100; //表示网关地址已得到解析
//return(3);
}
//如果发送方属于本网段
if((((arp*)ARP_REC_REQ_PTR)->SourceIp[0]&NetPort[num].My_Ip_Mark[0])==(NetPort[num].My_Ip[0]&NetPort[num].My_Ip_Mark[0]))
if((((arp*)ARP_REC_REQ_PTR)->SourceIp[1]&NetPort[num].My_Ip_Mark[1])==(NetPort[num].My_Ip[1]&NetPort[num].My_Ip_Mark[1]))
if((((arp*)ARP_REC_REQ_PTR)->SourceIp[2]&NetPort[num].My_Ip_Mark[2])==(NetPort[num].My_Ip[2]&NetPort[num].My_Ip_Mark[2]))
if((((arp*)ARP_REC_REQ_PTR)->SourceIp[3]&NetPort[num].My_Ip_Mark[3])==(NetPort[num].My_Ip[3]&NetPort[num].My_Ip_Mark[3]))
{//查找有否属于该IP的对应MAC表
for(i=0;i<MAX_ARP_TERM;i++)
{
if(NetPort[num].ARP_TERM[i].IP_NUM[2]==((arp*)ARP_REC_REQ_PTR)->SourceIp[2])
if(NetPort[num].ARP_TERM[i].IP_NUM[3]==((arp*)ARP_REC_REQ_PTR)->SourceIp[3])
if(NetPort[num].ARP_TERM[i].TTL>0)
{//有则刷新
NetPort[num].ARP_TERM[i].TTL=100;
return(0);
}
}
//查找有否空的MAC表项
for(i=0;i<MAX_ARP_TERM;i++)
{
if(NetPort[num].ARP_TERM[i].TTL==0)
{//有则保存
NetPort[num].ARP_TERM[i].IP_NUM[0]=((arp*)ARP_REC_REQ_PTR)->SourceIp[0];
NetPort[num].ARP_TERM[i].IP_NUM[1]=((arp*)ARP_REC_REQ_PTR)->SourceIp[1];
NetPort[num].ARP_TERM[i].IP_NUM[2]=((arp*)ARP_REC_REQ_PTR)->SourceIp[2];
NetPort[num].ARP_TERM[i].IP_NUM[3]=((arp*)ARP_REC_REQ_PTR)->SourceIp[3];
NetPort[num].ARP_TERM[i].MAC_NUM[0]=((arp*)ARP_REC_REQ_PTR)->SourceMacId[0];
NetPort[num].ARP_TERM[i].MAC_NUM[1]=((arp*)ARP_REC_REQ_PTR)->SourceMacId[1];
NetPort[num].ARP_TERM[i].MAC_NUM[2]=((arp*)ARP_REC_REQ_PTR)->SourceMacId[2];
NetPort[num].ARP_TERM[i].MAC_NUM[3]=((arp*)ARP_REC_REQ_PTR)->SourceMacId[3];
NetPort[num].ARP_TERM[i].MAC_NUM[4]=((arp*)ARP_REC_REQ_PTR)->SourceMacId[4];
NetPort[num].ARP_TERM[i].MAC_NUM[5]=((arp*)ARP_REC_REQ_PTR)->SourceMacId[5];
NetPort[num].ARP_TERM[i].TTL=100;
return(2);
}
}
//MAC表已经满
return(4);
}
//对方IP即不是本网段也不是GATEWAY
return(1);
}
//===========================================================================
//=======================================================================
void PROCESS_ARP_REC(uint8 * ARP_PTR,uint8 num)
{
// EX_RAM PKST ARP_PACKED;
#ifdef Big_End
if(((arp*)ARP_PTR)->Operation==0X0001)
#endif
#ifdef Little_End
if(((arp*)ARP_PTR)->Operation==0X0100)
#endif
{
Arp_Answer(ARP_PTR,num);
}
#ifdef Big_End
else if(((arp*)ARP_PTR)->Operation==0X0002)
#endif
#ifdef Little_End
else if(((arp*)ARP_PTR)->Operation==0X0200)
#endif
{
REC_ARP_REQ(ARP_PTR,num);
}
//可添加REARP操作。
}
void Arp_Request(uint8 * ip_address,uint8 num)
{
struct _pkst TxdArpReq;
uint8 ARPREQ[46];
uint8 i;
for(i=0;i<6;i++) //复制对方网卡地址或网关地址
{
((arp*)ARPREQ)->SourceMacId[i]=NetPort[num].My_Mac[i];
((arp*)ARPREQ)->DestMacId[i]=0x00; //arp报文的目的物理地址填为0,由arp回答报文 //负责填充
}
for(i=0;i<4;i++)
{
((arp*)ARPREQ)->SourceIp[i]=NetPort[num].My_Ip[i];//填充源IP地址
((arp*)ARPREQ)->DestId[i]=*ip_address;//填充目的IP地址
ip_address++;
}
#ifdef Big_End
((arp*)ARPREQ)->HardwareType=0x0001; //硬件类型:0x0001,以太网类型
((arp*)ARPREQ)->ProtocalType=0x0800; //协议类型:0x0800,对应IPv4
((arp*)ARPREQ)->Operation=0x0001; //操作类型:ARP请求
#endif
#ifdef Little_End
((arp*)ARPREQ)->HardwareType=0x0100; //硬件类型:0x0001,以太网类型
((arp*)ARPREQ)->ProtocalType=0x0008; //协议类型:0x0800,对应IPv4
((arp*)ARPREQ)->Operation=0x0100; //操作类型:ARP请求
#endif
((arp*)ARPREQ)->HardwareLen=0x06; //硬件长度:即物理地址长度,单位字节
((arp*)ARPREQ)->ProtocalLen=0x04; //协议长度:即逻辑地址长度,单位字节
//for(i=28;i<29;i++) //不需要46 //当数据长度<60字节时,需要补足60字节数据
// {
ARPREQ[28]=0x00; //填充数据为0x00
// }
//启动发送数据,发送的是一个arp请求.
TxdArpReq.STPTR=NULL;
TxdArpReq.length=46;
TxdArpReq.DAPTR=ARPREQ;
Send_ethernet_Frame(&TxdArpReq,ARP_REP_MAC,ARP_PACKED,num);
}
void Initial_arp()
{
uint8 i,num;
Ping_IP_TTL=0;
for(num=0;num<MAX_NET_PORT;num++)
{
NetPort[num].Gateway_IP_TTL=0;
for(i=0;i<MAX_ARP_TERM;i++)
{
NetPort[num].ARP_TERM[i].TTL=0;
}
Arp_Request(NetPort[num].My_Gateway,num);
}
}
5.ICMP.C
#define ICMP_GLOBALS
#include "config.h"
#include "../include/cfg_net.h"
uint16 CreateIcmpCrc(union icmp_rc icmppk)
{
uint8 i;
union w CrcTemp;
CrcTemp.dwords=0;
for(i=0;i<20;i++) //用于查询的ICMP报文共40字节
{
CrcTemp.dwords = CrcTemp.dwords + icmppk.words.wordbuf[i];
}
while(CrcTemp.words.high>0)
{
CrcTemp.dwords = (uint32)(CrcTemp.words.high+CrcTemp.words.low);
}
CrcTemp.words.low = 0xffff - CrcTemp.words.low;
return(CrcTemp.words.low);
}
uint16 CreateIcmpCrc1(union icmp_rc icmppk)
{
uint8 i;
union w CrcTemp;
CrcTemp.dwords=0;
for(i=0;i<18;i++) //用于查询的ICMP报文共40字节
{
CrcTemp.dwords = CrcTemp.dwords + icmppk.words.wordbuf[i];
}
while(CrcTemp.words.high>0)
{
CrcTemp.dwords = (uint32)(CrcTemp.words.high+CrcTemp.words.low);
}
CrcTemp.words.low = 0xffff - CrcTemp.words.low;
return(CrcTemp.words.low);
}
void icmp_process(uint8 * ICMPSENDPTR,uint8 num)
{
uint8 i,temp;
struct _pkst SendIcmpData;
if(((icmppro*)ICMPSENDPTR)->icmpf.e_icmp.type==0x08)//请求!!
{
((icmppro*)ICMPSENDPTR)->icmpf.e_icmp.type=0;//回复
((icmppro*)ICMPSENDPTR)->ipf.e_ip.ttl--;
for (i = 0; i < 4; i++)//IP翻转
{
temp = ((icmppro*)ICMPSENDPTR)->ipf.e_ip.SourceIp[i];
((icmppro*)ICMPSENDPTR)->ipf.e_ip.SourceIp[i] = ((icmppro*)ICMPSENDPTR)->ipf.e_ip.DestId[i];
((icmppro*)ICMPSENDPTR)->ipf.e_ip.DestId[i] = temp;
}
((icmppro*)ICMPSENDPTR)->ipf.e_ip.Crc=0;
OS_ENTER_CRITICAL();
((icmppro*)ICMPSENDPTR)->ipf.e_ip.Crc=CreateIpHeadCrc(ICMPSENDPTR);//(((icmppro*)ICMPSENDPTR)->ipf);
#ifdef Little_End
((icmppro*)ICMPSENDPTR)->ipf.e_ip.Crc=swap_int16(((icmppro*)ICMPSENDPTR)->ipf.e_ip.Crc);
#endif
OS_EXIT_CRITICAL();
((icmppro*)ICMPSENDPTR)->icmpf.e_icmp.Crc=0;
((icmppro*)ICMPSENDPTR)->icmpf.e_icmp.Crc=CreateIcmpCrc(((icmppro*)ICMPSENDPTR)->icmpf);
SendIcmpData.length=((icmppro*)ICMPSENDPTR)->ipf.e_ip.TotalLen;
#ifdef Little_End
SendIcmpData.length=swap_int16(SendIcmpData.length);
#endif
SendIcmpData.STPTR=NULL;
SendIcmpData.DAPTR=ICMPSENDPTR;
OSSemPend(SendFlag,5,&temp);
if(temp==OS_NO_ERR)
{
Send_Ip_To_LLC(&SendIcmpData,((icmppro*)ICMPSENDPTR)->ipf.e_ip.DestId,num);
OSSemPost(SendFlag);
}
}
else if(((icmppro*)ICMPSENDPTR)->icmpf.e_icmp.type==0x00)//回复
{
i=i;
}
}
void Icmp_Send(uint8 type,uint8 option,uint8 *so_ip,uint8 *de_ip, uint8 * REC_FRAME )
{
struct _pkst SendPingData;
union icmp_rc IcmpPacked;
//static uint16 temp_findex=0x2345;
OS_ENTER_CRITICAL();
memcpy (&(IcmpPacked.e_icmp.icmpdata), REC_FRAME, 28);
OS_EXIT_CRITICAL();
IcmpPacked.e_icmp.type=type;
IcmpPacked.e_icmp.option=option;
IcmpPacked.e_icmp.Crc=0;
IcmpPacked.e_icmp.id=0;
IcmpPacked.e_icmp.seq=0;
IcmpPacked.e_icmp.Crc=CreateIcmpCrc1(IcmpPacked);
SendPingData.length=36;
SendPingData.STPTR=NULL;
SendPingData.DAPTR=( uint8 * )&IcmpPacked;
Send_Ip_Frame (
&SendPingData,
de_ip,
so_ip,
1
);
}
void Ping_Precess(uint8 * de_ip,uint8 * so_ip)
{
struct _pkst SendPingData;
union icmp_rc IcmpPacked;
static uint16 temp_findex=0x2345;
IcmpPacked.e_icmp.type=0x08;
IcmpPacked.e_icmp.option=0;
IcmpPacked.e_icmp.Crc=0;
#ifdef Big_End
IcmpPacked.e_icmp.id=0x0300;
#endif
#ifdef Little_End
IcmpPacked.e_icmp.id=0x0003;
#endif
IcmpPacked.e_icmp.seq=temp_findex++;
IcmpPacked.e_icmp.Crc=CreateIcmpCrc(IcmpPacked);
SendPingData.length=40;
SendPingData.STPTR=NULL;
SendPingData.DAPTR=( uint8 * )&IcmpPacked;
Send_Ip_Frame (
&SendPingData,
de_ip,
so_ip,
1
);
}
6.Ethernet.C
#define ETHERNET_GLOBALS
#include "config.h"
#include "../include/cfg_net.h"
uint8 My_Ip_1ID[4]=MY_IP_1ID;
uint8 My_Gateway_1ID[4]=MY_GATEWAY_1ID;
uint8 MY_IP_MARK_1ID[4]=IP_MARK_1ID;
uint8 MY_MAC_1ID[6]=My_Mac_1ID;//MAC地址
#if (MAX_NET_PORT>=2)
uint8 My_Ip_2ID[4]=MY_IP_2ID;
uint8 My_Gateway_2ID[4]=MY_GATEWAY_2ID;
uint8 MY_IP_MARK_2ID[4]=IP_MARK_2ID;
uint8 MY_MAC_2ID[6]=My_Mac_2ID;//MAC地址
#endif
uint8 Send_ethernet_Frame (
struct _pkst *TxdData,//结构指针
uint8 * de_mac, //对方的MAC地址指针
uint8 PROTOCOL, //IP协议或ARP协议
uint8 num
)
{ //如果没有MAC地址,就发送一个ARP包。得到MAC地址再发。
ipethernet ethernet_head; //ETHERNET处理缓存区
struct _pkst PKethernet;
ethernet_head.DestMacId[0]=*de_mac; //设置对方MAC
de_mac++;
ethernet_head.DestMacId[1]=*de_mac;
de_mac++;
ethernet_head.DestMacId[2]=*de_mac;
de_mac++;
ethernet_head.DestMacId[3]=*de_mac;
de_mac++;
ethernet_head.DestMacId[4]=*de_mac;
de_mac++;
ethernet_head.DestMacId[5]=*de_mac;
ethernet_head.SourceMacId[0]=NetPort[num].My_Mac[0];//设置本机MAC地址
ethernet_head.SourceMacId[1]=NetPort[num].My_Mac[1];
ethernet_head.SourceMacId[2]=NetPort[num].My_Mac[2];
ethernet_head.SourceMacId[3]=NetPort[num].My_Mac[3];
ethernet_head.SourceMacId[4]=NetPort[num].My_Mac[4];
ethernet_head.SourceMacId[5]=NetPort[num].My_Mac[5];
#ifdef Little_End
if(PROTOCOL==IP_PACKED)//如果是IP包,就设为0X0800
ethernet_head.NextProtocal=0X0008;
else if(PROTOCOL==ARP_PACKED)//如果是ARP包,就设为0X0806
ethernet_head.NextProtocal=0X0608;//0X0806;
#endif
#ifdef Big_End
if(PROTOCOL==IP_PACKED)//如果是IP包,就设为0X0800
ethernet_head.NextProtocal=0X0800;
else if(PROTOCOL==ARP_PACKED)//如果是ARP包,就设为0X0806
ethernet_head.NextProtocal=0X0806;//0X0806;
#endif
PKethernet.STPTR=TxdData;//指向前一个结构数组
PKethernet.length=14; //ETHERNET报头的长度
PKethernet.DAPTR=(uint8 EX_RAM *)ðernet_head;//ETHERNET报头的指针
OS_ENTER_CRITICAL();//保护
switch(num)
{
case 0:
Send_Packet(&PKethernet);//发送该帧
break;
case 1:
break;
case 2:
break;
default:
break;
}
OS_EXIT_CRITICAL();//恢复
return(1);
}
uint8 Send_Ip_To_LLC(struct _pkst *TxdData,uint8 * de_ip,uint8 num)
{
uint8 i;
//如果该包在
if((de_ip[0]&NetPort[num].My_Ip_Mark[0])==(NetPort[num].My_Ip[0]&NetPort[num].My_Ip_Mark[0]))
if((de_ip[1]&NetPort[num].My_Ip_Mark[1])==(NetPort[num].My_Ip[1]&NetPort[num].My_Ip_Mark[1]))
if((de_ip[2]&NetPort[num].My_Ip_Mark[2])==(NetPort[num].My_Ip[2]&NetPort[num].My_Ip_Mark[2]))
if((de_ip[3]&NetPort[num].My_Ip_Mark[3])==(NetPort[num].My_Ip[3]&NetPort[num].My_Ip_Mark[3]))
{
i=0;//查找一次MAC表。
do
{
OS_ENTER_CRITICAL();//保护
if(NetPort[num].ARP_TERM[i].IP_NUM[2]==((IP_NUMBER*)de_ip)->IP[2])
if(NetPort[num].ARP_TERM[i].IP_NUM[3]==((IP_NUMBER*)de_ip)->IP[3])//如果ARP表的最后两个数相等而且TTL>0表示,有对应的MAC
if(NetPort[num].ARP_TERM[i].TTL>0)
{
NetPort[num].ARP_TERM[i].TTL=100;//发送
Send_ethernet_Frame(TxdData,NetPort[num].ARP_TERM[i].MAC_NUM,IP_PACKED,num);
OS_EXIT_CRITICAL();//保护
return(1);
}
OS_EXIT_CRITICAL();//保护
i++;
}
while(i<MAX_ARP_TERM);//如果arp表查完了还没有,就退出
Arp_Request(de_ip,num);//请求对方MAC
return(0);
}
Send_ethernet_Frame(TxdData,NetPort[num].My_Gateway_Mac,IP_PACKED,num);
return(1);
}
uint8 Rec_Ethernet_Packed(uint8 * RecData,uint8 num)
{
uint8 i;
#ifdef Big_End
if(((ipethernet*)RecData)->NextProtocal==0x0800)//可以减少对数据缓冲取的利用
#endif
#ifdef Little_End
if(((ipethernet*)RecData)->NextProtocal==0x0008)//可以减少对数据缓冲取的利用
#endif
{
RecData=RecData+14;//指向IP包头
//检查是否属于本IP段,否则不保存到ARP表种小?
if((((eip*)RecData)->SourceIp[0]&NetPort[num].My_Ip_Mark[0])==(NetPort[num].My_Ip[0]&NetPort[num].My_Ip_Mark[0]))
if((((eip*)RecData)->SourceIp[1]&NetPort[num].My_Ip_Mark[1])==(NetPort[num].My_Ip[1]&NetPort[num].My_Ip_Mark[1]))
if((((eip*)RecData)->SourceIp[2]&NetPort[num].My_Ip_Mark[2])==(NetPort[num].My_Ip[2]&NetPort[num].My_Ip_Mark[2]))
if((((eip*)RecData)->SourceIp[3]&NetPort[num].My_Ip_Mark[3])==(NetPort[num].My_Ip[3]&NetPort[num].My_Ip_Mark[3]))
{
for(i=0;i<MAX_ARP_TERM;i++)
{//同一网段内最多65535台节点
if(NetPort[num].ARP_TERM[i].IP_NUM[2]==((eip*)RecData)->SourceIp[2])
if(NetPort[num].ARP_TERM[i].IP_NUM[3]==((eip*)RecData)->SourceIp[3])
if(NetPort[num].ARP_TERM[i].TTL>0)//如果已经保存有对应的MAC地址而且TTL有效
{//刷新TTL,处理IP包
NetPort[num].ARP_TERM[i].TTL=100;
IP_PROCESS(RecData,num);
return(2);
}
}
//ARP表没有对应的MAC地址
for(i=0;i<MAX_ARP_TERM;i++)
{
if(NetPort[num].ARP_TERM[i].TTL==0)//如果该ARP表项无效
{
NetPort[num].ARP_TERM[i].IP_NUM[0]=((eip*)RecData)->SourceIp[0];
NetPort[num].ARP_TERM[i].IP_NUM[1]=((eip*)RecData)->SourceIp[1];
NetPort[num].ARP_TERM[i].IP_NUM[2]=((eip*)RecData)->SourceIp[2];
NetPort[num].ARP_TERM[i].IP_NUM[3]=((eip*)RecData)->SourceIp[3];
RecData=RecData-14;
NetPort[num].ARP_TERM[i].MAC_NUM[0]=((ipethernet*)RecData)->SourceMacId[0];
NetPort[num].ARP_TERM[i].MAC_NUM[1]=((ipethernet*)RecData)->SourceMacId[1];
NetPort[num].ARP_TERM[i].MAC_NUM[2]=((ipethernet*)RecData)->SourceMacId[2];
NetPort[num].ARP_TERM[i].MAC_NUM[3]=((ipethernet*)RecData)->SourceMacId[3];
NetPort[num].ARP_TERM[i].MAC_NUM[4]=((ipethernet*)RecData)->SourceMacId[4];
NetPort[num].ARP_TERM[i].MAC_NUM[5]=((ipethernet*)RecData)->SourceMacId[5];
NetPort[num].ARP_TERM[i].TTL=100;
RecData=RecData+14;//保存其IP和对应的MAC地址
IP_PROCESS(RecData,num);
return(2);
}
}
//MAC表溢出!!
IP_PROCESS(RecData,num);
return(4);
}//if((((eip*)TEMP)->
//非本地IP
IP_PROCESS(RecData,num);
return(3);
}//if(((ipethernet*)
#ifdef Big_End
else if(((ipethernet*)RecData)->NextProtocal==0x0806)//可以减少对数据缓冲取的利用
#endif
#ifdef Little_End
else if(((ipethernet*)RecData)->NextProtocal==0x0608)//可以减少对数据缓冲取的利用
#endif
{
RecData=RecData+14;
PROCESS_ARP_REC(RecData,num);
return(1);
}
return(3);
}
void SetNetPort(uint8 * mcu_ip,uint8* mcu_gateway,uint8 * mcu_mark)
{
uint8 i;
for(i=0;i<4;i++)
{
NetPort[0].My_Ip[i]=mcu_ip[i];
NetPort[0].My_Gateway[i]=mcu_gateway[i];
NetPort[0].My_Ip_Mark[i]=mcu_mark[i];
#if MAX_NET_PORT>=2
NetPort[1].My_Ip[i]=My_Ip_2ID[i];
NetPort[1].My_Gateway[i]=My_Gateway_2ID[i];
NetPort[1].My_Ip_Mark[i]=MY_IP_MARK_2ID[i];
#endif
}
for(i=0;i<6;i++)
{
NetPort[0].My_Mac[i]=MY_MAC_1ID[i];
#if MAX_NET_PORT>=2
NetPort[1].My_Mac[i]=MY_MAC_2ID[i];
#endif
}
}
7.Hardware.C
#define HARDWARE_GLOBALS
#include "config.h"
#include "../include/cfg_net.h"
#include "LPC24XX_ADD_R.h"
union REC_BUFF_UNION REC_BUFF[MAX_REC_BUFF];
#define PHILIPS_EMAC_MODULE_ID ((0x3902 << 16) | 0x2000)
uint32 tempreg1,tempreg;
uint16 PHYREG[80];
uint16 PHYID;
uint16 EINTSTA;
uint8 LINKSTATUS;
volatile uint32 RXOverrunCount = 0;
volatile uint32 RXErrorCount = 0;
volatile uint32 TXUnderrunCount = 0;
volatile uint32 TXErrorCount = 0;
volatile uint32 RxFinishedCount = 0;
volatile uint32 TxFinishedCount = 0;
volatile uint32 TxDoneCount = 0;
volatile uint32 RxDoneCount = 0;
volatile uint32 CurrentRxPtr = EMAC_RX_BUFFER_ADDR;
volatile uint32 ReceiveLength = 0;
volatile uint32 PacketReceived = FALSE;
uint16 EthernetPHYRead(uint8 paddr,uint8 raddr)
{
uint16 temp16;
rMCMD = 1; //enable read
rMADR = ((paddr&0X1F) <<8 ) + (raddr&0X1F);
while(rMIND & 0X01);
rMCMD = 0;
temp16 = rMRDD;
return temp16;
}
void EMAC_TxEnable( void )
{
MAC_COMMAND |= 0x02;
return;
}
void EMAC_TxDisable( void )
{
MAC_COMMAND &= ~0x02;
return;
}
void EMAC_RxEnable( void )
{
MAC_COMMAND |= 0x01;
MAC_MAC1 |= 0x01;
return;
}
void EMAC_RxDisable( void )
{
MAC_COMMAND &= ~0x01;
MAC_MAC1 &= ~0x01;
return;
}
void WritePHY( uint32 PHYReg, uint32 PHYData )
{
MAC_MCMD = 0x0000;
MAC_MADR = 0X0300 | PHYReg;
MAC_MWTD = PHYData;
while ( MAC_MIND != 0 );
return;
}
void Write_PHY (uint16 phyadd,int PhyReg, int Value)
{
unsigned int tout;
MAC_MADR = (phyadd<<8) | PhyReg;
MAC_MWTD = Value;
tout = 0;
for (tout = 0; tout < 50000; tout++) {
if ((MAC_MIND & 1) == 0) {
break;
}
}
}
uint32 ReadPHY( uint16 phyadd,uint32 PHYReg )
{
uint32 i32;
MAC_MCMD = 0x0001;
i32 = (phyadd<<8) | PHYReg;
MAC_MADR = i32;
while ( MAC_MIND != 0 );
MAC_MCMD = 0x0000;
return( MAC_MRDD );
}
unsigned short Read_PHY ( uint16 phyadd ,unsigned char PhyReg)
{
unsigned int tout;
MAC_MADR = (phyadd<<8) | PhyReg;
MAC_MCMD = 1;
for (tout = 0; tout < 50000; tout++) {
if ((MAC_MIND & 1) == 0) {
break;
}
}
MAC_MCMD = 0;
return (MAC_MRDD);
}
void EMACTxDescriptorInit( void )
{
uint32 i;
uint32 *tx_desc_addr, *tx_status_addr;
MAC_TXDESCRIPTOR = TX_DESCRIPTOR_ADDR;
MAC_TXSTATUS = TX_STATUS_ADDR;
MAC_TXDESCRIPTORNUM = EMAC_TX_DESCRIPTOR_COUNT - 1;
for ( i = 0; i < EMAC_TX_DESCRIPTOR_COUNT; i++ )
{
tx_desc_addr = (uint32 *)(TX_DESCRIPTOR_ADDR + i * 8);
*tx_desc_addr = (uint32)(EMAC_TX_BUFFER_ADDR + i * EMAC_BLOCK_SIZE);
*(tx_desc_addr+1) = (uint32)(EMAC_TX_DESC_INT | (EMAC_BLOCK_SIZE - 1));
}
for ( i = 0; i < EMAC_TX_DESCRIPTOR_COUNT; i++ )
{
tx_status_addr = (uint32 *)(TX_STATUS_ADDR + i * 4);
*tx_status_addr = (uint32)0;
}
MAC_TXPRODUCEINDEX = 0x0;
return;
}
void EMACRxDescriptorInit( void )
{
uint32 i;
uint32 *rx_desc_addr, *rx_status_addr;
MAC_RXDESCRIPTOR = RX_DESCRIPTOR_ADDR;
MAC_RXSTATUS = RX_STATUS_ADDR;
MAC_RXDESCRIPTORNUM = EMAC_RX_DESCRIPTOR_COUNT - 1;
for ( i = 0; i < EMAC_RX_DESCRIPTOR_COUNT; i++ )
{
rx_desc_addr = (uint32 *)(RX_DESCRIPTOR_ADDR + i * 8);
*rx_desc_addr = (uint32)(EMAC_RX_BUFFER_ADDR + i * EMAC_BLOCK_SIZE);
*(rx_desc_addr+1) = (uint32)(EMAC_RX_DESC_INT | (EMAC_BLOCK_SIZE - 1));
}
for ( i = 0; i < EMAC_RX_DESCRIPTOR_COUNT; i++ )
{
rx_status_addr = (uint32 *)(RX_STATUS_ADDR + i * 8);
*rx_status_addr = (uint32)0;
*(rx_status_addr+1) = (uint32)0;
}
MAC_RXCONSUMEINDEX = 0x0;
return;
}
void SetMacID(uint8 * mac_ptr)
{
MAC_SA0 = mac_ptr[0]*256+mac_ptr[1];
MAC_SA1 = mac_ptr[2]*256+mac_ptr[3];
MAC_SA2 = mac_ptr[4]*256+mac_ptr[5];
//把MAC地址写入MY——MAC——ID中
}
uint32 EMACSend( uint32 *EMACBuf, uint32 length )
{
uint32 *tx_desc_addr;
uint32 TxProduceIndex;
uint32 TxConsumeIndex;
uint32 i, templen;
TxProduceIndex = MAC_TXPRODUCEINDEX;
TxConsumeIndex = MAC_TXCONSUMEINDEX;
if ( TxConsumeIndex != TxProduceIndex )
{
return ( FALSE );
}
if ( TxProduceIndex == EMAC_TX_DESCRIPTOR_COUNT )
{
MAC_TXPRODUCEINDEX = 0;
}
if ( length > EMAC_BLOCK_SIZE )
{
templen = length;
for ( i = 0; (uint32)(length/EMAC_BLOCK_SIZE) + 1; i++ )
{
templen = length - EMAC_BLOCK_SIZE;
tx_desc_addr = (uint32 *)(TX_DESCRIPTOR_ADDR + TxProduceIndex * 8);
if ( templen % EMAC_BLOCK_SIZE )
{
*tx_desc_addr = (uint32)(EMACBuf + i * EMAC_BLOCK_SIZE);
*(tx_desc_addr+1) = (uint32)(EMAC_TX_DESC_INT | (EMAC_BLOCK_SIZE - 1));
TxProduceIndex++;
if ( TxProduceIndex == EMAC_TX_DESCRIPTOR_COUNT )
{
TxProduceIndex = 0;
}
MAC_TXPRODUCEINDEX = TxProduceIndex;
}
else
{
*tx_desc_addr = (uint32)(EMACBuf + i * EMAC_BLOCK_SIZE);
*(tx_desc_addr+1) = (uint32)(EMAC_TX_DESC_INT | EMAC_TX_DESC_LAST | (templen -1) );
TxProduceIndex++;
if ( TxProduceIndex == EMAC_TX_DESCRIPTOR_COUNT )
{
TxProduceIndex = 0;
}
MAC_TXPRODUCEINDEX = TxProduceIndex;
break;
}
}
}
else
{
tx_desc_addr = (uint32 *)(TX_DESCRIPTOR_ADDR + TxProduceIndex * 8);
*tx_desc_addr = (uint32)(EMACBuf);
*(tx_desc_addr+1) = (uint32)(EMAC_TX_DESC_INT | EMAC_TX_DESC_LAST | EMAC_TX_DESC_PAD| (length -1));
TxProduceIndex++;
if ( TxProduceIndex == EMAC_TX_DESCRIPTOR_COUNT )
{
TxProduceIndex = 0;
}
MAC_TXPRODUCEINDEX = TxProduceIndex;
}
return ( TRUE );
}
void Send_Packet(struct _pkst *TxdData)//
{
struct _pkst *ExPtr;//
uint8 *TEPTR;
uint8 *rxptr;
uint16 ii,length=0,jj;
uint32 intstat;
length=length+TxdData->length;
ExPtr=TxdData->STPTR;
while(ExPtr!=NULL)//计算出要发送的数据的总长度
{
length=length+ExPtr->length;
ExPtr=ExPtr->STPTR;
}
ii=length;
rxptr = (uint8 *)EMAC_TX_BUFFER_ADDR;
TEPTR=TxdData->DAPTR;
for(ii=0;ii<TxdData->length;ii++)
{
*rxptr = *TEPTR;
rxptr++;
TEPTR++;
}
ExPtr=TxdData->STPTR;
while(ExPtr!=NULL)
{
TEPTR=ExPtr->DAPTR;
for(ii=0;ii<ExPtr->length;ii++)
{
*rxptr = *TEPTR;
rxptr++;
TEPTR++;
}
ExPtr=ExPtr->STPTR;
}
//如果少于60
ii=length;
if(length<60)
{
//如果数据长度<60字节,设置长度为60字节
ii=60;
}
rxptr = (uint8 *)EMAC_TX_BUFFER_ADDR;
EMACSend((uint32 *)rxptr,ii);
//重发数据的处理
for(length=0;length<6;length++) //最多重发6次
{
for(jj=0;jj<1000;jj++)
{//检查CR寄存器的txp位是否为低,为1说明正在发送,为0说明发完或出错放弃
intstat = MAC_INTSTATUS;
intstat |= EINTSTA;
if ( intstat & EMAC_INT_TXDONE )
{
TxDoneCount++;
break;
}
}
if(intstat & EMAC_INT_TXDONE)//表示发送成功,判断发送状态寄存器TSR,决定是否出错
{
MAC_INTCLEAR = 0x0f;//EMAC_INT_TXDONE;
EINTSTA &= 0xf0;
break;
}
EMACSend((uint32 *)rxptr,ii); //to sendpacket;
}
//OS_EXIT_CRITICAL();
}
uint32 EMACReceiveFractions( uint32 StartIndex, uint32 EndIndex, uint32 * dataptr32 )
{
uint32 i, RxLength = 0;
uint32 RxSize;
uint32 *rx_status_addr,*rx_desc_addr,*rx_data_addr;
for ( i = StartIndex; i < EndIndex; i++ )
{
rx_status_addr = (uint32 *)(RX_STATUS_ADDR + StartIndex * 8);
rx_desc_addr = (uint32 *)(RX_DESCRIPTOR_ADDR + StartIndex * 8);
rx_data_addr = (uint32 *)(*rx_desc_addr);
RxSize = (*rx_status_addr & DESC_SIZE_MASK) - 1;
for(i=0;i<(RxSize+3)/4;i++)
dataptr32[i]=*rx_data_addr++;
CurrentRxPtr += EMAC_BLOCK_SIZE;
StartIndex++;
if ( *rx_status_addr & RX_DESC_STATUS_LAST )
{
MAC_RXCONSUMEINDEX = StartIndex;
RxLength += RxSize;
break;
}
else
{
MAC_RXCONSUMEINDEX = StartIndex;
while ( (*rx_status_addr & DESC_SIZE_MASK) != (EMAC_BLOCK_SIZE - 1));
RxLength += RxSize;
}
}
return( RxLength );
}
void InitNic(uint8 num);
void EMACHandler (uint32 IntStatus)
{
//IENABLE;
IntStatus = MAC_INTSTATUS;
IntStatus |= EINTSTA;
if ( IntStatus != 0 )
{
if ( IntStatus & EMAC_INT_RXOVERRUN )
{
MAC_INTCLEAR = EMAC_INT_RXOVERRUN;
RXOverrunCount++;
//InitNic(0);
EMACRxDescriptorInit();
//IDISABLE;
//VICVectAddr = 0;
return;
}
if ( IntStatus & EMAC_INT_RXERROR )
{
MAC_INTCLEAR = EMAC_INT_RXERROR;
RXErrorCount++;
//InitNic(0);
//IDISABLE;
//VICVectAddr = 0;
return;
}
if ( IntStatus & EMAC_INT_RXFINISHED )
{
MAC_INTCLEAR = EMAC_INT_RXFINISHED;
RxFinishedCount++;
//while ( MAC_RXPRODUCEINDEX != (MAC_RXCONSUMEINDEX - 1) );
}
if ( IntStatus & EMAC_INT_TXUNDERRUN )
{
MAC_INTCLEAR = EMAC_INT_TXUNDERRUN;
TXUnderrunCount++;
//IDISABLE;
//VICVectAddr = 0;
return;
}
if ( IntStatus & EMAC_INT_TXERROR )
{
MAC_INTCLEAR = EMAC_INT_TXERROR;
TXErrorCount++;
//IDISABLE;
//VICVectAddr = 0;
return;
}
if ( IntStatus & EMAC_INT_TXFINISHED )
{
MAC_INTCLEAR = EMAC_INT_TXFINISHED;
TxFinishedCount++;
}
EINTSTA &= 0x88;
}
//IDISABLE;
//VICVectAddr = 0;
return;
}
uint8 Rec_Packet()
{
static uint8 REC_BUFF_NUM=0;
uint32 * REC_BUFF_PTR_DWORDS;
uint8 * REC_BUFF_PTR_BYTES;
uint32 RxProduceIndex, RxConsumeIndex;
uint32 RxLength = 0;
uint32 Counter = 0;
uint32 IntStatus;
uint16 templink;
templink = Read_PHY(PHYID ,1 );
if( (templink & 0x0004) ==0 )
{
LINKSTATUS = 0;
OSTimeDly(4);
templink = Read_PHY(PHYID ,1 );
if( templink & 0x0004 )
{
InitNic(0);
}
return 0;
}
if(LINKSTATUS == 0)
InitNic(0);
OS_ENTER_CRITICAL();
rea1:
IntStatus = MAC_INTSTATUS;
IntStatus |= EINTSTA;
if ( IntStatus & EMAC_INT_RXDONE )
{
MAC_INTCLEAR = EMAC_INT_RXDONE;
EINTSTA &= (~EMAC_INT_RXDONE);
RxDoneCount++;
}
else
{
EMACHandler(IntStatus);
OS_EXIT_CRITICAL();
return 0;
}
RxProduceIndex = MAC_RXPRODUCEINDEX;
RxConsumeIndex = MAC_RXCONSUMEINDEX;
if ( RxProduceIndex == EMAC_RX_DESCRIPTOR_COUNT )
{
MAC_RXPRODUCEINDEX = 0;
CurrentRxPtr = EMAC_RX_BUFFER_ADDR;
}
//此时表示有新的数据包在缓冲区里
if ( RxProduceIndex != RxConsumeIndex )
{ //在任何操作都最好返回page0
if(REC_BUFF_NUM==MAX_REC_BUFF)//接收缓冲区号清零
{
REC_BUFF_NUM=0;
}
REC_BUFF_PTR_DWORDS=REC_BUFF[REC_BUFF_NUM].Dwords;//设定接收缓冲区的起始地址
//=======================================
if ( RxProduceIndex < RxConsumeIndex ) //翻转了
{
RxLength += EMACReceiveFractions( RxConsumeIndex, EMAC_RX_DESCRIPTOR_COUNT, REC_BUFF_PTR_DWORDS );
Counter++;
PacketReceived = TRUE;
if ( RxProduceIndex > 0 )
{
RxLength += EMACReceiveFractions( 0, RxProduceIndex, REC_BUFF_PTR_DWORDS );
Counter++;
}
}
else //正常顺序
{
RxLength += EMACReceiveFractions( RxConsumeIndex, RxProduceIndex, REC_BUFF_PTR_DWORDS );
Counter++;
}
REC_BUFF_PTR_BYTES = REC_BUFF[REC_BUFF_NUM].bytes;
#ifdef Little_End
if((((ipethernet*)REC_BUFF_PTR_BYTES)->NextProtocal==0x0008)//可以减少对数据缓冲取的利用
||(((ipethernet*)REC_BUFF_PTR_BYTES)->NextProtocal==0x0608))//不过如果缓冲区足够大就最好不要
#endif
#ifdef Big_End
if((((ipethernet*)REC_BUFF_PTR_BYTES)->NextProtocal==0x0800)//可以减少对数据缓冲取的利用
||(((ipethernet*)REC_BUFF_PTR_BYTES)->NextProtocal==0x0806))//不过如果缓冲区足够大就最好不要
#endif
{
REC_BUFF_NUM++;
OS_EXIT_CRITICAL();
Rec_Ethernet_Packed(REC_BUFF_PTR_BYTES,0);
OS_ENTER_CRITICAL();
goto rea1;
//可以直接退出。
}
else
goto rea1;
}//end of if(bnry!=curr)
OS_EXIT_CRITICAL();
return(0);
}
uint32 intcnt;
void Ethernet_Exception(void)
{
extern OS_EVENT *RecPackedFlag;
OS_ENTER_CRITICAL();
OSSemPost(RecPackedFlag);
EINTSTA |= MAC_INTSTATUS;
MAC_INTCLEAR = 0XFF;
intcnt++;
OS_EXIT_CRITICAL();
VICVectAddr = 0; //interrupt close 通知中断控制器中断结束
}
uint16 dulxp,speed;
void InitNic(uint8 num)
{
uint32 i;
PCONP |= 0X40000000;
// PINSEL2 &= 0x0fc0ccf0;
//PINSEL2 |= 0X50151105; //PINSEL2 = 0x50151105;
//PINSEL3 &= 0xfffffff0;
//PINSEL3 |= 0X00000005; //PINSEL3 = 0x00000005;
i = rMAC_MODULEID;
if(i == OLD_EMAC_MODULE_ID)
PINSEL2 |= (1)|(1<<2)|(1<<8)|(1<<16)|(1<<18)|(1<<20)|(1<<28)|(1<<30);
else
PINSEL2 |= (1)|(1<<2)|(1<<8)|(1<<16)|(1<<18)|(1<<20)|(1<<28)|(1<<30);
PINSEL3 |= 0x00000005;
MAC_MAC1 = 0xCF00;
MAC_COMMAND = 0x0038;
for ( i = 0; i < 0x40; i++ );
MAC_MAC1 = 0x0;
EMAC_TxDisable();
EMAC_RxDisable();
MAC_MAC2 = 0x00;
MAC_IPGR = 0x0012;
MAC_CLRT = 0x370F;
MAC_MAXF = 0x0600;
///rECOMMAND |= (1<<9);
// rMCFG |= (7<<2) + 1; //clk div 28,address increment
//MAC_MCFG = 0x801d;
//MAC_MCFG |= 0x0019;
MAC_MCFG |= 0x0018;
//MAC_COMMAND |= 0x0200;
MAC_COMMAND = 0x0240;
//MAC_SUPP = 0x0900;
//for ( i = 0; i < 0x20; i++ );
//MAC_SUPP = 0x0100;
MAC_SUPP = 0x0000;
// probe phy address
for(i=0;i<32;i++)
{
PHYID = Read_PHY(i , 2 );
if(PHYID == 0X0022)
break;
}
if(i >= 32)
while(1);
PHYID = i;
/// PHY RESET
Write_PHY(PHYID, 0, 0x9200 );
do
{
OSTimeDly(OS_TICKS_PER_SEC);
tempreg = Read_PHY(PHYID, 0x1f );
if(tempreg&0x001c)
break;
}while(1);
OSTimeDly(OS_TICKS_PER_SEC);
tempreg = Read_PHY(PHYID, 0x1f );
//for(i=0;i<32;i++)
// PHYREG[i] = Read_PHY(PHYID ,i );
tempreg &= 0x001c;
SetMacID(NetPort[num].My_Mac);
if(tempreg == 0x0018)//100fdx
{
dulxp = 1;
speed = 100;
MAC_MAC2 = 0x31;
MAC_SUPP |= 0x0100;
MAC_COMMAND |= 0x0640;
MAC_IPGT = 0x0015;
}
else if(tempreg == 0x0008)//100hdx
{
dulxp = 0;
speed = 100;
MAC_MAC2 = 0x30;
MAC_SUPP |= 0x0100;
MAC_COMMAND |= 0x0240;
MAC_IPGT = 0x0012;
}
else if(tempreg == 0x0014)//10fdx
{
dulxp = 1;
speed = 10;
MAC_MAC2 = 0x31;
MAC_SUPP = 0;
MAC_COMMAND |= 0x0640;
MAC_IPGT = 0x0015;
}
else if(tempreg == 0x0004)//10hdx
{
dulxp = 0;
speed = 10;
MAC_MAC2 = 0x30;
MAC_SUPP = 0;
MAC_COMMAND |= 0x0240;
MAC_IPGT = 0x0012;
}
EMACTxDescriptorInit();
EMACRxDescriptorInit();
MAC_MAC1 |= 0x0002;
MAC_RXFILTERCTRL = 0x0022;
MAC_RXFILTERCTRL |= 0x0005;//MULTICAST_UNICAST
MAC_RXFILTERCTRL |= 0x0018;//ENABLE_HASH
MAC_INTCLEAR = 0xFFFF;
//if ( install_irq( EMAC_INT, (void *)EMACHandler, HIGHEST_PRIORITY ) == FALSE )
//{
// return (FALSE);
//}
EMAC_RxEnable();
EMAC_TxEnable();
LINKSTATUS = 1;
EINTSTA = 0;
SetVICIRQ(21, 5, (unsigned int)Ethernet_Exception);
MAC_INTENABLE = 0x000c;
}
uint16 swap_int16(uint16 temp)
{uint16 temp1;
temp1=(temp&0xff00)>>8;
temp=(temp&0x00ff)<<8;
return(temp+temp1);
}
uint32 swap_int32(uint32 temp)
{
union w temp232,temp132;
temp232.dwords=temp;
temp132.bytes.byte0=temp232.bytes.byte3;
temp132.bytes.byte1=temp232.bytes.byte2;
temp132.bytes.byte2=temp232.bytes.byte1;
temp132.bytes.byte3=temp232.bytes.byte0;
return(temp132.dwords);
}
uint16 Char2ToInt16(uint8 * temp)
{
uint16 temp16;
temp16=((uint16)(*temp))<<8;
temp++;
temp16=temp16+(uint16)(*temp);
return(temp16);
}
uint32 Char4ToInt32(uint8 * temp)
{
uint32 temp32;
temp32=((uint32)(*temp))<<24;
temp++;
temp32=temp32+(((uint32)(*temp))<<16);
temp++;
temp32=temp32+(((uint32)(*temp))<<8);
temp++;
temp32=temp32+(uint32)(*temp);
return(temp32);
}
8.SOCKET.C
#define SOCKET_GLOBALS
#include "config.h"
#include "../include/cfg_net.h"
#define SOCKET_ERROR 0xff
uint8 close(uint8 num)
{
uint8 err,i;
struct _pkst TxdData;
if(TcpStatus[num].State==TCP_STATE_CLOSED)
{
return (1);
}
else if(TcpStatus[num].State==TCP_STATE_LISTEN)
{
Delete_Socket(num);
return (1);
}
else if(TcpStatus[num].State==TCP_STATE_ESTABLISHED)
{
TcpStatus[num].SenPassSeq=TcpStatus[num].SenFutureSeq;
TcpStatus[num].SenPassAck=TcpStatus[num].SenPassAck;
TcpStatus[num].SenFutureSeq=TcpStatus[num].SenPassSeq+1;
TcpStatus[num].RecFutureAck=TcpStatus[num].SenPassSeq+1;
TcpHeadHandle(num ,0x5000+TCP_ACK+TCP_FIN);//headlength 0x20,
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
TxdData.length=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(TxdData.length&0xff00)>>8;;
TcpStatus[num].TcpHeadUint8[17]=TxdData.length&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
else if(TcpStatus[num].State==TCP_STATE_SYN_RCVD)
{
TcpStatus[num].SenPassSeq=TcpStatus[num].SenFutureSeq;
TcpStatus[num].SenPassAck=TcpStatus[num].SenPassAck;
TcpStatus[num].SenFutureSeq=TcpStatus[num].SenPassSeq+1;
TcpStatus[num].RecFutureAck=TcpStatus[num].SenPassSeq+1;
TcpHeadHandle(num ,0x5000+TCP_FIN);//headlength 0x20,
TcpStatus[num].TcpDHeadUint8[2]=0;
TcpStatus[num].TcpDHeadUint8[3]=20;
TxdData.length=CheckSumTcp1(num,32);//12+28
TcpStatus[num].TcpHeadUint8[16]=(TxdData.length&0xff00)>>8;
TcpStatus[num].TcpHeadUint8[17]=TxdData.length&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=20;
TxdData.DAPTR=TcpStatus[num].TcpHeadUint8;
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
else
{
Delete_Socket(num);
return (1);
}
TcpStatus[num].State=TCP_STATE_FIN_WAIT1;
for(i=0;i<5;i++)
{
OSSemPend(AppSemDisc[num],100,&err);
if(err==OS_NO_ERR)
{
Delete_Socket(num);
return (1);
}
else
{
Send_Ip_Frame
(
&TxdData,
TcpStatus[num].Dest_Ip,
TcpStatus[num].My_Ip,
6
);
}
}
Delete_Socket(num);
return(0);
}
uint8 connect(SOCKET * s, struct sockaddr * addr,uint16 addrlen) //
{
struct _pkst TxdData;
uint8 i,j,err;
for(i=0;i<MAX_TCP_LINKS;i++)
{
if(TcpStatus[i].State>=TCP_STATE_LISTEN)
{
if(TcpStatus[i].Dest_Ip[0]==addr->sin_addr[0])
if(TcpStatus[i].Dest_Ip[1]==addr->sin_addr[1])
if(TcpStatus[i].Dest_Ip[2]==addr->sin_addr[2])
if(TcpStatus[i].Dest_Ip[3]==addr->sin_addr[3])
if(TcpStatus[i].My_Ip[0]==s->My_Ip[0])
if(TcpStatus[i].My_Ip[1]==s->My_Ip[1])
if(TcpStatus[i].My_Ip[2]==s->My_Ip[2])
if(TcpStatus[i].My_Ip[3]==s->My_Ip[3])
if(s->My_Port==TcpStatus[i].My_Port)
if(addr->sin_port==TcpStatus[i].Dest_Port)
{
if(TcpStatus[i].State==TCP_STATE_ESTABLISHED)
return (i);
else
close (i);
//Delete_Socket(i);
}
}
}
j=0;
do
{
OS_ENTER_CRITICAL();
for(i=0;i<MAX_TCP_LINKS;i++)
{
if(TcpStatus[i].State==TCP_STATE_CLOSED)
{
TcpStatus[i].State=TCP_STATE_SYN_SENT;
break;
}
}
OS_EXIT_CRITICAL();
if(i>=MAX_TCP_LINKS)
return (MAX_TCP_LINKS);//连接已经满了
TcpStatus[i].My_Port=s->My_Port;
TcpStatus[i].Dest_Port=addr->sin_port;//对方端口
TcpStatus[i].Dest_Ip[0]=addr->sin_addr[0];
TcpStatus[i].Dest_Ip[1]=addr->sin_addr[1];
TcpStatus[i].Dest_Ip[2]=addr->sin_addr[2];
TcpStatus[i].Dest_Ip[3]=addr->sin_addr[3];
TcpStatus[i].My_Ip[0]=s->My_Ip[0];
TcpStatus[i].My_Ip[1]=s->My_Ip[1];
TcpStatus[i].My_Ip[2]=s->My_Ip[2];
TcpStatus[i].My_Ip[3]=s->My_Ip[3];
TcpStatus[i].RecPassSeq=0;
TcpStatus[i].RecPassAck=0;
TcpStatus[i].SenPassSeq=781010;
TcpStatus[i].SenPassAck=0;
TcpStatus[i].RecFutureAck=TcpStatus[i].SenPassSeq+1;
TcpStatus[i].SenFutureSeq=TcpStatus[i].SenPassSeq+1;
TcpStatus[i].My_Wl2=TcpStatus[i].SenPassSeq;
TcpStatus[i].Snd_Window=MAX_TCP_DATA;//通知对方本地最大接收1024字节的包,用于流控
TcpStatus[i].My_Max_Seg_Size=MAX_TCP_DATA;//本地机可以接受最大的以太网数据包
TcpHeadHandle(i ,0x7000+TCP_SYN);
//
TcpStatus[i].TcpHeadUint8[20]=0X02;
TcpStatus[i].TcpHeadUint8[21]=0X04;
TcpStatus[i].TcpHeadUint8[22]=MAX_TCP_DATA/256;
TcpStatus[i].TcpHeadUint8[23]=MAX_TCP_DATA%6;
TcpStatus[i].TcpHeadUint8[24]=0X01;
TcpStatus[i].TcpHeadUint8[25]=0X01;
TcpStatus[i].TcpHeadUint8[26]=0X04;
TcpStatus[i].TcpHeadUint8[27]=0X02;
TcpStatus[i].TcpDHeadUint8[2]=0;
TcpStatus[i].TcpDHeadUint8[3]=28;
TxdData.length=CheckSumTcp1(i,40);//12+28
TcpStatus[i].TcpHeadUint8[16]=(TxdData.length&0xff00)>>8;;
TcpStatus[i].TcpHeadUint8[17]=TxdData.length&0x00ff;
TxdData.STPTR=NULL;
TxdData.length=28;
TxdData.DAPTR=TcpStatus[i].TcpHeadUint8;
TcpStatus[i].StaSem=0x02;
Send_Ip_Frame
(
&TxdData,
TcpStatus[i].Dest_Ip,
TcpStatus[i].My_Ip,
6
);
OSSemPend(AppSemCon[i],100,&err);//挂起,等待SYN_ACK信号的成功接收
if(err==OS_NO_ERR)
{
//TcpStatus[i].State=TCP_STATE_ESTABLISHED;
return(i);
}
if(TcpStatus[i].State==TCP_STATE_SYN_SENT)
if(TcpStatus[i].Dest_Ip[0]==addr->sin_addr[0])
if(TcpStatus[i].Dest_Ip[1]==addr->sin_addr[1])
if(TcpStatus[i].Dest_Ip[2]==addr->sin_addr[2])
if(TcpStatus[i].Dest_Ip[3]==addr->sin_addr[3])
if(TcpStatus[i].My_Ip[0]==s->My_Ip[0])
if(TcpStatus[i].My_Ip[1]==s->My_Ip[1])
if(TcpStatus[i].My_Ip[2]==s->My_Ip[2])
if(TcpStatus[i].My_Ip[3]==s->My_Ip[3])
if(s->My_Port==TcpStatus[i].My_Port)
if(addr->sin_port==TcpStatus[i].Dest_Port)
{
Delete_Socket(i);
}
j++;
}while(j<3);
Delete_Socket(i);
return(MAX_TCP_LINKS);//退出返回连接失败。
}
SOCKET *socket( uint16 af, uint16 type, uint16 protocol)
{
uint8 i,j,k;
static uint16 port=1025;
OS_ENTER_CRITICAL();
for(k=0;k<MAX_SOCKETS;k++)
if(Zlg_Socket[k].State==0)
break;
Zlg_Socket[k].State=0x50;
Zlg_Socket[k].af=af;
Zlg_Socket[k].type=type;
Zlg_Socket[k].protocol=protocol;
i=0;
if(protocol==TCP_PROTOCOL)
{
do
{
if(TcpStatus[i].My_Port==port)
{
port++;
}
i++;
}while(i<MAX_TCP_LINKS);
}
else if(protocol==UDP_PROTOCOL)
{
j=0;
do
{
if(UdpStatus[i].My_Port==port)
{
port++;
}
i++;
}while(i<MAX_UDP_LINKS);
do
{
if(UdpStatus[j].My_Port==0)
{
break;
}
j++;
}while(j<MAX_UDP_LINKS);
if(j>=MAX_UDP_LINKS)
{
OS_EXIT_CRITICAL();
return ((SOCKET * )INVALID_SOCKET);
}
Zlg_Socket[k].Udp_Queue=j;
UdpStatus[j].My_Port=port;
UdpStatus[j].My_Ip[0]=NetPort[0].My_Ip[0];
UdpStatus[j].My_Ip[1]=NetPort[0].My_Ip[1];
UdpStatus[j].My_Ip[2]=NetPort[0].My_Ip[2];
UdpStatus[j].My_Ip[3]=NetPort[0].My_Ip[3];
}
else
{
OS_EXIT_CRITICAL();
return((SOCKET * )INVALID_SOCKET);
}
Zlg_Socket[k].My_Port=port;
port++;
if(port>=5000)
port=1025;
Zlg_Socket[k].My_Ip[0]=NetPort[0].My_Ip[0];
Zlg_Socket[k].My_Ip[1]=NetPort[0].My_Ip[1];
Zlg_Socket[k].My_Ip[2]=NetPort[0].My_Ip[2];
Zlg_Socket[k].My_Ip[3]=NetPort[0].My_Ip[3];
OS_EXIT_CRITICAL();
return (&Zlg_Socket[k]);
}
int bind( SOCKET * s, struct sockaddr * name,uint16 namelen)
{
s->My_Ip[0]=((struct sockaddr *)name)->sin_addr[0];
s->My_Ip[1]=((struct sockaddr *)name)->sin_addr[1];
s->My_Ip[2]=((struct sockaddr *)name)->sin_addr[2];
s->My_Ip[3]=((struct sockaddr *)name)->sin_addr[3];
s->My_Port=((struct sockaddr *)name)->sin_port;
if(s->protocol==UDP_PROTOCOL)
{
UdpStatus[s->Udp_Queue].My_Port=((struct sockaddr *)name)->sin_port;
UdpStatus[s->Udp_Queue].My_Ip[0]=((struct sockaddr *)name)->sin_addr[0];
UdpStatus[s->Udp_Queue].My_Ip[1]=((struct sockaddr *)name)->sin_addr[1];
UdpStatus[s->Udp_Queue].My_Ip[2]=((struct sockaddr *)name)->sin_addr[2];
UdpStatus[s->Udp_Queue].My_Ip[3]=((struct sockaddr *)name)->sin_addr[3];
return 1;
}
return 0;
}
int listen( SOCKET * s, uint16 backlog )
{
uint16 i;
if(backlog>MAX_TCP_LINKS)
return -1;
else if(backlog==0)
return -2;
else if(s->protocol!=0)
return -3;
s->Max_Queue=backlog;
OS_ENTER_CRITICAL();
for(i=0;i<MAX_TCP_LINKS;i++)
{
if(TcpStatus[i].State==TCP_STATE_CLOSED)
{
TcpStatus[i].State=TCP_STATE_LISTEN;
s->queue[s->Max_Queue-backlog]=i;
backlog--;
}
if(backlog==0)
break;//如果刚好backlog=MAX_TCP_LINKS,i=MAX_TCP_LINK-1;
}
if(i>=MAX_TCP_LINKS)
{
if(backlog==s->Max_Queue)
{
OS_EXIT_CRITICAL();
return -4;
}
i=0;
do
{
TcpStatus[s->queue[i]].State=TCP_STATE_CLOSED;
backlog++;
i++;
}
while(backlog!=s->Max_Queue);
memset(&s, 0, sizeof(s));
OS_EXIT_CRITICAL();
return -4;
}
else
{
s->State=1;
for(i=0;i<s->Max_Queue;i++)
{
TcpStatus[s->queue[i]].My_Ip[0]=s->My_Ip[0];
TcpStatus[s->queue[i]].My_Ip[1]=s->My_Ip[1];
TcpStatus[s->queue[i]].My_Ip[2]=s->My_Ip[2];
TcpStatus[s->queue[i]].My_Ip[3]=s->My_Ip[3];
TcpStatus[s->queue[i]].My_Port=s->My_Port;
TcpStatus[s->queue[i]].StaSem=0x82;
}
}
OS_EXIT_CRITICAL();
return (s->Max_Queue);
}
uint8 accept( SOCKET * s, struct sockaddr *addr, int *addrlen )
{
uint16 i;
uint8 err;
uint16 sock_addrlen;
if(s->protocol!=0)
return (0XFF);
if(s->Max_Queue==0)
return (0XFF);
for(i=0;i<s->Max_Queue;i++)
{
if(OSSemAccept(AppSemCon[s->queue[i]]))
{
break;
}
}
if(i>=s->Max_Queue)
{
for(i=0;i<s->Max_Queue;i++)
{
if(TcpStatus[s->queue[i]].State==TCP_STATE_LISTEN)
{
//OSSemPend(AppSemCon[s->queue[i]],0,&err);
OSSemPend(AppSemCon[s->queue[i]],400,&err);
if(err==OS_NO_ERR||err==OS_TIMEOUT)//等
break;
else
return (0XFF);
}
}
if(err==OS_TIMEOUT)
{
for(i=0;i<s->Max_Queue;i++)
{
if(TcpStatus[s->queue[i]].State==TCP_STATE_ESTABLISHED)
break;//连接中有已经建立连接的了
}
if(i>=s->Max_Queue)
{
return (0XFF);//连接已经满了
}
}
}
addr->sin_addr[0]=TcpStatus[s->queue[i]].Dest_Ip[0];
addr->sin_addr[1]=TcpStatus[s->queue[i]].Dest_Ip[0];
addr->sin_addr[2]=TcpStatus[s->queue[i]].Dest_Ip[0];
addr->sin_addr[3]=TcpStatus[s->queue[i]].Dest_Ip[0];
addr->sin_port=TcpStatus[s->queue[i]].Dest_Port;
sock_addrlen=(int)addrlen;
/
Delete_Socket(num);
return (SOCKET_ERROR);
case TCP_STATE_CLOSING:
case TCP_STATE_LAST_ACK:
case TCP_STATE_TIME_WAIT:
Delete_Socket(num);
return (SOCKET_ERROR);
default :
return 0;
}
}