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