home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / tcpipsrc / lapbtime.c < prev    next >
C/C++ Source or Header  |  1991-01-26  |  3KB  |  109 lines

  1. /* LAPB (AX25) timer recovery routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "ax25.h"
  7. #include "timer.h"
  8. #include "lapb.h"
  9.  
  10. static void tx_enq __ARGS((struct ax25_cb *axp));
  11.  
  12. /* Called whenever timer T1 expires */
  13. void
  14. recover(p)
  15. void *p;
  16. {
  17.     register struct ax25_cb *axp = (struct ax25_cb *)p;
  18.  
  19.     axp->flags.retrans = 1;
  20.     axp->retries++;
  21.     if((1L << axp->retries) < Blimit)
  22.         axp->t1.start *= 2;    /* Back off retransmit timer */
  23.  
  24.     switch(axp->state){
  25.     case LAPB_SETUP:
  26.         if(axp->n2 != 0 && axp->retries > axp->n2){
  27.             free_q(&axp->txq);
  28.             axp->reason = LB_TIMEOUT;
  29.             lapbstate(axp,LAPB_DISCONNECTED);
  30.         } else {
  31.             sendctl(axp,LAPB_COMMAND,SABM|PF);
  32.             start_timer(&axp->t1);
  33.         }
  34.         break;
  35.     case LAPB_DISCPENDING:
  36.         if(axp->n2 != 0 && axp->retries > axp->n2){
  37.             axp->reason = LB_TIMEOUT;
  38.             lapbstate(axp,LAPB_DISCONNECTED);
  39.         } else {
  40.             sendctl(axp,LAPB_COMMAND,DISC|PF);
  41.             start_timer(&axp->t1);
  42.         }
  43.         break;
  44.     case LAPB_CONNECTED:
  45.     case LAPB_RECOVERY:
  46.         if(axp->n2 != 0 && axp->retries > axp->n2){
  47.             /* Give up */
  48.             sendctl(axp,LAPB_RESPONSE,DM|PF);
  49.             free_q(&axp->txq);
  50.             axp->reason = LB_TIMEOUT;
  51.             lapbstate(axp,LAPB_DISCONNECTED);
  52.         } else {
  53.             /* Transmit poll */
  54.             tx_enq(axp);
  55.             lapbstate(axp,LAPB_RECOVERY);
  56.         }
  57.         break;
  58.     }
  59. }
  60.  
  61.  
  62. /* Send a poll (S-frame command with the poll bit set) */
  63. void
  64. pollthem(p)
  65. void *p;
  66. {
  67.     register struct ax25_cb *axp;
  68.  
  69.     axp = (struct ax25_cb *)p;
  70.     if(axp->proto == V1)
  71.         return;    /* Not supported in the old protocol */
  72.     switch(axp->state){
  73.     case LAPB_CONNECTED:
  74.         axp->retries = 0;
  75.         tx_enq(axp);
  76.         lapbstate(axp,LAPB_RECOVERY);
  77.         break;
  78.     }
  79. }
  80. /* Transmit query */
  81. static void
  82. tx_enq(axp)
  83. register struct ax25_cb *axp;
  84. {
  85.     char ctl;
  86.     struct mbuf *bp;
  87.  
  88.     /* I believe that retransmitting the oldest unacked
  89.      * I-frame tends to give better performance than polling,
  90.      * as long as the frame isn't too "large", because
  91.      * chances are that the I frame got lost anyway.
  92.      * This is an option in LAPB, but not in the official AX.25.
  93.      */
  94.     if(axp->txq != NULLBUF
  95.      && (len_p(axp->txq) < axp->pthresh || axp->proto == V1)){
  96.         /* Retransmit oldest unacked I-frame */
  97.         dup_p(&bp,axp->txq,0,len_p(axp->txq));
  98.         ctl = PF | I | ((axp->vs - axp->unack) & MMASK) << 1
  99.          | axp->vr << 5;
  100.         sendframe(axp,LAPB_COMMAND,ctl,bp);
  101.     } else {
  102.         ctl = len_p(axp->rxq) >= axp->window ? RNR|PF : RR|PF;    
  103.         sendctl(axp,LAPB_COMMAND,ctl);
  104.     }
  105.     axp->response = 0;    
  106.     stop_timer(&axp->t3);
  107.     start_timer(&axp->t1);
  108. }
  109.