protinit.c

来自「mcf5307实验源代码」· C语言 代码 · 共 781 行 · 第 1/3 页

C
781
字号
    p->tcpout.t.seq = longswap (p->out.nxt); /* got from setupwindow() above */
    p->state = SCLOSED;                     /* connection not established yet */
    p->credit = nncredit;                   /* presaved credit */
    p->sendsize = MAX_SEGMENT_LEN;          /* static size */
    p->rto = MINRTO;                        /* static timeout */

    /* added to the initialization of a new portlist entry by ATI - 11/9/92 */
#ifdef PLUS
    p->TXTask = NU_NULL;                  /* initialize user task number */
    p->RXTask = NU_NULL;                  /* initialize user task number */
#else
    p->TXTask = -1;                       /* initialize user task number */
    p->RXTask = -1;                       /* initialize user task number */
#endif

    p->task_entry = NU_NULL;

    return ((int16)retval);
}  /* end makeport */

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      makeuport                                                        */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*   This is the intialization for UDP based communication.  When a port */
/*   needs to be created, this routine is called to do as much pre-      */
/*   initialization as possible to save overhead during operation.       */
/*                                                                       */
/*   This structure is created upon open of a port, either listening or  */
/*   wanting to send.                                                    */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      NU_Allocate_Memory                                               */
/*      intswap                                                          */
/*                                                                       */
/*************************************************************************/
int16 makeuport (int16 myport)
{
    int16 i, retval = -1;
    struct uport *p;

    /* added during ATI mods - 10/1/92, bgh */
#ifdef PLUS
    STATUS  status;
#else
    int16 status;              /* status of memory allocation */
#endif
    uint16 *return_ptr;       /* pointer to memory block */
    /*
     *  Check to see if any other connection is done with its port buffer
     *  space.  Indicated by the connection state of SCLOSED
     */

    p = NU_NULL;
    i = 0;        

    for (i = 0; uportlist[i] != NU_NULL; i++)
    {
        if (i >= NUPORTS)
        {
            NU_Tcp_Log_Error (TCP_NO_TCP_PORTS, TCP_RECOVERABLE,
								  __FILE__, __LINE__);
            return (-1);               /* out of room for ports */
        } /* end if */
    } /* end for */

    if (p == NU_NULL)
    {
        /* added during ATI mods - 10/1/92, bgh */
#ifdef PLUS
        status = NU_Allocate_Memory(&System_Memory, (void *) &return_ptr,
                                (UNSIGNED)sizeof(struct uport),
                                (UNSIGNED)NU_NO_SUSPEND);
#else
        status = NU_Alloc_Memory (sizeof(struct uport),
                              (unsigned int **)&return_ptr, NU_WAIT_FOREVER);
#endif

        /* check status of memory allocation */
        if (status == NU_SUCCESS)
        {
            return_ptr = normalize_ptr(return_ptr);
            p = (struct uport *)return_ptr;
        }
        else
        {
			/* ERROR memory allocation error.\r\n */
            NU_Tcp_Log_Error (UDP_NO_UDP_PORTS, UDP_RECOVERABLE,
							  __FILE__, __LINE__);
            return (-1);               /* out of room for ports */
        } /* end if */

        uportlist[i] = p;
        retval = i;

    } /* end if */

    /*  Copy pre-initialized data into the UDP Packet. */
    memcpy ((void *)&p->out.d, (const void *)&blankip,
             (sizeof(DLAYER) + sizeof(IPLAYER)));

    /*  Set up non-pre-initialized fields. */
    p->out.i.tlen = 0;                   /* total length to 0 for now. */
    p->tcps.z = 0;                          /* zero field to 0. */
    p->tcps.proto = PROTUDP;                /* protocol number. */

    memcpy ((void *)p->tcps.source, (const void *)nnipnum, 4);  /* pseudo tcp my ip number. */

    /* If my port number has not been specified then find a unique one
       for me. */
    if (myport <= 0)
        /* Build a local port number.  Needs to be unique and greater than
           2048.  */
        myport = (int16)get_unique_port_number();

    p->in.u.dest = intswap((uint16)myport); /* save for incoming comparison */
    p->in_dgrams = 0;                       /* input not received yet */
    p->out_stale = 0;                       /* output not sent yet */

    p->dgram_list.head = NU_NULL;
    p->dgram_list.tail = NU_NULL;

    /* added to the initialization of a new portlist entry by ATI - 11/9/92 */
#ifdef PLUS
    p->TXTask = NU_NULL;                  /* initialize user task number */
    p->RXTask = NU_NULL;                  /* initialize user task number */
#else
    p->TXTask = -1;                       /* initialize user task number */
    p->RXTask = -1;                       /* initialize user task number */
#endif

#if SNMP_INCLUDED
    /* Update the UDP Listen Table. */
    SNMP_udpListenTableUpdate(SNMP_ADD, nnipnum, myport);
#endif

    return (retval);
}  /* end makeport */

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      setupwindow                                                      */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*   Configure information about a window *w.                            */
/*                                                                       */
/* CALLED BY                                                             */
/*      makeport                                                         */
/*                                                                       */
/* CALLS                                                                 */
/*      NONE                                                             */
/*                                                                       */
/* HISTORY                                                               */
/*                                                                       */
/*  NAME             DATE        REMARKS                                 */
/*                                                                       */
/*  MQ              12/27/97     Fixed bugs.                             */
/*                                                                       */
/*************************************************************************/
static void setupwindow (struct window *w, uint16 wsize)
{
    INT     i;
    CHAR    *temp = (CHAR *)w;

    for (i = 0; i < sizeof (struct window); i++, *temp++ = 0);

    w->packet_list.head = w->packet_list.tail = NU_NULL;
    w->ooo_list.head = w->ooo_list.tail = NU_NULL;
    w->nextPacket = NU_NULL;
    w->num_packets = 0;
    w->contain = 0;                     /* nothing here yet */
    w->lasttime = n_clicks();
    w->size = wsize;
    w->push = 0;
    /*
    *  base this on time of day clock, for uniqueness
    */
    w->ack = w->nxt = (((uint32)w->lasttime << 12) & 0x0fffffff);
}

