PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/epan/exported_pdu.c

https://github.com/labx-technologies-llc/wireshark
C | 353 lines | 290 code | 29 blank | 34 comment | 74 complexity | 558649003abe00847553a369172b63aa MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /*
  2. * exported_pdu.c
  3. * exported_pdu helper functions
  4. * Copyright 2013, Anders Broman <anders-broman@ericsson.com>
  5. *
  6. * $Id$
  7. *
  8. * Wireshark - Network traffic analyzer
  9. * By Gerald Combs <gerald@wireshark.org>
  10. * Copyright 1998 Gerald Combs
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. */
  26. #include "config.h"
  27. #include <glib.h>
  28. #include <epan/packet.h>
  29. #include <epan/exported_pdu.h>
  30. #include <epan/dissectors/packet-mtp3.h>
  31. #include <epan/dissectors/packet-dvbci.h>
  32. /**
  33. * Allocates and fills the exp_pdu_data_t struct according to the wanted_exp_tags
  34. * bit_fileld, if proto_name is != NULL, wtap_encap must be -1 or vice-versa
  35. */
  36. exp_pdu_data_t *
  37. load_export_pdu_tags(packet_info *pinfo, const char* proto_name, int wtap_encap _U_, guint32 tags_bit_field)
  38. {
  39. exp_pdu_data_t *exp_pdu_data;
  40. int tag_buf_size = 0;
  41. int str_len = 0;
  42. int tag_str_len = 0;
  43. int i = 0, j;
  44. gboolean port_type_defined = FALSE;
  45. exp_pdu_data = (exp_pdu_data_t *)g_malloc(sizeof(exp_pdu_data_t));
  46. /* If we have a protocol name, calculate the buffer size needed including padding and tag + length */
  47. if(proto_name){
  48. str_len = (int)strlen(proto_name);
  49. /* Ensure that tag length is a multiple of 4 bytes */
  50. tag_str_len = (str_len + 3) & 0xfffffffc;
  51. /* Add Tag + length */
  52. tag_buf_size = tag_str_len + 4;
  53. }
  54. if((tags_bit_field & EXP_PDU_TAG_IP_SRC_BIT) == EXP_PDU_TAG_IP_SRC_BIT){
  55. if(pinfo->net_src.type == AT_IPv4){
  56. tag_buf_size += 4 + EXP_PDU_TAG_IPV4_SRC_LEN;
  57. }else if(pinfo->net_src.type == AT_IPv6){
  58. tag_buf_size += 4 + EXP_PDU_TAG_IPV6_SRC_LEN;
  59. }
  60. }
  61. if((tags_bit_field & EXP_PDU_TAG_IP_DST_BIT) == EXP_PDU_TAG_IP_DST_BIT){
  62. if(pinfo->net_dst.type == AT_IPv4){
  63. tag_buf_size += 4 + EXP_PDU_TAG_IPV4_DST_LEN;
  64. }else if(pinfo->net_dst.type == AT_IPv6){
  65. tag_buf_size += 4 + EXP_PDU_TAG_IPV6_DST_LEN;
  66. }
  67. }
  68. if((tags_bit_field & EXP_PDU_TAG_SRC_PORT_BIT) == EXP_PDU_TAG_SRC_PORT_BIT){
  69. if (!port_type_defined) {
  70. tag_buf_size= tag_buf_size + EXP_PDU_TAG_PORT_TYPE_LEN + 4;
  71. port_type_defined = TRUE;
  72. }
  73. tag_buf_size= tag_buf_size + EXP_PDU_TAG_SRC_PORT_LEN + 4;
  74. }
  75. if((tags_bit_field & EXP_PDU_TAG_DST_PORT_BIT) == EXP_PDU_TAG_DST_PORT_BIT){
  76. if (!port_type_defined) {
  77. tag_buf_size= tag_buf_size + EXP_PDU_TAG_PORT_TYPE_LEN + 4;
  78. }
  79. tag_buf_size= tag_buf_size + EXP_PDU_TAG_DST_PORT_LEN + 4;
  80. }
  81. if((tags_bit_field & EXP_PDU_TAG_SCTP_PPID_BIT) == EXP_PDU_TAG_SCTP_PPID_BIT){
  82. for(j = 0; j < MAX_NUMBER_OF_PPIDS; j++) {
  83. if (pinfo->ppids[j] != LAST_PPID) {
  84. tag_buf_size= tag_buf_size + EXP_PDU_TAG_SCTP_PPID_LEN + 4;
  85. } else {
  86. break;
  87. }
  88. }
  89. }
  90. if((tags_bit_field & EXP_PDU_TAG_SS7_OPC_BIT) == EXP_PDU_TAG_SS7_OPC_BIT){
  91. if(pinfo->src.type == AT_SS7PC){
  92. tag_buf_size += 4 + EXP_PDU_TAG_SS7_OPC_LEN;
  93. }
  94. }
  95. if((tags_bit_field & EXP_PDU_TAG_SS7_DPC_BIT) == EXP_PDU_TAG_SS7_DPC_BIT){
  96. if(pinfo->dst.type == AT_SS7PC){
  97. tag_buf_size += 4 + EXP_PDU_TAG_SS7_DPC_LEN;
  98. }
  99. }
  100. if((tags_bit_field & EXP_PDU_TAG_ORIG_FNO_BIT) == EXP_PDU_TAG_ORIG_FNO_BIT){
  101. tag_buf_size= tag_buf_size + EXP_PDU_TAG_ORIG_FNO_LEN + 4;
  102. }
  103. if((tags_bit_field & EXP_PDU_TAG_DVBCI_EVT_BIT) == EXP_PDU_TAG_DVBCI_EVT_BIT){
  104. tag_buf_size= tag_buf_size + EXP_PDU_TAG_DVBCI_EVT_LEN + 4;
  105. }
  106. /* Add end of options length */
  107. tag_buf_size+=4;
  108. exp_pdu_data->tlv_buffer = (guint8 *)g_malloc0(tag_buf_size);
  109. exp_pdu_data->tlv_buffer_len = tag_buf_size;
  110. port_type_defined = FALSE;
  111. if(proto_name){
  112. exp_pdu_data->tlv_buffer[i] = 0;
  113. i++;
  114. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_PROTO_NAME;
  115. i++;
  116. exp_pdu_data->tlv_buffer[i] = 0;
  117. i++;
  118. exp_pdu_data->tlv_buffer[i] = tag_str_len; /* tag length */
  119. i++;
  120. memcpy(exp_pdu_data->tlv_buffer+i, proto_name, str_len);
  121. i = i + tag_str_len;
  122. }
  123. if((tags_bit_field & EXP_PDU_TAG_IP_SRC_BIT) == EXP_PDU_TAG_IP_SRC_BIT){
  124. if(pinfo->net_src.type == AT_IPv4){
  125. exp_pdu_data->tlv_buffer[i] = 0;
  126. i++;
  127. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_SRC;
  128. i++;
  129. exp_pdu_data->tlv_buffer[i] = 0;
  130. i++;
  131. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_SRC_LEN; /* tag length */
  132. i++;
  133. memcpy(exp_pdu_data->tlv_buffer+i, pinfo->net_src.data, EXP_PDU_TAG_IPV4_SRC_LEN);
  134. i += EXP_PDU_TAG_IPV4_SRC_LEN;
  135. }else if(pinfo->net_src.type == AT_IPv6){
  136. exp_pdu_data->tlv_buffer[i] = 0;
  137. i++;
  138. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_SRC;
  139. i++;
  140. exp_pdu_data->tlv_buffer[i] = 0;
  141. i++;
  142. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_SRC_LEN; /* tag length */
  143. i++;
  144. memcpy(exp_pdu_data->tlv_buffer+i, pinfo->net_src.data, EXP_PDU_TAG_IPV6_SRC_LEN);
  145. i += EXP_PDU_TAG_IPV6_SRC_LEN;
  146. }
  147. }
  148. if((tags_bit_field & EXP_PDU_TAG_IP_DST_BIT) == EXP_PDU_TAG_IP_DST_BIT){
  149. if(pinfo->net_dst.type == AT_IPv4){
  150. exp_pdu_data->tlv_buffer[i] = 0;
  151. i++;
  152. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_DST;
  153. i++;
  154. exp_pdu_data->tlv_buffer[i] = 0;
  155. i++;
  156. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV4_DST_LEN; /* tag length */
  157. i++;
  158. memcpy(exp_pdu_data->tlv_buffer+i, pinfo->net_dst.data, EXP_PDU_TAG_IPV4_DST_LEN);
  159. i += EXP_PDU_TAG_IPV4_DST_LEN;
  160. }else if(pinfo->net_dst.type == AT_IPv6){
  161. exp_pdu_data->tlv_buffer[i] = 0;
  162. i++;
  163. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_DST;
  164. i++;
  165. exp_pdu_data->tlv_buffer[i] = 0;
  166. i++;
  167. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_IPV6_DST_LEN; /* tag length */
  168. i++;
  169. memcpy(exp_pdu_data->tlv_buffer+i, pinfo->net_dst.data, EXP_PDU_TAG_IPV6_DST_LEN);
  170. i += EXP_PDU_TAG_IPV6_DST_LEN;
  171. }
  172. }
  173. if((tags_bit_field & EXP_PDU_TAG_SRC_PORT_BIT) == EXP_PDU_TAG_SRC_PORT_BIT){
  174. if (!port_type_defined) {
  175. exp_pdu_data->tlv_buffer[i] = 0;
  176. i++;
  177. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_PORT_TYPE;
  178. i++;
  179. exp_pdu_data->tlv_buffer[i] = 0;
  180. i++;
  181. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_PORT_TYPE_LEN; /* tag length */
  182. i++;
  183. exp_pdu_data->tlv_buffer[i] = (pinfo->ptype & 0xff000000) >> 24;
  184. exp_pdu_data->tlv_buffer[i+1] = (pinfo->ptype & 0x00ff0000) >> 16;
  185. exp_pdu_data->tlv_buffer[i+2] = (pinfo->ptype & 0x0000ff00) >> 8;
  186. exp_pdu_data->tlv_buffer[i+3] = (pinfo->ptype & 0x000000ff);
  187. i = i +EXP_PDU_TAG_PORT_TYPE_LEN;
  188. port_type_defined = TRUE;
  189. }
  190. exp_pdu_data->tlv_buffer[i] = 0;
  191. i++;
  192. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SRC_PORT;
  193. i++;
  194. exp_pdu_data->tlv_buffer[i] = 0;
  195. i++;
  196. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SRC_PORT_LEN; /* tag length */
  197. i++;
  198. exp_pdu_data->tlv_buffer[i] = (pinfo->srcport & 0xff000000) >> 24;
  199. exp_pdu_data->tlv_buffer[i+1] = (pinfo->srcport & 0x00ff0000) >> 16;
  200. exp_pdu_data->tlv_buffer[i+2] = (pinfo->srcport & 0x0000ff00) >> 8;
  201. exp_pdu_data->tlv_buffer[i+3] = (pinfo->srcport & 0x000000ff);
  202. i = i +EXP_PDU_TAG_SRC_PORT_LEN;
  203. }
  204. if((tags_bit_field & EXP_PDU_TAG_DST_PORT_BIT) == EXP_PDU_TAG_DST_PORT_BIT){
  205. if (!port_type_defined) {
  206. exp_pdu_data->tlv_buffer[i] = 0;
  207. i++;
  208. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_PORT_TYPE;
  209. i++;
  210. exp_pdu_data->tlv_buffer[i] = 0;
  211. i++;
  212. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_PORT_TYPE_LEN; /* tag length */
  213. i++;
  214. exp_pdu_data->tlv_buffer[i] = (pinfo->ptype & 0xff000000) >> 24;
  215. exp_pdu_data->tlv_buffer[i+1] = (pinfo->ptype & 0x00ff0000) >> 16;
  216. exp_pdu_data->tlv_buffer[i+2] = (pinfo->ptype & 0x0000ff00) >> 8;
  217. exp_pdu_data->tlv_buffer[i+3] = (pinfo->ptype & 0x000000ff);
  218. i = i +EXP_PDU_TAG_PORT_TYPE_LEN;
  219. }
  220. exp_pdu_data->tlv_buffer[i] = 0;
  221. i++;
  222. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_DST_PORT;
  223. i++;
  224. exp_pdu_data->tlv_buffer[i] = 0;
  225. i++;
  226. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_DST_PORT_LEN; /* tag length */
  227. i++;
  228. exp_pdu_data->tlv_buffer[i] = (pinfo->destport & 0xff000000) >> 24;
  229. exp_pdu_data->tlv_buffer[i+1] = (pinfo->destport & 0x00ff0000) >> 16;
  230. exp_pdu_data->tlv_buffer[i+2] = (pinfo->destport & 0x0000ff00) >> 8;
  231. exp_pdu_data->tlv_buffer[i+3] = (pinfo->destport & 0x000000ff);
  232. i = i +EXP_PDU_TAG_DST_PORT_LEN;
  233. }
  234. if((tags_bit_field & EXP_PDU_TAG_SCTP_PPID_BIT) == EXP_PDU_TAG_SCTP_PPID_BIT){
  235. for(j = 0; j < MAX_NUMBER_OF_PPIDS; j++) {
  236. if (pinfo->ppids[j] != LAST_PPID) {
  237. exp_pdu_data->tlv_buffer[i] = 0;
  238. i++;
  239. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SCTP_PPID;
  240. i++;
  241. exp_pdu_data->tlv_buffer[i] = 0;
  242. i++;
  243. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SCTP_PPID_LEN; /* tag length */
  244. i++;
  245. exp_pdu_data->tlv_buffer[i] = (pinfo->ppids[j] & 0xff000000) >> 24;
  246. exp_pdu_data->tlv_buffer[i+1] = (pinfo->ppids[j] & 0x00ff0000) >> 16;
  247. exp_pdu_data->tlv_buffer[i+2] = (pinfo->ppids[j] & 0x0000ff00) >> 8;
  248. exp_pdu_data->tlv_buffer[i+3] = (pinfo->ppids[j] & 0x000000ff);
  249. i = i +EXP_PDU_TAG_SCTP_PPID_LEN;
  250. } else {
  251. break;
  252. }
  253. }
  254. }
  255. if((tags_bit_field & EXP_PDU_TAG_SS7_OPC_BIT) == EXP_PDU_TAG_SS7_OPC_BIT){
  256. if(pinfo->src.type == AT_SS7PC){
  257. mtp3_addr_pc_t *mtp3_addr = (mtp3_addr_pc_t *)(pinfo->src.data);
  258. exp_pdu_data->tlv_buffer[i] = 0;
  259. i++;
  260. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SS7_OPC;
  261. i++;
  262. exp_pdu_data->tlv_buffer[i] = 0;
  263. i++;
  264. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SS7_OPC_LEN; /* tag length */
  265. i++;
  266. exp_pdu_data->tlv_buffer[i] = (mtp3_addr->pc & 0xff000000) >> 24;
  267. exp_pdu_data->tlv_buffer[i+1] = (mtp3_addr->pc & 0x00ff0000) >> 16;
  268. exp_pdu_data->tlv_buffer[i+2] = (mtp3_addr->pc & 0x0000ff00) >> 8;
  269. exp_pdu_data->tlv_buffer[i+3] = (mtp3_addr->pc & 0x000000ff);
  270. exp_pdu_data->tlv_buffer[i+4] = (mtp3_addr->type & 0xff00) >> 8;
  271. exp_pdu_data->tlv_buffer[i+5] = (mtp3_addr->type & 0x00ff);
  272. exp_pdu_data->tlv_buffer[i+6] = mtp3_addr->ni;
  273. i += EXP_PDU_TAG_SS7_OPC_LEN;
  274. }
  275. }
  276. if((tags_bit_field & EXP_PDU_TAG_SS7_DPC_BIT) == EXP_PDU_TAG_SS7_DPC_BIT){
  277. if(pinfo->dst.type == AT_SS7PC){
  278. mtp3_addr_pc_t *mtp3_addr = (mtp3_addr_pc_t *)(pinfo->dst.data);
  279. exp_pdu_data->tlv_buffer[i] = 0;
  280. i++;
  281. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SS7_DPC;
  282. i++;
  283. exp_pdu_data->tlv_buffer[i] = 0;
  284. i++;
  285. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_SS7_DPC_LEN; /* tag length */
  286. i++;
  287. exp_pdu_data->tlv_buffer[i] = (mtp3_addr->pc & 0xff000000) >> 24;
  288. exp_pdu_data->tlv_buffer[i+1] = (mtp3_addr->pc & 0x00ff0000) >> 16;
  289. exp_pdu_data->tlv_buffer[i+2] = (mtp3_addr->pc & 0x0000ff00) >> 8;
  290. exp_pdu_data->tlv_buffer[i+3] = (mtp3_addr->pc & 0x000000ff);
  291. exp_pdu_data->tlv_buffer[i+4] = (mtp3_addr->type & 0xff00) >> 8;
  292. exp_pdu_data->tlv_buffer[i+5] = (mtp3_addr->type & 0x00ff);
  293. exp_pdu_data->tlv_buffer[i+6] = mtp3_addr->ni;
  294. i += EXP_PDU_TAG_SS7_DPC_LEN;
  295. }
  296. }
  297. if((tags_bit_field & EXP_PDU_TAG_ORIG_FNO_BIT) == EXP_PDU_TAG_ORIG_FNO_BIT){
  298. exp_pdu_data->tlv_buffer[i] = 0;
  299. i++;
  300. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_ORIG_FNO;
  301. i++;
  302. exp_pdu_data->tlv_buffer[i] = 0;
  303. i++;
  304. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_ORIG_FNO_LEN; /* tag length */
  305. i++;
  306. exp_pdu_data->tlv_buffer[i] = (pinfo->fd->num & 0xff000000) >> 24;
  307. exp_pdu_data->tlv_buffer[i+1] = (pinfo->fd->num & 0x00ff0000) >> 16;
  308. exp_pdu_data->tlv_buffer[i+2] = (pinfo->fd->num & 0x0000ff00) >> 8;
  309. exp_pdu_data->tlv_buffer[i+3] = (pinfo->fd->num & 0x000000ff);
  310. /*i = i +EXP_PDU_TAG_ORIG_FNO_LEN;*/
  311. }
  312. if((tags_bit_field & EXP_PDU_TAG_DVBCI_EVT_BIT) == EXP_PDU_TAG_DVBCI_EVT_BIT){
  313. exp_pdu_data->tlv_buffer[i] = 0;
  314. i++;
  315. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_DVBCI_EVT;
  316. i++;
  317. exp_pdu_data->tlv_buffer[i] = 0;
  318. i++;
  319. exp_pdu_data->tlv_buffer[i] = EXP_PDU_TAG_DVBCI_EVT_LEN;
  320. i++;
  321. exp_pdu_data->tlv_buffer[i] = dvbci_get_evt_from_addrs(pinfo);
  322. }
  323. return exp_pdu_data;
  324. }