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

  1. /*
  2.  * nr4subr.c:  subroutines for net/rom transport layer.
  3.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  4.  * non-commercial distribution only.
  5.  */
  6.  
  7. /****************************************************************************
  8. *    $Id: nr4subr.c 1.2 93/07/16 11:47:42 ROOT_DOS Exp $
  9. *    14 Jul 93    1.2        GT    Fix warnings.                                    *
  10. ****************************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include "global.h"
  14. #include "mbuf.h"
  15. #include "timer.h"
  16. #include "ax25.h"
  17. #include "netrom.h"
  18. #include "nr4.h"
  19. #include "lapb.h"
  20. #include <ctype.h>
  21. #include "ip.h"
  22.  
  23. #if    NETROM
  24.  
  25. /* Get a free circuit table entry, and allocate a circuit descriptor.
  26.  * Initialize control block circuit number and ID fields.
  27.  * Return a pointer to the circuit control block if successful,
  28.  * NULLNR4CB if not.
  29.  */
  30.  
  31. struct nr4cb *
  32. new_n4circ()
  33. {
  34.     int i ;
  35.     struct nr4cb *cb ;
  36.  
  37.     for (i = 0 ; i <  NR4MAXCIRC ; i++)        /* find a free circuit */
  38.         if (Nr4circuits[i].ccb == NULLNR4CB)
  39.             break ;
  40.  
  41.     if (i == NR4MAXCIRC)    /* no more circuits */
  42.         return NULLNR4CB ;
  43.  
  44.     cb = Nr4circuits[i].ccb =
  45.          (struct nr4cb *)callocw(1,sizeof(struct nr4cb));
  46.     cb->mynum = i ;
  47.     cb->myid = Nr4circuits[i].cid ;
  48.     return cb ;
  49. }
  50.  
  51.  
  52. /* Set the window size for a circuit and allocate the buffers for
  53.  * the transmit and receive windows.  Set the control block window
  54.  * parameter.  Return 0 if successful, -1 if not.
  55.  */
  56.  
  57. int
  58. init_nr4window(cb, window)
  59. struct nr4cb *cb ;
  60. unsigned window ;
  61. {
  62.     
  63.     if (window == 0 || window > NR4MAXWIN) /* reject silly window sizes */
  64.         return -1 ;
  65.         
  66.     cb->txbufs =
  67.          (struct nr4txbuf *)callocw(window,sizeof(struct nr4txbuf));
  68.  
  69.     cb->rxbufs =
  70.          (struct nr4rxbuf *)callocw(window,sizeof(struct nr4rxbuf));
  71.  
  72.     cb->window = window ;
  73.     
  74.     return 0 ;
  75. }
  76.  
  77.  
  78. /* Free a circuit.  Deallocate the control block and buffers, and
  79.  * increment the circuit ID.  No return value.
  80.  */
  81.  
  82. void
  83. free_n4circ(cb)
  84. struct nr4cb *cb ;
  85. {
  86.     unsigned circ ;
  87.  
  88.     if (cb == NULLNR4CB)
  89.         return ;
  90.  
  91.     circ = cb->mynum ;
  92.     
  93.     if (cb->txbufs != (struct nr4txbuf *)0)
  94.         free(cb->txbufs) ;
  95.  
  96.     if (cb->rxbufs != (struct nr4rxbuf *)0)
  97.         free(cb->rxbufs) ;
  98.  
  99.     /* Better be safe than sorry: */
  100.  
  101.     free_q(&cb->txq) ;
  102.     free_q(&cb->rxq) ;
  103.     
  104.     free(cb) ;
  105.  
  106.     if (circ > NR4MAXCIRC)        /* Shouldn't happen. */
  107.         return ;
  108.         
  109.     Nr4circuits[circ].ccb = NULLNR4CB ;
  110.  
  111.     Nr4circuits[circ].cid++ ;
  112. }
  113.  
  114. /* See if any open circuit matches the given parameters.  This is used
  115.  * to prevent opening multiple circuits on a duplicate connect request.
  116.  * Returns the control block address if a match is found, or NULLNR4CB
  117.  * otherwise.
  118.  */
  119.  
  120. struct nr4cb *
  121. match_n4circ(index, id, user, node)
  122. int index ;                    /* index of remote circuit */
  123. int id ;                    /* id of remote circuit */
  124. char *user ;    /* address of remote user */
  125. char *node ;    /* address of originating node */
  126. {
  127.     int i ;
  128.     struct nr4cb *cb ;
  129.  
  130.     for (i = 0 ; i < NR4MAXCIRC ; i++) {
  131.         if ((cb = Nr4circuits[i].ccb) == NULLNR4CB)
  132.             continue ;        /* not an open circuit */
  133.         if (cb->yournum == index && cb->yourid == id
  134.             && addreq(cb->remote.user,user)
  135.             && addreq(cb->remote.node,node))
  136.             return cb ;
  137.     }
  138.     /* if we get to here, we didn't find a match */
  139.  
  140.     return NULLNR4CB ;
  141. }
  142.  
  143. /* Validate the index and id of a local circuit, returning the control
  144.  * block if it is valid, or NULLNR4CB if it is not.
  145.  */
  146.  
  147. struct nr4cb *
  148. get_n4circ(index, id)
  149. int index ;                /* local circuit index */
  150. int id ;                /* local circuit id */
  151. {
  152.     struct nr4cb *cb ;
  153.  
  154.     if (index >= NR4MAXCIRC)
  155.         return NULLNR4CB ;
  156.  
  157.     if ((cb = Nr4circuits[index].ccb) == NULLNR4CB)
  158.         return NULLNR4CB ;
  159.  
  160.     if (cb->myid == id)
  161.         return cb ;
  162.     else
  163.         return NULLNR4CB ;
  164. }
  165.  
  166. /* Return 1 if b is "between" (modulo the size of an unsigned char)
  167.  * a and c, 0 otherwise.
  168.  */
  169.  
  170. int
  171. nr4between(a, b, c)
  172. unsigned a, b, c ;
  173. {
  174.     if ((a <= b && b < c) || (c < a && a <= b) || (b < c && c < a))
  175.         return 1 ;
  176.     else
  177.         return 0 ;
  178. }
  179.  
  180. /* Set up default timer values, etc., in newly connected control block.
  181.  */
  182.  
  183. void
  184. nr4defaults(cb)
  185. struct nr4cb *cb ;
  186. {
  187.     int i ;
  188.     struct timer *t ;
  189.  
  190.     if (cb == NULLNR4CB)
  191.         return ;
  192.  
  193.     /* Set up the ACK and CHOKE timers */
  194.     
  195.     set_timer(&cb->tack,Nr4acktime) ;
  196.     cb->tack.func = nr4ackit ;
  197.     cb->tack.arg = cb ;
  198.  
  199.     set_timer(&cb->tchoke,Nr4choketime) ;
  200.     cb->tchoke.func = nr4unchoke ;
  201.     cb->tchoke.arg = cb ;
  202.  
  203.     cb->rxpastwin = cb->window ;
  204.  
  205.     /* Don't actually set the timers, since this is done */
  206.     /* in nr4sbuf */
  207.     
  208.     for (i = 0 ; i < cb->window ; i++) {
  209.         t = &cb->txbufs[i].tretry ;
  210.         t->func = nr4txtimeout ;
  211.         t->arg = cb ;
  212.     }
  213. }
  214.  
  215. /* See if this control block address is valid */
  216.  
  217. int
  218. nr4valcb(cb)
  219. struct nr4cb *cb ;
  220. {
  221.     int i ;
  222.  
  223.     if (cb == NULLNR4CB)
  224.         return 0 ;
  225.         
  226.     for (i = 0 ; i < NR4MAXCIRC ; i++)
  227.         if (Nr4circuits[i].ccb == cb)
  228.             return 1 ;
  229.  
  230.     return 0 ;
  231. }
  232.  
  233. void
  234. nr_garbage(red)
  235. int red;
  236. {
  237.     int i;
  238.     struct nr4cb *ncp;
  239.  
  240.     for(i=0;i<NR4MAXCIRC;i++){
  241.         ncp = Nr4circuits[i].ccb;
  242.         if(ncp != NULLNR4CB)
  243.             mbuf_crunch(&ncp->rxq);
  244.     }
  245. }
  246.  
  247. #endif    /* NETROM */
  248.