home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 3 / hamradioversion3.0examsandprograms1992.iso / packet / n17jsrc / ax25dump.c < prev    next >
C/C++ Source or Header  |  1991-06-15  |  4KB  |  192 lines

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