For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-tpncp.c
index 18850a173e6ac3ddeb9142bb40f444cb8aa0e9db..f3d88fa72a328efdb4e74f8498c741df86e07dea 100644 (file)
@@ -54,9 +54,7 @@
 #define UDP_PORT_TPNCP_HOST BASE_TPNCP_PORT
 
 #define BASE_TPNCP_DATA_LEN 256
-#define MAX_TPNCP_DAT_FILE_PATH_LEN BASE_TPNCP_DATA_LEN
 #define MAX_TPNCP_DB_ENTRY_LEN BASE_TPNCP_DATA_LEN
-#define MAX_TPNCP_HEADER_LEN BASE_TPNCP_DATA_LEN
 
 #define MAX_TPNCP_DB_SIZE 3000
 #define MAX_ENUMS_NUM 500
@@ -82,9 +80,11 @@ typedef struct tpncp_data_field_info
 static gboolean tpncp_desegment = TRUE;
 
 /* Database for storing information about all TPNCP events. */
+/* XXX: ToDo: allocate at runtime as needed */
 static tpncp_data_field_info tpncp_events_info_db[MAX_TPNCP_DB_SIZE];
 
 /* Database for storing information about all TPNCP commands. */
+/* XXX: ToDo: allocate at runtime as needed */
 static tpncp_data_field_info tpncp_commands_info_db[MAX_TPNCP_DB_SIZE];
 
 /* Global variables for bitfields representation. */
@@ -117,6 +117,9 @@ static guint trunkpack_tcp_port = 0,
              host_tcp_port = 0,
              host_udp_port = 0;
 
+/* XXX: ToDo: allocate at runtime as needed */
+/*      The following allocates something on the order of 2M of static memory ! */
+/*      Also: Runtime value_string_ext arrays should be used                    */
 static value_string tpncp_commands_id_vals[MAX_TPNCP_DB_SIZE];
 static value_string tpncp_events_id_vals[MAX_TPNCP_DB_SIZE];
 static value_string tpncp_enums_id_vals[MAX_ENUMS_NUM][MAX_ENUM_ENTRIES];
@@ -207,7 +210,7 @@ static void dissect_tpncp_data(gint data_id, tvbuff_t *tvb, proto_item *item,
                 }
                 /* Add string representation for ip_address's field (if needed). */
                 if (current_tpncp_data_field_info->tpncp_data_field_is_ip_addr) {
-                    proto_item_append_text(pi, " (%s)", ip_to_str(tvb_get_ptr(tvb, *offset, 4)));
+                    proto_item_append_text(pi, " (%s)", tvb_ip_to_str(tvb, *offset));
                 }
                 (*offset) += 4;
                 break;
@@ -253,7 +256,7 @@ static void dissect_tpncp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
     gint offset = 0;
     guint32 id, cid = 0;
     guint16 seq_number, len, ver, reserved;
-    gchar *tpncp_header = NULL;
+    gchar *tpncp_header;
 
     ver = tvb_get_ntohs(tvb, 0);
     len = tvb_get_ntohs(tvb, 2);
