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