⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 user.c

📁 mcf5307实验源代码
💻 C
📖 第 1 页 / 共 5 页
字号:

/*************************************************************************/
/*                                                                       */
/*       CopyrIght (c)  1993 - 1996 Accelerated Technology, Inc.         */
/*                                                                       */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the      */
/* subject matter of this material.  All manufacturing, reproduction,    */
/* use, and sales rights pertaining to this subject matter are governed  */
/* by the license agreement.  The recipient of this software implicitly  */
/* accepts the terms of the license.                                     */
/*                                                                       */
/*************************************************************************/
/*
 *
 * Portions of this program were written by:       */
/*****************************************************************************
*                                                                           *
*     part of:                                                              *
*     TCP/IP kernel for NCSA Telnet                                         *
*     by Tim Krauskopf                                                      *
*                                                                           *
*     National Center for Supercomputing Applications                       *
*     152 Computing Applications Building                                   *
*     605 E. Springfield Ave.                                               *
*     Champaign, IL  61820                                                  *
*/
/******************************************************************************/
/*                                                                            */
/* FILENAME                                                 VERSION           */
/*                                                                            */
/*  user.c                                                     3.2            */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*  Network library interface routines                                        */
/*                                                                            */
/* AUTHOR                                                                     */
/*                                                                            */
/*                                                                            */
/* DATA STRUCTURES                                                            */
/*                                                                            */
/*                                                                            */
/* FUNCTIONS                                                                  */
/*                                                                            */
/*  netread                                                                   */
/*  netwrite                                                                  */
/*  netqlen                                                                   */
/*  netroom                                                                   */
/*  netsetip                                                                  */
/*  netgetip                                                                  */
/*  netsetbroad                                                               */
/*  netsetmask                                                                */
/*  netest                                                                    */
/*  netlisten                                                                 */
/*  netxopen                                                                  */
/*  Send_SYN_FIN                                                              */
/*  netclose                                                                  */
/*  netinit                                                                   */
/*  windowprobe                                                               */
/*  netsend                                                                   */
/*                                                                            */
/*                                                                            */
/* DEPENDENCIES                                                               */
/*                                                                            */
/*  No other file dependencies                                                */
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/*  NAME                DATE        REMARKS                                   */
/*                                                                            */
/*  Glen Johnson      10/26/94      Updated netwrite for release 1.0.G1.E     */
/*  Glen Johnson      04/30/96      Made some changes based on recommedations */
/*                                  of K. Goto of Hitachi.                    */
/*  Maiqi Qian        12/06/96      Fixed the problem in spr0229.             */
/*  Maiqi Qian        12/11/96      Moved the assignment of retransmits value */
/*                                  into tcpsend() from netwrite().           */
/*  Maiqi Qian     12/12/96     Modified windowprobe to probe forever.        */
/*  Maiqi Qian           04/10/97   Added SNMP control                        */
/*                                                                            */
/******************************************************************************/
#ifdef PLUS
  #include "nucleus.h"
#else   /* !PLUS */
  #include "nu_defs.h"    /* added during ATI mods - 10/20/92, bgh */
  #include "nu_extr.h"
#endif  /* !PLUS */

#include "protocol.h"
#include "tcp.h"
#include "tcpdefs.h"
#include "socketd.h"
#include "externs.h"
#include "data.h"
#include "target.h"
#include "netevent.h"
#include "tcp_errs.h"

#if SNMP_INCLUDED
#include "snmp_g.h"
#endif

// following for debug only.
char buffer[40];
unsigned short syntimes = 0;
unsigned short syntimes_add = 0;



