protinit.c

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

C
781
字号
    memcpy ((void *)blankip.i.ipdest, (const void *)broadip, 4);

    /*
     *  Create a mask which can determine whether a machine is on the same
     *  network or not.  RFC950
     *  First set the network mask to the user configurable mask.  If no mask
     *  was specified, then assign a mask based upon the IP address.
     */

    *(uint32 *)nnmask = longswap(CFG_NETMASK);

    if (!*(uint32 *)nnmask)
    {
        /* No mask was specified by the user so assign a mask based on the class
           of the IP address. */
        ip_addr = longswap(*(uint32 *)Scon.myipnum);

        if(IN_CLASSA(ip_addr))
            netsetmask(nnamask);
        else if (IN_CLASSB(ip_addr))
            netsetmask(nnbmask);
        else
            netsetmask(nncmask);
    }

    /* Identification field of outgoing packets. */
    nnipident = 1;

#if SNMP_INCLUDED
    dest = *(uint32 *)nnipnum & *(uint32 *)nnmask;

    /* Add an entry to the routing table.  This is a network route, as opposed
       to a route to a host.  The parameters specify 1) add an entry to the
       routing table.  2) For interface 1 (the only one).  3)  The destination
       for this route 4) - 8)  The routing metric (none are used).  9)  The IP
       address of the interface this route is associated with.  10) The route
       type is 3, direct.  11) The Routing protocol is 1, other.  12) The age we
       don't support, 13) The network mask.  14) No information. */
    SNMP_ipRouteTableUpdate(SNMP_ADD, 1, (char *)&dest, -1L, -1L, -1L, -1L, -1L,
                            nnipnum, 3, 1, 0, nnmask, "\0");

#endif
}  /* end ipinit */

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      udpinit                                                          */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*  Setup ulist for receive of udp packets                               */
/*                                                                       */
/* CALLED BY                                                             */
/*      protinit                                                         */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*                                                                       */
/*************************************************************************/
static void udpinit (void)
{
    int16     i;


    for (i = 0; i < NUPORTS; i++)
    {
        uportlist[i] = NU_NULL;            /* no ports open yet */
    }  /* end init the portlist array pointer */
}

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*    tcpinit                                                            */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*  setup for makeport ()                                                */
/*                                                                       */
/* CALLED BY                                                             */
/*      protinit                                                         */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*                                                                       */
/*************************************************************************/
static void tcpinit(void)
{
    int16 i;
      
    for (i = 0; i < NPORTS; i++)
    {
        portlist[i] = NU_NULL;            /* no ports open yet */
    }  /* end init the portlist array pointer */

    tasks_waiting_to_send = 0;

    /* Initialize the task table pointers.  These are pointers for the list of
       listeners (servers).
    */
    Task_Head = Task_Tail = NU_NULL;

}  /* end tcpinit routine */

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      makeport                                                         */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*   This is the intialization for TCP 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.                                                    */
/*                                                                       */
/*   A TCP port, in this implementation, includes all of the state data  */
/*   for the port connection, a complete packet for the TCP transmission,*/
/*   and two queues, one each for sending and receiving.  The data       */
/*   associated with the queues is in struct window.                     */
/*                                                                       */
/* CALLED BY                                                             */
/*      netlisten                                                        */
/*      netxopen                                                         */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      NU_Allocate_Memory                                               */
/*      setupwindow                                                      */
/*                                                                       */
/* HISTORY                                                               */
/*                                                                       */
/*  NAME                DATE        REMARKS                              */
/*                                                                       */
/*   Maiqi Qian      01/29/96        Fixed the bugs in makeport.         */
/*                                  1. Check timeout for SLOSING state   */
/*                                  2. retval = i, not i++.              */
/*                                                                       */
/*************************************************************************/
int16 makeport (void)
{
    uint16 i, retval;
    int32  timeout;
    struct port *p, *q;
    /* 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;

    /* search for a pre-initialized port to re-use */
    do
    {
        q = portlist[i];
        if (q != NU_NULL)
        {
            timeout = INT32_CMP(q->out.lasttime + WAITTIME, n_clicks());
            if ( (q->state==SCLOSED) || (q->state==STWAIT) && (timeout<0)
                                     || (q->state==SCLOSING) && (timeout<0) )
            {
               p = q;
               p -> pindex = i;
            }  /* end if SCLOSED/STWAIT and timeout < n_clicks */
        }  /* end if q != NU_NULL */
        retval = i;                   /* port # to return */
	} while ((p == NU_NULL) && (++i < NPORTS));

    /*
    * None available pre-allocated, get a new one, about 8.5 K with a 4K
    * windowsize
    */

    if (p == NU_NULL)
    {

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

        /* added during ATI mods - 10/1/92, bgh */
#ifdef PLUS
        status = NU_Allocate_Memory(&System_Memory, (void *) &return_ptr,
                                (UNSIGNED)sizeof(struct port),
                                (UNSIGNED)NU_NO_SUSPEND);
#else
        status = NU_Alloc_Memory (sizeof(struct port),
                              (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 port *)return_ptr;
        }
        else
        {
			/* ERROR memory allocation error.\r\n */
			NU_Tcp_Log_Error (TCP_NO_TCP_PORTS, TCP_RECOVERABLE,
							  __FILE__, __LINE__);
			return (-1);               /* out of room for ports */
		} /* end if */

		p -> pindex = i;
		portlist[i] = p;
		retval = i;

	} /* end if */

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

    /*  Set up non-pre-initialized fields. */
    p->tcpout.i.tlen = 0;           /* total length to 0 for now. */
    p->tcpout.t.urgent = 0;         /* no urgent data */
    p->tcpout.t.hlen = 20<<2;       /* header length << 2 */
    p->tcps.z = 0;                  /* zero field to 0. */
    p->tcps.proto = PROTTCP;        /* protocol number. */

    p->portFlags = 0;

    p->odh_flag = NU_ODH_DISABLE;   /* no Optimized Data Handling being used */
    p->probeFlag = NU_CLEAR;
    p->closeFlag = NU_CLEAR;
    p->selectFlag = NU_CLEAR;

    /* pseudo tcp my ip number. */
    memcpy ((void *)p->tcps.source, (const void *)nnipnum, 4);
    setupwindow (&p->in, WINDOWSIZE);       /* queuing parameters */
    setupwindow (&p->out, WINDOWSIZE);

    /* Build a local port number.  Needs to be unique and greater than
       2048.  */

    i = get_unique_port_number();

    p->in.port = i;                         /* save for incoming comparison */
    p->tcpout.t.source = intswap (i);        /* use it for our port number */

⌨️ 快捷键说明

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