home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / NR4TIMER.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  4KB  |  180 lines

  1. /* net/rom level 4 (transport) protocol timer management.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5.  
  6. /****************************************************************************
  7. *    $Id: nr4timer.c 1.2 93/07/16 11:47:46 ROOT_DOS Exp $
  8. *    14 Jul 93    1.2        GT    FIx warnings.                                    *
  9. ****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include "global.h"
  13. #include "mbuf.h"
  14. #include "timer.h"
  15. #include "ax25.h"
  16. #include "lapb.h"
  17. #include "netrom.h"
  18. #include "nr4.h"
  19. #include <ctype.h>
  20. #include "ip.h"
  21.  
  22. #undef NR4DEBUG
  23.  
  24. #if    NETROM
  25.  
  26. /* The ACK timer has expired without any data becoming available.
  27.  * Go ahead and send an ACK.
  28.  */
  29.  
  30. void
  31. nr4ackit(p)
  32. void *p ;
  33. {
  34.     struct nr4cb *cb  = (struct nr4cb *)p ;
  35.     struct nr4hdr rhdr ;
  36.  
  37. #ifdef NR4DEBUG
  38.     printf("ACKIT called.\n") ;
  39. #endif
  40.     if (cb->qfull)                /* Are we choked? */
  41.         rhdr.opcode = NR4OPACK | NR4CHOKE ;
  42.     else
  43.         rhdr.opcode = NR4OPACK ;
  44.     rhdr.yourindex = cb->yournum ;
  45.     rhdr.yourid = cb->yourid ;
  46.     rhdr.u.ack.rxseq = cb->rxpected ;
  47.  
  48.     nr4sframe(cb->remote.node, &rhdr, NULLBUF) ;
  49. }
  50.  
  51. /* Called when one of the transmit timers has expired */
  52.  
  53. void
  54. nr4txtimeout(p)
  55. void *p ;
  56. {
  57.     struct nr4cb *cb = (struct nr4cb *)p ;
  58.     unsigned seq ;
  59.     struct nr4txbuf *t ;
  60.  
  61.     /* Sanity check */
  62.  
  63.     if (cb->state != NR4STCON)
  64.         return ;
  65.  
  66.     /* Scan through the send window looking for expired timers */
  67.     
  68.     for (seq = cb->ackxpected ;
  69.          nr4between(cb->ackxpected, seq, cb->nextosend) ;
  70.          seq = (seq + 1) & NR4SEQMASK) {
  71.         
  72.         t = &cb->txbufs[seq % cb->window] ;
  73.  
  74.         if (t->tretry.state == TIMER_EXPIRE) {
  75.             t->tretry.state = TIMER_STOP ;    /* So we don't do it again */
  76.  
  77.             if (t->retries == Nr4retries) {
  78.                 cb->dreason = NR4RTIMEOUT ;
  79.                 nr4state(cb, NR4STDISC) ;
  80.             }
  81.  
  82.             t->retries++ ;
  83.             
  84.             /* We keep track of the highest retry count in the window. */
  85.             /* If packet times out and its new retry count exceeds the *
  86.             /* max, we update the max and bump the backoff level.  This */
  87.             /* expedient is to avoid bumping the backoff level for every */
  88.             /* expiration, since with more than one timer we would back */
  89.             /* off way too fast (and at a rate dependent on the window */
  90.             /* size! */
  91.  
  92.             if (t->retries > cb->txmax) {
  93.                 cb->blevel++ ;
  94.                 cb->txmax = t->retries ;    /* update the max */
  95.             }
  96.             
  97.             nr4sbuf(cb,seq) ;    /* Resend buffer */
  98.         }
  99.      }
  100.     
  101. }
  102.  
  103. /* Connect/disconnect acknowledgement timeout */
  104.  
  105. void
  106. nr4cdtimeout(p)
  107. void *p ;
  108. {
  109.     struct nr4cb *cb = (struct nr4cb *)p ;
  110.     struct nr4hdr hdr ;
  111.  
  112.     switch(cb->state) {
  113.       case NR4STCPEND:
  114.           if (cb->cdtries == Nr4retries) {    /* Have we tried long enough? */
  115.             cb->dreason = NR4RTIMEOUT ;
  116.             nr4state(cb, NR4STDISC) ;        /* Give it up */
  117.         } else {
  118.             /* Set up header */
  119.             
  120.             hdr.opcode = NR4OPCONRQ ;
  121.             hdr.u.conreq.myindex = cb->mynum ;
  122.             hdr.u.conreq.myid = cb->myid ;
  123.             hdr.u.conreq.window = Nr4window ;
  124.             memcpy(hdr.u.conreq.user,cb->local.user,AXALEN) ;
  125.             memcpy(hdr.u.conreq.node,cb->local.node,AXALEN) ;
  126.  
  127.             /* Bump tries counter and backoff level, and restart timer */
  128.             /* We use a binary exponential backoff. */
  129.             
  130.             cb->cdtries++ ;
  131.             cb->blevel++ ;
  132.             set_timer(&cb->tcd,dur_timer(&cb->tcd)*2);
  133.             start_timer(&cb->tcd) ;
  134.  
  135.             /* Send connect request packet */
  136.  
  137.             nr4sframe(cb->remote.node,&hdr, NULLBUF) ;
  138.         }
  139.         break ;
  140.         
  141.       case NR4STDPEND:
  142.           if (cb->cdtries == Nr4retries) {    /* Have we tried long enough? */
  143.             cb->dreason = NR4RTIMEOUT ;
  144.             nr4state(cb, NR4STDISC) ;        /* Give it up */
  145.         } else {
  146.             /* Format header */
  147.             
  148.             hdr.opcode = NR4OPDISRQ ;
  149.             hdr.yourindex = cb->yournum ;
  150.             hdr.yourid = cb->yourid ;
  151.  
  152.             /* Bump retry count and start timer */
  153.             /* We don't really need to be fancy here, since we */
  154.             /* should have a good idea of the round trip time by now. */
  155.             
  156.             cb->cdtries++ ;
  157.             start_timer(&cb->tcd) ;
  158.  
  159.             /* Send disconnect request packet */
  160.  
  161.             nr4sframe(cb->remote.node,&hdr, NULLBUF) ;
  162.         }
  163.         break ;
  164.     }
  165. }
  166.  
  167. /* The choke timer has expired.  Unchoke and kick. */
  168.  
  169. void
  170. nr4unchoke(p)
  171. void *p ;
  172. {
  173.     struct nr4cb *cb = (struct nr4cb *)p ;
  174.  
  175.     cb->choked = 0 ;
  176.     nr4output(cb) ;
  177. }
  178.  
  179. #endif    /* NETROM */
  180.