Changeset 8b49230


Ignore:
Timestamp:
08/30/13 13:30:41 (7 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
bf1029a
Parents:
8780774
Message:

Fixed problems with decoding RadioTap? headers with libpacketdump on big endian
hosts

  • Moved byteswapX functions to a separate source file so that libpacketdump can also link against them.
  • bswap_* macros have been moved into lt_bswap.h so they can be used without having to include the entire libtrace_int.h header
  • Fixed endianness issues with the "fields" inside the RadioTap? header.
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • lib/Makefile.am

    rc04929c r8b49230  
    4444                format_duck.c format_tsh.c $(NATIVEFORMATS) $(BPFFORMATS) \
    4545                format_atmhdr.c \
    46                 libtrace_int.h lt_inttypes.h \
    47                 linktypes.c link_wireless.c \
     46                libtrace_int.h lt_inttypes.h lt_bswap.h \
     47                linktypes.c link_wireless.c byteswap.c \
    4848                checksum.c checksum.h \
    4949                protocols_pktmeta.c protocols_l2.c protocols_l3.c \
  • lib/format_helper.c

    rd57ae6f r8b49230  
    320320        va_end(va);
    321321}
    322 
    323 /* Byte swapping functions for various inttypes */
    324 uint64_t byteswap64(uint64_t num)
    325 {
    326         return (byteswap32((num&0xFFFFFFFF00000000ULL)>>32))
    327               |((uint64_t)byteswap32(num&0x00000000FFFFFFFFULL)<<32);
    328 }
    329 
    330 uint32_t byteswap32(uint32_t num)
    331 {
    332         return ((num&0x000000FFU)<<24)
    333                 | ((num&0x0000FF00U)<<8)
    334                 | ((num&0x00FF0000U)>>8)
    335                 | ((num&0xFF000000U)>>24);
    336 }
    337 
    338 uint16_t byteswap16(uint16_t num)
    339 {
    340         return ((num<<8)&0xFF00)|((num>>8)&0x00FF);
    341 }
    342 
  • lib/libtrace_int.h

    r3799f51 r8b49230  
    5959#include "libtrace.h"
    6060#include "wandio.h"
     61#include "lt_bswap.h"
    6162
    6263#ifdef _MSC_VER
     
    900901                uint32_t *remaining);
    901902
    902 /** Byteswaps a 64-bit value.
    903  *
    904  * @param num           The value to be byteswapped.
    905  * @return The byteswapped 64-bit number
    906  *
    907  */
    908 uint64_t byteswap64(uint64_t num);
    909 
    910 /** Byteswaps a 32-bit value.
    911  *
    912  * @param num           The value to be byteswapped.
    913  * @return The byteswapped 32-bit number
    914  *
    915  */
    916 uint32_t byteswap32(uint32_t num);
    917 
    918 /** Byteswaps a 16-bit value.
    919  *
    920  * @param num           The value to be byteswapped.
    921  * @return The byteswapped 16-bit number
    922  *
    923  */
    924 uint16_t byteswap16(uint16_t num);
    925 
    926 /** @name Byte ordering
    927  * Macros that define how to convert a value into a particular byte-order
    928  *
    929  * @{
    930  */
    931 #if __BYTE_ORDER == __BIG_ENDIAN
    932 #define bswap_host_to_be64(num) ((uint64_t)(num))
    933 #define bswap_host_to_le64(num) byteswap64(num)
    934 #define bswap_host_to_be32(num) ((uint32_t)(num))
    935 #define bswap_host_to_le32(num) byteswap32(num)
    936 #define bswap_host_to_be16(num) ((uint16_t)(num))
    937 #define bswap_host_to_le16(num) byteswap16(num)
    938 
    939 #define bswap_be_to_host64(num) ((uint64_t)(num))
    940 #define bswap_le_to_host64(num) byteswap64(num)
    941 #define bswap_be_to_host32(num) ((uint32_t)(num))
    942 #define bswap_le_to_host32(num) byteswap32(num)
    943 #define bswap_be_to_host16(num) ((uint16_t)(num))
    944 #define bswap_le_to_host16(num) byteswap16(num)
    945 
    946 /* We use ntoh*() here, because the compiler may
    947  * attempt to optimise it
    948  */
    949 #elif __BYTE_ORDER == __LITTLE_ENDIAN
    950 #define bswap_host_to_be64(num) (byteswap64(num))
    951 #define bswap_host_to_le64(num) ((uint64_t)(num))
    952 #define bswap_host_to_be32(num) (htonl(num))
    953 #define bswap_host_to_le32(num) ((uint32_t)(num))
    954 #define bswap_host_to_be16(num) (htons(num))
    955 #define bswap_host_to_le16(num) ((uint16_t)(num))
    956 
    957 #define bswap_be_to_host64(num) (byteswap64(num))
    958 #define bswap_le_to_host64(num) ((uint64_t)(num))
    959 #define bswap_be_to_host32(num) (ntohl(num))
    960 #define bswap_le_to_host32(num) ((uint32_t)(num))
    961 #define bswap_be_to_host16(num) (ntohs(num))
    962 #define bswap_le_to_host16(num) ((uint16_t)(num))
    963 
    964 #else
    965 #error "Unknown byte order"
    966 #endif
    967 /** @} */
    968903
    969904#ifdef HAVE_BPF
  • libpacketdump/Makefile.am

    rb471b67 r8b49230  
    166166ospf2_1005_la_LDFLAGS=$(modflags)
    167167
    168 libpacketdump_la_SOURCES = libpacketdump.cc \
     168libpacketdump_la_SOURCES = libpacketdump.cc ../lib/byteswap.c \
    169169                        lexer.l parser.y bitbuffer.c bitbuffer.h grammar.h
    170170
  • libpacketdump/libpacketdump.cc

    rc7f8451 r8b49230  
    44#include <time.h>
    55#include "libpacketdump.h"
     6#include "lt_bswap.h"
    67#include <stdio.h>
    78#include <netdb.h>
     
    303304        };
    304305}
     306
  • libpacketdump/link_15.c

    ref07202 r8b49230  
    22 * libpacketdump decoder for Radiotap
    33 */
    4 #include "libtrace_int.h" /* bswaps */
    54#include <sys/types.h>
    65#include <netinet/in.h>
     
    109#include "libtrace.h"
    1110#include "libpacketdump.h"
     11#include "lt_bswap.h"
    1212
    1313#define ALIGN_NATURAL_32(_p,_s,_c) \
     
    2323        struct libtrace_radiotap_t *rtap;
    2424        uint16_t rtap_len;
     25        uint32_t rtap_pres;
    2526        uint16_t rtap_real_len; /* to make sure length in header matches fields present */
    2627        rtap = (libtrace_radiotap_t *)packet;
     
    3536        rtap_real_len = sizeof(struct libtrace_radiotap_t);
    3637        rtap_len = bswap_le_to_host16(rtap->it_len);
     38        rtap_pres = bswap_le_to_host32(rtap->it_present);
    3739
    3840        printf(" version: %u, length: %u, fields: %#08x\n", rtap->it_version,
    39                         rtap_len, rtap->it_present);
     41                        rtap_len, rtap_pres);
    4042       
    4143        /* Check for extended bitmasks */
    4244        ptr = (uint32_t *) &(rtap->it_present);
    4345       
    44         if ( (*ptr) & (1 << TRACE_RADIOTAP_EXT) )
     46        if ( (rtap_pres) & (1 << TRACE_RADIOTAP_EXT) )
    4547                printf("  extended fields:");
    4648       
    47         while( (*ptr) & (1 << TRACE_RADIOTAP_EXT) ) {
     49        while( (rtap_pres) & (1 << TRACE_RADIOTAP_EXT) ) {
    4850                rtap_real_len += sizeof (uint32_t);
    49                 printf(" %#08x", *(++ptr));     
     51                ptr++;
     52                printf(" %#08x", bswap_le_to_host32(*ptr));     
    5053        }
    5154
     
    5457        s = p = (uint8_t *) ++ptr;
    5558
    56         if (rtap->it_present & (1 << TRACE_RADIOTAP_TSFT)) {
     59        if (rtap_pres & (1 << TRACE_RADIOTAP_TSFT)) {
    5760                printf(" Radiotap: TSFT = %" PRIu64 " microseconds\n", bswap_le_to_host64(*((uint64_t *)p)));
    5861                p += sizeof (uint64_t);
     
    6063        }
    6164
    62         if (rtap->it_present & (1 << TRACE_RADIOTAP_FLAGS)) {
     65        if (rtap_pres & (1 << TRACE_RADIOTAP_FLAGS)) {
    6366                printf(" Radiotap: Flags = 0x%02x\n", *p);
    6467                p += sizeof (uint8_t);
     
    6770
    6871       
    69         if (rtap->it_present & (1 << TRACE_RADIOTAP_RATE)) {
     72        if (rtap_pres & (1 << TRACE_RADIOTAP_RATE)) {
    7073                printf(" Radiotap: Rate = %u kbps\n", (*p) * 500);
    7174                p +=  sizeof (uint8_t);
     
    7376        }
    7477       
    75         if (rtap->it_present & (1 << TRACE_RADIOTAP_CHANNEL)) {
     78        if (rtap_pres & (1 << TRACE_RADIOTAP_CHANNEL)) {
    7679                ALIGN_NATURAL_16(p,s,rtap_real_len);
    7780                printf(" Radiotap: Freq = %u MHz, ChanFlags: 0x%04x\n", bswap_le_to_host16(*((uint16_t *)p)),
     
    8184        }
    8285                                                                                       
    83         if (rtap->it_present & (1 << TRACE_RADIOTAP_FHSS)) {
     86        if (rtap_pres & (1 << TRACE_RADIOTAP_FHSS)) {
    8487                ALIGN_NATURAL_16(p,s, rtap_real_len);
    8588                printf(" Radiotap: FHSS HopSet = %u , HopPattern: %u\n", *p, *(p+1));
     
    8992
    9093
    91         if (rtap->it_present & (1 << TRACE_RADIOTAP_DBM_ANTSIGNAL)) {
     94        if (rtap_pres & (1 << TRACE_RADIOTAP_DBM_ANTSIGNAL)) {
    9295                printf(" Radiotap: Signal = %i dBm\n", (int8_t) *p) ;
    9396                p += sizeof (uint8_t);
     
    9699
    97100
    98         if (rtap->it_present & (1 << TRACE_RADIOTAP_DBM_ANTNOISE)) {
     101        if (rtap_pres & (1 << TRACE_RADIOTAP_DBM_ANTNOISE)) {
    99102                printf(" Radiotap: Noise = %i dBm\n", (int8_t) *p);
    100103                p += sizeof (uint8_t);
     
    103106
    104107
    105         if (rtap->it_present & (1 << TRACE_RADIOTAP_LOCK_QUALITY)) {
     108        if (rtap_pres & (1 << TRACE_RADIOTAP_LOCK_QUALITY)) {
    106109                ALIGN_NATURAL_16(p,s, rtap_real_len);
    107110                printf(" Radiotap: Barker Code Lock Quality = %u\n", bswap_le_to_host16(*((uint16_t *)p)));
     
    111114
    112115
    113         if (rtap->it_present & (1 << TRACE_RADIOTAP_TX_ATTENUATION)) {
     116        if (rtap_pres & (1 << TRACE_RADIOTAP_TX_ATTENUATION)) {
    114117                ALIGN_NATURAL_16(p,s, rtap_real_len);
    115118                printf(" Radiotap: TX Attenuation = %u\n", bswap_le_to_host16(*((uint16_t *)p)));
     
    118121        }
    119122
    120         if (rtap->it_present & (1 << TRACE_RADIOTAP_DB_TX_ATTENUATION)) {
     123        if (rtap_pres & (1 << TRACE_RADIOTAP_DB_TX_ATTENUATION)) {
    121124                ALIGN_NATURAL_16(p,s,rtap_real_len);
    122125                printf(" Radiotap: TX Attenuation = %u dB\n", bswap_le_to_host16(*((uint16_t *)p)));
     
    125128        }
    126129
    127         if (rtap->it_present & (1 << TRACE_RADIOTAP_DBM_TX_POWER)) {
     130        if (rtap_pres & (1 << TRACE_RADIOTAP_DBM_TX_POWER)) {
    128131                printf(" Radiotap: TX Power = %i dBm\n", *p);
    129132                p += sizeof (uint8_t);
     
    131134        }
    132135
    133         if (rtap->it_present & (1 << TRACE_RADIOTAP_ANTENNA)) {
     136        if (rtap_pres & (1 << TRACE_RADIOTAP_ANTENNA)) {
    134137                printf(" Radiotap: Antenna = %u\n", *p);
    135138                p += sizeof (uint8_t);
     
    137140        }
    138141
    139         if (rtap->it_present & (1 << TRACE_RADIOTAP_DB_ANTSIGNAL)) {
     142        if (rtap_pres & (1 << TRACE_RADIOTAP_DB_ANTSIGNAL)) {
    140143                printf(" Radiotap: Signal = %u dB\n", *p);
    141144                p += sizeof (uint8_t);
     
    143146        }
    144147
    145         if (rtap->it_present & (1 << TRACE_RADIOTAP_DB_ANTNOISE)) {
     148        if (rtap_pres & (1 << TRACE_RADIOTAP_DB_ANTNOISE)) {
    146149                printf(" Radiotap: Noise = %u dB\n", *p);
    147150                p += sizeof (uint8_t);
     
    149152        }
    150153
    151         if (rtap->it_present & (1 << TRACE_RADIOTAP_RX_FLAGS)) {
     154        if (rtap_pres & (1 << TRACE_RADIOTAP_RX_FLAGS)) {
    152155                ALIGN_NATURAL_16(p,s,rtap_real_len);
    153156                printf(" Radiotap: RX Flags = 0x%04x\n", *((uint16_t *)p));
     
    156159        }
    157160
    158         if (rtap->it_present & (1 << TRACE_RADIOTAP_TX_FLAGS)) {
     161        if (rtap_pres & (1 << TRACE_RADIOTAP_TX_FLAGS)) {
    159162                ALIGN_NATURAL_16(p,s,rtap_real_len);
    160163                printf(" Radiotap: TX Flags = 0x%04x\n", *((uint16_t *)p));
     
    163166        }
    164167
    165         if (rtap->it_present & (1 << TRACE_RADIOTAP_RTS_RETRIES)) {
     168        if (rtap_pres & (1 << TRACE_RADIOTAP_RTS_RETRIES)) {
    166169                printf(" Radiotap: RTS Retries = %u\n", *p);
    167170                p += sizeof (uint8_t);
     
    169172        }
    170173
    171         if (rtap->it_present & (1 << TRACE_RADIOTAP_DATA_RETRIES)) {
     174        if (rtap_pres & (1 << TRACE_RADIOTAP_DATA_RETRIES)) {
    172175                printf(" Radiotap: Data Retries = %u\n", *p);
    173176                p += sizeof (uint8_t);
Note: See TracChangeset for help on using the changeset viewer.