home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / TCPOUT.C < prev    next >
C/C++ Source or Header  |  1991-12-18  |  7KB  |  216 lines

  1. /* TCP output segment processing
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "timer.h"
  6. #include "mbuf.h"
  7. #include "netuser.h"
  8. #include "internet.h"
  9. #include "tcp.h"
  10. #include "ip.h"
  11.  
  12. /* Send a segment on the specified connection. One gets sent only
  13.  * if there is data to be sent or if "force" is non zero
  14.  */
  15. void
  16. tcp_output(tcb)
  17. register struct tcb *tcb;
  18. {
  19.     struct pseudo_header ph;/* Pseudo-header for checksum calcs */
  20.     struct mbuf *hbp,*dbp;    /* Header and data buffer pointers */
  21.     int16 hsize;        /* Size of header */
  22.     struct tcp seg;        /* Local working copy of header */
  23.     int16 ssize;        /* Size of current segment being sent,
  24.                  * including SYN and FIN flags */
  25.     int16 dsize;        /* Size of segment less SYN and FIN */
  26.     int16 usable;        /* Usable window */
  27.     int16 sent;        /* Sequence count (incl SYN/FIN) already
  28.                  * in the pipe but not yet acked */
  29.     int32 rto;        /* Retransmit timeout setting */
  30.  
  31.     if(tcb == NULLTCB)
  32.         return;
  33.  
  34.     switch(tcb->state){
  35.     case TCP_LISTEN:
  36.     case TCP_CLOSED:
  37.         return;    /* Don't send anything */
  38.     }
  39.     for(;;){
  40.         /* Compute data already in flight */
  41.         sent = tcb->snd.ptr - tcb->snd.una;
  42.  
  43.         /* If transmitter has been idle for more than a RTT,
  44.          * take the congestion window back down to one packet.
  45.          */
  46.         if(!run_timer(&tcb->timer)
  47.          && (msclock() - tcb->lastactive) > tcb->srtt)
  48.             tcb->cwind = tcb->mss;
  49.  
  50.         /* Compute usable send window as minimum of offered
  51.          * and congestion windows, minus data already in flight.
  52.          * Be careful that the window hasn't shrunk --
  53.          * these are unsigned vars.
  54.          */
  55.         usable = min(tcb->snd.wnd,tcb->cwind);
  56.         if(usable > sent)
  57.             usable -= sent;    /* Most common case */
  58.         else if(usable == 0 && sent == 0)
  59.             usable = 1;    /* Closed window probe */
  60.         else
  61.             usable = 0;    /* Window closed or shrunken */
  62.  
  63.         /* Compute size of segment we *could* send. This is the
  64.          * smallest of the usable window, the mss, or the amount
  65.          * we have on hand. (I don't like optimistic windows)
  66.          */
  67.         ssize = min(tcb->sndcnt - sent,usable);
  68.         ssize = min(ssize,tcb->mss);
  69.  
  70.         /* Now we decide if we actually want to send it.
  71.          * Apply John Nagle's "single outstanding segment" rule.
  72.          * If data is already in the pipeline, don't send
  73.          * more unless it is MSS-sized or the very last packet.
  74.          */
  75.         if(sent != 0 && ssize < tcb->mss
  76.          && !(tcb->state == TCP_FINWAIT1 && ssize == tcb->sndcnt-sent)){
  77.             ssize = 0;
  78.         }
  79.          /* Unless the tcp syndata option is on, inhibit data until
  80.          * our SYN has been acked. This ought to be OK, but some
  81.          * old TCPs have problems with data piggybacked on SYNs.
  82.          */
  83.         if(!tcb->flags.synack && !Tcp_syndata){
  84.             if(tcb->snd.ptr == tcb->iss)
  85.                 ssize = min(1,ssize);    /* Send only SYN */
  86.             else
  87.                 ssize = 0;    /* Don't send anything */
  88.         }
  89.         if(ssize == 0 && !tcb->flags.force)
  90.             break;        /* No need to send anything */
  91.  
  92.         tcb->flags.force = 0;    /* Only one forced segment! */
  93.  
  94.         seg.source = tcb->conn.local.port;
  95.         seg.dest = tcb->conn.remote.port;
  96.  
  97.         /* Set the flags according to the state we're in. It is
  98.          * assumed that if this segment is associated with a state
  99.          * transition, then the state change will already have been
  100.          * made. This allows this routine to be called from a
  101.          * retransmission timeout with force=1.
  102.          */
  103.         seg.flags.urg = 0; /* Not used in this implementation */
  104.         seg.flags.rst = 0;
  105.         seg.flags.ack = 1; /* Every state except TCP_SYN_SENT */
  106.         seg.flags.syn = 0; /* syn/fin/psh set later if needed */
  107.         seg.flags.fin = 0;
  108.         seg.flags.psh = 0;
  109.         seg.flags.congest = tcb->flags.congest;
  110.  
  111.         hsize = TCPLEN;    /* Except when SYN being sent */
  112.         seg.mss = 0;
  113.         seg.optlen = 0;
  114.  
  115.         if(tcb->state == TCP_SYN_SENT)
  116.             seg.flags.ack = 0; /* Haven't seen anything yet */
  117.  
  118.         dsize = ssize;
  119.         if(!tcb->flags.synack && tcb->snd.ptr == tcb->iss){
  120.             /* Send SYN */
  121.             seg.flags.syn = 1;
  122.             dsize--;    /* SYN isn't really in snd queue */
  123.             /* Also send MSS */
  124.             seg.mss = Tcp_mss;
  125.             seg.optlen = 0;
  126.             hsize = TCPLEN + MSS_LENGTH;
  127.         }
  128.         seg.seq = tcb->snd.ptr;
  129.         seg.ack = tcb->rcv.nxt;
  130.         seg.wnd = tcb->rcv.wnd;
  131.         seg.up = 0;
  132.  
  133.         /* Now try to extract some data from the send queue. Since
  134.          * SYN and FIN occupy sequence space and are reflected in
  135.          * sndcnt but don't actually sit in the send queue, dup_p
  136.          * will return one less than dsize if a FIN needs to be sent.
  137.          */
  138.         if(dsize != 0){
  139.             int16 offset;
  140.  
  141.             /* SYN doesn't actually take up space on the sndq,
  142.              * so take it out of the sent count
  143.              */
  144.             offset = sent;
  145.             if(!tcb->flags.synack && sent != 0)
  146.                 offset--;
  147.  
  148.             if(dup_p(&dbp,tcb->sndq,offset,dsize) != dsize){
  149.                 /* We ran past the end of the send queue;
  150.                  * send a FIN
  151.                  */
  152.                 seg.flags.fin = 1;
  153.                 dsize--;
  154.             }
  155.         } else {
  156.             dbp = NULLBUF;
  157.         }
  158.         /* If the entire send queue will now be in the pipe, set the
  159.          * push flag
  160.          */
  161.         if(dsize != 0 && sent + ssize == tcb->sndcnt)
  162.             seg.flags.psh = 1;
  163.  
  164.         /* If this transmission includes previously transmitted data,
  165.          * snd.nxt will already be past snd.ptr. In this case,
  166.          * compute the amount of retransmitted data and keep score
  167.          */
  168.         if(tcb->snd.ptr < tcb->snd.nxt)
  169.             tcb->resent += min(tcb->snd.nxt - tcb->snd.ptr,ssize);
  170.  
  171.         tcb->snd.ptr += ssize;
  172.         /* If this is the first transmission of a range of sequence
  173.          * numbers, record it so we'll accept acknowledgments
  174.          * for it later
  175.          */
  176.         if(seq_gt(tcb->snd.ptr,tcb->snd.nxt))
  177.             tcb->snd.nxt = tcb->snd.ptr;
  178.  
  179.         /* Fill in fields of pseudo IP header */
  180.         ph.source = tcb->conn.local.address;
  181.         ph.dest = tcb->conn.remote.address;
  182.         ph.protocol = TCP_PTCL;
  183.         ph.length = hsize + dsize;
  184.  
  185.         /* Generate TCP header, compute checksum, and link in data */
  186.         if((hbp = htontcp(&seg,dbp,&ph)) == NULLBUF){
  187.             free_p(dbp);
  188.             return;
  189.         }
  190.         /* If we're sending some data or flags, start retransmission
  191.          * and round trip timers if they aren't already running.
  192.          */
  193.         if(ssize != 0){
  194.             /* Set round trip timer. */
  195.             rto = backoff(tcb->backoff) * (4 * tcb->mdev + tcb->srtt);
  196.             set_timer(&tcb->timer,max(MIN_RTO,rto));
  197.             if(!run_timer(&tcb->timer))
  198.                 start_timer(&tcb->timer);
  199.  
  200.             /* If round trip timer isn't running, start it */
  201.             if(!tcb->flags.rtt_run){
  202.                 tcb->flags.rtt_run = 1;
  203.                 tcb->rtt_time = msclock();
  204.                 tcb->rttseq = tcb->snd.ptr;
  205.             }
  206.         }
  207.         if(tcb->flags.retran)
  208.             tcpRetransSegs++;
  209.         else
  210.             tcpOutSegs++;
  211.  
  212.         ip_send(tcb->conn.local.address,tcb->conn.remote.address,
  213.          TCP_PTCL,tcb->tos,0,hbp,ph.length,0,0);
  214.     }
  215. }
  216.