- Đầu tiên ta cộng các từ 16 bit trong header: Code:
Hàm tính checksum cho gói TCP Code:
Code:
//--- ---
//Duoc goi moi giay de kiem tra Time out cho cac phien TCP, // giai phong cac phine TCP bi treo
void TCPCheckTimeOut(){ unsigned char i;
for(i=0; i<TCP_MAX_SESSION; i++){ //decrement ttl:
if ((tcpSessionTable[i].sesState != TCP_STATE_CLOSED) && (tcpSessionTable[i].sesState != TCP_STATE_LISTEN)){
if(tcpSessionTable[i].timeOut)
tcpSessionTable[i].timeOut--;
//if socket TTL count is zero, close this socket! if (tcpSessionTable[i].timeOut == 0){ TCPCloseSession(i); } } } }
Hàm tính checksum cho gói TCPCode: Code:
//--- ---
//Tinh checksum cho goi TCP
unsigned int checksum(unsigned char *buffer, unsigned int len, unsigned long csum32)
{
unsigned int res16 = 0x0000; unsigned char data_hi;
unsigned char data_lo; while(len > 1){
data_hi = *buffer++; data_lo = *buffer++;
res16 = (((unsigned int)data_hi << 8) + data_lo); csum32 = csum32 + res16;
len -=2; }
if(len > 0){
data_hi = *buffer;
res16 = (unsigned int)data_hi<<8; csum32 = csum32 + res16;
}
while(csum32>>16)
csum32 = (csum32 & 0xFFFF)+ (csum32 >> 16);
//csum32 = ((csum32 & 0x0000FFFF)+ ((csum32 & 0xFFFF0000) >> 16)); res16 =~(csum32 & 0x0000FFFF);
return (res16); }