/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      netread (uint16, char *, uint16)                                 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*   Read data from the connection buffer (specified by *pnum*) into the */
/*   user buffer for up to *n* bytes.                                    */
/*   Returns number of bytes read, or <0 if error.  Does not block.      */
/*                                                                       */
/* CALLED BY                                                             */
/*          NU_Recv                                                      */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      tcp_sendack                                                      */
/*      dequeue                                                          */
/*      Send_SYN_FIN                                                     */
/*                                                                       */
/*************************************************************************/
int16 netread(uint16 pnum, char *buffer, uint16 nbytes)
{
    int16 howmany;
    uint16 i, lowwater;
    struct port *prt;
    
    if(pnum > NPORTS)            /* check validity */
        return(-2);

    if(NU_NULL == (prt = portlist[pnum]))
        return(-2);

    switch (prt->state)
    {
        case SCWAIT:
            /* If the state is SCWAIT that means the remote host has issued a
               FIN and he will be sending no more data.  We now have a half-open
			   connection.  If there is no data to process let the application
               layer know that the other guy has closed.  Else return drop
               through to the SEST processing and return the requested data. */
            if (!prt->in.contain)
            {
                howmany = NU_NOT_CONNECTED;
                break;
            }

        case SEST:
            /* read from tcp buffer */
            howmany = dequeue(&prt->in, buffer, nbytes, prt->odh_flag);
            i = prt->in.size;                     /* how much before? */
            prt->in.size += howmany;              /* increment leftover room  */

            lowwater = prt->credit>>1;
            if((i<lowwater) && ((prt->in.size) >= (uint)lowwater))
            {   /* we passed mark */
                tcp_sendack(prt, 0);
            }

            break;

        default:
            howmany = NU_NOT_CONNECTED;
            break;
    }

    return (howmany);

}

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      netwrite                                                         */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*   Write data into the output queue (specified by pnum).               */
/*   As long as there is buffer space and the foreign host's receive     */
/*   window has not been filled, data will be sent.  The number of bytes */
/*   actually sent is returned ( a number between 0 and nbytes).         */
/*                                                                       */
/* CALLED BY                                                             */
/*      NU_Send                                                          */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      windowprobe                                                      */
/*      allocate_buffer                                                  */
/*      Stimerset                                                        */
/*      Stimerunset                                                      */
/*      netsend                                                          */
/*                                                                       */
/* INPUTS                                                                */
/*      pnum            Index into the portlist.                         */
/*      buffer          Pointer to the data to be sent.                  */
/*      nbytes          The number of bytes of data to send.             */
/*      status          Indicates why all data could not be sent.        */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      The number of bytes that were sent.                              */
/*                                                                       */
/* HISTORY                                                               */
/*         NAME            DATE                    REMARKS               */
/*                                                                       */
/*     G. Johnson        04/13/95    Modified to make use of window      */
/*                                   probes and the Nagle alogorithm.    */
/*     G. Johnson        11/10/95    The new field buf_ptr->retransmits  */
/*                                   is initialized here.                */
/*     Maiqi Qian        12/06/96    Added sent_by_wp to windowprobe     */
/*                                   problem in spr0229.                 */
/*  Maiqi Qian        12/11/96      Moved the assignment of retransmits  */
/*                                  value into tcpsend().                */
/*                                                                       */
/*************************************************************************/
int16 netwrite(uint16 pnum, char *buffer, uint16 nbytes, int16 *status)
{
    uint16   nsent=0;
    INT      sent_by_wp=0;
    struct   port *prt;
    struct   window *wind;
    BUFFER   *buf_ptr;
    int32    bytes_to_move = MAX_SEGMENT_LEN;
    int32    numbytes;

    /* Assume a successful operation until we are proven wrong. */
    *status = NU_SUCCESS;

    if(pnum > NPORTS)
    {
        *status = NU_INVALID_PORT;
        return(0);
    }

    if((prt = portlist[pnum]) == NU_NULL)
    {
        *status = NU_INVALID_PORT;
        return(0);
    }

    /* must be established connection */
    if((prt->state != SEST) && (prt->state != SCWAIT))
    {
        *status = NU_NOT_CONNECTED;
        return(0);
    }

    wind = &prt->out;

    numbytes = nbytes;


    /*  Find out what their advertised window size is.  If we want to
        send more bytes than they are advertising, then decrease the
        amount of bytes we want to send. */
    if ((wind->size - wind->contain) < numbytes)
    {
        /* If the ODH flag is set then we want to transmit all or nothing. */
        if (prt->odh_flag & NU_ODH_TRANSMIT)
        {
           *status = NU_WINDOW_FULL;
           return(0);
        }
        /* If ODH flag is not set then send whatever the other side will
           accept. */
        else

⌨️ 快捷键说明

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