tcpnxtp.c

来自「TCP-IP红宝书源代码」· C语言 代码 · 共 36 行

C
36
字号
/* tcpnxtp.c - tcpnxtp */

#include <conf.h>
#include <kernel.h>
#include <network.h>

#define	IPPORT_RESERVED		1024	/* from BSD */

/*------------------------------------------------------------------------
 *  tcpnxtp  -  return the next available TCP local "port" number
 *------------------------------------------------------------------------
 */
u_short
tcpnxtp()
{
	static	u_short	lastport=1;	/* #'s 1-1023 */
	u_short		start;
	int		i;

	wait(tcps_tmutex);
	for (start=lastport++; start != lastport; ++lastport) {
		if (lastport == IPPORT_RESERVED)
			lastport = 1;
		for (i=0; i<Ntcp; ++i)
			if (tcbtab[i].tcb_state != TCPS_FREE &&
					tcbtab[i].tcb_lport == lastport)
				break;
		if (i == Ntcp)
			break;
	}
	if (lastport == start)
		panic("out of TCP ports");
	signal(tcps_tmutex);
	return lastport;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?