source: lib/linktypes.c @ b721834

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since b721834 was b721834, checked in by Matt Brown <mattb@…>, 16 years ago

Define ARPHRD_ETHER ourselves as Windows doesn't appear to provide it

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#include "libtrace.h"
2#include "config.h"
3#ifdef HAVE_PCAP
4#include <pcap.h>
5#endif
6
7#include "rt_protocol.h"
8#include <assert.h>
9#include "libtrace_int.h"
10#include <stdlib.h>
11#include <string.h>
12
13#ifndef WIN32
14#include <net/if_arp.h>
15#else
16#define ARPHRD_ETHER    1               /* Ethernet 10/100Mbps.  */
17#endif
18
19/* This file maps libtrace types to/from pcap DLT and erf types
20 *
21 * When adding a new linktype to libtrace, add the mapping(s) here,
22 * and add the understanding of the type to get_ip(), and perhaps
23 * get_{source,destination}_mac (if your linklayer has mac's)
24 */
25
26libtrace_linktype_t pcap_dlt_to_libtrace(libtrace_dlt_t dlt)
27{
28        switch(dlt) {
29                case TRACE_DLT_NULL: return TRACE_TYPE_NONE;
30                case TRACE_DLT_EN10MB: return TRACE_TYPE_ETH;
31                case TRACE_DLT_ATM_RFC1483: return TRACE_TYPE_ATM;
32                case TRACE_DLT_IEEE802_11: return TRACE_TYPE_80211;
33                case TRACE_DLT_LINUX_SLL: return TRACE_TYPE_LINUX_SLL;
34                case TRACE_DLT_PFLOG: return TRACE_TYPE_PFLOG;
35        }
36        return ~0;
37}
38
39libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type)
40{
41        switch(type) {
42                case TRACE_TYPE_NONE: return TRACE_DLT_NULL;
43                case TRACE_TYPE_ETH: return TRACE_DLT_EN10MB;
44                case TRACE_TYPE_ATM: return TRACE_DLT_ATM_RFC1483;
45                case TRACE_TYPE_80211: return TRACE_DLT_IEEE802_11;
46                case TRACE_TYPE_LINUX_SLL: return TRACE_DLT_LINUX_SLL;
47                case TRACE_TYPE_PFLOG: return TRACE_DLT_PFLOG;
48        }
49        return ~0;
50}
51
52enum rt_field_t pcap_dlt_to_rt(libtrace_dlt_t dlt) 
53{
54        /* For pcap the rt type is just the dlt + a fixed value */
55        return dlt + RT_DATA_PCAP;
56}
57
58libtrace_dlt_t rt_to_pcap_dlt(enum rt_field_t rt_type)
59{
60        assert(rt_type >= RT_DATA_PCAP);
61        return rt_type - RT_DATA_PCAP;
62}
63
64libtrace_linktype_t erf_type_to_libtrace(char erf)
65{
66        switch (erf) {
67                case TYPE_HDLC_POS:     return TRACE_TYPE_HDLC_POS;
68                case TYPE_ETH:          return TRACE_TYPE_ETH;
69                case TYPE_ATM:          return TRACE_TYPE_ATM;
70                case TYPE_AAL5:         return TRACE_TYPE_AAL5;
71        }
72        return ~0;
73}
74
75char libtrace_to_erf_type(libtrace_linktype_t linktype)
76{
77        switch(linktype) {
78                case TRACE_TYPE_HDLC_POS: return TYPE_HDLC_POS;
79                case TRACE_TYPE_ETH:    return TYPE_ETH;
80                case TRACE_TYPE_ATM:    return TYPE_ATM;
81                case TRACE_TYPE_AAL5:   return TYPE_AAL5;
82        }
83        return -1;
84}
85
86/** Tinker with a packet
87 * packets that don't support direction tagging are annoying, especially
88 * when we have direction tagging information!  So this converts the packet
89 * to TRACE_TYPE_LINUX_SLL which does support direction tagging.  This is a
90 * pcap style packet for the reason that it means it works with bpf filters.
91 *
92 * @note this will copy the packet, so use sparingly if possible.
93 */
94void promote_packet(libtrace_packet_t *packet)
95{
96        if (packet->trace->format->type == TRACE_FORMAT_PCAP) {
97                char *tmpbuffer;
98                libtrace_sll_header_t *hdr;
99
100                switch(pcap_dlt_to_libtrace(rt_to_pcap_dlt(packet->type))) {
101                        case TRACE_TYPE_LINUX_SLL:
102                                return; /* Unnecessary */
103
104                        case TRACE_TYPE_ETH:
105                                /* This should be easy, just prepend the header */
106                                tmpbuffer= malloc(sizeof(libtrace_sll_header_t)
107                                                +trace_get_capture_length(packet)
108                                                +trace_get_framing_length(packet)
109                                                );
110
111                                hdr=(void*)((char*)tmpbuffer
112                                        +trace_get_framing_length(packet));
113
114                                hdr->pkttype=0; /* "outgoing" */
115                                hdr->hatype = ARPHRD_ETHER;
116                                break;
117                        default:
118                                /* failed */
119                                return;
120                }
121                memcpy(tmpbuffer,packet->header,
122                                trace_get_framing_length(packet));
123                memcpy(tmpbuffer
124                                +sizeof(libtrace_sll_header_t)
125                                +trace_get_framing_length(packet),
126                                packet->payload,
127                                trace_get_capture_length(packet));
128                if (packet->buf_control == TRACE_CTRL_EXTERNAL) {
129                        packet->buf_control=TRACE_CTRL_PACKET;
130                }
131                else {
132                        free(packet->buffer);
133                }
134                packet->buffer=tmpbuffer;
135                packet->header=tmpbuffer;
136                packet->payload=tmpbuffer+trace_get_framing_length(packet);
137                packet->type=TRACE_TYPE_LINUX_SLL;
138                return;
139        }
140}
Note: See TracBrowser for help on using the repository browser.