@@ -281,7 +284,7 @@ static void dissect_tpncp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
     }
 
     if (tree) {
-        item = proto_tree_add_item(tree, proto_tpncp, tvb, 0, -1, FALSE);
+        item = proto_tree_add_item(tree, proto_tpncp, tvb, 0, -1, ENC_NA);
         tpncp_tree = proto_item_add_subtree(item, ett_tpncp);
 
         proto_tree_add_uint(tpncp_tree, hf_tpncp_version, tvb, 0, 2, ver);
@@ -289,16 +292,13 @@ static void dissect_tpncp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
         proto_tree_add_uint(tpncp_tree, hf_tpncp_seq_number, tvb, 4, 2, seq_number);
         proto_tree_add_uint(tpncp_tree, hf_tpncp_reserved, tvb, 6, 2, reserved);
 
-        tpncp_header = ep_alloc(MAX_TPNCP_HEADER_LEN);
-        tpncp_header[0] = 0;
-
         if (pinfo->srcport == UDP_PORT_TPNCP_TRUNKPACK) {
             if (match_strval(id, tpncp_events_id_vals)) {
                 proto_tree_add_uint(tpncp_tree, hf_tpncp_event_id, tvb, 8, 4, id);
                 proto_tree_add_int(tpncp_tree, hf_tpncp_cid, tvb, 12, 4, cid);
                 offset += 16;
                 if (tpncp_events_info_db[id].tpncp_data_field_size) {
-                    g_snprintf(tpncp_header, MAX_TPNCP_HEADER_LEN, "TPNCP Event: %s (%d)", val_to_str(id, tpncp_events_id_vals, "Unknown"), id);
+                    tpncp_header = ep_strdup_printf("TPNCP Event: %s (%d)", val_to_str(id, tpncp_events_id_vals, "Unknown"), id);
                     tpncp_item = proto_tree_add_text(tree, tvb, offset, -1, "%s", tpncp_header);
                     dissect_tpncp_event(id, tvb, tpncp_item, &offset);
                 }
@@ -309,7 +309,7 @@ static void dissect_tpncp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
                 proto_tree_add_uint(tpncp_tree, hf_tpncp_command_id, tvb, 8, 4, id);
                 offset += 12;
                 if (tpncp_commands_info_db[id].tpncp_data_field_size) {
-                    g_snprintf(tpncp_header, MAX_TPNCP_HEADER_LEN, "TPNCP Command: %s (%d)", val_to_str(id, tpncp_commands_id_vals, "Unknown"), id);
+                    tpncp_header = ep_strdup_printf("TPNCP Command: %s (%d)", val_to_str(id, tpncp_commands_id_vals, "Unknown"), id);
                     tpncp_item = proto_tree_add_text(tree, tvb, offset, -1, "%s", tpncp_header);
                     dissect_tpncp_command(id, tvb, tpncp_item, &offset);
                 }
@@ -357,10 +357,10 @@ static gint fill_tpncp_id_vals(value_string string[], FILE *file) {
         if (!strncmp(line_in_file, "#####", 5)) {
             break;
         }
-        if (sscanf(line_in_file, "%s %d", tpncp_name, &tpncp_id) == 2) {
+        if (sscanf(line_in_file, "%255s %d", tpncp_name, &tpncp_id) == 2) {
             string[i].strptr = g_strdup(tpncp_name);
             string[i].value = tpncp_id;
-            if (i < MAX_TPNCP_DB_SIZE) {
+            if (i < (MAX_TPNCP_DB_SIZE-1)) {
                 i++;
             }
             else {
@@ -392,12 +392,12 @@ static gint fill_enums_id_vals(FILE *file) {
         if (!strncmp(line_in_file, "#####", 5)) {
             break;
         }
-        if (sscanf(line_in_file, "%s %s %d", enum_name, enum_str, &enum_id) == 3) {
+        if (sscanf(line_in_file, "%255s %255s %d", enum_name, enum_str, &enum_id) == 3) {
             if (strcmp(enum_type, enum_name)) {
                 if (!first_entry) {
-                    if (enum_val < MAX_ENUMS_NUM) {
-                        tpncp_enums_id_vals[enum_val][i].strptr = NULL;
-                        tpncp_enums_id_vals[enum_val][i].value = 0;
+                    tpncp_enums_id_vals[enum_val][i].strptr = NULL;
+                    tpncp_enums_id_vals[enum_val][i].value = 0;
+                    if (enum_val < (MAX_ENUMS_NUM-1)) {
                         enum_val++; i = 0;
                     }
                     else {
@@ -411,7 +411,7 @@ static gint fill_enums_id_vals(FILE *file) {
             }
             tpncp_enums_id_vals[enum_val][i].strptr = g_strdup(enum_str);
             tpncp_enums_id_vals[enum_val][i].value = enum_id;
-            if (i < MAX_ENUM_ENTRIES) {
+            if (i < (MAX_ENUM_ENTRIES-1)) {
                 i++;
             }
             else {
@@ -445,7 +445,7 @@ static gint init_tpncp_data_fields_info(tpncp_data_field_info *data_fields_info,
     gint enum_val, data_id, current_data_id = -1,
          tpncp_data_field_sign, tpncp_data_field_size,
          tpncp_data_field_array_dim, tpncp_data_field_is_ip_addr;
-    guint index;
+    guint idx;
     tpncp_data_field_info *current_tpncp_data_field_info = NULL;
     hf_register_info hf_entr;
 
@@ -580,8 +580,8 @@ static gint init_tpncp_data_fields_info(tpncp_data_field_info *data_fields_info,
         hf_allocated = hf_size+array_length(hf_tpncp)-1;
         if ((hf = (hf_register_info *)g_realloc(hf, hf_allocated * sizeof(hf_register_info))) == NULL)
             return (-1);
-        for (index = 0; index < array_length(hf_tpncp); index++) {
-            memcpy(hf + (hf_size - 1), hf_tpncp + index, sizeof(hf_register_info));
+        for (idx = 0; idx < array_length(hf_tpncp); idx++) {
+            memcpy(hf + (hf_size - 1), hf_tpncp + idx, sizeof(hf_register_info));
             hf_size++;
         }
         was_registered = TRUE;
@@ -686,12 +686,10 @@ static gint init_tpncp_data_fields_info(tpncp_data_field_info *data_fields_info,
 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
 
 static gint init_tpncp_db(void) {
-    gchar *tpncp_dat_file_path = NULL;
-    FILE *file = NULL;
+    gchar *tpncp_dat_file_path;
+    FILE *file;
 
-    tpncp_dat_file_path = ep_alloc(MAX_TPNCP_DAT_FILE_PATH_LEN);
-    tpncp_dat_file_path[0] = 0;
-    g_snprintf(tpncp_dat_file_path, MAX_TPNCP_DAT_FILE_PATH_LEN, "%s" G_DIR_SEPARATOR_S"tpncp" G_DIR_SEPARATOR_S "tpncp.dat", get_datafile_dir());
+    tpncp_dat_file_path = ep_strdup_printf("%s" G_DIR_SEPARATOR_S"tpncp" G_DIR_SEPARATOR_S "tpncp.dat", get_datafile_dir());
 
     /* Open file with TPNCP data. */
     if ((file = ws_fopen(tpncp_dat_file_path, "r")) == NULL)
@@ -729,10 +727,10 @@ void proto_reg_handoff_tpncp(void) {
         tpncp_prefs_initialized = TRUE;
     }
     else {
-        dissector_delete("tcp.port", trunkpack_tcp_port, tpncp_tcp_handle);
-        dissector_delete("udp.port", trunkpack_udp_port, tpncp_udp_handle);
-        dissector_delete("tcp.port", host_tcp_port,      tpncp_tcp_handle);
-        dissector_delete("udp.port", host_udp_port,      tpncp_udp_handle);
+        dissector_delete_uint("tcp.port", trunkpack_tcp_port, tpncp_tcp_handle);
+        dissector_delete_uint("udp.port", trunkpack_udp_port, tpncp_udp_handle);
+        dissector_delete_uint("tcp.port", host_tcp_port,      tpncp_tcp_handle);
+        dissector_delete_uint("udp.port", host_udp_port,      tpncp_udp_handle);
     }
 
     trunkpack_tcp_port = global_tpncp_trunkpack_tcp_port;
@@ -741,14 +739,14 @@ void proto_reg_handoff_tpncp(void) {
     host_tcp_port = global_tpncp_host_tcp_port;
     host_udp_port = global_tpncp_host_udp_port;
 
-    dissector_add("tcp.port", global_tpncp_trunkpack_tcp_port, tpncp_tcp_handle);
-    dissector_add("udp.port", global_tpncp_trunkpack_udp_port, tpncp_udp_handle);
+    dissector_add_uint("tcp.port", global_tpncp_trunkpack_tcp_port, tpncp_tcp_handle);
+    dissector_add_uint("udp.port", global_tpncp_trunkpack_udp_port, tpncp_udp_handle);
 }
 
 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
 
 void proto_register_tpncp(void) {
-    gint index;
+    gint idx;
     module_t *tpncp_module;
     static gint *ett[] = {
         &ett_tpncp,
@@ -765,8 +763,8 @@ void proto_register_tpncp(void) {
      * The function proto_register_field_array can not work with dynamic arrays,
      * so passing dynamic array elements one-by-one in the loop.
      */
-    for(index = 0; index < hf_size; index++) {
-        proto_register_field_array(proto_tpncp, &hf[index], 1);
+    for(idx = 0; idx < hf_size; idx++) {
+        proto_register_field_array(proto_tpncp, &hf[idx], 1);
     }
 
     proto_register_subtree_array(ett, array_length(ett));