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

  1. /* AX25 header tracing
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  
  5. /****************************************************************************
  6. *    $Id: ax25dump.c 1.2 93/07/16 11:42:35 ROOT_DOS Exp $
  7. *    15 Jul 93    1.2        GT    Fix warnings.                                    *
  8. ****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include "global.h"
  12. #include "mbuf.h"
  13. #include "ax25.h"
  14. #include "lapb.h"
  15. #include "trace.h"
  16. #include "socket.h"
  17. #include "ip.h"
  18.  
  19. static char *decode_type __ARGS((int16 type));
  20.  
  21. /* Dump an AX.25 packet header */
  22. void
  23. ax25_dump(fp,bpp,check)
  24. FILE *fp;
  25. struct mbuf **bpp;
  26. int check;    /* Not used */
  27. {
  28.     char tmp[AXBUF];
  29.     char frmr[3];
  30.     int control,pid,seg;
  31.     int16 type;
  32.     int unsegmented;
  33.     struct ax25 hdr;
  34.     char *hp;
  35.  
  36.     fprintf(fp,"AX25: ");
  37.     /* Extract the address header */
  38.     if(ntohax25(&hdr,bpp) < 0){
  39.         /* Something wrong with the header */
  40.         fprintf(fp," bad header!\n");
  41.         return;
  42.     }
  43.     fprintf(fp,"%s",pax25(tmp,hdr.source));
  44.     fprintf(fp,"->%s",pax25(tmp,hdr.dest));
  45.     if(hdr.ndigis > 0){
  46.         fprintf(fp," v");
  47.         for(hp = hdr.digis[0]; hp < &hdr.digis[hdr.ndigis][0];
  48.          hp += AXALEN){
  49.             /* Print digi string */
  50.             fprintf(fp," %s%s",pax25(tmp,hp),
  51.              (hp[ALEN] & REPEATED) ? "*":"");
  52.         }
  53.     }
  54.     if((control = PULLCHAR(bpp)) == -1)
  55.         return;
  56.  
  57.     putc(' ',fp);
  58.     type = ftype(control);
  59.     fprintf(fp,"%s",decode_type(type));
  60.     /* Dump poll/final bit */
  61.     if(control & PF){
  62.         switch(hdr.cmdrsp){
  63.         case LAPB_COMMAND:
  64.             fprintf(fp,"(P)");
  65.             break;
  66.         case LAPB_RESPONSE:
  67.             fprintf(fp,"(F)");
  68.             break;
  69.         default:
  70.             fprintf(fp,"(P/F)");
  71.             break;
  72.         }
  73.     }
  74.     /* Dump sequence numbers */
  75.     if((type & 0x3) != U)    /* I or S frame? */
  76.         fprintf(fp," NR=%d",(control>>5)&7);
  77.     if(type == I || type == UI){    
  78.         if(type == I)
  79.             fprintf(fp," NS=%d",(control>>1)&7);
  80.         /* Decode I field */
  81.         if((pid = PULLCHAR(bpp)) != -1){    /* Get pid */
  82.             if(pid == PID_SEGMENT){
  83.                 unsegmented = 0;
  84.                 seg = PULLCHAR(bpp);
  85.                 fprintf(fp,"%s remain %u",seg & SEG_FIRST ?
  86.                  " First seg;" : "",seg & SEG_REM);
  87.                 if(seg & SEG_FIRST)
  88.                     pid = PULLCHAR(bpp);
  89.             } else
  90.                 unsegmented = 1;
  91.  
  92.             switch(pid){
  93.             case PID_SEGMENT:
  94.                 putc('\n',fp);
  95.                 break;    /* Already displayed */
  96.             case PID_ARP:
  97.                 fprintf(fp," pid=ARP\n");
  98.                 arp_dump(fp,bpp);
  99.                 break;
  100.             case PID_NETROM:
  101.                 fprintf(fp," pid=NET/ROM\n");
  102.                 /* Don't verify checksums unless unsegmented */
  103.                 netrom_dump(fp,bpp,unsegmented);
  104.                 break;
  105.             case PID_IP:
  106.                 fprintf(fp," pid=IP\n");
  107.                 /* Don't verify checksums unless unsegmented */
  108.                 ip_dump(fp,bpp,unsegmented);
  109.                 break;
  110.             case PID_X25:
  111.                 fprintf(fp," pid=X.25\n");
  112.                 break;
  113.             case PID_TEXNET:
  114.                 fprintf(fp," pid=TEXNET\n");
  115.                 break;
  116.             case PID_NO_L3:
  117.                 fprintf(fp," pid=Text\n");
  118.                 break;
  119.             default:
  120.                 fprintf(fp," pid=0x%x\n",pid);
  121.             }
  122.         }
  123.     } else if(type == FRMR && pullup(bpp,frmr,3) == 3){
  124.         fprintf(fp,": %s",decode_type(ftype(frmr[0])));
  125.         fprintf(fp," Vr = %d Vs = %d",(frmr[1] >> 5) & MMASK,
  126.             (frmr[1] >> 1) & MMASK);
  127.         if(frmr[2] & W)
  128.             fprintf(fp," Invalid control field");
  129.         if(frmr[2] & X)
  130.             fprintf(fp," Illegal I-field");
  131.         if(frmr[2] & Y)
  132.             fprintf(fp," Too-long I-field");
  133.         if(frmr[2] & Z)
  134.             fprintf(fp," Invalid seq number");
  135.         putc('\n',fp);
  136.     } else
  137.         putc('\n',fp);
  138.  
  139. }
  140. static char *
  141. decode_type(type)
  142. int16 type;
  143. {
  144.     switch(type){
  145.     case I:
  146.         return "I";
  147.     case SABM:
  148.         return "SABM";
  149.     case DISC:
  150.         return "DISC";
  151.     case DM:
  152.         return "DM";
  153.     case UA:
  154.         return "UA";
  155.     case RR:
  156.         return "RR";
  157.     case RNR:
  158.         return "RNR";
  159.     case REJ:
  160.         return "REJ";
  161.     case FRMR:
  162.         return "FRMR";
  163.     case UI:
  164.         return "UI";
  165.     default:
  166.         return "[invalid]";
  167.     }
  168. }
  169.  
  170. /* Return 1 if this packet is directed to us, 0 otherwise. Note that
  171.  * this checks only the ultimate destination, not the digipeater field
  172.  */
  173. int
  174. ax_forus(iface,bp)
  175. struct iface *iface;
  176. struct mbuf *bp;
  177. {
  178.     struct mbuf *bpp;
  179.     char dest[AXALEN];
  180.  
  181.     /* Duplicate the destination address */
  182.     if(dup_p(&bpp,bp,0,AXALEN) != AXALEN){
  183.         free_p(bpp);
  184.         return 0;
  185.     }
  186.     if(pullup(&bpp,dest,AXALEN) < AXALEN)
  187.         return 0;
  188.     if(addreq(dest,iface->hwaddr))
  189.         return 1;
  190.     else
  191.         return 0;
  192. }
  193.  
  194.