PageRenderTime 72ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/epan/proto.c

https://github.com/labx-technologies-llc/wireshark
C | 7564 lines | 5237 code | 1162 blank | 1165 comment | 898 complexity | 949f7def75a5a6f0cc4c3a4d9679517f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /* proto.c
  2. * Routines for protocol tree
  3. *
  4. * $Id$
  5. *
  6. * Wireshark - Network traffic analyzer
  7. * By Gerald Combs <gerald@wireshark.org>
  8. * Copyright 1998 Gerald Combs
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. */
  24. #include "config.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <glib.h>
  29. #include <float.h>
  30. #include "packet.h"
  31. #include "ptvcursor.h"
  32. #include "strutil.h"
  33. #include "addr_resolv.h"
  34. #include "oids.h"
  35. #include "plugins.h"
  36. #include "proto.h"
  37. #include "epan_dissect.h"
  38. #include "tvbuff.h"
  39. #include "emem.h"
  40. #include "charsets.h"
  41. #include "asm_utils.h"
  42. #include "column-utils.h"
  43. #include "to_str.h"
  44. #include "expert.h"
  45. #include "show_exception.h"
  46. #include "wspython/wspy_register.h"
  47. #define SUBTREE_ONCE_ALLOCATION_NUMBER 8
  48. #define SUBTREE_MAX_LEVELS 256
  49. /* Throw an exception if we exceed this many tree items. */
  50. /* XXX - This should probably be a preference */
  51. #define MAX_TREE_ITEMS (1 * 1000 * 1000)
  52. typedef struct __subtree_lvl {
  53. gint cursor_offset;
  54. proto_item *it;
  55. proto_tree *tree;
  56. } subtree_lvl;
  57. struct ptvcursor {
  58. subtree_lvl *pushed_tree;
  59. guint8 pushed_tree_index;
  60. guint8 pushed_tree_max;
  61. proto_tree *tree;
  62. tvbuff_t *tvb;
  63. gint offset;
  64. };
  65. #define cVALS(x) (const value_string*)(x)
  66. /** See inlined comments.
  67. @param tree the tree to append this item to
  68. @param hfindex field index
  69. @param hfinfo header_field
  70. @return the header field matching 'hfinfo' */
  71. #define TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo) \
  72. /* If this item is not referenced we dont have to do much work \
  73. at all but we should still return a node so that field items \
  74. below this node (think proto_item_add_subtree()) will still \
  75. have somewhere to attach to or else filtering will not work \
  76. (they would be ignored since tree would be NULL). \
  77. DONT try to fake a node where PTREE_FINFO(tree) is NULL \
  78. since dissectors that want to do proto_item_set_len() or \
  79. other operations that dereference this would crash. \
  80. We fake FT_PROTOCOL unless some clients have requested us \
  81. not to do so. \
  82. */ \
  83. if (!tree) \
  84. return NULL; \
  85. PTREE_DATA(tree)->count++; \
  86. if (PTREE_DATA(tree)->count > MAX_TREE_ITEMS) { \
  87. if (getenv("WIRESHARK_ABORT_ON_TOO_MANY_ITEMS") != NULL) \
  88. g_error("More than %d items in the tree -- possible infinite loop", MAX_TREE_ITEMS); \
  89. /* Let the exception handler add items to the tree */ \
  90. PTREE_DATA(tree)->count = 0; \
  91. THROW_MESSAGE(DissectorError, \
  92. ep_strdup_printf("More than %d items in the tree -- possible infinite loop", MAX_TREE_ITEMS)); \
  93. } \
  94. PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo); \
  95. if (!(PTREE_DATA(tree)->visible)) { \
  96. if (PTREE_FINFO(tree)) { \
  97. if ((hfinfo->ref_type != HF_REF_TYPE_DIRECT) \
  98. && (hfinfo->type != FT_PROTOCOL || \
  99. PTREE_DATA(tree)->fake_protocols)) { \
  100. /* just return tree back to the caller */\
  101. return tree; \
  102. } \
  103. } \
  104. }
  105. /** See inlined comments.
  106. @param pi the created protocol item we're about to return */
  107. #define TRY_TO_FAKE_THIS_REPR(pi) \
  108. g_assert(pi); \
  109. if (!(PTREE_DATA(pi)->visible)) { \
  110. /* If the tree (GUI) isn't visible it's pointless for us to generate the protocol \
  111. * items string representation */ \
  112. return pi; \
  113. }
  114. static const char *hf_try_val_to_str(guint32 value, const header_field_info *hfinfo);
  115. static void fill_label_boolean(field_info *fi, gchar *label_str);
  116. static void fill_label_bitfield(field_info *fi, gchar *label_str);
  117. static void fill_label_number(field_info *fi, gchar *label_str, gboolean is_signed);
  118. static void fill_label_number64(field_info *fi, gchar *label_str, gboolean is_signed);
  119. static const char *hfinfo_number_value_format_display(const header_field_info *hfinfo, int display, char buf[32], guint32 value);
  120. static const char *hfinfo_number_vals_format(const header_field_info *hfinfo, char buf[32], guint32 value);
  121. static const char *hfinfo_number_value_format(const header_field_info *hfinfo, char buf[32], guint32 value);
  122. static const char *hfinfo_numeric_value_format(const header_field_info *hfinfo, char buf[32], guint32 value);
  123. static const char* hfinfo_uint64_format(const header_field_info *hfinfo);
  124. static const char* hfinfo_int64_format(const header_field_info *hfinfo);
  125. static proto_item *
  126. proto_tree_add_node(proto_tree *tree, field_info *fi);
  127. static void
  128. get_hfi_length(header_field_info *hfinfo, tvbuff_t *tvb, const gint start, gint *length,
  129. gint *item_length);
  130. static field_info *
  131. new_field_info(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb,
  132. const gint start, const gint item_length);
  133. static field_info *
  134. alloc_field_info(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb,
  135. const gint start, gint *length);
  136. static proto_item *
  137. proto_tree_add_pi(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb,
  138. gint start, gint *length, field_info **pfi);
  139. static void
  140. proto_tree_set_representation_value(proto_item *pi, const char *format, va_list ap);
  141. static void
  142. proto_tree_set_representation(proto_item *pi, const char *format, va_list ap);
  143. static void
  144. proto_tree_set_protocol_tvb(field_info *fi, tvbuff_t *tvb);
  145. static void
  146. proto_tree_set_bytes(field_info *fi, const guint8* start_ptr, gint length);
  147. static void
  148. proto_tree_set_bytes_tvb(field_info *fi, tvbuff_t *tvb, gint offset, gint length);
  149. static void
  150. proto_tree_set_time(field_info *fi, nstime_t *value_ptr);
  151. static void
  152. proto_tree_set_string(field_info *fi, const char* value);
  153. static void
  154. proto_tree_set_string_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length, gint encoding);
  155. static void
  156. proto_tree_set_ax25(field_info *fi, const guint8* value);
  157. static void
  158. proto_tree_set_ax25_tvb(field_info *fi, tvbuff_t *tvb, gint start);
  159. static void
  160. proto_tree_set_vines(field_info *fi, const guint8* value);
  161. static void
  162. proto_tree_set_vines_tvb(field_info *fi, tvbuff_t *tvb, gint start);
  163. static void
  164. proto_tree_set_ether(field_info *fi, const guint8* value);
  165. static void
  166. proto_tree_set_ether_tvb(field_info *fi, tvbuff_t *tvb, gint start);
  167. static void
  168. proto_tree_set_ipxnet(field_info *fi, guint32 value);
  169. static void
  170. proto_tree_set_ipv4(field_info *fi, guint32 value);
  171. static void
  172. proto_tree_set_ipv6(field_info *fi, const guint8* value_ptr);
  173. static void
  174. proto_tree_set_ipv6_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length);
  175. static void
  176. proto_tree_set_guid(field_info *fi, const e_guid_t *value_ptr);
  177. static void
  178. proto_tree_set_guid_tvb(field_info *fi, tvbuff_t *tvb, gint start, const guint encoding);
  179. static void
  180. proto_tree_set_oid(field_info *fi, const guint8* value_ptr, gint length);
  181. static void
  182. proto_tree_set_oid_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length);
  183. static void
  184. proto_tree_set_boolean(field_info *fi, guint32 value);
  185. static void
  186. proto_tree_set_float(field_info *fi, float value);
  187. static void
  188. proto_tree_set_double(field_info *fi, double value);
  189. static void
  190. proto_tree_set_uint(field_info *fi, guint32 value);
  191. static void
  192. proto_tree_set_int(field_info *fi, gint32 value);
  193. static void
  194. proto_tree_set_uint64(field_info *fi, guint64 value);
  195. static void
  196. proto_tree_set_uint64_tvb(field_info *fi, tvbuff_t *tvb, gint start, guint length, const guint encoding);
  197. static void
  198. proto_tree_set_eui64(field_info *fi, const guint64 value);
  199. static void
  200. proto_tree_set_eui64_tvb(field_info *fi, tvbuff_t *tvb, gint start, const guint encoding);
  201. static gboolean
  202. proto_item_add_bitmask_tree(proto_item *item, tvbuff_t *tvb, const int offset,
  203. const int len, const gint ett, const gint **fields,
  204. const guint encoding, const int flags,
  205. gboolean first);
  206. static int proto_register_field_init(header_field_info *hfinfo, const int parent);
  207. /* special-case header field used within proto.c */
  208. static header_field_info hfi_text_only =
  209. { "Text item", "text", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL };
  210. int hf_text_only = -1;
  211. /* Structure for information about a protocol */
  212. struct _protocol {
  213. const char *name; /* long description */
  214. const char *short_name; /* short description */
  215. const char *filter_name; /* name of this protocol in filters */
  216. int proto_id; /* field ID for this protocol */
  217. GSList *fields; /* fields for this protocol */
  218. GSList *last_field; /* pointer to end of list of fields */
  219. gboolean is_enabled; /* TRUE if protocol is enabled */
  220. gboolean can_toggle; /* TRUE if is_enabled can be changed */
  221. gboolean is_private; /* TRUE is protocol is private */
  222. };
  223. /* List of all protocols */
  224. static GList *protocols = NULL;
  225. #define INITIAL_NUM_PROTOCOL_HFINFO 1500
  226. /* Contains information about a field when a dissector calls
  227. * proto_tree_add_item. */
  228. #define FIELD_INFO_NEW(fi) fi = g_slice_new(field_info)
  229. #define FIELD_INFO_FREE(fi) g_slice_free(field_info, fi)
  230. /* Contains the space for proto_nodes. */
  231. #define PROTO_NODE_NEW(node) \
  232. node = g_slice_new(proto_node); \
  233. node->first_child = NULL; \
  234. node->last_child = NULL; \
  235. node->next = NULL;
  236. #define PROTO_NODE_FREE(node) \
  237. g_slice_free(proto_node, node)
  238. /* String space for protocol and field items for the GUI */
  239. #define ITEM_LABEL_NEW(il) \
  240. il = g_slice_new(item_label_t);
  241. #define ITEM_LABEL_FREE(il) \
  242. g_slice_free(item_label_t, il);
  243. #define PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo) \
  244. if((guint)hfindex >= gpa_hfinfo.len && getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG")) \
  245. g_error("Unregistered hf! index=%d", hfindex); \
  246. DISSECTOR_ASSERT_HINT((guint)hfindex < gpa_hfinfo.len, "Unregistered hf!"); \
  247. hfinfo = gpa_hfinfo.hfi[hfindex];
  248. /* List which stores protocols and fields that have been registered */
  249. typedef struct _gpa_hfinfo_t {
  250. guint32 len;
  251. guint32 allocated_len;
  252. header_field_info **hfi;
  253. } gpa_hfinfo_t;
  254. static gpa_hfinfo_t gpa_hfinfo;
  255. /* Balanced tree of abbreviations and IDs */
  256. static GTree *gpa_name_tree = NULL;
  257. static header_field_info *same_name_hfinfo;
  258. static void save_same_name_hfinfo(gpointer data)
  259. {
  260. same_name_hfinfo = (header_field_info*)data;
  261. }
  262. /* Points to the first element of an array of bits, indexed by
  263. a subtree item type; that array element is TRUE if subtrees of
  264. an item of that type are to be expanded. */
  265. static guint32 *tree_is_expanded;
  266. /* Number of elements in that array. */
  267. int num_tree_types;
  268. /* Name hashtables for fast detection of duplicate names */
  269. static GHashTable* proto_names = NULL;
  270. static GHashTable* proto_short_names = NULL;
  271. static GHashTable* proto_filter_names = NULL;
  272. static gint
  273. proto_compare_name(gconstpointer p1_arg, gconstpointer p2_arg)
  274. {
  275. const protocol_t *p1 = (const protocol_t *)p1_arg;
  276. const protocol_t *p2 = (const protocol_t *)p2_arg;
  277. return g_ascii_strcasecmp(p1->short_name, p2->short_name);
  278. }
  279. /* initialize data structures and register protocols and fields */
  280. void
  281. proto_init(void (register_all_protocols_func)(register_cb cb, gpointer client_data),
  282. void (register_all_handoffs_func)(register_cb cb, gpointer client_data),
  283. register_cb cb,
  284. gpointer client_data)
  285. {
  286. proto_cleanup();
  287. proto_names = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, NULL);
  288. proto_short_names = g_hash_table_new(wrs_str_hash, g_str_equal);
  289. proto_filter_names = g_hash_table_new(wrs_str_hash, g_str_equal);
  290. gpa_hfinfo.len = 0;
  291. gpa_hfinfo.allocated_len = 0;
  292. gpa_hfinfo.hfi = NULL;
  293. gpa_name_tree = g_tree_new_full(wrs_strcmp_with_data, NULL, NULL, save_same_name_hfinfo);
  294. /* Initialize the ftype subsystem */
  295. ftypes_initialize();
  296. /* Register one special-case FT_TEXT_ONLY field for use when
  297. converting wireshark to new-style proto_tree. These fields
  298. are merely strings on the GUI tree; they are not filterable */
  299. hf_text_only = proto_register_field_init(&hfi_text_only, -1);
  300. /* Register the pseudo-protocols used for exceptions. */
  301. register_show_exception();
  302. /* Have each built-in dissector register its protocols, fields,
  303. dissector tables, and dissectors to be called through a
  304. handle, and do whatever one-time initialization it needs to
  305. do. */
  306. register_all_protocols_func(cb, client_data);
  307. #ifdef HAVE_PYTHON
  308. /* Now scan for python protocols */
  309. if (cb)
  310. (*cb)(RA_PYTHON_REGISTER, NULL, client_data);
  311. register_all_py_protocols_func();
  312. #endif
  313. #ifdef HAVE_PLUGINS
  314. /* Now scan for plugins and load all the ones we find, calling
  315. their register routines to do the stuff described above. */
  316. if (cb)
  317. (*cb)(RA_PLUGIN_REGISTER, NULL, client_data);
  318. init_plugins();
  319. register_all_plugin_registrations();
  320. #endif
  321. /* Now call the "handoff registration" routines of all built-in
  322. dissectors; those routines register the dissector in other
  323. dissectors' handoff tables, and fetch any dissector handles
  324. they need. */
  325. register_all_handoffs_func(cb, client_data);
  326. #ifdef HAVE_PYTHON
  327. /* Now do the same with python dissectors */
  328. if (cb)
  329. (*cb)(RA_PYTHON_HANDOFF, NULL, client_data);
  330. register_all_py_handoffs_func();
  331. #endif
  332. #ifdef HAVE_PLUGINS
  333. /* Now do the same with plugins. */
  334. if (cb)
  335. (*cb)(RA_PLUGIN_HANDOFF, NULL, client_data);
  336. register_all_plugin_handoffs();
  337. #endif
  338. /* sort the protocols by protocol name */
  339. protocols = g_list_sort(protocols, proto_compare_name);
  340. /* We've assigned all the subtree type values; allocate the array
  341. for them, and zero it out. */
  342. tree_is_expanded = g_new0(guint32, (num_tree_types/32)+1);
  343. }
  344. void
  345. proto_cleanup(void)
  346. {
  347. /* Free the abbrev/ID GTree */
  348. if (gpa_name_tree) {
  349. g_tree_destroy(gpa_name_tree);
  350. gpa_name_tree = NULL;
  351. }
  352. while (protocols) {
  353. protocol_t *protocol = (protocol_t *)protocols->data;
  354. header_field_info *hfinfo;
  355. PROTO_REGISTRAR_GET_NTH(protocol->proto_id, hfinfo);
  356. DISSECTOR_ASSERT(protocol->proto_id == hfinfo->id);
  357. g_slice_free(header_field_info, hfinfo);
  358. g_slist_free(protocol->fields);
  359. protocols = g_list_remove(protocols, protocol);
  360. g_free(protocol);
  361. }
  362. if (proto_names) {
  363. g_hash_table_destroy(proto_names);
  364. proto_names = NULL;
  365. }
  366. if (proto_short_names) {
  367. g_hash_table_destroy(proto_short_names);
  368. proto_short_names = NULL;
  369. }
  370. if (proto_filter_names) {
  371. g_hash_table_destroy(proto_filter_names);
  372. proto_filter_names = NULL;
  373. }
  374. if (gpa_hfinfo.allocated_len) {
  375. gpa_hfinfo.len = 0;
  376. gpa_hfinfo.allocated_len = 0;
  377. g_free(gpa_hfinfo.hfi);
  378. gpa_hfinfo.hfi = NULL;
  379. }
  380. g_free(tree_is_expanded);
  381. tree_is_expanded = NULL;
  382. }
  383. static gboolean
  384. proto_tree_traverse_pre_order(proto_tree *tree, proto_tree_traverse_func func,
  385. gpointer data)
  386. {
  387. proto_node *pnode = tree;
  388. proto_node *child;
  389. proto_node *current;
  390. if (func(pnode, data))
  391. return TRUE;
  392. child = pnode->first_child;
  393. while (child != NULL) {
  394. /*
  395. * The routine we call might modify the child, e.g. by
  396. * freeing it, so we get the child's successor before
  397. * calling that routine.
  398. */
  399. current = child;
  400. child = current->next;
  401. if (proto_tree_traverse_pre_order((proto_tree *)current, func, data))
  402. return TRUE;
  403. }
  404. return FALSE;
  405. }
  406. gboolean
  407. proto_tree_traverse_post_order(proto_tree *tree, proto_tree_traverse_func func,
  408. gpointer data)
  409. {
  410. proto_node *pnode = tree;
  411. proto_node *child;
  412. proto_node *current;
  413. child = pnode->first_child;
  414. while (child != NULL) {
  415. /*
  416. * The routine we call might modify the child, e.g. by
  417. * freeing it, so we get the child's successor before
  418. * calling that routine.
  419. */
  420. current = child;
  421. child = current->next;
  422. if (proto_tree_traverse_post_order((proto_tree *)current, func, data))
  423. return TRUE;
  424. }
  425. if (func(pnode, data))
  426. return TRUE;
  427. return FALSE;
  428. }
  429. void
  430. proto_tree_children_foreach(proto_tree *tree, proto_tree_foreach_func func,
  431. gpointer data)
  432. {
  433. proto_node *node = tree;
  434. proto_node *current;
  435. if (!node)
  436. return;
  437. node = node->first_child;
  438. while (node != NULL) {
  439. current = node;
  440. node = current->next;
  441. func((proto_tree *)current, data);
  442. }
  443. }
  444. static void
  445. free_GPtrArray_value(gpointer key, gpointer value, gpointer user_data _U_)
  446. {
  447. GPtrArray *ptrs = (GPtrArray *)value;
  448. gint hfid = (gint)(long)key;
  449. header_field_info *hfinfo;
  450. PROTO_REGISTRAR_GET_NTH(hfid, hfinfo);
  451. if (hfinfo->ref_type != HF_REF_TYPE_NONE) {
  452. /* when a field is referenced by a filter this also
  453. affects the refcount for the parent protocol so we need
  454. to adjust the refcount for the parent as well
  455. */
  456. if (hfinfo->parent != -1) {
  457. header_field_info *parent_hfinfo;
  458. PROTO_REGISTRAR_GET_NTH(hfinfo->parent, parent_hfinfo);
  459. parent_hfinfo->ref_type = HF_REF_TYPE_NONE;
  460. }
  461. hfinfo->ref_type = HF_REF_TYPE_NONE;
  462. }
  463. g_ptr_array_free(ptrs, TRUE);
  464. }
  465. static void
  466. free_node_tree_data(tree_data_t *tree_data)
  467. {
  468. if (tree_data->interesting_hfids) {
  469. /* Free all the GPtrArray's in the interesting_hfids hash. */
  470. g_hash_table_foreach(tree_data->interesting_hfids,
  471. free_GPtrArray_value, NULL);
  472. /* And then destroy the hash. */
  473. g_hash_table_destroy(tree_data->interesting_hfids);
  474. }
  475. if (tree_data->fi_tmp)
  476. FIELD_INFO_FREE(tree_data->fi_tmp);
  477. /* And finally the tree_data_t itself. */
  478. g_free(tree_data);
  479. }
  480. #define FREE_NODE_FIELD_INFO(finfo) \
  481. if (finfo->rep) { \
  482. ITEM_LABEL_FREE(finfo->rep); \
  483. } \
  484. FVALUE_CLEANUP(&finfo->value); \
  485. FIELD_INFO_FREE(finfo);
  486. static void
  487. proto_tree_free_node(proto_node *node, gpointer data _U_)
  488. {
  489. field_info *finfo = PNODE_FINFO(node);
  490. proto_tree_children_foreach(node, proto_tree_free_node, NULL);
  491. /* free the field_info data. */
  492. FREE_NODE_FIELD_INFO(finfo);
  493. node->finfo = NULL;
  494. /* Free the proto_node. */
  495. PROTO_NODE_FREE(node);
  496. }
  497. /* frees the resources that the dissection a proto_tree uses */
  498. void
  499. proto_tree_free(proto_tree *tree)
  500. {
  501. tree_data_t *tree_data = PTREE_DATA(tree);
  502. proto_tree_children_foreach(tree, proto_tree_free_node, NULL);
  503. /* free root node */
  504. PROTO_NODE_FREE(tree);
  505. /* free tree data */
  506. free_node_tree_data(tree_data);
  507. }
  508. /* Is the parsing being done for a visible proto_tree or an invisible one?
  509. * By setting this correctly, the proto_tree creation is sped up by not
  510. * having to call g_vsnprintf and copy strings around.
  511. */
  512. gboolean
  513. proto_tree_set_visible(proto_tree *tree, gboolean visible)
  514. {
  515. gboolean old_visible = PTREE_DATA(tree)->visible;
  516. PTREE_DATA(tree)->visible = visible;
  517. return old_visible;
  518. }
  519. void
  520. proto_tree_set_fake_protocols(proto_tree *tree, gboolean fake_protocols)
  521. {
  522. PTREE_DATA(tree)->fake_protocols = fake_protocols;
  523. }
  524. /* Assume dissector set only its protocol fields.
  525. This function is called by dissectors and allows the speeding up of filtering
  526. in wireshark; if this function returns FALSE it is safe to reset tree to NULL
  527. and thus skip calling most of the expensive proto_tree_add_...()
  528. functions.
  529. If the tree is visible we implicitly assume the field is referenced.
  530. */
  531. gboolean
  532. proto_field_is_referenced(proto_tree *tree, int proto_id)
  533. {
  534. register header_field_info *hfinfo;
  535. if (!tree)
  536. return FALSE;
  537. if (PTREE_DATA(tree)->visible)
  538. return TRUE;
  539. PROTO_REGISTRAR_GET_NTH(proto_id, hfinfo);
  540. if (hfinfo->ref_type != HF_REF_TYPE_NONE)
  541. return TRUE;
  542. if (hfinfo->type == FT_PROTOCOL && !PTREE_DATA(tree)->fake_protocols)
  543. return TRUE;
  544. return FALSE;
  545. }
  546. /* Finds a record in the hfinfo array by id. */
  547. header_field_info *
  548. proto_registrar_get_nth(guint hfindex)
  549. {
  550. register header_field_info *hfinfo;
  551. PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
  552. return hfinfo;
  553. }
  554. /* Prefix initialization
  555. * this allows for a dissector to register a display filter name prefix
  556. * so that it can delay the initialization of the hf array as long as
  557. * possible.
  558. */
  559. /* compute a hash for the part before the dot of a display filter */
  560. static guint
  561. prefix_hash (gconstpointer key) {
  562. /* end the string at the dot and compute its hash */
  563. gchar* copy = ep_strdup((const gchar *)key);
  564. gchar* c = copy;
  565. for (; *c; c++) {
  566. if (*c == '.') {
  567. *c = 0;
  568. break;
  569. }
  570. }
  571. return g_str_hash(copy);
  572. }
  573. /* are both strings equal up to the end or the dot? */
  574. static gboolean
  575. prefix_equal (gconstpointer ap, gconstpointer bp) {
  576. const gchar* a = (const gchar *)ap;
  577. const gchar* b = (const gchar *)bp;
  578. do {
  579. gchar ac = *a++;
  580. gchar bc = *b++;
  581. if ( (ac == '.' || ac == '\0') && (bc == '.' || bc == '\0') ) return TRUE;
  582. if ( (ac == '.' || ac == '\0') && ! (bc == '.' || bc == '\0') ) return FALSE;
  583. if ( (bc == '.' || bc == '\0') && ! (ac == '.' || ac == '\0') ) return FALSE;
  584. if (ac != bc) return FALSE;
  585. } while (1);
  586. return FALSE;
  587. }
  588. /* indexed by prefix, contains initializers */
  589. static GHashTable* prefixes = NULL;
  590. /* Register a new prefix for "delayed" initialization of field arrays */
  591. void
  592. proto_register_prefix(const char *prefix, prefix_initializer_t pi ) {
  593. if (! prefixes ) {
  594. prefixes = g_hash_table_new(prefix_hash, prefix_equal);
  595. }
  596. g_hash_table_insert(prefixes, (gpointer)prefix, pi);
  597. }
  598. /* helper to call all prefix initializers */
  599. static gboolean
  600. initialize_prefix(gpointer k, gpointer v, gpointer u _U_) {
  601. ((prefix_initializer_t)v)((const char *)k);
  602. return TRUE;
  603. }
  604. /** Initialize every remaining uninitialized prefix. */
  605. void
  606. proto_initialize_all_prefixes(void) {
  607. g_hash_table_foreach_remove(prefixes, initialize_prefix, NULL);
  608. }
  609. /* Finds a record in the hfinfo array by name.
  610. * If it fails to find it in the already registered fields,
  611. * it tries to find and call an initializer in the prefixes
  612. * table and if so it looks again.
  613. */
  614. header_field_info *
  615. proto_registrar_get_byname(const char *field_name)
  616. {
  617. header_field_info *hfinfo;
  618. prefix_initializer_t pi;
  619. if (!field_name)
  620. return NULL;
  621. hfinfo = (header_field_info *)g_tree_lookup(gpa_name_tree, field_name);
  622. if (hfinfo)
  623. return hfinfo;
  624. if (!prefixes)
  625. return NULL;
  626. if ((pi = (prefix_initializer_t)g_hash_table_lookup(prefixes, field_name) ) != NULL) {
  627. pi(field_name);
  628. g_hash_table_remove(prefixes, field_name);
  629. } else {
  630. return NULL;
  631. }
  632. return (header_field_info *)g_tree_lookup(gpa_name_tree, field_name);
  633. }
  634. int
  635. proto_registrar_get_id_byname(const char *field_name)
  636. {
  637. header_field_info *hfinfo;
  638. hfinfo = proto_registrar_get_byname(field_name);
  639. if (!hfinfo)
  640. return -1;
  641. return hfinfo->id;
  642. }
  643. static void
  644. ptvcursor_new_subtree_levels(ptvcursor_t *ptvc)
  645. {
  646. subtree_lvl *pushed_tree;
  647. DISSECTOR_ASSERT(ptvc->pushed_tree_max <= SUBTREE_MAX_LEVELS-SUBTREE_ONCE_ALLOCATION_NUMBER);
  648. ptvc->pushed_tree_max += SUBTREE_ONCE_ALLOCATION_NUMBER;
  649. pushed_tree = (subtree_lvl *)ep_alloc(sizeof(subtree_lvl) * ptvc->pushed_tree_max);
  650. DISSECTOR_ASSERT(pushed_tree != NULL);
  651. if (ptvc->pushed_tree)
  652. memcpy(pushed_tree, ptvc->pushed_tree, ptvc->pushed_tree_max - SUBTREE_ONCE_ALLOCATION_NUMBER);
  653. ptvc->pushed_tree = pushed_tree;
  654. }
  655. static void
  656. ptvcursor_free_subtree_levels(ptvcursor_t *ptvc)
  657. {
  658. ptvc->pushed_tree = NULL;
  659. ptvc->pushed_tree_max = 0;
  660. DISSECTOR_ASSERT(ptvc->pushed_tree_index == 0);
  661. ptvc->pushed_tree_index = 0;
  662. }
  663. /* Allocates an initializes a ptvcursor_t with 3 variables:
  664. * proto_tree, tvbuff, and offset. */
  665. ptvcursor_t *
  666. ptvcursor_new(proto_tree *tree, tvbuff_t *tvb, gint offset)
  667. {
  668. ptvcursor_t *ptvc;
  669. ptvc = (ptvcursor_t *)ep_alloc(sizeof(ptvcursor_t));
  670. ptvc->tree = tree;
  671. ptvc->tvb = tvb;
  672. ptvc->offset = offset;
  673. ptvc->pushed_tree = NULL;
  674. ptvc->pushed_tree_max = 0;
  675. ptvc->pushed_tree_index = 0;
  676. return ptvc;
  677. }
  678. /* Frees memory for ptvcursor_t, but nothing deeper than that. */
  679. void
  680. ptvcursor_free(ptvcursor_t *ptvc)
  681. {
  682. ptvcursor_free_subtree_levels(ptvc);
  683. /*g_free(ptvc);*/
  684. }
  685. /* Returns tvbuff. */
  686. tvbuff_t *
  687. ptvcursor_tvbuff(ptvcursor_t *ptvc)
  688. {
  689. return ptvc->tvb;
  690. }
  691. /* Returns current offset. */
  692. gint
  693. ptvcursor_current_offset(ptvcursor_t *ptvc)
  694. {
  695. return ptvc->offset;
  696. }
  697. proto_tree *
  698. ptvcursor_tree(ptvcursor_t *ptvc)
  699. {
  700. if (!ptvc)
  701. return NULL;
  702. return ptvc->tree;
  703. }
  704. void
  705. ptvcursor_set_tree(ptvcursor_t *ptvc, proto_tree *tree)
  706. {
  707. ptvc->tree = tree;
  708. }
  709. /* creates a subtree, sets it as the working tree and pushes the old working tree */
  710. proto_tree *
  711. ptvcursor_push_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree)
  712. {
  713. subtree_lvl *subtree;
  714. if (ptvc->pushed_tree_index >= ptvc->pushed_tree_max)
  715. ptvcursor_new_subtree_levels(ptvc);
  716. subtree = ptvc->pushed_tree + ptvc->pushed_tree_index;
  717. subtree->tree = ptvc->tree;
  718. subtree->it= NULL;
  719. ptvc->pushed_tree_index++;
  720. return ptvcursor_set_subtree(ptvc, it, ett_subtree);
  721. }
  722. /* pops a subtree */
  723. void
  724. ptvcursor_pop_subtree(ptvcursor_t *ptvc)
  725. {
  726. subtree_lvl *subtree;
  727. if (ptvc->pushed_tree_index <= 0)
  728. return;
  729. ptvc->pushed_tree_index--;
  730. subtree = ptvc->pushed_tree + ptvc->pushed_tree_index;
  731. if (subtree->it != NULL)
  732. proto_item_set_len(subtree->it, ptvcursor_current_offset(ptvc) - subtree->cursor_offset);
  733. ptvc->tree = subtree->tree;
  734. }
  735. /* saves the current tvb offset and the item in the current subtree level */
  736. static void
  737. ptvcursor_subtree_set_item(ptvcursor_t *ptvc, proto_item *it)
  738. {
  739. subtree_lvl *subtree;
  740. DISSECTOR_ASSERT(ptvc->pushed_tree_index > 0);
  741. subtree = ptvc->pushed_tree + ptvc->pushed_tree_index - 1;
  742. subtree->it = it;
  743. subtree->cursor_offset = ptvcursor_current_offset(ptvc);
  744. }
  745. /* Creates a subtree and adds it to the cursor as the working tree but does not
  746. * save the old working tree */
  747. proto_tree *
  748. ptvcursor_set_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree)
  749. {
  750. ptvc->tree = proto_item_add_subtree(it, ett_subtree);
  751. return ptvc->tree;
  752. }
  753. static proto_tree *
  754. ptvcursor_add_subtree_item(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree, gint length)
  755. {
  756. ptvcursor_push_subtree(ptvc, it, ett_subtree);
  757. if (length == SUBTREE_UNDEFINED_LENGTH)
  758. ptvcursor_subtree_set_item(ptvc, it);
  759. return ptvcursor_tree(ptvc);
  760. }
  761. /* Add an item to the tree and create a subtree
  762. * If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
  763. * In this case, when the subtree will be closed, the parent item length will
  764. * be equal to the advancement of the cursor since the creation of the subtree.
  765. */
  766. proto_tree *
  767. ptvcursor_add_with_subtree(ptvcursor_t *ptvc, int hfindex, gint length,
  768. const guint encoding, gint ett_subtree)
  769. {
  770. proto_item *it;
  771. it = ptvcursor_add_no_advance(ptvc, hfindex, length, encoding);
  772. return ptvcursor_add_subtree_item(ptvc, it, ett_subtree, length);
  773. }
  774. static proto_item *
  775. proto_tree_add_text_node(proto_tree *tree, tvbuff_t *tvb, gint start, gint length);
  776. /* Add a text node to the tree and create a subtree
  777. * If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
  778. * In this case, when the subtree will be closed, the item length will be equal
  779. * to the advancement of the cursor since the creation of the subtree.
  780. */
  781. proto_tree *
  782. ptvcursor_add_text_with_subtree(ptvcursor_t *ptvc, gint length,
  783. gint ett_subtree, const char *format, ...)
  784. {
  785. proto_item *pi;
  786. va_list ap;
  787. header_field_info *hfinfo;
  788. proto_tree *tree;
  789. tree = ptvcursor_tree(ptvc);
  790. TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
  791. pi = proto_tree_add_text_node(tree, ptvcursor_tvbuff(ptvc),
  792. ptvcursor_current_offset(ptvc), length);
  793. TRY_TO_FAKE_THIS_REPR(pi);
  794. va_start(ap, format);
  795. proto_tree_set_representation(pi, format, ap);
  796. va_end(ap);
  797. return ptvcursor_add_subtree_item(ptvc, pi, ett_subtree, length);
  798. }
  799. /* Add a text-only node, leaving it to our caller to fill the text in */
  800. static proto_item *
  801. proto_tree_add_text_node(proto_tree *tree, tvbuff_t *tvb, gint start, gint length)
  802. {
  803. proto_item *pi;
  804. field_info *new_fi;
  805. if (tree == NULL)
  806. return NULL;
  807. pi = proto_tree_add_pi(tree, &hfi_text_only, tvb, start, &length, &new_fi);
  808. return pi;
  809. }
  810. /* Add a text-only node to the proto_tree */
  811. proto_item *
  812. proto_tree_add_text(proto_tree *tree, tvbuff_t *tvb, gint start, gint length,
  813. const char *format, ...)
  814. {
  815. proto_item *pi;
  816. va_list ap;
  817. header_field_info *hfinfo;
  818. TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
  819. pi = proto_tree_add_text_node(tree, tvb, start, length);
  820. TRY_TO_FAKE_THIS_REPR(pi);
  821. va_start(ap, format);
  822. proto_tree_set_representation(pi, format, ap);
  823. va_end(ap);
  824. return pi;
  825. }
  826. /* Add a text-only node to the proto_tree (va_list version) */
  827. proto_item *
  828. proto_tree_add_text_valist(proto_tree *tree, tvbuff_t *tvb, gint start,
  829. gint length, const char *format, va_list ap)
  830. {
  831. proto_item *pi;
  832. header_field_info *hfinfo;
  833. TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
  834. pi = proto_tree_add_text_node(tree, tvb, start, length);
  835. TRY_TO_FAKE_THIS_REPR(pi);
  836. proto_tree_set_representation(pi, format, ap);
  837. return pi;
  838. }
  839. /* Add a text-only node for debugging purposes. The caller doesn't need
  840. * to worry about tvbuff, start, or length. Debug message gets sent to
  841. * STDOUT, too */
  842. proto_item *
  843. proto_tree_add_debug_text(proto_tree *tree, const char *format, ...)
  844. {
  845. proto_item *pi;
  846. va_list ap;
  847. pi = proto_tree_add_text_node(tree, NULL, 0, 0);
  848. if (pi) {
  849. va_start(ap, format);
  850. proto_tree_set_representation(pi, format, ap);
  851. va_end(ap);
  852. }
  853. va_start(ap, format);
  854. vprintf(format, ap);
  855. va_end(ap);
  856. printf("\n");
  857. return pi;
  858. }
  859. /* We could probably get away with changing is_error to a minimum length value. */
  860. static void
  861. report_type_length_mismatch(proto_tree *tree, const gchar *descr, int length, gboolean is_error) {
  862. if (tree) {
  863. tree_data_t *tree_data = PTREE_DATA(tree);
  864. field_info *fi_save = tree_data->fi_tmp;
  865. /* Keep the current item from getting freed by proto_tree_new_item. */
  866. tree_data->fi_tmp = NULL;
  867. expert_add_info_format(NULL, tree, PI_MALFORMED, is_error ? PI_ERROR : PI_WARN, "Trying to fetch %s with length %d", descr, length);
  868. tree_data->fi_tmp = fi_save;
  869. }
  870. if (is_error) {
  871. THROW(ReportedBoundsError);
  872. }
  873. }
  874. static guint32
  875. get_uint_value(proto_tree *tree, tvbuff_t *tvb, gint offset, gint length, const guint encoding)
  876. {
  877. guint32 value;
  878. gboolean length_error;
  879. switch (length) {
  880. case 1:
  881. value = tvb_get_guint8(tvb, offset);
  882. break;
  883. case 2:
  884. value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohs(tvb, offset)
  885. : tvb_get_ntohs(tvb, offset);
  886. break;
  887. case 3:
  888. value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh24(tvb, offset)
  889. : tvb_get_ntoh24(tvb, offset);
  890. break;
  891. case 4:
  892. value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohl(tvb, offset)
  893. : tvb_get_ntohl(tvb, offset);
  894. break;
  895. default:
  896. if (length < 1) {
  897. length_error = TRUE;
  898. value = 0;
  899. } else {
  900. length_error = FALSE;
  901. value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohl(tvb, offset)
  902. : tvb_get_ntohl(tvb, offset);
  903. }
  904. report_type_length_mismatch(tree, "an unsigned integer", length, length_error);
  905. break;
  906. }
  907. return value;
  908. }
  909. /*
  910. * NOTE: to support code written when proto_tree_add_item() took a
  911. * gboolean as its last argument, with FALSE meaning "big-endian"
  912. * and TRUE meaning "little-endian", we treat any non-zero value of
  913. * "encoding" as meaning "little-endian".
  914. */
  915. static gint32
  916. get_int_value(proto_tree *tree, tvbuff_t *tvb, gint offset, gint length, const guint encoding)
  917. {
  918. gint32 value;
  919. gboolean length_error;
  920. switch (length) {
  921. case 1:
  922. value = (gint8)tvb_get_guint8(tvb, offset);
  923. break;
  924. case 2:
  925. value = (gint16) (encoding ? tvb_get_letohs(tvb, offset)
  926. : tvb_get_ntohs(tvb, offset));
  927. break;
  928. case 3:
  929. value = encoding ? tvb_get_letoh24(tvb, offset)
  930. : tvb_get_ntoh24(tvb, offset);
  931. if (value & 0x00800000) {
  932. /* Sign bit is set; sign-extend it. */
  933. value |= 0xFF000000;
  934. }
  935. break;
  936. case 4:
  937. value = encoding ? tvb_get_letohl(tvb, offset)
  938. : tvb_get_ntohl(tvb, offset);
  939. break;
  940. default:
  941. if (length < 1) {
  942. length_error = TRUE;
  943. value = 0;
  944. } else {
  945. length_error = FALSE;
  946. value = encoding ? tvb_get_letohl(tvb, offset)
  947. : tvb_get_ntohl(tvb, offset);
  948. }
  949. report_type_length_mismatch(tree, "a signed integer", length, length_error);
  950. break;
  951. }
  952. return value;
  953. }
  954. static void
  955. tree_data_add_maybe_interesting_field(tree_data_t *tree_data, field_info *fi)
  956. {
  957. const header_field_info *hfinfo = fi->hfinfo;
  958. if (hfinfo->ref_type == HF_REF_TYPE_DIRECT) {
  959. GPtrArray *ptrs = NULL;
  960. if (tree_data->interesting_hfids == NULL) {
  961. /* Initialize the hash because we now know that it is needed */
  962. tree_data->interesting_hfids =
  963. g_hash_table_new(g_direct_hash, NULL /* g_direct_equal */);
  964. } else
  965. ptrs = (GPtrArray *)g_hash_table_lookup(tree_data->interesting_hfids,
  966. GINT_TO_POINTER(hfinfo->id));
  967. if (!ptrs) {
  968. /* First element triggers the creation of pointer array */
  969. ptrs = g_ptr_array_new();
  970. g_hash_table_insert(tree_data->interesting_hfids,
  971. GINT_TO_POINTER(hfinfo->id), ptrs);
  972. }
  973. g_ptr_array_add(ptrs, fi);
  974. }
  975. }
  976. /* Add an item to a proto_tree, using the text label registered to that item;
  977. the item is extracted from the tvbuff handed to it. */
  978. static proto_item *
  979. proto_tree_new_item(field_info *new_fi, proto_tree *tree,
  980. tvbuff_t *tvb, gint start, gint length,
  981. guint encoding)
  982. {
  983. tree_data_t *tree_data = PTREE_DATA(tree);
  984. proto_item *pi;
  985. guint32 value, n;
  986. float floatval;
  987. double doubleval;
  988. const char *string;
  989. nstime_t time_stamp;
  990. guint32 tmpsecs;
  991. gboolean length_error;
  992. /* there is a possibility here that we might raise an exception
  993. * and thus would lose track of the field_info.
  994. * store it in a temp so that if we come here again we can reclaim
  995. * the field_info without leaking memory.
  996. */
  997. if (tree_data->fi_tmp) {
  998. /* oops, last one we got must have been lost due
  999. * to an exception.
  1000. * good thing we saved it, now we can reverse the
  1001. * memory leak and reclaim it.
  1002. */
  1003. FIELD_INFO_FREE(tree_data->fi_tmp);
  1004. }
  1005. /* we might throw an exception, keep track of this one
  1006. * across the "dangerous" section below.
  1007. */
  1008. tree_data->fi_tmp = new_fi;
  1009. switch (new_fi->hfinfo->type) {
  1010. case FT_NONE:
  1011. /* no value to set for FT_NONE */
  1012. break;
  1013. case FT_PROTOCOL:
  1014. proto_tree_set_protocol_tvb(new_fi, tvb);
  1015. break;
  1016. case FT_BYTES:
  1017. proto_tree_set_bytes_tvb(new_fi, tvb, start, length);
  1018. break;
  1019. case FT_UINT_BYTES:
  1020. /*
  1021. * Map all non-zero values to little-endian for
  1022. * backwards compatibility.
  1023. */
  1024. if (encoding)
  1025. encoding = ENC_LITTLE_ENDIAN;
  1026. n = get_uint_value(tree, tvb, start, length, encoding);
  1027. proto_tree_set_bytes_tvb(new_fi, tvb, start + length, n);
  1028. /* Instead of calling proto_item_set_len(), since we don't yet
  1029. * have a proto_item, we set the field_info's length ourselves. */
  1030. new_fi->length = n + length;
  1031. break;
  1032. case FT_BOOLEAN:
  1033. /*
  1034. * Map all non-zero values to little-endian for
  1035. * backwards compatibility.
  1036. */
  1037. if (encoding)
  1038. encoding = ENC_LITTLE_ENDIAN;
  1039. proto_tree_set_boolean(new_fi,
  1040. get_uint_value(tree, tvb, start, length, encoding));
  1041. break;
  1042. /* XXX - make these just FT_UINT? */
  1043. case FT_UINT8:
  1044. case FT_UINT16:
  1045. case FT_UINT24:
  1046. case FT_UINT32:
  1047. /*
  1048. * Map all non-zero values to little-endian for
  1049. * backwards compatibility.
  1050. */
  1051. if (encoding)
  1052. encoding = ENC_LITTLE_ENDIAN;
  1053. proto_tree_set_uint(new_fi,
  1054. get_uint_value(tree, tvb, start, length, encoding));
  1055. break;
  1056. case FT_INT64:
  1057. case FT_UINT64:
  1058. /*
  1059. * Map all non-zero values to little-endian for
  1060. * backwards compatibility.
  1061. */
  1062. if (encoding)
  1063. encoding = ENC_LITTLE_ENDIAN;
  1064. if (length < 1 || length > 8) {
  1065. length_error = length < 1 ? TRUE : FALSE;
  1066. report_type_length_mismatch(tree, "a 64-bit integer", length, length_error);
  1067. }
  1068. proto_tree_set_uint64_tvb(new_fi, tvb, start, length, encoding);
  1069. break;
  1070. /* XXX - make these just FT_INT? */
  1071. case FT_INT8:
  1072. case FT_INT16:
  1073. case FT_INT24:
  1074. case FT_INT32:
  1075. /*
  1076. * Map all non-zero values to little-endian for
  1077. * backwards compatibility.
  1078. */
  1079. if (encoding)
  1080. encoding = ENC_LITTLE_ENDIAN;
  1081. proto_tree_set_int(new_fi,
  1082. get_int_value(tree, tvb, start, length, encoding));
  1083. break;
  1084. case FT_IPv4:
  1085. /*
  1086. * Map all non-zero values to little-endian for
  1087. * backwards compatibility.
  1088. */
  1089. if (encoding)
  1090. encoding = ENC_LITTLE_ENDIAN;
  1091. if (length != FT_IPv4_LEN) {
  1092. length_error = length < FT_IPv4_LEN ? TRUE : FALSE;
  1093. report_type_length_mismatch(tree, "an IPv4 address", length, length_error);
  1094. }
  1095. value = tvb_get_ipv4(tvb, start);
  1096. /*
  1097. * NOTE: to support code written when
  1098. * proto_tree_add_item() took a gboolean as its
  1099. * last argument, with FALSE meaning "big-endian"
  1100. * and TRUE meaning "little-endian", we treat any
  1101. * non-zero value of "encoding" as meaning
  1102. * "little-endian".
  1103. */
  1104. proto_tree_set_ipv4(new_fi, encoding ? GUINT32_SWAP_LE_BE(value) : value);
  1105. break;
  1106. case FT_IPXNET:
  1107. if (length != FT_IPXNET_LEN) {
  1108. length_error = length < FT_IPXNET_LEN ? TRUE : FALSE;
  1109. report_type_length_mismatch(tree, "an IPXNET address", length, length_error);
  1110. }
  1111. proto_tree_set_ipxnet(new_fi,
  1112. get_uint_value(tree, tvb, start, FT_IPXNET_LEN, ENC_BIG_ENDIAN));
  1113. break;
  1114. case FT_IPv6:
  1115. if (length != FT_IPv6_LEN) {
  1116. length_error = length < FT_IPv6_LEN ? TRUE : FALSE;
  1117. report_type_length_mismatch(tree, "an IPv6 address", length, length_error);
  1118. }
  1119. proto_tree_set_ipv6_tvb(new_fi, tvb, start, length);
  1120. break;
  1121. case FT_AX25:
  1122. if (length != 7) {
  1123. length_error = length < 7 ? TRUE : FALSE;
  1124. report_type_length_mismatch(tree, "an AX.25 address", length, length_error);
  1125. }
  1126. proto_tree_set_ax25_tvb(new_fi, tvb, start);
  1127. break;
  1128. case FT_VINES:
  1129. if (length != VINES_ADDR_LEN) {
  1130. length_error = length < VINES_ADDR_LEN ? TRUE : FALSE;
  1131. report_type_length_mismatch(tree, "a Vines address", length, length_error);
  1132. }
  1133. proto_tree_set_vines_tvb(new_fi, tvb, start);
  1134. break;
  1135. case FT_ETHER:
  1136. if (length != FT_ETHER_LEN) {
  1137. length_error = length < FT_ETHER_LEN ? TRUE : FALSE;
  1138. report_type_length_mismatch(tree, "an Ethernet", length, length_error);
  1139. }
  1140. proto_tree_set_ether_tvb(new_fi, tvb, start);
  1141. break;
  1142. case FT_EUI64:
  1143. /*
  1144. * Map all non-zero values to little-endian for
  1145. * backwards compatibility.
  1146. */
  1147. if (encoding)
  1148. encoding = ENC_LITTLE_ENDIAN;
  1149. if (length != FT_EUI64_LEN) {
  1150. length_error = length < FT_EUI64_LEN ? TRUE : FALSE;
  1151. report_type_length_mismatch(tree, "an EUI-64 address", length, length_error);
  1152. }
  1153. proto_tree_set_eui64_tvb(new_fi, tvb, start, encoding);
  1154. break;
  1155. case FT_GUID:
  1156. /*
  1157. * Map all non-zero values to little-endian for
  1158. * backwards compatibility.
  1159. */
  1160. if (encoding)
  1161. encoding = ENC_LITTLE_ENDIAN;
  1162. if (length != FT_GUID_LEN) {
  1163. length_error = length < FT_GUID_LEN ? TRUE : FALSE;
  1164. report_type_length_mismatch(tree, "a GUID", length, length_error);
  1165. }
  1166. proto_tree_set_guid_tvb(new_fi, tvb, start, encoding);
  1167. break;
  1168. case FT_OID:
  1169. proto_tree_set_oid_tvb(new_fi, tvb, start, length);
  1170. break;
  1171. case FT_FLOAT:
  1172. /*
  1173. * NOTE: to support code written when
  1174. * proto_tree_add_item() took a gboolean as its
  1175. * last argument, with FALSE meaning "big-endian"
  1176. * and TRUE meaning "little-endian", we treat any
  1177. * non-zero value of "encoding" as meaning
  1178. * "little-endian".
  1179. *
  1180. * At some point in the future, we might
  1181. * support non-IEEE-binary floating-point
  1182. * formats in the encoding as well
  1183. * (IEEE decimal, System/3x0, VAX).
  1184. */
  1185. if (encoding)
  1186. encoding = ENC_LITTLE_ENDIAN;
  1187. if (length != 4) {
  1188. length_error = length < 4 ? TRUE : FALSE;
  1189. report_type_length_mismatch(tree, "a single-precision floating point number", length, length_error);
  1190. }
  1191. if (encoding)
  1192. floatval = tvb_get_letohieee_float(tvb, start);
  1193. else
  1194. floatval = tvb_get_ntohieee_float(tvb, start);
  1195. proto_tree_set_float(new_fi, floatval);
  1196. break;
  1197. case FT_DOUBLE:
  1198. /*
  1199. * NOTE: to support code written when
  1200. * proto_tree_add_item() took a gboolean as its
  1201. * last argument, with FALSE meaning "big-endian"
  1202. * and TRUE meaning "little-endian", we treat any
  1203. * non-zero value of "encoding" as meaning
  1204. * "little-endian".
  1205. *
  1206. * At some point in the future, we might
  1207. * support non-IEEE-binary floating-point
  1208. * formats in the encoding as well
  1209. * (IEEE decimal, System/3x0, VAX).
  1210. */
  1211. if (encoding == TRUE)
  1212. encoding = ENC_LITTLE_ENDIAN;
  1213. if (length != 8) {
  1214. length_error = length < 8 ? TRUE : FALSE;
  1215. report_type_length_mismatch(tree, "a double-precision floating point number", length, length_error);
  1216. }
  1217. if (encoding)
  1218. doubleval = tvb_get_letohieee_double(tvb, start);
  1219. else
  1220. doubleval = tvb_get_ntohieee_double(tvb, start);
  1221. proto_tree_set_double(new_fi, doubleval);
  1222. break;
  1223. case FT_STRING:
  1224. proto_tree_set_string_tvb(new_fi, tvb, start, length,
  1225. encoding);
  1226. break;
  1227. case FT_STRINGZ:
  1228. if (length < -1 ) {
  1229. report_type_length_mismatch(tree, "a string", length, TRUE);
  1230. }
  1231. /* Instead of calling proto_item_set_len(),
  1232. * since we don't yet have a proto_item, we
  1233. * set the field_info's length ourselves.
  1234. *
  1235. * XXX - our caller can't use that length to
  1236. * advance an offset unless they arrange that
  1237. * there always be a protocol tree into which
  1238. * we're putting this item.
  1239. */
  1240. if (length == -1) {
  1241. /* This can throw an exception */
  1242. string = tvb_get_stringz_enc(tvb, start, &length, encoding);
  1243. } else if (length == 0) {
  1244. string = "[Empty]";
  1245. } else {
  1246. /* In this case, length signifies
  1247. * the length of the string.
  1248. *
  1249. * This could either be a null-padded
  1250. * string, which doesn't necessarily
  1251. * have a '\0' at the end, or a
  1252. * null-terminated string, with a
  1253. * trailing '\0'. (Yes, there are
  1254. * cases where you have a string
  1255. * that's both counted and null-
  1256. * terminated.)
  1257. *
  1258. * In the first case, we must
  1259. * allocate a buffer of length
  1260. * "length+1", to make room for
  1261. * a trailing '\0'.
  1262. *
  1263. * In the second case, we don't
  1264. * assume that there is a trailing
  1265. * '\0' there, as the packet might
  1266. * be malformed. (XXX - should we
  1267. * throw an exception if there's no
  1268. * trailing '\0'?) Therefore, we
  1269. * allocate a buffer of length
  1270. * "length+1", and put in a trailing
  1271. * '\0', just to be safe.
  1272. *
  1273. * (XXX - this would change if
  1274. * we made string values counted
  1275. * rather than null-terminated.)
  1276. */
  1277. string = tvb_get_ephemeral_string_enc(tvb, start, length, encoding);
  1278. }
  1279. new_fi->length = length;
  1280. proto_tree_set_string(new_fi, string);
  1281. break;
  1282. case FT_UINT_STRING:
  1283. /*
  1284. * NOTE: to support code written when
  1285. * proto_tree_add_item() took a gboolean as its
  1286. * last argument, with FALSE meaning "big-endian"
  1287. * and TRUE meaning "little-endian", if the
  1288. * encoding value is TRUE, treat that as
  1289. * ASCII with a little-endian length.
  1290. *
  1291. * This won't work for code that passes
  1292. * arbitrary non-zero values; that code
  1293. * will need to be fixed.
  1294. */
  1295. if (encoding == TRUE)
  1296. encoding = ENC_ASCII|ENC_LITTLE_ENDIAN;
  1297. n = get_uint_value(tree, tvb, start, length, encoding & ~ENC_CHARENCODING_MASK);
  1298. proto_tree_set_string_tvb(new_fi, tvb, start + length, n,
  1299. encoding);
  1300. /* Instead of calling proto_item_set_len(), since we
  1301. * don't yet have a proto_item, we set the
  1302. * field_info's length ourselves.
  1303. *
  1304. * XXX - our caller can't use that length to
  1305. * advance an offset unless they arrange that
  1306. * there always be a protocol tree into which
  1307. * we're putting this item.
  1308. */
  1309. new_fi->length = n + length;
  1310. break;
  1311. case FT_ABSOLUTE_TIME:
  1312. /*
  1313. * Absolute times can be in any of a number of
  1314. * formats, and they can be big-endian or
  1315. * little-endian.
  1316. *
  1317. * Historically FT_TIMEs were only timespecs;
  1318. * the only question was whether they were stored
  1319. * in big- or little-endian format.
  1320. *
  1321. * For backwards compatibility, we interpret an
  1322. * encoding of 1 as meaning "little-endian timespec",
  1323. * so that passing TRUE is interpreted as that.
  1324. */
  1325. if (encoding == TRUE)
  1326. encoding = ENC_TIME_TIMESPEC|ENC_LITTLE_ENDIAN;
  1327. if (length != 8 && length != 4) {
  1328. length_error = length < 4 ? TRUE : FALSE;
  1329. report_type_length_mismatch(tree, "an absolute time value", length, length_error);
  1330. }
  1331. switch (encoding) {
  1332. case ENC_TIME_TIMESPEC|ENC_BIG_ENDIAN:
  1333. /*
  1334. * 4-byte UNIX epoch, possibly followed by
  1335. * 4-byte fractional time in nanoseconds,
  1336. * both big-endian.
  1337. */
  1338. time_stamp.secs = tvb_get_ntohl(tvb, start);
  1339. if (length == 8)
  1340. time_stamp.nsecs = tvb_get_ntohl(tvb, start+4);
  1341. else
  1342. time_stamp.nsecs = 0;
  1343. break;
  1344. case ENC_TIME_TIMESPEC|ENC_LITTLE_ENDIAN:
  1345. /*
  1346. * 4-byte UNIX epoch, possibly followed by
  1347. * 4-byte fractional time in nanoseconds,
  1348. * both little-endian.
  1349. */
  1350. time_stamp.secs = tvb_get_letohl(tvb, start);
  1351. if (length == 8)
  1352. time_stamp.nsecs = tvb_get_letohl(tvb, start+4);
  1353. else
  1354. time_stamp.nsecs = 0;
  1355. break;
  1356. case ENC_TIME_NTP|ENC_BIG_ENDIAN:
  1357. /*
  1358. * NTP time stamp, big-endian.
  1359. */
  1360. /* XXX - where should this go? */
  1361. #define NTP_BASETIME 2208988800ul
  1362. /* We need a temporary variable here so the unsigned math
  1363. * works correctly (for years > 2036 according to RFC 2030
  1364. * chapter 3).
  1365. */
  1366. tmpsecs = tvb_get_ntohl(tvb, start);
  1367. if (tmpsecs)
  1368. time_stamp.secs = tmpsecs - (guint32)NTP_BASETIME;
  1369. else
  1370. time_stamp.secs = tmpsecs; /* 0 */
  1371. if (length == 8) {
  1372. /*
  1373. * We're using nanoseconds here (and we will
  1374. * display nanoseconds), but NTP's timestamps
  1375. * have a precision in microseconds or greater.
  1376. * Round to 1 microsecond.
  1377. */
  1378. time_stamp.nsecs = (int)(1000000*(tvb_get_ntohl(tvb, start+4)/4294967296.0));
  1379. time_stamp.nsecs *= 1000;
  1380. } else {
  1381. time_stamp.nsecs = 0;
  1382. }
  1383. break;
  1384. case ENC_TIME_NTP|ENC_LITTLE_ENDIAN:
  1385. /*
  1386. * NTP time stamp, big-endian.
  1387. */
  1388. tmpsecs = tvb_get_letohl(tvb, start);
  1389. if (tmpsecs)
  1390. time_stamp.secs = tmpsecs - (guint32)NTP_BASETIME;
  1391. else
  1392. time_stamp.secs = tmpsecs; /* 0 */
  1393. if (length == 8) {
  1394. /*
  1395. * We're using nanoseconds here (and we will
  1396. * display nanoseconds), but NTP's timestamps
  1397. * have a precision in microseconds or greater.
  1398. * Round to 1 microsecond.
  1399. */
  1400. time_stamp.nsecs = (int)(1000000*(tvb_get_letohl(tvb, start+4)/4294967296.0));
  1401. time_stamp.nsecs *= 1000;
  1402. } else {
  1403. time_stamp.nsecs = 0;
  1404. }
  1405. break;
  1406. default:
  1407. DISSECTOR_ASSERT_NOT_REACHED();
  1408. time_stamp.secs = 0;
  1409. time_stamp.nsecs = 0;
  1410. break;
  1411. }
  1412. proto_tree_set_time(new_fi, &time_stamp);
  1413. break;
  1414. case FT_RELATIVE_TIME:
  1415. /*
  1416. * Relative times can be in any of a number of
  1417. * formats, and they can be big-endian or
  1418. * little-endian.
  1419. *
  1420. * Historically FT_TIMEs were only timespecs;
  1421. * the only question was whether they were stored
  1422. * in big- or little-endian format.
  1423. *
  1424. * For backwards compatibility, we interpret an
  1425. * encoding of 1 as meaning "little-endian timespec",
  1426. * so that passing TRUE is interpreted as that.
  1427. */
  1428. if (encoding == TRUE)
  1429. encoding = ENC_TIME_TIMESPEC|ENC_LITTLE_ENDIAN;
  1430. switch (encoding) {
  1431. if (length != 8 && length != 4) {
  1432. length_error = length < 4 ? TRUE : FALSE;
  1433. report_type_length_mismatch(tree, "a relative time value", length, length_error);
  1434. }
  1435. case ENC_TIME_TIMESPEC|ENC_BIG_ENDIAN:
  1436. /*
  1437. * 4-byte UNIX epoch, possibly followed by
  1438. * 4-byte fractional time in nanoseconds,
  1439. * both big-endian.
  1440. */
  1441. time_stamp.secs = tvb_get_ntohl(tvb, start);
  1442. if (length == 8)
  1443. time_stamp.nsecs = tvb_get_ntohl(tvb, start+4);
  1444. else
  1445. time_stamp.nsecs = 0;
  1446. break;
  1447. case ENC_TIME_TIMESPEC|ENC_LITTLE_ENDIAN:
  1448. /*
  1449. * 4-byte UNIX epoch, possibly followed by
  1450. * 4-byte fractional time in nanoseconds,
  1451. * both little-endian.
  1452. */
  1453. time_stamp.secs = tvb_get_letohl(tvb, start);
  1454. if (length == 8)
  1455. time_stamp.nsecs = tvb_get_letohl(tvb, start+4);
  1456. else
  1457. time_stamp.nsecs = 0;
  1458. break;
  1459. }
  1460. proto_tree_set_time(new_fi, &time_stamp);
  1461. break;
  1462. default:
  1463. g_error("new_fi->hfinfo->type %d (%s) not handled\n",
  1464. new_fi->hfinfo->type,
  1465. ftype_name(new_fi->hfinfo->type));
  1466. DISSECTOR_ASSERT_NOT_REACHED();
  1467. break;
  1468. }
  1469. FI_SET_FLAG(new_fi, (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN);
  1470. /* Don't add new node to proto_tree until now so that any exceptions
  1471. * raised by a tvbuff access method doesn't leave junk in the proto_tree. */
  1472. /* XXX. wouldn't be better to add this item to tree, with some special flag (FI_EXCEPTION?)
  1473. * to know which item caused exception? */
  1474. pi = proto_tree_add_node(tree, new_fi);
  1475. /* we did not raise an exception so we dont have to remember this
  1476. * field_info struct any more.
  1477. */
  1478. tree_data->fi_tmp = NULL;
  1479. return pi;
  1480. }
  1481. /* Gets data from tvbuff, adds it to proto_tree, increments offset,
  1482. and returns proto_item* */
  1483. proto_item *
  1484. ptvcursor_add(ptvcursor_t *ptvc, int hfindex, gint length,
  1485. const guint encoding)
  1486. {
  1487. field_info *new_fi;
  1488. header_field_info *hfinfo;
  1489. gint item_length;
  1490. guint32 n;
  1491. int offset;
  1492. /* We can't fake it just yet. We have to advance the cursor
  1493. TRY_TO_FAKE_THIS_ITEā€¦

Large files files are truncated, but you can click here to view the full file