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

  1. /* AX25 link callsign monitoring. Also contains beginnings of
  2.  * an automatic link quality monitoring scheme (incomplete)
  3.  *
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6.  
  7. /****************************************************************************
  8. *    $Id: axheard.c 1.2 93/07/16 11:42:56 ROOT_DOS Exp $
  9. *    15 Jul 93    1.2        GT    AX25 conditional.                                *
  10. ****************************************************************************/
  11.  
  12. #include "global.h"
  13. #include "mbuf.h"
  14. #include "iface.h"
  15. #include "ax25.h"
  16. #include "ip.h"
  17. #include "timer.h"
  18. #include "config.h"
  19.  
  20. #if    AX25
  21.  
  22. static struct lq *al_create __ARGS((struct iface *ifp,char *addr));
  23. static struct ld *ad_lookup __ARGS((struct iface *ifp,char *addr,int sort));
  24. static struct ld *ad_create __ARGS((struct iface *ifp,char *addr));
  25. struct lq *Lq;
  26. struct ld *Ld;
  27.  
  28. #ifdef    notdef
  29. /* Send link quality reports to interface */
  30. void
  31. genrpt(ifp)
  32. struct iface *ifp;
  33. {
  34.     struct mbuf *bp;
  35.     register char *cp;
  36.     int i;
  37.     struct lq *lp;
  38.     int maxentries,nentries;
  39.  
  40.     maxentries = (Paclen - LQHDR) / LQENTRY;
  41.     if((bp = alloc_mbuf(Paclen)) == NULLBUF)
  42.         return;
  43.     cp = bp->data;
  44.     nentries = 0;
  45.  
  46.     /* Build and emit header */
  47.     cp = putlqhdr(cp,LINKVERS,Ip_addr);
  48.  
  49.     /* First entry is for ourselves. Since we're examining the Axsent
  50.      * variable before we've sent this frame, add one to it so it'll
  51.      * match the receiver's count after he gets this frame.
  52.      */
  53.     cp = putlqentry(cp,ifp->hwaddr,Axsent+1);
  54.     nentries++;
  55.  
  56.     /* Now add entries from table */
  57.     for(lp = lq;lp != NULLLQ;lp = lp->next){
  58.         cp = putlqentry(cp,&lp->addr,lp->currxcnt);
  59.         if(++nentries >= MAXENTRIES){
  60.             /* Flush */
  61.             bp->cnt = nentries*LQENTRY + LQHDR;
  62.             ax_output(ifp,Ax25multi[0],ifp->hwaddr,PID_LQ,bp);
  63.             if((bp = alloc_mbuf(Paclen)) == NULLBUF)
  64.                 return;
  65.             cp = bp->data;
  66.         }
  67.     }
  68.     if(nentries > 0){
  69.         bp->cnt = nentries*LQENTRY + LQHDR;
  70.         ax_output(ifp,Ax25multi[0],ifp->hwaddr,LQPID,bp);
  71.     } else {
  72.         free_p(bp);
  73.     }
  74. }
  75.  
  76. /* Pull the header off a link quality packet */
  77. void
  78. getlqhdr(hp,bpp)
  79. struct lqhdr *hp;
  80. struct mbuf **bpp;
  81. {
  82.     hp->version = pull16(bpp);
  83.     hp->ip_addr = pull32(bpp);
  84. }
  85.  
  86. /* Put a header on a link quality packet.
  87.  * Return pointer to buffer immediately following header
  88.  */
  89. char *
  90. putlqhdr(cp,version,ip_addr)
  91. register char *cp;
  92. int16 version;
  93. int32 ip_addr;
  94. {
  95.     cp = put16(cp,version);
  96.     return put32(cp,ip_addr);
  97. }
  98.  
  99. /* Pull an entry off a link quality packet */
  100. void
  101. getlqentry(ep,bpp)
  102. struct lqentry *ep;
  103. struct mbuf **bpp;
  104. {
  105.     pullup(bpp,ep->addr,AXALEN);
  106.     ep->count = pull32(bpp);
  107. }
  108.  
  109. /* Put an entry on a link quality packet
  110.  * Return pointer to buffer immediately following header
  111.  */
  112. char *
  113. putlqentry(cp,addr,count)
  114. char *cp;
  115. char *addr;
  116. int32 count;
  117. {
  118.     memcpy(cp,addr,AXALEN);
  119.     cp += AXALEN;
  120.     return put32(cp,count);
  121. }
  122. #endif
  123.  
  124. /* Log the source address of an incoming packet */
  125. void
  126. logsrc(ifp,addr)
  127. struct iface *ifp;
  128. char *addr;
  129. {
  130.     register struct lq *lp;
  131.  
  132.     if((lp = al_lookup(ifp,addr,1)) == NULLLQ
  133.      && (lp = al_create(ifp,addr)) == NULLLQ)
  134.         return;
  135.     lp->currxcnt++;
  136.     lp->time = secclock();
  137. }
  138. /* Log the destination address of an incoming packet */
  139. void
  140. logdest(ifp,addr)
  141. struct iface *ifp;
  142. char *addr;
  143. {
  144.     register struct ld *lp;
  145.  
  146.     if((lp = ad_lookup(ifp,addr,1)) == NULLLD
  147.      && (lp = ad_create(ifp,addr)) == NULLLD)
  148.         return;
  149.     lp->currxcnt++;
  150.     lp->time = secclock();
  151. }
  152. /* Look up an entry in the source data base */
  153. struct lq *
  154. al_lookup(ifp,addr,sort)
  155. struct iface *ifp;
  156. char *addr;
  157. int sort;
  158. {
  159.     register struct lq *lp;
  160.     struct lq *lplast = NULLLQ;
  161.  
  162.     for(lp = Lq;lp != NULLLQ;lplast = lp,lp = lp->next){
  163.         if(addreq(lp->addr,addr) && lp->iface == ifp){
  164.             if(sort && lplast != NULLLQ){
  165.                 /* Move entry to top of list */
  166.                 lplast->next = lp->next;
  167.                 lp->next = Lq;
  168.                 Lq = lp;
  169.             }
  170.             return lp;
  171.         }
  172.     }
  173.     return NULLLQ;
  174. }
  175. /* Create a new entry in the source database */
  176. static struct lq *
  177. al_create(ifp,addr)
  178. struct iface *ifp;
  179. char *addr;
  180. {
  181.     register struct lq *lp;
  182.  
  183.     lp = (struct lq *)callocw(1,sizeof(struct lq));
  184.     memcpy(lp->addr,addr,AXALEN);
  185.     lp->next = Lq;
  186.     Lq = lp;
  187.     lp->iface = ifp;
  188.  
  189.     return lp;
  190. }
  191. /* Look up an entry in the destination database */
  192. static struct ld *
  193. ad_lookup(ifp,addr,sort)
  194. struct iface *ifp;
  195. char *addr;
  196. int sort;
  197. {
  198.     register struct ld *lp;
  199.     struct ld *lplast = NULLLD;
  200.  
  201.     for(lp = Ld;lp != NULLLD;lplast = lp,lp = lp->next){
  202.         if(lp->iface == ifp && addreq(lp->addr,addr)){
  203.             if(sort && lplast != NULLLD){
  204.                 /* Move entry to top of list */
  205.                 lplast->next = lp->next;
  206.                 lp->next = Ld;
  207.                 Ld = lp;
  208.             }
  209.             return lp;
  210.         }
  211.     }
  212.     return NULLLD;
  213. }
  214. /* Create a new entry in the destination database */
  215. static struct ld *
  216. ad_create(ifp,addr)
  217. struct iface *ifp;
  218. char *addr;
  219. {
  220.     register struct ld *lp;
  221.  
  222.     lp = (struct ld *)callocw(1,sizeof(struct ld));
  223.     memcpy(lp->addr,addr,AXALEN);
  224.     lp->next = Ld;
  225.     Ld = lp;
  226.     lp->iface = ifp;
  227.  
  228.     return lp;
  229. }
  230.  
  231. #endif    /* AX25 */
  232.