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

  1. /* AX25 header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  
  5. /****************************************************************************
  6. *    $Id: ax25hdr.c 1.2 93/07/16 11:42:38 ROOT_DOS Exp $
  7. *    15 Jul 93    1.2        GT    AX25 conditional.                                *
  8. ****************************************************************************/
  9.  
  10. #include "global.h"
  11. #include "mbuf.h"
  12. #include "ax25.h"
  13. #include "ip.h"
  14. #include "config.h"
  15.  
  16. #if    AX25
  17.  
  18. /* Convert a host-format AX.25 header into a mbuf ready for transmission */
  19. struct mbuf *
  20. htonax25(hdr,data)
  21. register struct ax25 *hdr;
  22. struct mbuf *data;
  23. {
  24.     struct mbuf *bp;
  25.     register char *cp;
  26.     register int16 i;
  27.  
  28.     if(hdr == (struct ax25 *)NULL || hdr->ndigis > MAXDIGIS)
  29.         return NULLBUF;
  30.  
  31.     /* Allocate space for return buffer */
  32.     i = AXALEN * (2 + hdr->ndigis);
  33.     if((bp = pushdown(data,i)) == NULLBUF)
  34.         return NULLBUF;
  35.  
  36.     /* Now convert */
  37.     cp = bp->data;        /* cp -> dest field */
  38.  
  39.     /* Generate destination field */
  40.     memcpy(cp,hdr->dest,AXALEN);
  41.     if(hdr->cmdrsp == LAPB_COMMAND)
  42.         cp[ALEN] |= C;    /* Command frame sets C bit in dest */
  43.     else
  44.         cp[ALEN] &= ~C;
  45.     cp[ALEN] &= ~E;    /* Dest E-bit is always off */
  46.  
  47.     cp += AXALEN;        /* cp -> source field */
  48.  
  49.     /* Generate source field */
  50.     memcpy(cp,hdr->source,AXALEN);
  51.     if(hdr->cmdrsp == LAPB_RESPONSE)
  52.         cp[ALEN] |= C;
  53.     else
  54.         cp[ALEN] &= ~C;
  55.     /* Set E bit on source address if no digis */
  56.     if(hdr->ndigis == 0){
  57.         cp[ALEN] |= E;
  58.         return bp;
  59.     }
  60.  
  61.     cp += AXALEN;        /* cp -> first digi field */
  62.  
  63.     /* All but last digi get copied with E bit off */
  64.     for(i=0; i < hdr->ndigis; i++){
  65.         memcpy(cp,hdr->digis[i],AXALEN);
  66.         if(i < hdr->ndigis - 1)
  67.             cp[ALEN] &= ~E;
  68.         else
  69.             cp[ALEN] |= E;    /* Last digipeater has E bit set */
  70.         if(i < hdr->nextdigi)
  71.             cp[ALEN] |= REPEATED;
  72.         else
  73.             cp[ALEN] &= ~REPEATED;
  74.         cp += AXALEN;        /* cp -> next digi field */
  75.     }
  76.     return bp;
  77. }
  78. /* Convert a network-format AX.25 header into a host format structure
  79.  * Return -1 if error, number of addresses if OK
  80.  */
  81. int
  82. ntohax25(hdr,bpp)
  83. register struct ax25 *hdr;    /* Output structure */
  84. struct mbuf **bpp;
  85. {
  86.     register char *axp;
  87.  
  88.     if(pullup(bpp,hdr->dest,AXALEN) < AXALEN)
  89.         return -1;
  90.  
  91.     if(pullup(bpp,hdr->source,AXALEN) < AXALEN)
  92.         return -1;
  93.  
  94.     /* Process C bits to get command/response indication */
  95.     if((hdr->source[ALEN] & C) == (hdr->dest[ALEN] & C))
  96.         hdr->cmdrsp = LAPB_UNKNOWN;
  97.     else if(hdr->source[ALEN] & C)
  98.         hdr->cmdrsp = LAPB_RESPONSE;
  99.     else
  100.         hdr->cmdrsp = LAPB_COMMAND;
  101.  
  102.     hdr->ndigis = 0;
  103.     hdr->nextdigi = 0;
  104.     if(hdr->source[ALEN] & E)
  105.         return 2;    /* No digis */
  106.  
  107.     /* Count and process the digipeaters */
  108.     axp = hdr->digis[0];
  109.     while(hdr->ndigis < MAXDIGIS && pullup(bpp,axp,AXALEN) == AXALEN){
  110.         hdr->ndigis++;
  111.         if(axp[ALEN] & REPEATED)
  112.             hdr->nextdigi++;
  113.         if(axp[ALEN] & E)    /* Last one */
  114.             return hdr->ndigis + 2;            
  115.         axp += AXALEN;
  116.     }
  117.     return -1;    /* Too many digis */
  118. }
  119.  
  120. #endif    /* AX25 */
  121.