Ignore:
Timestamp:
02/09/10 13:43:51 (11 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:
ebf8071
Parents:
22a9ccc
Message:
  • Updated licensing and documentation for the protocol decoders
  • Moved trace_get_source_mac and trace_get_destination_mac into protocols_l2.c which is much more appropriate
  • Re-wrote trace_get_destination_mac to match the behaviour of trace_get_source_mac, especially with regard to ignoring link types that match meta-data headers
  • Added TRACE_TYPE_NONDATA handlers to several switch statements
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/protocols_pktmeta.c

    raa22b5b rf6730d8  
    1 /* Protocol decodes for packet metadata headers */
     1/*
     2 * This file is part of libtrace
     3 *
     4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
     5 * New Zealand.
     6 *
     7 * Authors: Daniel Lawson
     8 *          Perry Lorier
     9 *          Shane Alcock
     10 *         
     11 * All rights reserved.
     12 *
     13 * This code has been developed by the University of Waikato WAND
     14 * research group. For further information please see http://www.wand.net.nz/
     15 *
     16 * libtrace is free software; you can redistribute it and/or modify
     17 * it under the terms of the GNU General Public License as published by
     18 * the Free Software Foundation; either version 2 of the License, or
     19 * (at your option) any later version.
     20 *
     21 * libtrace is distributed in the hope that it will be useful,
     22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     24 * GNU General Public License for more details.
     25 *
     26 * You should have received a copy of the GNU General Public License
     27 * along with libtrace; if not, write to the Free Software
     28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     29 *
     30 * $Id$
     31 *
     32 */
     33
    234#include "libtrace.h"
    335#include "libtrace_int.h"
     
    1648#define ARPHRD_PPP      512
    1749#endif
     50
     51/* This file contains all the protocol decoding functions for the meta-data
     52 * headers that may be prepended to captured packets.
     53 *
     54 * Supported protocols include (but are not limited to):
     55 *      Linux SLL
     56 *      PFLOG
     57 *      RadioTap
     58 *      Prism
     59 */
    1860
    1961/* NB: type is returned as an ARPHRD_ type for SLL*/
     
    182224}
    183225
    184 /* Take a pointer to the start of an IEEE 802.11 MAC frame and return a pointer
    185  * to the source MAC address. 
    186  * If the frame does not contain a sender address, e.g. ACK frame, return NULL.
    187  * If the frame is a 4-address WDS frame, return TA, i.e. addr2.
    188  * NB: This function decodes the 802.11 header, so it assumes that there are no
    189  * bit-errors. If there are, all bets are off.
    190  */
    191 static
    192 uint8_t *get_source_mac_from_wifi(void *wifi) {
    193         struct libtrace_80211_t *w;
    194         if (wifi == NULL) return NULL;
    195         w = (struct libtrace_80211_t *) wifi;
    196        
    197         /* If the frame is of type CTRL */
    198         if (w->type == 0x1)
    199                 /* If bit 2 of the subtype field is zero, this indicates that
    200                  * there is no transmitter address, i.e. the frame is either an
    201                  * ACK or a CTS frame */
    202                 if ((w->subtype & 0x2) == 0)
    203                         return NULL;
    204 
    205         /* Always return the address of the transmitter, i.e. address 2 */
    206         return (uint8_t *) &w->mac2;
    207 }
    208 
    209 DLLEXPORT uint8_t *trace_get_source_mac(libtrace_packet_t *packet) {
    210         void *link;
    211         uint32_t remaining;
    212         libtrace_linktype_t linktype;
    213         assert(packet);
    214         link = trace_get_layer2(packet,&linktype,&remaining);
    215 
    216         if (!link)
    217                 return NULL;
    218        
    219         switch (linktype) {
    220                 case TRACE_TYPE_ETH:
    221                         return (uint8_t *)&(((libtrace_ether_t*)link)->ether_shost);
    222                 case TRACE_TYPE_80211:
    223                         return get_source_mac_from_wifi(link);
    224                 /* These packets don't have MAC addresses */
    225                 case TRACE_TYPE_POS:
    226                 case TRACE_TYPE_NONE:
    227                 case TRACE_TYPE_HDLC_POS:
    228                 case TRACE_TYPE_PFLOG:
    229                 case TRACE_TYPE_ATM:
    230                 case TRACE_TYPE_DUCK:
    231                 case TRACE_TYPE_METADATA:
    232                 case TRACE_TYPE_AAL5:
    233                 case TRACE_TYPE_LLCSNAP:
    234                 case TRACE_TYPE_PPP:
    235                         return NULL;
    236 
    237                 /* Metadata headers should already be skipped */
    238                 case TRACE_TYPE_LINUX_SLL:
    239                 case TRACE_TYPE_80211_PRISM:
    240                 case TRACE_TYPE_80211_RADIO:
    241                         assert(!"Metadata headers should already be skipped");
    242                         break;
    243         }
    244         fprintf(stderr,"%s not implemented for linktype %i\n", __func__, linktype);
    245         assert(0);
    246         return NULL;
    247 }
    248 
    249 DLLEXPORT uint8_t *trace_get_destination_mac(libtrace_packet_t *packet)
    250 {
    251         void *link;
    252         libtrace_linktype_t linktype;
    253         uint32_t remaining;
    254         libtrace_80211_t *wifi;
    255         libtrace_ether_t *ethptr;
    256        
    257         link = trace_get_layer2(packet,&linktype,&remaining);
    258 
    259         ethptr = (libtrace_ether_t*)link;
    260 
    261 
    262         if (!link)
    263                 return NULL;
    264 
    265         switch (linktype) {
    266                 case TRACE_TYPE_80211:
    267                         wifi=(libtrace_80211_t*)link;
    268                         return (uint8_t*)&wifi->mac1;
    269                 case TRACE_TYPE_80211_RADIO:
    270                         wifi=(libtrace_80211_t*)trace_get_payload_from_radiotap(
    271                                         link,NULL,NULL);
    272                         return (uint8_t*)&wifi->mac1;
    273                 case TRACE_TYPE_80211_PRISM:
    274                         wifi=(libtrace_80211_t*)((char*)link+144);
    275                         return (uint8_t*)&wifi->mac1;
    276                 case TRACE_TYPE_ETH:
    277                         return (uint8_t*)&ethptr->ether_dhost;
    278                 case TRACE_TYPE_POS:
    279                 case TRACE_TYPE_NONE:
    280                 case TRACE_TYPE_ATM:
    281                 case TRACE_TYPE_HDLC_POS:
    282                 case TRACE_TYPE_LINUX_SLL:
    283                 case TRACE_TYPE_PFLOG:
    284                 case TRACE_TYPE_DUCK:
    285                 case TRACE_TYPE_METADATA:
    286                         /* No MAC address */
    287                         return NULL;
    288                 default:
    289                         break;
    290         }
    291         fprintf(stderr,"Not implemented\n");
    292         assert(0);
    293         return NULL;
    294 }
    295 
    296 
Note: See TracChangeset for help on using the changeset viewer.