source: lib/protocols_pktmeta.c @ f6730d8

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since f6730d8 was f6730d8, checked in by Shane Alcock <salcock@…>, 11 years ago
  • 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
  • Property mode set to 100644
File size: 5.4 KB
Line 
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
34#include "libtrace.h"
35#include "libtrace_int.h"
36#include "protocols.h"
37#include <assert.h>
38
39#ifndef WIN32
40#include <net/if_arp.h>
41#endif
42
43#ifndef ARPHRD_ETHER
44#define ARPHRD_ETHER    1               /* Ethernet 10/100Mbps.  */
45#endif
46
47#ifndef ARPHRD_PPP
48#define ARPHRD_PPP      512
49#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 */
60
61/* NB: type is returned as an ARPHRD_ type for SLL*/
62void *trace_get_payload_from_linux_sll(const void *link,
63                uint16_t *type, uint32_t *remaining) 
64{
65        libtrace_sll_header_t *sll;
66
67        sll = (libtrace_sll_header_t*) link;
68
69        if (remaining) {
70                if (*remaining < sizeof(*sll)) {
71                        *remaining = 0;
72                        return NULL;
73                }
74                *remaining-=sizeof(*sll);
75        }
76
77        if (type) *type = ntohs(sll->hatype);
78
79        return (void*)((char*)sll+sizeof(*sll));
80
81}
82
83/* NB: type is returned as an ethertype */
84static void *trace_get_payload_from_pflog(const void *link,
85                libtrace_linktype_t *type, uint32_t *remaining)
86{
87        libtrace_pflog_header_t *pflog = (libtrace_pflog_header_t*)link;
88        if (remaining) {
89                if (*remaining<sizeof(*pflog)) {
90                        *remaining = 0;
91                        return NULL;
92                }
93                *remaining-=sizeof(*pflog);
94        }
95        if (type) {
96                *type = TRACE_TYPE_NONE;
97        }
98        return (void*)((char*)pflog+ sizeof(*pflog));
99}
100
101/* Returns the 'payload' of the prism header, which is the 802.11 frame */
102static void *trace_get_payload_from_prism (const void *link,
103                libtrace_linktype_t *type, uint32_t *remaining)
104{
105        if (remaining) {
106                /* Prism header is 144 bytes long */
107                if (*remaining<144) {
108                        *remaining = 0;
109                        return NULL;
110                }
111                *remaining-=144;
112        }
113
114        if (type) *type = TRACE_TYPE_80211;
115
116        return (void *) ((char*)link+144);
117}
118
119/* Returns the 'payload' of the radiotap header, which is the 802.11 frame */
120static void *trace_get_payload_from_radiotap (const void *link, 
121                libtrace_linktype_t *type, uint32_t *remaining)
122{
123        struct libtrace_radiotap_t *rtap = (struct libtrace_radiotap_t*)link;
124        uint16_t rtaplen = bswap_le_to_host16(rtap->it_len);
125        if (remaining) {
126                if (*remaining < rtaplen) {
127                        *remaining = 0;
128                        return NULL;
129                }
130                *remaining -= rtaplen;
131        }
132
133        if (type) *type = TRACE_TYPE_80211;
134
135        return (void*) ((char*)link + rtaplen);
136}
137
138DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet, 
139                libtrace_linktype_t *linktype,
140                uint32_t *remaining)
141{
142        uint32_t dummyrem;
143        void *pktbuf = NULL;
144        assert(packet != NULL);
145        assert(linktype != NULL);
146       
147        if (remaining == NULL) 
148                remaining = &dummyrem;
149       
150        pktbuf = trace_get_packet_buffer(packet, linktype, remaining);
151        switch (*linktype) {
152                case TRACE_TYPE_LINUX_SLL:
153                case TRACE_TYPE_80211_RADIO:
154                case TRACE_TYPE_80211_PRISM:
155                        return pktbuf;
156                /* Non metadata packets */
157                case TRACE_TYPE_HDLC_POS:
158                case TRACE_TYPE_ETH:
159                case TRACE_TYPE_ATM:
160                case TRACE_TYPE_80211:
161                case TRACE_TYPE_NONE:
162                case TRACE_TYPE_PFLOG:
163                case TRACE_TYPE_POS:
164                case TRACE_TYPE_AAL5:
165                case TRACE_TYPE_DUCK:
166                case TRACE_TYPE_LLCSNAP:
167                case TRACE_TYPE_PPP:
168                case TRACE_TYPE_METADATA:
169                        return NULL;
170        }
171
172        /* Shouldn't get here */
173        return NULL;
174}
175
176DLLEXPORT void *trace_get_payload_from_meta(const void *meta,
177                libtrace_linktype_t *linktype,
178                uint32_t *remaining)
179{
180        void *nexthdr; 
181        uint16_t arphrd;
182       
183        assert(meta != NULL);
184        assert(linktype != NULL);
185        assert(remaining != NULL);
186       
187        switch(*linktype) {
188                case TRACE_TYPE_LINUX_SLL:
189                        nexthdr = trace_get_payload_from_linux_sll(meta,
190                                        &arphrd, remaining);
191                        *linktype = arphrd_type_to_libtrace(arphrd);
192                        return nexthdr;
193                case TRACE_TYPE_80211_RADIO:
194                        nexthdr = trace_get_payload_from_radiotap(meta,
195                                        linktype, remaining);
196                        return nexthdr;
197                case TRACE_TYPE_80211_PRISM:
198                        nexthdr = trace_get_payload_from_prism(meta,
199                                        linktype, remaining);
200                        return nexthdr;
201                case TRACE_TYPE_PFLOG:
202                        nexthdr = trace_get_payload_from_pflog(meta,
203                                        linktype, remaining);
204                        return nexthdr;
205                case TRACE_TYPE_HDLC_POS:
206                case TRACE_TYPE_ETH:
207                case TRACE_TYPE_ATM:
208                case TRACE_TYPE_80211:
209                case TRACE_TYPE_NONE:
210                case TRACE_TYPE_POS:
211                case TRACE_TYPE_AAL5:
212                case TRACE_TYPE_DUCK:
213                case TRACE_TYPE_LLCSNAP:
214                case TRACE_TYPE_PPP:
215                case TRACE_TYPE_METADATA:
216                        /* In this case, the pointer passed in does not point
217                         * to a metadata header and so we cannot get the
218                         * payload.
219                         */
220                        return NULL;
221        }
222        /* Shouldn't get here */
223        return NULL;
224}
225
Note: See TracBrowser for help on using the repository browser.