home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / tcpipsrc / axheard.c < prev    next >
C/C++ Source or Header  |  1991-01-26  |  4KB  |  169 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_lookup __ARGS((struct iface *ifp,char *addr));
  14. static struct lq *al_create __ARGS((struct iface *ifp,char *addr));
  15.  
  16. struct lq *Lq[NHASH];
  17.  
  18. #ifdef    notdef
  19. /* Send link quality reports to interface */
  20. void
  21. genrpt(ifp)
  22. struct iface *ifp;
  23. {
  24.     struct mbuf *bp;
  25.     register char *cp;
  26.     int i;
  27.     struct lq *lp;
  28.     int maxentries,nentries;
  29.  
  30.     maxentries = (Paclen - LQHDR) / LQENTRY;
  31.     if((bp = alloc_mbuf(Paclen)) == NULLBUF)
  32.         return;
  33.     cp = bp->data;
  34.     nentries = 0;
  35.  
  36.     /* Build and emit header */
  37.     cp = putlqhdr(cp,LINKVERS,Ip_addr);
  38.  
  39.     /* First entry is for ourselves. Since we're examining the Axsent
  40.      * variable before we've sent this frame, add one to it so it'll
  41.      * match the receiver's count after he gets this frame.
  42.      */
  43.     cp = putlqentry(cp,ifp->hwaddr,Axsent+1);
  44.     nentries++;
  45.  
  46.     /* Now add entries from table */
  47.     for(i=0;i<NHASH;i++){
  48.         for(lp = lq[i];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,Ax25_bdcst,ifp->hwaddr,PID_LQ,bp);
  54.                 if((bp = alloc_mbuf(Paclen)) == NULLBUF)
  55.                     return;
  56.                 cp = bp->data;
  57.             }
  58.         }
  59.     }
  60.     if(nentries > 0){
  61.         bp->cnt = nentries*LQENTRY + LQHDR;
  62.         ax_output(ifp,Ax25_bdcst,ifp->hwaddr,LQPID,bp);
  63.     } else {
  64.         free_p(bp);
  65.     }
  66. }
  67.  
  68. /* Pull the header off a link quality packet */
  69. void
  70. getlqhdr(hp,bpp)
  71. struct lqhdr *hp;
  72. struct mbuf **bpp;
  73. {
  74.     hp->version = pull16(bpp);
  75.     hp->ip_addr = pull32(bpp);
  76. }
  77.  
  78. /* Put a header on a link quality packet.
  79.  * Return pointer to buffer immediately following header
  80.  */
  81. char *
  82. putlqhdr(cp,version,ip_addr)
  83. register char *cp;
  84. int16 version;
  85. int32 ip_addr;
  86. {
  87.     cp = put16(cp,version);
  88.     return put32(cp,ip_addr);
  89. }
  90.  
  91. /* Pull an entry off a link quality packet */
  92. void
  93. getlqentry(ep,bpp)
  94. struct lqentry *ep;
  95. struct mbuf **bpp;
  96. {
  97.     pullup(bpp,ep->addr,AXALEN);
  98.     ep->count = pull32(bpp);
  99. }
  100.  
  101. /* Put an entry on a link quality packet
  102.  * Return pointer to buffer immediately following header
  103.  */
  104. char *
  105. putlqentry(cp,addr,count)
  106. char *cp;
  107. char *addr;
  108. int32 count;
  109. {
  110.     memcpy(cp,addr,AXALEN);
  111.     cp += AXALEN;
  112.     return put32(cp,count);
  113. }
  114. #endif
  115.  
  116. /* Log the address of an incoming packet */
  117. void
  118. logaddr(ifp,addr)
  119. struct iface *ifp;
  120. char *addr;
  121. {
  122.     register struct lq *lp;
  123.  
  124.     if(addreq(addr,ifp->hwaddr))
  125.         return;    /* Don't log our own packets if we hear them */
  126.  
  127.     if((lp = al_lookup(ifp,addr)) == NULLLQ
  128.      && (lp = al_create(ifp,addr)) == NULLLQ)
  129.         return;
  130.     lp->currxcnt++;
  131.     lp->time = secclock();
  132. }
  133. static struct lq *
  134. al_lookup(ifp,addr)
  135. struct iface *ifp;
  136. char *addr;
  137. {
  138.     register struct lq *lp;
  139.  
  140.     for(lp = Lq[ax25hash(addr)];lp != NULLLQ;lp = lp->next){
  141.         if(addreq(lp->addr,addr) && lp->iface == ifp){
  142.             return lp;
  143.         }
  144.     }
  145.     return NULLLQ;
  146. }
  147. /* Create a new entry in the AX.25 link table */
  148. static struct lq *
  149. al_create(ifp,addr)
  150. struct iface *ifp;
  151. char *addr;
  152. {
  153.     register struct lq *lp;
  154.     int16 hashval;
  155.  
  156.     lp = (struct lq *)callocw(1,sizeof(struct lq));
  157.  
  158.     lp->addr = mallocw(AXALEN);
  159.     memcpy(lp->addr,addr,AXALEN);
  160.     hashval = ax25hash(addr);
  161.     lp->next = Lq[hashval];
  162.     if(lp->next != NULLLQ)
  163.         lp->next->prev = lp;
  164.     Lq[hashval] = lp;
  165.     lp->iface = ifp;
  166.  
  167.     return lp;
  168. }
  169.