home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / AX25DUMP.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  7KB  |  262 lines

  1. /* AX25 header tracing
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  /* Mods by PA0GRI */
  5. #include "global.h"
  6. #ifdef AX25
  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. #ifdef MONITOR
  31.     if (!Trace_compact_header)
  32. #endif
  33.         fprintf(fp,"AX25: ");
  34.     /* Extract the address header */
  35.     if(ntohax25(&hdr,bpp) < 0){
  36.         /* Something wrong with the header */
  37.         fprintf(fp," bad header!\n");
  38.         return;
  39.     }
  40.     fprintf(fp,"%s",pax25(tmp,hdr.source));
  41.     fprintf(fp,"->%s",pax25(tmp,hdr.dest));
  42.     if(hdr.ndigis > 0){
  43. #ifdef MONITOR
  44.         if (!Trace_compact_header)
  45. #endif
  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.     type = ftype(control);
  58. #ifdef MONITOR
  59.     if (!Trace_compact_header || (type != I && type != UI))
  60.     {
  61. #endif
  62.         fprintf(fp," %s",decode_type(type));
  63.     /* Dump poll/final bit */
  64.         if(control & PF){
  65. #ifdef MONITOR
  66.             if (Trace_compact_header)
  67.                 fprintf(fp, "!");
  68.             else
  69. #endif
  70.             switch(hdr.cmdrsp){
  71.                 case LAPB_COMMAND:
  72.                     fprintf(fp,"(P)");
  73.                     break;
  74.                 case LAPB_RESPONSE:
  75.                     fprintf(fp,"(F)");
  76.                     break;
  77.                 default:
  78.                     fprintf(fp,"(P/F)");
  79.                     break;
  80.             }
  81.         }
  82. #ifdef MONITOR
  83.     }
  84. #endif
  85.     /* Dump sequence numbers */
  86. #ifdef MONITOR
  87.     if (!Trace_compact_header)
  88. #endif
  89.         if((type & 0x3) != U)   /* I or S frame? */
  90.             fprintf(fp," NR=%d",(control>>5)&7);
  91.     if(type == I || type == UI){
  92. #ifdef MONITOR
  93.         if (!Trace_compact_header)
  94. #endif
  95.             if(type == I)
  96.                 fprintf(fp," NS=%d",(control>>1)&7);
  97.         /* Decode I field */
  98.         if((pid = PULLCHAR(bpp)) != -1){    /* Get pid */
  99.             if(pid == PID_SEGMENT){
  100.                 unsegmented = 0;
  101.                 seg = PULLCHAR(bpp);
  102. #ifdef MONITOR
  103.                 if (!Trace_compact_header)
  104. #endif
  105.                     fprintf(fp,"%s remain %u",seg & SEG_FIRST ?
  106.                     " First seg;" : "",seg & SEG_REM);
  107.                 if(seg & SEG_FIRST)
  108.                     pid = PULLCHAR(bpp);
  109.             } else
  110.                 unsegmented = 1;
  111.   
  112.             switch(pid){
  113.                 case PID_SEGMENT:
  114.                     putc('\n',fp);
  115.                     break;  /* Already displayed */
  116.                 case PID_ARP:
  117. #ifdef MONITOR
  118.                     if (Trace_compact_header)
  119.                         fprintf(fp, " ARP ");
  120.                     else
  121. #endif
  122.                         fprintf(fp," pid=ARP\n");
  123.                     arp_dump(fp,bpp);
  124.                     break;
  125.                 case PID_RARP:
  126. #ifdef MONITOR
  127.                     if (Trace_compact_header)
  128.                         fprintf(fp, " RARP ");
  129.                     else
  130. #endif
  131.                         fprintf(fp," pid=RARP\n");
  132.                     arp_dump(fp,bpp);
  133.                     break;
  134. #ifdef NETROM
  135.                 case PID_NETROM:
  136. #ifdef MONITOR
  137.                     if (Trace_compact_header)
  138.                         fprintf(fp, " ");
  139.                     else
  140. #endif
  141.                         fprintf(fp," pid=NET/ROM\n");
  142.                 /* Don't verify checksums unless unsegmented */
  143.                     netrom_dump(fp,bpp,unsegmented);
  144.                     break;
  145. #endif
  146.                 case PID_IP:
  147. #ifdef MONITOR
  148.                     if (Trace_compact_header)
  149.                         fprintf(fp, " IP ");
  150.                     else
  151. #endif
  152.                         fprintf(fp," pid=IP\n");
  153.                 /* Don't verify checksums unless unsegmented */
  154.                     ip_dump(fp,bpp,unsegmented);
  155.                     break;
  156.                 case PID_X25:
  157. #ifdef MONITOR
  158.                     if (Trace_compact_header)
  159.                         fprintf(fp, " X.25\n");
  160.                     else
  161. #endif
  162.                         fprintf(fp," pid=X.25\n");
  163.                     break;
  164.                 case PID_TEXNET:
  165. #ifdef MONITOR
  166.                     if (Trace_compact_header)
  167.                         fprintf(fp, " TEXNET\n");
  168.                     else
  169. #endif
  170.                         fprintf(fp," pid=TEXNET\n");
  171.                     break;
  172.                 case PID_NO_L3:
  173. #ifdef MONITOR
  174.                     if (Trace_compact_header)
  175.                         fprintf(fp, "\n");
  176.                     else
  177. #endif
  178.                         fprintf(fp," pid=Text\n");
  179.                     break;
  180.                 default:
  181. #ifdef MONITOR
  182.                     if (Trace_compact_header)
  183.                         fprintf(fp, " [0x%x]\n", pid);
  184.                     else
  185. #endif
  186.                         fprintf(fp," pid=0x%x\n",pid);
  187.             }
  188.         }
  189.     } else if(type == FRMR && pullup(bpp,frmr,3) == 3){
  190.         fprintf(fp,": %s",decode_type(ftype(frmr[0])));
  191.         fprintf(fp," Vr = %d Vs = %d",(frmr[1] >> 5) & MMASK,
  192.         (frmr[1] >> 1) & MMASK);
  193.         if(frmr[2] & W)
  194.             fprintf(fp," Invalid control field");
  195.         if(frmr[2] & X)
  196.             fprintf(fp," Illegal I-field");
  197.         if(frmr[2] & Y)
  198.             fprintf(fp," Too-long I-field");
  199.         if(frmr[2] & Z)
  200.             fprintf(fp," Invalid seq number");
  201.         putc('\n',fp);
  202.     } else
  203.         putc('\n',fp);
  204.   
  205. }
  206. static char *
  207. decode_type(type)
  208. int16 type;
  209. {
  210.     switch(type){
  211.         case I:
  212.             return "I";
  213.         case SABM:
  214.             return "SABM";
  215.         case DISC:
  216.             return "DISC";
  217.         case DM:
  218.             return "DM";
  219.         case UA:
  220.             return "UA";
  221.         case RR:
  222.             return "RR";
  223.         case RNR:
  224.             return "RNR";
  225.         case REJ:
  226.             return "REJ";
  227.         case FRMR:
  228.             return "FRMR";
  229.         case UI:
  230.             return "UI";
  231.         default:
  232.             return "[invalid]";
  233.     }
  234. }
  235.   
  236. /* Return 1 if this packet is directed to us, 0 otherwise. Note that
  237.  * this checks only the ultimate destination, not the digipeater field
  238.  */
  239. int
  240. ax_forus(iface,bp)
  241. struct iface *iface;
  242. struct mbuf *bp;
  243. {
  244.     struct mbuf *bpp;
  245.     char dest[AXALEN];
  246.   
  247.     /* Duplicate the destination address */
  248.     if(dup_p(&bpp,bp,0,AXALEN) != AXALEN){
  249.         free_p(bpp);
  250.         return 0;
  251.     }
  252.     if(pullup(&bpp,dest,AXALEN) < AXALEN)
  253.         return 0;
  254.     if(addreq(dest,iface->hwaddr))
  255.         return 1;
  256.     else
  257.         return 0;
  258. }
  259.   
  260. #endif /* AX25 */
  261.   
  262.