summaryrefslogtreecommitdiff
path: root/epan/exported_pdu.c
blob: 166797dd3f5a5820735cd4475207d9b63c77ef30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* 
 * exported_pdu.c
 * exported_pdu helper functions
 * Copyright 2013, Anders Broman <anders-broman@ericsson.com>
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include <glib.h>

#include <epan/packet.h>
#include <epan/exported_pdu.h>

/**
 * Allocates and fills the exp_pdu_data_t struct according to the wanted_exp_tags
 * bit_fileld, if proto_name is != NULL, wtap_encap must be -1 or vice-versa
 */
exp_pdu_data_t *
load_export_pdu_tags(packet_info *pinfo, const char* proto_name, int wtap_encap _U_, guint32 tags_bit_field)
{
	exp_pdu_data_t *exp_pdu_data;
	int tag_buf_size = 0;
	int str_len = 0;
	int tag_str_len = 0;
	int i = 0;

	exp_pdu_data = (exp_pdu_data_t *)g_malloc(sizeof(exp_pdu_data_t));

	/* If we have a protocol name, calculate the buffer size needed including padding and tag + length */
	if(proto_name){
		str_len = (int)strlen(proto_name);

		/* Ensure that tag length is a multiple of 4 bytes */
		tag_str_len = (str_len + 3) & 0xfffffffc;
		/* Add Tag + length */
		tag_buf_size = tag_str_len + 4;
	}

	if((tags_bit_field & EXP_PDU_TAG_IP_SRC_BIT) == EXP_PDU_TAG_IP_SRC_BIT){
		/* tag+length */
		tag_buf_size+=4;
		if(pinfo->net_src.type == AT_IPv4){
			tag_buf_size = tag_buf_size + EXP_PDU_TAG_IPV4_SRC_LEN;
		}else{
			tag_buf_size = tag_buf_size + EXP_PDU_TAG_IPV6_SRC_LEN;
		}
	}

	if((tags_bit_field & EXP_PDU_TAG_IP_DST_BIT) == EXP_PDU_TAG_IP_DST_BIT){
		/* tag+length */
		tag_buf_size+=4;
		if(pinfo->net_dst.type == AT_IPv4){
			tag_buf_size = tag_buf_size + EXP_PDU_TAG_IPV4_DST_LEN;
		}else{
			tag_buf_size = tag_buf_size + EXP_PDU_TAG_IPV6_DST_LEN;
		}
	}

	if((tags_bit_field & EXP_PDU_TAG_SRC_PORT_BIT) == EXP_PDU_TAG_SRC_PORT_BIT){
		tag_buf_size= tag_buf_size + EXP_PDU_TAG_SRC_PORT_LEN + 4;
	}

	if((tags_bit_field & EXP_PDU_TAG_DST_PORT_BIT) == EXP_PDU_TAG_DST_PORT_BIT){
		tag_buf_size= tag_buf_size + EXP_PDU_TAG_DST_PORT_LEN + 4;
	}

	/* Add end of options length */
	tag_buf_size+=4;

	exp_pdu_data->tlv_buffer = (guint8 *)g_malloc0(tag_buf_size);
	exp_pdu_data->tlv_buffer_len = tag_buf_size;

	if(proto_name){
		exp_pdu_data->tlv_buffer[i] = 0;
		i++;
		exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_PROTO_NAME;
		i++;
		exp_pdu_data->tlv_buffer[i] = 0;
		i++;
		exp_pdu_data->tlv_buffer[i] = tag_str_len; /* tag length */
		i++;
		memcpy(exp_pdu_data->tlv_buffer+i, proto_name, str_len);
		i = i + tag_str_len;

	}

	if((tags_bit_field & EXP_PDU_TAG_IP_SRC_BIT) == EXP_PDU_TAG_IP_SRC_BIT){
		if(pinfo->net_src.type == AT_IPv4){
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_SRC;
			i++;
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_SRC_LEN; /* tag length */
			i++;
		}else{
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_SRC;
			i++;
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_SRC_LEN; /* tag length */
			i++;
		}

		memcpy(exp_pdu_data->tlv_buffer+i, pinfo->net_src.data, pinfo->net_src.len);
		i += (pinfo->net_src.type == AT_IPv4) ? EXP_PDU_TAG_IPV4_SRC_LEN : EXP_PDU_TAG_IPV6_SRC_LEN;
	}

	if((tags_bit_field & EXP_PDU_TAG_IP_DST_BIT) == EXP_PDU_TAG_IP_DST_BIT){
		if(pinfo->net_dst.type == AT_IPv4){
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_DST;
			i++;
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_DST_LEN; /* tag length */
			i++;
		}else{
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_DST;
			i++;
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_DST_LEN; /* tag length */
			i++;
		}

		memcpy(exp_pdu_data->tlv_buffer+i, pinfo->net_dst.data, pinfo->net_dst.len);
		i += (pinfo->net_dst.type == AT_IPv4) ? EXP_PDU_TAG_IPV4_DST_LEN : EXP_PDU_TAG_IPV6_DST_LEN;
	}

	if((tags_bit_field & EXP_PDU_TAG_SRC_PORT_BIT) == EXP_PDU_TAG_SRC_PORT_BIT){
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SRC_PORT;
			i++;
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SRC_PORT_LEN; /* tag length */
			i++;
			exp_pdu_data->tlv_buffer[i]   = (pinfo->srcport & 0xff000000) >> 24;
			exp_pdu_data->tlv_buffer[i+1] = (pinfo->srcport & 0x00ff0000) >> 16;
			exp_pdu_data->tlv_buffer[i+2] = (pinfo->srcport & 0x0000ff00) >> 8;
			exp_pdu_data->tlv_buffer[i+3] = (pinfo->srcport & 0x000000ff);
			i = i +EXP_PDU_TAG_SRC_PORT_LEN;
	}

	if((tags_bit_field & EXP_PDU_TAG_DST_PORT_BIT) == EXP_PDU_TAG_DST_PORT_BIT){
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_DST_PORT;
			i++;
			exp_pdu_data->tlv_buffer[i] = 0;
			i++;
			exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_DST_PORT_LEN; /* tag length */
			i++;
			exp_pdu_data->tlv_buffer[i]   = (pinfo->destport & 0xff000000) >> 24;
			exp_pdu_data->tlv_buffer[i+1] = (pinfo->destport & 0x00ff0000) >> 16;
			exp_pdu_data->tlv_buffer[i+2] = (pinfo->destport & 0x0000ff00) >> 8;
			exp_pdu_data->tlv_buffer[i+3] = (pinfo->destport & 0x000000ff);
			/*i = i +EXP_PDU_TAG_DST_PORT_LEN;*/
	}

	return exp_pdu_data;

}