📄 tcp_util.c
字号:
/* Set and then increment initial segment sequence number. */ /*-------------------------------------------------------------------*/ sock->snd_nxt = Net.ISN; sock->snt_una = sock->max_snt = sock->snd_nxt; Net.ISN += 125000; /*-------------------------------------------------------------------*/ /* Return socket pointer. */ /*-------------------------------------------------------------------*/ TcpAssert(sock->rq_head == NULL); return sock;}/***********************************************************************//* TcpStrip: Free all socket resources but control block and timers *//* *//* Input: sock = pointer to socket control block *//* flag = combination of FREE_RBUF and FREE_SBUF *//* *//* Note: All the socket fields checked below must either have *//* been zeroed or must contain a valid value. *//* *//***********************************************************************/void TcpStrip(SOCKET sock, int flag){ /*-------------------------------------------------------------------*/ /* Delete queued listen sockets if any connections are pending. */ /*-------------------------------------------------------------------*/ while (sock->lq_head) { SOCKET queued = sock->lq_head; sock->lq_head = sock->lq_head->lq_next; SockRelease(queued); /* for TCP/IP stack */ SockRelease(queued); /* for application */ } /*-------------------------------------------------------------------*/ /* If reserving a parent socket, release it now. */ /*-------------------------------------------------------------------*/ if (sock->psock) { SockRelease(sock->psock); sock->psock = NULL; } /*-------------------------------------------------------------------*/ /* Pull buffers off receive queue, clearing their flush flag. */ /*-------------------------------------------------------------------*/ while (sock->rq_head) { sock->rq_head->flush = NULL; TcpAssert(sock->rq_head->seg_record == NULL); sock->rq_head = sock->rq_head->next; } /*-------------------------------------------------------------------*/ /* If requested, free socket send and receive buffers. */ /*-------------------------------------------------------------------*/ if (sock->sbuf && (flag & FREE_SBUF)) tcpRetBuf(&sock->sbuf); if (sock->rbuf && (flag & FREE_RBUF)) { tcpRetBuf(&sock->rbuf); sock->rb_count = 0; } /*-------------------------------------------------------------------*/ /* Free any records on the out-of-order segment queue. */ /*-------------------------------------------------------------------*/ TcpFreeOoQ(sock);}/***********************************************************************//* TcpCheckSum: Compute TCP pseudo-header checksum *//* *//* Input: buf = pointer to network buffer containing TCP segment *//* *//* Returns: 16-bit checksum in network byte order *//* *//***********************************************************************/ui16 TcpCheckSum(const NetBuf *buf){ Ip *ip = (Ip *)buf->ip_pkt; ui32 cksum; ui16 *sptr; /*-------------------------------------------------------------------*/ /* Include source and destination IP addresses. */ /*-------------------------------------------------------------------*/ sptr = (ui16 *)&ip->src_ip; /*lint !e740 */ cksum = *sptr++; cksum += *sptr++; /*lint -save -e415 -e416 -specific(-e415) -specific(-e416) */ cksum += *sptr++; cksum += *sptr++; /*lint -restore */ /*-------------------------------------------------------------------*/ /* Include segment length and protocol value. */ /*-------------------------------------------------------------------*/ cksum += htons(buf->length + IPT_TCP); /*-------------------------------------------------------------------*/ /* Calculate checksum on possibly unaligned TCP data. */ /*-------------------------------------------------------------------*/ return DataChecksum(cksum, buf);}/***********************************************************************//* DataChecksum: Calculate IP checksum on possibly unaligned data *//* *//* Inputs: sum = initial partial sum in host byte order *//* src = pointer to source data *//* *//* Returns: 16-bit ones complement checksum in network byte order *//* *//***********************************************************************/ui16 DataChecksum(ui32 sum, const NetBuf *buf){ ui8 *bp; ui16 *wp; ui16 *end; uint len; /*-------------------------------------------------------------------*/ /* Calculate length of buffer's first data region. */ /*-------------------------------------------------------------------*/ len = buf->length - (buf->app_len + buf->app_len2); /*-------------------------------------------------------------------*/ /* Sum the TCP/UDP header and any contiguous data (always aligned). */ /*-------------------------------------------------------------------*/ { /*-----------------------------------------------------------------*/ /* Sum the 16-bit chunks. */ /*-----------------------------------------------------------------*/ wp = (ui16 *)buf->ip_data; end = wp + (len >> 1); while (wp < end) sum += *wp++; /*-----------------------------------------------------------------*/ /* Add left-over byte, if any. Finish and return since order = 0. */ /*-----------------------------------------------------------------*/ if (len & 1) { sum += htons(*(ui8*)wp << 8); sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); return (ui16)~sum; } } /*-------------------------------------------------------------------*/ /* If second region contains (possibly unaligned) data, sum it also. */ /*-------------------------------------------------------------------*/ len = buf->app_len; if (len) { bp = buf->app_data; /*-----------------------------------------------------------------*/ /* Check if data is aligned on odd byte boundary. */ /*-----------------------------------------------------------------*/ if ((int)bp & 1) { ui32 tmpsum = 0; /*---------------------------------------------------------------*/ /* Add initial unaligned byte by itself and decrement length. */ /*---------------------------------------------------------------*/ tmpsum += htons(*bp); --len; /*---------------------------------------------------------------*/ /* Sum the remaining 16-bit chunks. */ /*---------------------------------------------------------------*/ wp = (ui16 *)(bp + 1); end = wp + (len >> 1); while (wp < end) tmpsum += *wp++; /*---------------------------------------------------------------*/ /* Add left-over byte, if any. */ /*---------------------------------------------------------------*/ if (len & 1) tmpsum += htons(*(ui8*)wp << 8); /*---------------------------------------------------------------*/ /* Convert to 16-bit sum and swap to correct byte order. */ /*---------------------------------------------------------------*/ tmpsum = (tmpsum >> 16) + (tmpsum & 0xFFFF); tmpsum += (tmpsum >> 16); tmpsum = (ui16)((tmpsum << 8) | (ui8)(tmpsum >> 8)); /*---------------------------------------------------------------*/ /* Add partial sum to overall checksum. */ /*---------------------------------------------------------------*/ sum += tmpsum; } /*-----------------------------------------------------------------*/ /* Else data is aligned on even byte boundary. */ /*-----------------------------------------------------------------*/ else { /*---------------------------------------------------------------*/ /* Sum the existing 16-bit chunks. */ /*---------------------------------------------------------------*/ wp = (ui16 *)bp; end = wp + (len >> 1); while (wp < end) sum += *wp++; /*---------------------------------------------------------------*/ /* Add left-over byte, if any. */ /*---------------------------------------------------------------*/ if (len & 1) sum += htons(*(ui8*)wp << 8); } /*-----------------------------------------------------------------*/ /* If third region also contains (always aligned) data, sum it. */ /*-----------------------------------------------------------------*/ len = buf->app_len2; if (len) { ui32 tmpsum = 0; /*---------------------------------------------------------------*/ /* Sum the existing 16-bit chunks. */ /*---------------------------------------------------------------*/ wp = (ui16 *)buf->app_data2; end = wp + (len >> 1); while (wp < end) tmpsum += *wp++; /*---------------------------------------------------------------*/ /* Add left-over byte, if any. */ /*---------------------------------------------------------------*/ if (len & 1) tmpsum += htons(*(ui8*)wp << 8); /*---------------------------------------------------------------*/ /* Check if third region started with odd byte in sum sequence. */ /*---------------------------------------------------------------*/ if (buf->app_len & 1) { /*-------------------------------------------------------------*/ /* Convert to 16-bit sum and swap to correct byte order. */ /*-------------------------------------------------------------*/ tmpsum = (tmpsum >> 16) + (tmpsum & 0xFFFF); tmpsum += (tmpsum >> 16); tmpsum = (ui16)((tmpsum << 8) | (ui8)(tmpsum >> 8)); } /*---------------------------------------------------------------*/ /* Add partial sum to overall checksum. */ /*---------------------------------------------------------------*/ sum += tmpsum; } } /*-------------------------------------------------------------------*/ /* Add remaining carries and return complement. */ /*-------------------------------------------------------------------*/ sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); return (ui16)~sum;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -