home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / NR4USER.C < prev    next >
C/C++ Source or Header  |  1990-08-20  |  5KB  |  224 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.         return -1 ;
  109.     enqueue(&cb->txq,bp) ;
  110.     return nr4output(cb) ;
  111. }
  112.  
  113. /* Receive incoming net/rom transport data */
  114. struct mbuf *
  115. recv_nr4(cb,cnt)
  116. struct nr4cb *cb ;
  117. int16 cnt ;
  118. {
  119.     struct mbuf *bp ;
  120.  
  121.     if (cb->rxq == NULLBUF)
  122.         return NULLBUF ;
  123.  
  124.     if (cnt == 0) {
  125.         bp = cb->rxq ;            /* Just give `em everything */
  126.         cb->rxq = NULLBUF ;
  127.     }
  128.     else {
  129.         bp = ambufw(cnt);
  130.         bp->cnt = pullup(&cb->rxq,bp->data,cnt);
  131.     }
  132.     /* If this has un-choked us, reopen the window */
  133.     if (cb->qfull && len_p(cb->rxq) < Nr4qlimit) {
  134.         cb->qfull = 0 ;                /* Choke flag off */
  135.         nr4ackit(cb) ;        /* Get things rolling again */
  136.     }
  137.  
  138.     return bp ;
  139. }
  140.  
  141. /* Close a NET/ROM connection */
  142. void
  143. disc_nr4(cb)
  144. struct nr4cb *cb ;
  145. {
  146.     struct nr4hdr hdr ;
  147.     
  148.     if (cb->state == NR4STLISTEN) {
  149.         free_n4circ(cb);
  150.         return;
  151.     }
  152.     if (cb->state != NR4STCON)
  153.         return ;
  154.  
  155.     /* Format disconnect request packet */
  156.     
  157.     hdr.opcode = NR4OPDISRQ ;
  158.     hdr.yourindex = cb->yournum ;
  159.     hdr.yourid = cb->yourid ;
  160.  
  161.     /* Set and start timer */
  162.     
  163.     cb->cdtries = 1 ;
  164.     set_timer(&cb->tcd,2 * cb->srtt);
  165.     cb->tcd.func = nr4cdtimeout ;
  166.     cb->tcd.arg = cb ;
  167.     start_timer(&cb->tcd) ;
  168.  
  169.     /* Send packet */
  170.  
  171.     nr4sframe(cb->remote.node, &hdr, NULLBUF) ;
  172.  
  173.     /* Signal state change.  nr4state will take care of stopping */
  174.     /* the appropriate timers and resetting window pointers. */
  175.  
  176.     nr4state(cb, NR4STDPEND) ;
  177.     
  178. }
  179.  
  180. /* Abruptly terminate a NET/ROM transport connection */
  181. void
  182. reset_nr4(cb)
  183. struct nr4cb *cb ;
  184. {
  185.     cb->dreason = NR4RRESET ;
  186.     nr4state(cb,NR4STDISC) ;
  187. }
  188.  
  189.  
  190. /* Force retransmission on a NET/ROM transport connection */
  191. int
  192. kick_nr4(cb)
  193. struct nr4cb *cb ;
  194. {
  195.     unsigned seq ;
  196.     struct timer *t ;
  197.  
  198.     if(!nr4valcb(cb))
  199.         return -1 ;
  200.  
  201.     switch (cb->state) {
  202.       case NR4STCPEND:
  203.       case NR4STDPEND:
  204.           stop_timer(&cb->tcd) ;
  205.         nr4cdtimeout(cb) ;
  206.         break ;
  207.  
  208.       case NR4STCON:
  209.         if (cb->nextosend != cb->ackxpected) {    /* if send window is open: */
  210.             for (seq = cb->ackxpected ;
  211.                  nr4between(cb->ackxpected, seq, cb->nextosend) ;
  212.                  seq = (seq + 1) & NR4SEQMASK) {
  213.                 t = &cb->txbufs[seq % cb->window].tretry ;
  214.                 stop_timer(t) ;
  215.                 t->state = TIMER_EXPIRE ;    /* fool retry routine */
  216.             }
  217.             nr4txtimeout(cb) ;
  218.         }
  219.         break ;
  220.     }
  221.  
  222.     return 0 ;
  223. }
  224.