/****************************************************************************/
/*                                                                          */
/*  FUNCTION                                "get_unique_port_number"        */
/*                                                                          */
/*                                                                          */
/*  DESCRIPTION                                                             */
/*                                                                          */
/*      This function derives a new port number and then searches           */
/*      both the TCP portlist and the UDP portlist to see if it has         */
/*      already been used.                                                  */
/*                                                                          */
/*  AUTHOR                                                                  */
/*                                                                          */
/*      Neil Henderson              Accelerated Technology, Inc             */
/*                                                                          */
/*  CALLED FROM                                                             */
/*                                                                          */
/*      makeport                                                            */
/*      makeuport                                                           */
/*                                                                          */
/*  ROUTINES CALLED                                                         */
/*                                                                          */
/*      n_clicks                                                            */
/*                                                                          */
/*  INPUTS                                                                  */
/*                                                                          */
/*      None                                                                */
/*                                                                          */
/*  OUTPUTS                                                                 */
/*                                                                          */
/*      port_number                                                         */
/*                                                                          */
/*  NAME        DATE        REMARKS                                         */
/*                                                                          */
/*  MQ         02/20/96     Fixed bug that non-unique UDP port number.      */
/*  MQ         02/20/97     Fixed bug that non-unique TCP port number.      */
/*                                                                          */
/****************************************************************************/
uint16 get_unique_port_number(void)
{
    static uint16  unique=2048;
    uint16  i=0;
    int16     j;

    while (i==0)
    {
        i = unique++;
        /* see page 12 of TCP/IP Illustrated, Volume I, library #13163 */
        if (unique<2048 || unique>5000)
            unique = 2048;
#if 0
		i = (uint16) n_clicks ();           /* get a unique number */
		i |= 2048;                          /* make sure >= 0x800 */
		i &= 0x3fff;                        /* and <=0x3fff */
#endif
		/*  search for an un-used TCP port.  Search as long as we have
		 *  not searched all ports and we have not found an existing port
		 *  of the same number as the new one.
		 */
		for (j=0; j<NPORTS && i; j++)           /* don't check NULL ports */
            if (portlist[j]!=NU_NULL && i==portlist[j]->in.port)
				i = 0;
		for (j=0; j<NUPORTS && i; j++)
			if (uportlist[j]!=NU_NULL && intswap(i)==uportlist[j]->in.u.dest)
				i = 0;
	}

	return(i);
} /* get_unique_port_number */

⌨️ 快捷键说明

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