?? tcp.c
字號:
#include "../config.h"
CONNECTION conxn[1];
const uint8 opt[10] = {0x02, 0x04, 0x05, 0xB4,0x01, 0x01,0x04, 0x02};
uint8 just_closed; // Keeps track of when a conxn closed
uint8 initial_sequence_nr[4];
uint8 send_wait_timer,rcv_wait_timer;
static uint8 sender_ipaddr[4];
uint8 sender_tcpport[2];
uint8 plussta;
uint8 comm_state;
extern uint16 nPassword;
extern TCP_SEND tcp_rec;
extern uint8 i2c_buf[256];
void init_netpara(void)
{
while(!I2C_RcvStr(CAT24c02,0,i2c_buf, 30));
if(i2c_buf[29]==0x55)
{
if(i2c_buf[28] & (DEFSEVIP))
{
server_ipaddr[0] = 192;
server_ipaddr[1] = 168;
server_ipaddr[2] = 0;
server_ipaddr[3] = 121;
}
else
{
server_ipaddr[0] = i2c_buf[0];
server_ipaddr[1] = i2c_buf[1];
server_ipaddr[2] = i2c_buf[2];
server_ipaddr[3] = i2c_buf[3];
}
if(i2c_buf[28] & DEFMYIP)
{
my_ipaddr[0] = 192;
my_ipaddr[1] = 168;
my_ipaddr[2] = 0;
my_ipaddr[3] = 176;
}
else
{
my_ipaddr[0] = i2c_buf[4];
my_ipaddr[1] = i2c_buf[5];
my_ipaddr[2] = i2c_buf[6];
my_ipaddr[3] = i2c_buf[7];
}
if(i2c_buf[28] & DEFGATE)
{
gateway_ipaddr[0] =192 ;
gateway_ipaddr[1] =168 ;
gateway_ipaddr[2] =0 ;
gateway_ipaddr[3] =1 ;
}
else
{
gateway_ipaddr[0] =i2c_buf[8] ;
gateway_ipaddr[1] =i2c_buf[9] ;
gateway_ipaddr[2] =i2c_buf[10] ;
gateway_ipaddr[3] =i2c_buf[11] ;
}
if(i2c_buf[28] & DEFSUB)
{
my_subnet[0] = 255;
my_subnet[1] = 255;
my_subnet[2] = 255;
my_subnet[3] = 0;
}
else
{
my_subnet[0] = i2c_buf[12];
my_subnet[1] = i2c_buf[13];
my_subnet[2] = i2c_buf[14];
my_subnet[3] = i2c_buf[15];
}
if(i2c_buf[28] & DEFHWADD)
{
my_mac[0] = 0;
my_mac[1] = 0;
my_mac[2] = 0;
my_mac[3] = 0;
my_mac[4] = 0;
my_mac[5] = 10;
}
else
{
my_mac[0] = i2c_buf[16];
my_mac[1] = i2c_buf[17];
my_mac[2] = i2c_buf[18];
my_mac[3] = i2c_buf[19];
my_mac[4] = i2c_buf[20];
my_mac[5] = i2c_buf[21];
}
if(i2c_buf[28] & DEFSEVPORT)
{
server_port = 2000;
}
else
{
server_port = i2c_buf[22];
server_port |= (i2c_buf[23]<<8);
}
if(i2c_buf[28] & DEFDEVNO)
{
nDeviceNo = 1;
}
else
{
nDeviceNo = i2c_buf[24];
nDeviceNo |= (i2c_buf[25]<<8);
}
if(i2c_buf[28] & DEFPASS)
{
nPassword = 1234;
}
else
{
nPassword = i2c_buf[26];
nPassword |= (i2c_buf[27]<<8);
}
}
else
{
server_ipaddr[0] = 192;
server_ipaddr[1] = 168;
server_ipaddr[2] = 0;
server_ipaddr[3] = 121;
my_ipaddr[0] = 192;
my_ipaddr[1] = 168;
my_ipaddr[2] = 0;
my_ipaddr[3] = 176;
gateway_ipaddr[0] =192 ;
gateway_ipaddr[1] =168 ;
gateway_ipaddr[2] =0 ;
gateway_ipaddr[3] =1 ;
my_subnet[0] = 255;
my_subnet[1] = 255;
my_subnet[2] = 255;
my_subnet[3] = 0;
my_mac[0] = 0;
my_mac[1] = 0;
my_mac[2] = 0;
my_mac[3] = 0;
my_mac[4] = 0;
my_mac[5] = 10;
server_port = 2000;
nDeviceNo = 1;
nPassword = 1234;
}
}
void init_tcp(void)
{
memset( conxn, 0, sizeof(CONNECTION));
just_closed = FALSE;
initial_sequence_nr[0] = 0;
initial_sequence_nr[1] = 0;
initial_sequence_nr[2] = 0;
initial_sequence_nr[3] = 1;
send_wait_timer=0;
rcv_wait_timer = 0;
plussta = 0 ;
time_s = 5;
init_netpara();
}
void tcp_rcve(uint8 * inbuf, uint16 len)
{
// UCHAR idata i, j, nr;
uint16 result, header_len,temp16;
TCP_HEADER * tcp;
IP_HEADER * ip;
uint32 sum,temp32;
uint16 data_len;
tcp = (TCP_HEADER *)(inbuf + 34);
ip = (IP_HEADER *)(inbuf + 14);
sum = (uint32)cksum(inbuf + 26, 8 + len);
sum += (uint32)0x0006;
sum += (uint32)len;
result = (uint32)(sum + (sum >> 16));
if (result != 0xFFFF)
{
return;
}
sender_ipaddr[0] = ip->source_ipaddr[0];
sender_ipaddr[1] = ip->source_ipaddr[1];
sender_ipaddr[2] = ip->source_ipaddr[2];
sender_ipaddr[3] = ip->source_ipaddr[3];
sender_tcpport[0] = tcp->source_port[0];
sender_tcpport[1] = tcp->source_port[1];
if(((ip->source_ipaddr[0] != server_ipaddr[0])||
(ip->source_ipaddr[1] != server_ipaddr[1])||
(ip->source_ipaddr[2] != server_ipaddr[2])||
(ip->source_ipaddr[3] != server_ipaddr[3]))&&
((tcp->source_port[0] != server_port>>8)||
(tcp->source_port[1] != (uint8)server_port)))
return;
/* for (i=0; i < 5; i++)
{
if ((ip->source_ipaddr == conxn[i].ipaddr) &&
(tcp->source_port == conxn[i].port))
{
nr = i;
break;
}
}
if (i == 5)
{
if (tcp->flags & FLG_SYN)
{
for (j=0; j < 5; j++)
{
if (conxn[j].ipaddr == 0)
{
nr = j;
// Initialize new connection
conxn[nr].state = STATE_LISTEN;
break;
}
}
if (j == 5) return;
}
}*/
// if (nr > 4) return;
temp32 = (tcp->sequence[0]<<24)+(tcp->sequence[1]<<16)+(tcp->sequence[2]<<8)+(tcp->sequence[3]);
if (temp32 > 0xFFFFFF00L)
{
conxn[0].ipaddr[0] = 0;
conxn[0].ipaddr[1] = 0;
conxn[0].ipaddr[2] = 0;
conxn[0].ipaddr[3] = 0;
tcp_send(FLG_RST, 20, NO_CONNECTION);
return;
}
temp16 = (tcp->flags[0]<<8)+(tcp->flags[1]);
if (temp16 & FLG_RST)
{
conxn[0].ipaddr[0] = 0;
conxn[0].ipaddr[1] = 0;
conxn[0].ipaddr[2] = 0;
conxn[0].ipaddr[3] = 0;
conxn[0].state = STATE_CLOSED;
return;
}
header_len = (temp16 & 0xF000) >> 10;
data_len = len - header_len;
comm_state = COMM_STATE_RCV;
switch (conxn[0].state)
{
// case STATE_CLOSED:
case STATE_LISTEN: //監(jiān)聽狀態(tài)
if (temp16 & FLG_SYN) //同步報
{
conxn[0].ipaddr[0] = ip->source_ipaddr[0];
conxn[0].ipaddr[1] = ip->source_ipaddr[1];
conxn[0].ipaddr[2] = ip->source_ipaddr[2];
conxn[0].ipaddr[3] = ip->source_ipaddr[3];
conxn[0].port[0] = tcp->source_port[0];
conxn[0].port[1] = tcp->source_port[1];
conxn[0].state = STATE_LISTEN;
temp32 = (tcp->sequence[0]<<24)+(tcp->sequence[1]<<16)+(tcp->sequence[2]<<8)+(tcp->sequence[3]);
temp32++;
conxn[0].his_sequence[0] = temp32>>24;
conxn[0].his_sequence[1] = (temp32 & 0x00ffffff)>>16;
conxn[0].his_sequence[2] = (temp32 & 0x0000ffff)>>8;
conxn[0].his_sequence[3] = (temp32 & 0x000000ff);
conxn[0].his_ack[0] = tcp->ack_number[0];
conxn[0].his_ack[1] = tcp->ack_number[1];
conxn[0].his_ack[2] = tcp->ack_number[2];
conxn[0].his_ack[3] = tcp->ack_number[3];
conxn[0].my_sequence[0] = initial_sequence_nr[0];
conxn[0].my_sequence[1] = initial_sequence_nr[1];
conxn[0].my_sequence[2] = initial_sequence_nr[2];
conxn[0].my_sequence[3] = initial_sequence_nr[3];
temp32 = (initial_sequence_nr[0]<<24)+(initial_sequence_nr[1]<<16)+(initial_sequence_nr[2]<<8)+(initial_sequence_nr[3]);
temp32 += 64000;
initial_sequence_nr[0] = temp32>>24;
initial_sequence_nr[1] = (temp32 & 0x00ffffff)>>16;
initial_sequence_nr[2] = (temp32 & 0x0000ffff)>>8;
initial_sequence_nr[3] = (temp32 & 0x000000ff);
tcp_send(FLG_SYN | FLG_ACK, 28, 0);
temp32 = (conxn[0].my_sequence[0]<<24)+(conxn[0].my_sequence[1]<<16)+(conxn[0].my_sequence[2]<<8)+(conxn[0].my_sequence[3]);
temp32 ++;
conxn[0].my_sequence[0] = temp32>>24;
conxn[0].my_sequence[1] = (temp32 & 0x00ffffff)>>16;
conxn[0].my_sequence[2] = (temp32 & 0x0000ffff)>>8;
conxn[0].my_sequence[3] = (temp32 & 0x000000ff);
conxn[0].state = STATE_SYN_RCVD;
comm_state = COMM_STATE_RCV;
rcv_wait_timer =RCV_WAIT_TIME;
}
break;
case STATE_SYN_RCVD: //同步報收到
temp32 = (conxn[0].his_sequence[0]<<24)+(conxn[0].his_sequence[1]<<16)+(conxn[0].his_sequence[2]<<8)+(conxn[0].his_sequence[3]);
temp32 += data_len;
conxn[0].his_sequence[0] = temp32>>24;
conxn[0].his_sequence[1] = (temp32 & 0x00ffffff)>>16;
conxn[0].his_sequence[2] = (temp32 & 0x0000ffff)>>8;
conxn[0].his_sequence[3] = (temp32 & 0x000000ff);
conxn[0].his_ack[0] = tcp->ack_number[0];
conxn[0].his_ack[1] = tcp->ack_number[1];
conxn[0].his_ack[2] = tcp->ack_number[2];
conxn[0].his_ack[3] = tcp->ack_number[3];
if (temp16 & FLG_SYN) //同步報處理
{
tcp_send(FLG_RST, 20, 0);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -