home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / AX25DUMP.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  4KB  |  187 lines

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