Exemplo n.º 1
0
/* 
 * 在指定协议@prot的sock链表中,找到一个未使用的槽 
 */
unsigned short get_new_socknum(struct proto *prot, unsigned short base)
{
	static int start=0;

	/*
	 * Used to cycle through the port numbers so the
	 * chances of a confused connection drop.
	 */
	 
	int i, j;
	int best = 0;
	int size = 32767; /* a big num. */
	struct sock *sk;

	if (base == 0) 
		base = PROT_SOCK+1+(start % 1024);
	if (base <= PROT_SOCK) 
	{
		base += PROT_SOCK+(start % 1024);
	}

	/* Now look through the entire array and try to find an empty ptr. */
	for(i=0; i < SOCK_ARRAY_SIZE; i++) 
	{
		j = 0;
		sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
		while(sk != NULL) 
		{
			sk = sk->next;
			j++;
		}
		if (j == 0) 
		{
			start =(i+1+start )%1024;
			return(i+base+1);
		}
		if (j < size) 
		{
			best = i;
			size = j;
		}
	}

	/* Now make sure the one we want is not in use. */

	while(sk_inuse(prot, base +best+1)) 
	{
		best += SOCK_ARRAY_SIZE;
	}
	return(best+base+1);
}
Exemplo n.º 2
0
unsigned short get_new_socknum(struct proto *prot, unsigned short base)
{
    static int start = 0;
    int i,j;
    int best = 0;
    int size = 32767;
    struct sock *sk;

    if(base == 0)
        base = PROT_SOCK + 1 + (start % PROT_SOCK);
    if(base <= PROT_SOCK)
        base += PROT_SOCK + (start % PROT_SOCK);

    for(i = 0; i < SOCK_ARRAY_SIZE; i++)
    {
        j = 0;
        sk = prot->sock_array[(i + base + 1) & (SOCK_ARRAY_SIZE - 1)];
        while(sk != NULL)
        {
            sk = sk->next;
            j++;
        }
        if(j == 0)
        {
            start = (i + 1 + start)%PROT_SOCK;
            return (i + base + 1);
        }
        if(j < size)
        {
            best = i;
            size = j;
        }
    }
    while(sk_inuse(prot, base + best + 1))
    {
        best += SOCK_ARRAY_SIZE;
    }
    return (base + best + 1);
}