home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / NR4USER.C < prev    next >
C/C++ Source or Header  |  1992-04-11  |  5KB  |  226 lines

  1. /* net/rom level 4 (transport) protocol user level calls
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "timer.h"
  10. #include "ax25.h"
  11. #include "lapb.h"
  12. #include "netrom.h"
  13. #include "nr4.h"
  14. #include <ctype.h>
  15.  
  16. #undef NR4DEBUG
  17.  
  18. /* Open a NET/ROM transport connection */
  19. struct nr4cb *
  20. open_nr4(local,remote,mode,r_upcall,t_upcall,s_upcall,user)
  21. struct nr4_addr *local ;    /* local node address */
  22. struct nr4_addr *remote ;    /* destination node address */
  23. int mode ;            /* active/passive/server */
  24. void (*r_upcall)() ;        /* received data upcall */
  25. void (*t_upcall)() ;        /* transmit upcall */
  26. void (*s_upcall)() ;        /* state change upcall */
  27. int user ;            /* user linkage area */
  28. {
  29.     struct nr4cb *cb ;
  30.     struct nr4hdr hdr ;
  31.     struct nr4_addr nr4tmp;
  32.  
  33.     if ((cb = new_n4circ()) == NULLNR4CB)
  34.         return NULLNR4CB ;        /* No circuits available */
  35.  
  36.     if(remote == NULLNRADDR){
  37.         remote = &nr4tmp;
  38.         setcall(remote->user," ");
  39.         setcall(remote->node," ");
  40.     }
  41.     
  42.     /* Stuff what info we can into control block */
  43.  
  44.     ASSIGN(cb->remote,*remote) ;
  45.     /* Save local address for connect retries */
  46.     ASSIGN(cb->local,*local) ;
  47.  
  48.     cb->r_upcall = r_upcall ;
  49.     cb->t_upcall = t_upcall ;
  50.     cb->s_upcall = s_upcall ;
  51.     cb->user = user ;
  52.     cb->clone = 0 ;
  53.  
  54.     switch(mode){
  55.     case AX_SERVER:
  56.         cb->clone = 1;
  57.     case AX_PASSIVE:    /* Note fall-thru */
  58.         cb->state = NR4STLISTEN;
  59.         return cb;
  60.     case AX_ACTIVE:
  61.         break;
  62.     }    
  63.     /* Format connect request header */
  64.  
  65.     hdr.opcode = NR4OPCONRQ ;
  66.     hdr.u.conreq.myindex = cb->mynum ;
  67.     hdr.u.conreq.myid = cb->myid ;
  68.     hdr.u.conreq.window = Nr4window ;
  69.     memcpy(hdr.u.conreq.user,local->user,AXALEN);
  70.  
  71.     /* If I have a unique callsign per interface, then a layer violation */
  72.     /* will be required to determine the "real" callsign for my */
  73.     /* (virtual) node.  This suggests that callsign-per-interface is not */
  74.     /* desirable, which answers *that* particular open question. */
  75.     
  76.     memcpy(hdr.u.conreq.node,local->node,AXALEN);
  77.  
  78.     /* Set and start connection retry timer */
  79.  
  80.     cb->cdtries = 1 ;
  81.     cb->srtt = Nr4irtt ;
  82.     set_timer(&cb->tcd,2 * cb->srtt);
  83.     cb->tcd.func = nr4cdtimeout ;
  84.     cb->tcd.arg = cb ;
  85.     start_timer(&cb->tcd) ;
  86.     
  87.     /* Send connect request packet */
  88.  
  89.     nr4sframe(remote->node,&hdr,NULLBUF) ;
  90.  
  91.     /* Set up initial state and signal state change */
  92.  
  93.     cb->state = NR4STDISC ;
  94.     nr4state(cb, NR4STCPEND) ;
  95.  
  96.     /* Return control block address */
  97.  
  98.     return cb ;
  99. }
  100.  
  101. /* Send a net/rom transport data packet */
  102. int
  103. send_nr4(cb,bp)
  104. struct nr4cb *cb ;
  105. struct mbuf *bp ;
  106. {
  107.     if(cb == NULLNR4CB || bp == NULLBUF){
  108.         free_p(bp);
  109.         return -1 ;
  110.     }
  111.     enqueue(&cb->txq,bp) ;
  112.     return nr4output(cb) ;
  113. }
  114.  
  115. /* Receive incoming net/rom transport data */
  116. struct mbuf *
  117. recv_nr4(cb,cnt)
  118. struct nr4cb *cb ;
  119. int16 cnt ;
  120. {
  121.     struct mbuf *bp ;
  122.  
  123.     if (cb->rxq == NULLBUF)
  124.         return NULLBUF ;
  125.  
  126.     if (cnt == 0) {
  127.         bp = cb->rxq ;            /* Just give `em everything */
  128.         cb->rxq = NULLBUF ;
  129.     }
  130.     else {
  131.         bp = ambufw(cnt);
  132.         bp->cnt = pullup(&cb->rxq,bp->data,cnt);
  133.     }
  134.     /* If this has un-choked us, reopen the window */
  135.     if (cb->qfull && len_p(cb->rxq) < Nr4qlimit) {
  136.         cb->qfull = 0 ;                /* Choke flag off */
  137.         nr4ackit(cb) ;        /* Get things rolling again */
  138.     }
  139.  
  140.     return bp ;
  141. }
  142.  
  143. /* Close a NET/ROM connection */
  144. void
  145. disc_nr4(cb)
  146. struct nr4cb *cb ;
  147. {
  148.     struct nr4hdr hdr ;
  149.     
  150.     if (cb->state == NR4STLISTEN) {
  151.         free_n4circ(cb);
  152.         return;
  153.     }
  154.     if (cb->state != NR4STCON)
  155.         return ;
  156.  
  157.     /* Format disconnect request packet */
  158.     
  159.     hdr.opcode = NR4OPDISRQ ;
  160.     hdr.yourindex = cb->yournum ;
  161.     hdr.yourid = cb->yourid ;
  162.  
  163.     /* Set and start timer */
  164.     
  165.     cb->cdtries = 1 ;
  166.     set_timer(&cb->tcd,2 * cb->srtt);
  167.     cb->tcd.func = nr4cdtimeout ;
  168.     cb->tcd.arg = cb ;
  169.     start_timer(&cb->tcd) ;
  170.  
  171.     /* Send packet */
  172.  
  173.     nr4sframe(cb->remote.node, &hdr, NULLBUF) ;
  174.  
  175.     /* Signal state change.  nr4state will take care of stopping */
  176.     /* the appropriate timers and resetting window pointers. */
  177.  
  178.     nr4state(cb, NR4STDPEND) ;
  179.     
  180. }
  181.  
  182. /* Abruptly terminate a NET/ROM transport connection */
  183. void
  184. reset_nr4(cb)
  185. struct nr4cb *cb ;
  186. {
  187.     cb->dreason = NR4RRESET ;
  188.     nr4state(cb,NR4STDISC) ;
  189. }
  190.  
  191.  
  192. /* Force retransmission on a NET/ROM transport connection */
  193. int
  194. kick_nr4(cb)
  195. struct nr4cb *cb ;
  196. {
  197.     unsigned seq ;
  198.     struct timer *t ;
  199.  
  200.     if(!nr4valcb(cb))
  201.         return -1 ;
  202.  
  203.     switch (cb->state) {
  204.       case NR4STCPEND:
  205.       case NR4STDPEND:
  206.           stop_timer(&cb->tcd) ;
  207.         nr4cdtimeout(cb) ;
  208.         break ;
  209.  
  210.       case NR4STCON:
  211.         if (cb->nextosend != cb->ackxpected) {    /* if send window is open: */
  212.             for (seq = cb->ackxpected ;
  213.                  nr4between(cb->ackxpected, seq, cb->nextosend) ;
  214.                  seq = (seq + 1) & NR4SEQMASK) {
  215.                 t = &cb->txbufs[seq % cb->window].tretry ;
  216.                 stop_timer(t) ;
  217.                 t->state = TIMER_EXPIRE ;    /* fool retry routine */
  218.             }
  219.             nr4txtimeout(cb) ;
  220.         }
  221.         break ;
  222.     }
  223.  
  224.     return 0 ;
  225. }
  226.