bugfix to a bug reported by Ian Schorr:
[obnox/wireshark/wip.git] / packet-q931.c
index 7f940d100cd81d87863328fbfccebe98ce7d3578..3e09da7165ff0069ef76fc4c2ff5f935d5428ee2 100644 (file)
@@ -2,25 +2,24 @@
  * Routines for Q.931 frame disassembly
  * Guy Harris <guy@alum.mit.edu>
  *
- * $Id: packet-q931.c,v 1.20 2000/11/13 07:18:56 guy Exp $
+ * $Id: packet-q931.c,v 1.76 2004/04/17 04:43:58 guy Exp $
  *
  * Modified by Andreas Sikkema for possible use with H.323
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998
  *
- * 
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 # include "config.h"
 #endif
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
 #include <stdio.h>
 #include <glib.h>
 #include <string.h>
-#include "packet.h"
-#include "strutil.h"
+#include <epan/packet.h>
+#include <epan/strutil.h>
 #include "nlpid.h"
 #include "packet-q931.h"
+#include "packet-e164.h"
+#include "prefs.h"
+#include "reassemble.h"
 
-#ifdef H323
+#include "lapd_sapi.h"
 #include "packet-tpkt.h"
-#include "packet-h225.h"
-#endif
 
 /* Q.931 references:
  *
  * http://www.tulatelecom.ru/staff/german/DSSHelp/MessList/InfEl/InfElList.html
  */
 
-static int proto_q931 = -1;
-static int hf_q931_discriminator = -1;
-static int hf_q931_call_ref_len = -1;
-static int hf_q931_call_ref = -1;
-static int hf_q931_message_type = -1;
+static int proto_q931                                  = -1;
+static int hf_q931_discriminator                       = -1;
+static int hf_q931_coding_standard                     = -1;
+static int hf_q931_information_transfer_capability     = -1;
+static int hf_q931_transfer_mode                       = -1;
+static int hf_q931_information_transfer_rate           = -1;
+static int hf_q931_uil1                                        = -1;
+static int hf_q931_call_ref_len                        = -1;
+static int hf_q931_call_ref_flag                       = -1;
+static int hf_q931_call_ref                            = -1;
+static int hf_q931_message_type                        = -1;
+static int hf_q931_segment_type                        = -1;
+static int hf_q931_cause_location                      = -1;
+static int hf_q931_cause_value                                 = -1;
+static int hf_q931_number_type                         = -1;
+static int hf_q931_numbering_plan                      = -1;
+static int hf_q931_extension_ind                       = -1;
+static int hf_q931_calling_party_number                = -1;
+static int hf_q931_called_party_number                         = -1;
+static int hf_q931_connected_number                    = -1;
+static int hf_q931_redirecting_number                  = -1;
+static int hf_q931_screening_ind                               = -1;
+static int hf_q931_presentation_ind                            = -1;
+
+static int hf_q931_segments = -1;
+static int hf_q931_segment = -1;
+static int hf_q931_segment_overlap = -1;
+static int hf_q931_segment_overlap_conflict = -1;
+static int hf_q931_segment_multiple_tails = -1;
+static int hf_q931_segment_too_long_segment = -1;
+static int hf_q931_segment_error = -1;
+static int hf_q931_reassembled_in = -1; 
+
+static gint ett_q931                                   = -1;
+static gint ett_q931_ie                                = -1;
+
+static gint ett_q931_segments = -1;
+static gint ett_q931_segment = -1;
+
+static const fragment_items q931_frag_items = {
+       &ett_q931_segment,
+       &ett_q931_segments,
+
+       &hf_q931_segments,
+       &hf_q931_segment,
+       &hf_q931_segment_overlap,
+       &hf_q931_segment_overlap_conflict,
+       &hf_q931_segment_multiple_tails,
+       &hf_q931_segment_too_long_segment,
+       &hf_q931_segment_error,
+       &hf_q931_reassembled_in,
+       "segments"
+};
+
+/* Tables for reassembly of fragments. */
+static GHashTable *q931_fragment_table = NULL;
+static GHashTable *q931_reassembled_table = NULL;
+
+/* Preferences */
+static gboolean q931_reassembly = TRUE;
 
-static gint ett_q931 = -1;
-static gint ett_q931_ie = -1;
+static dissector_table_t codeset_dissector_table;
+static dissector_table_t ie_dissector_table;
+
+/* desegmentation of Q.931 over TPKT over TCP */
+static gboolean q931_desegment = TRUE;
+
+static dissector_handle_t h225_handle;
+static dissector_handle_t q931_tpkt_pdu_handle;
+
+static void
+dissect_q931_IEs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *root_tree,
+    proto_tree *q931_tree, gboolean is_tpkt, int offset, int initial_codeset);
 
 /*
  * Q.931 message types.
@@ -78,7 +139,7 @@ static gint ett_q931_ie = -1;
 #define        Q931_CONNECT_ACK        0x0F
 #define        Q931_PROGRESS           0x03
 #define        Q931_SETUP              0x05
-#define        Q931_SETUP_ACK          0x0B
+#define        Q931_SETUP_ACK          0x0D
 #define        Q931_HOLD               0x24
 #define        Q931_HOLD_ACK           0x28
 #define        Q931_HOLD_REJECT        0x30
@@ -107,7 +168,7 @@ static gint ett_q931_ie = -1;
 #define        Q931_STATUS_ENQUIRY     0x75
 
 static const value_string q931_message_type_vals[] = {
-  { Q931_ESCAPE,               "ESCAPE" },
+       { Q931_ESCAPE,                  "ESCAPE" },
        { Q931_ALERTING,                "ALERTING" },
        { Q931_CALL_PROCEEDING,         "CALL PROCEEDING" },
        { Q931_CONNECT,                 "CONNECT" },
@@ -144,10 +205,26 @@ static const value_string q931_message_type_vals[] = {
        { 0,                            NULL }
 };
 
+static const true_false_string tfs_call_ref_flag = {
+       "Message sent to originating side",
+       "Message sent from originating side"
+};
+
 /*
  * Information elements.
  */
 
+/* Shifted codeset values */
+#define CS0 0x000
+#define CS1 0x100
+#define CS2 0x200
+#define CS3 0x300
+#define CS4 0x400
+#define CS5 0x500
+#define CS6 0x600
+#define CS7 0x700
+
+#define        Q931_IE_SO_MASK 0x80    /* single-octet/variable-length mask */
 /*
  * Single-octet IEs.
  */
@@ -156,8 +233,8 @@ static const value_string q931_message_type_vals[] = {
 #define        Q931_IE_SO_IE_MASK              0x0F    /* IE mask */
 
 #define        Q931_IE_SHIFT                   0x90
-#define        Q931_IE_SHIFT_LOCKING           0x08    /* locking shift */
-#define        Q931_IE_SHIFT_CODESET           0x0F    /* codeset */
+#define        Q931_IE_SHIFT_NON_LOCKING       0x08    /* non-locking shift */
+#define        Q931_IE_SHIFT_CODESET           0x07    /* codeset */
 
 #define        Q931_IE_MORE_DATA_OR_SEND_COMP  0xA0    /* More Data or Sending Complete */
 #define        Q931_IE_MORE_DATA               0xA0
@@ -170,6 +247,15 @@ static const value_string q931_message_type_vals[] = {
  * Variable-length IEs.
  */
 #define        Q931_IE_VL_EXTENSION            0x80    /* Extension flag */
+/*     extension bit. The bit value "0" indicates that the octet continues through the         */
+/*     next octet. The bit value "1" indicates that this octet is the last octet               */
+
+static const true_false_string q931_extension_ind_value = {
+  "last octet",
+  "information continues through the next octet",
+
+};
+
 
 /*
  * Codeset 0 (default).
@@ -202,6 +288,7 @@ static const value_string q931_message_type_vals[] = {
 #define        Q931_IE_PACKET_SIZE             0x46    /* Packet size */
 #define        Q931_IE_CUG                     0x47    /* Closed user group */
 #define        Q931_IE_REVERSE_CHARGE_IND      0x4A    /* Reverse charging indication */
+#define        Q931_IE_CONNECTED_NUMBER_DEFAULT        0x4C    /* Connected Number */
 #define        Q931_IE_CALLING_PARTY_NUMBER    0x6C    /* Calling Party Number */
 #define        Q931_IE_CALLING_PARTY_SUBADDR   0x6D    /* Calling Party Subaddress */
 #define        Q931_IE_CALLED_PARTY_NUMBER     0x70    /* Called Party Number */
@@ -249,7 +336,8 @@ static const value_string q931_message_type_vals[] = {
 /* 0x76 is Redirection Number, but that's also Codeset 0 */
 #define        Q931_IE_CALL_APPEARANCE         0x7B
 
-static const value_string q931_info_element_vals[] = {
+/* Codeset 0 */
+static const value_string q931_info_element_vals0[] = {
        { Q931_IE_SEGMENTED_MESSAGE,            "Segmented message" },
        { Q931_IE_BEARER_CAPABILITY,            "Bearer capability" },
        { Q931_IE_CAUSE,                        "Cause" },
@@ -278,6 +366,7 @@ static const value_string q931_info_element_vals[] = {
        { Q931_IE_PACKET_SIZE,                  "Packet size" },
        { Q931_IE_CUG,                          "Closed user group" },
        { Q931_IE_REVERSE_CHARGE_IND,           "Reverse charging indication" },
+       { Q931_IE_CONNECTED_NUMBER_DEFAULT,     "Connected number" },
        { Q931_IE_CALLING_PARTY_NUMBER,         "Calling party number" },
        { Q931_IE_CALLING_PARTY_SUBADDR,        "Calling party subaddress" },
        { Q931_IE_CALLED_PARTY_NUMBER,          "Called party number" },
@@ -292,13 +381,54 @@ static const value_string q931_info_element_vals[] = {
        { Q931_IE_ESCAPE,                       "Escape" },
        { Q931_IE_CONNECTED_NUMBER,             "Connected number" },
        { Q931_IE_CONNECTED_SUBADDR,            "Connected subaddress" },
+       { 0,                                    NULL }
+};
+/* Codeset 1 */
+static const value_string q931_info_element_vals1[] = {
+       { 0,                                    NULL }
+};
+/* Codeset 2 */
+static const value_string q931_info_element_vals2[] = {
+       { 0,                                    NULL }
+};
+/* Codeset 3 */
+static const value_string q931_info_element_vals3[] = {
+       { 0,                                    NULL }
+};
+/* Codeset 4 */
+static const value_string q931_info_element_vals4[] = {
+       { 0,                                    NULL }
+};
+/* Codeset 5 */
+static const value_string q931_info_element_vals5[] = {
        { Q931_IE_CHARGING_ADVICE,              "Charging advice" },
        { Q931_IE_OPERATOR_SYSTEM_ACCESS,       "Operator system access" },
+       { 0,                                    NULL }
+};
+/* Codeset 6 */
+static const value_string q931_info_element_vals6[] = {
        { Q931_IE_REDIRECTING_NUMBER,           "Redirecting number" },
        { Q931_IE_REDIRECTING_SUBADDR,          "Redirecting subaddress" },
        { Q931_IE_CALL_APPEARANCE,              "Call appearance" },
        { 0,                                    NULL }
 };
+/* Codeset 7 */
+static const value_string q931_info_element_vals7[] = {
+       { 0,                                    NULL }
+};
+
+/* Codeset array */
+#define NUM_INFO_ELEMENT_VALS  (Q931_IE_SHIFT_CODESET+1)
+static const value_string *q931_info_element_vals[NUM_INFO_ELEMENT_VALS] = {
+  q931_info_element_vals0,
+  q931_info_element_vals1,
+  q931_info_element_vals2,
+  q931_info_element_vals3,
+  q931_info_element_vals4,
+  q931_info_element_vals5,
+  q931_info_element_vals6,
+  q931_info_element_vals7,
+};
 
 static const value_string q931_congestion_level_vals[] = {
        { 0x0, "Receiver ready" },
@@ -325,7 +455,7 @@ dissect_q931_segmented_message_ie(tvbuff_t *tvb, int offset, int len,
 {
        if (len != 2) {
                proto_tree_add_text(tree, tvb, offset, len,
-                   "Segmented message: length is %d, should be 2\n", len);
+                   "Segmented message: length is %d, should be 2", len);
                return;
        }
        if (tvb_get_guint8(tvb, offset) & 0x80) {
@@ -337,18 +467,17 @@ dissect_q931_segmented_message_ie(tvbuff_t *tvb, int offset, int len,
                    "Not first segment: %u segments remaining",
                    tvb_get_guint8(tvb, offset) & 0x7F);
        }
-       proto_tree_add_text(tree, tvb, offset + 1, 1,
-           "Segmented message type: %u\n", tvb_get_guint8(tvb, offset + 1));
+       proto_tree_add_item(tree, hf_q931_segment_type, tvb, offset + 1, 1, FALSE);
 }
 
 /*
  * Dissect a Bearer capability or Low-layer compatibility information element.
  */
-static const value_string q931_bc_coding_standard_vals[] = {
-       { 0x00, "ITU-T standardized coding" },
-       { 0x20, "ISO/IEC standard" },
-       { 0x40, "National standard" },
-       { 0x60, "Standard defined for this particular network" },
+static const value_string q931_coding_standard_vals[] = {
+       { 0x0, "ITU-T standardized coding" },
+       { 0x1, "ISO/IEC standard" },
+       { 0x2, "National standard" },
+       { 0x3, "Standard defined for this particular network" },
        { 0,    NULL }
 };
 
@@ -361,10 +490,10 @@ static const value_string q931_information_transfer_capability_vals[] = {
        { 0x18, "Video" },
        { 0,    NULL }
 };
-       
+
 static const value_string q931_transfer_mode_vals[] = {
        { 0x00, "Circuit mode" },
-       { 0x40, "Packet mode" },
+       { 0x02, "Packet mode" },
        { 0,    NULL }
 };
 
@@ -380,7 +509,7 @@ static const value_string q931_information_transfer_rate_vals[] = {
        { Q931_IT_RATE_MULTIRATE,       "Multirate (64 kbit/s base rate)" },
        { 0,                            NULL }
 };
-       
+
 static const value_string q931_uil1_vals[] = {
        { 0x01, "V.110/I.460/X.30 rate adaption" },
        { 0x02, "Recommendation G.711 u-law" },
@@ -516,43 +645,35 @@ static const value_string q931_uil3_vals[] = {
        { 0,                    NULL }
 };
 
-/*
- * XXX - should this (or, rather, a routine to return a string containing
- * the stuff we put after "Protocol discriminator:") be in "packet-osi.c"?
- *
- * I.e., is the convention that 16-63 and 80-254 are for network-layer
- * or layer-3 protocols, and 64-79 are for national use, specific to
- * Q.931 (and maybe Q.2931), or is it a more general ISO standard?
- */
-static void 
+static void
 dissect_q931_protocol_discriminator(tvbuff_t *tvb, int offset, proto_tree *tree)
 {
        unsigned int discriminator = tvb_get_guint8(tvb, offset);
-       char *nlpid_string;
 
-       nlpid_string = match_strval(discriminator, nlpid_vals);
-       if (nlpid_string != NULL) {
+       if (discriminator == NLPID_Q_931) {
+               proto_tree_add_uint_format(tree, hf_q931_discriminator,
+                        tvb, offset, 1, discriminator,
+                        "Protocol discriminator: Q.931");
+       } else if (discriminator == NLPID_Q_2931) {
                proto_tree_add_uint_format(tree, hf_q931_discriminator,
                         tvb, offset, 1, discriminator,
-                        "Protocol discriminator: %s", nlpid_string);
+                        "Protocol discriminator: Q.2931");
+       } else if ((discriminator >= 16 && discriminator < 63)
+           || ((discriminator >= 80) && (discriminator < 254))) {
+               proto_tree_add_uint_format(tree, hf_q931_discriminator,
+                   tvb, offset, 1, discriminator,
+                   "Protocol discriminator: Network layer or layer 3 protocol (0x%02X)",
+                   discriminator);
+       } else if (discriminator >= 64 && discriminator <= 79) {
+               proto_tree_add_uint_format(tree, hf_q931_discriminator,
+                   tvb, offset, 1, discriminator,
+                   "Protocol discriminator: National use (0x%02X)",
+                   discriminator);
        } else {
-               if ((discriminator >= 16 && discriminator < 63)
-                   || ((discriminator >= 80) && (discriminator < 254))) {
-                       proto_tree_add_uint_format(tree, hf_q931_discriminator,
-                           tvb, offset, 1, discriminator,
-                           "Protocol discriminator: Network layer or layer 3 protocol (0x%02X)",
-                           discriminator);
-               } else if (discriminator >= 64 && discriminator <= 79) {
-                       proto_tree_add_uint_format(tree, hf_q931_discriminator,
-                           tvb, offset, 1, discriminator,
-                           "Protocol discriminator: National use (0x%02X)",
-                           discriminator);
-               } else {
-                       proto_tree_add_uint_format(tree, hf_q931_discriminator,
-                           tvb, offset, 1, discriminator,
-                           "Protocol discriminator: Reserved (0x%02X)",
-                           discriminator);
-               }
+               proto_tree_add_uint_format(tree, hf_q931_discriminator,
+                   tvb, offset, 1, discriminator,
+                   "Protocol discriminator: Reserved (0x%02X)",
+                   discriminator);
        }
 }
 
@@ -572,9 +693,6 @@ dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
                return;
        octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Coding standard: %s",
-           val_to_str(coding_standard, q931_bc_coding_standard_vals, NULL));
        if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
                /*
                 * We don't know how the bearer capability is encoded,
@@ -583,12 +701,13 @@ dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
                proto_tree_add_text(tree, tvb, offset,
                    len, "Data: %s",
                    tvb_bytes_to_str(tvb, offset, len));
+               proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
+               proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
                return;
        }
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Information transfer capability: %s",
-           val_to_str(octet & 0x1F, q931_information_transfer_capability_vals,
-             "Unknown (0x%02X)"));
+       proto_tree_add_uint(tree, hf_q931_information_transfer_capability, tvb, offset, 1, octet);
+       proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
+       proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
        offset += 1;
        len -= 1;
 
@@ -609,15 +728,10 @@ dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
        if (len == 0)
                return;
        octet = tvb_get_guint8(tvb, offset);
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Transfer mode: %s",
-           val_to_str(octet & 0x60, q931_transfer_mode_vals,
-             "Unknown (0x%02X)"));
+       proto_tree_add_uint(tree, hf_q931_information_transfer_rate, tvb, offset, 1, octet);
+       proto_tree_add_uint(tree, hf_q931_transfer_mode, tvb, offset, 1, octet);
+       proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
        it_rate = octet & 0x1F;
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Information transfer rate: %s",
-           val_to_str(it_rate, q931_information_transfer_rate_vals,
-             "Unknown (0x%02X)"));
        offset += 1;
        len -= 1;
 
@@ -636,13 +750,11 @@ dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
                /*
                 * Layer 1 information.
                 */
-               proto_tree_add_text(tree, tvb, offset, 1,
-                   "User information layer 1 protocol: %s",
-                   val_to_str(octet & 0x1F, q931_uil1_vals,
-                     "Unknown (0x%02X)"));
+               proto_tree_add_uint(tree, hf_q931_uil1, tvb, offset, 1, octet);
+               proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
                offset += 1;
                len -= 1;
-               
+
                if (octet & Q931_IE_VL_EXTENSION)
                        goto l1_done;
                if (len == 0)
@@ -726,7 +838,7 @@ dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
                       "Unknown (0x%X)"));
                proto_tree_add_text(tree, tvb, offset, 1,
                    "Parity: %s",
-                     val_to_str(octet & 0x08, q931_l1_parity_vals,
+                     val_to_str(octet & 0x07, q931_l1_parity_vals,
                       "Unknown (0x%X)"));
 
                if (octet & Q931_IE_VL_EXTENSION)
@@ -895,15 +1007,9 @@ l3_done:
 /*
  * Dissect a Cause information element.
  */
-static const value_string q931_cause_coding_standard_vals[] = {
-       { 0x00, "ITU-T standardized coding" },
-       { 0x20, "ISO/IEC standard" },
-       { 0x40, "National standard" },
-       { 0x60, "Standard specific to identified location" },
-       { 0,    NULL }
-};
-       
-static const value_string q931_cause_location_vals[] = {
+
+
+const value_string q931_cause_location_vals[] = {
        { 0x00, "User (U)" },
        { 0x01, "Private network serving the local user (LPN)" },
        { 0x02, "Public network serving the local user (LN)" },
@@ -926,124 +1032,165 @@ static const value_string q931_cause_recommendation_vals[] = {
 /*
  * Cause codes for Cause.
  */
-static const value_string q931_cause_code_vals[] = {
-       { 0x00, "Valid cause code not yet received" },
-       { 0x01, "Unallocated (unassigned) number" },
-       { 0x02, "No route to specified transit network" },
-       { 0x03, "No route to destination" },
-       { 0x04, "Send special information tone" },
-       { 0x05, "Misdialled trunk prefix" },
-       { 0x06, "Channel unacceptable" },
-       { 0x07, "Call awarded and being delivered in an established channel" },
-       { 0x08, "Prefix 0 dialed but not allowed" },
-       { 0x09, "Prefix 1 dialed but not allowed" },
-       { 0x0A, "Prefix 1 dialed but not required" },
-       { 0x0B, "More digits received than allowed, call is proceeding" },
-       { 0x10, "Normal call clearing" },
-       { 0x11, "User busy" },
-       { 0x12, "No user responding" },
-       { 0x13, "No answer from user (user alerted)" },
-       { 0x14, "Subscriber absent" },
-       { 0x15, "Call rejected" },
-       { 0x16, "Number changed" },
-       { 0x17, "Reverse charging rejected" },
-       { 0x18, "Call suspended" },
-       { 0x19, "Call resumed" },
-       { 0x1A, "Non-selected user clearing" },
-       { 0x1B, "Destination out of order" },
-       { 0x1C, "Invalid number format (incomplete number)" },
-       { 0x1D, "Facility rejected" },
-       { 0x1E, "Response to STATUS ENQUIRY" },
-       { 0x1F, "Normal unspecified" },
-       { 0x21, "Circuit out of order" },
-       { 0x22, "No circuit/channel available" },
-       { 0x23, "Destination unattainable" },
-       { 0x25, "Degraded service" },
-       { 0x26, "Network out of order" },
-       { 0x27, "Transit delay range cannot be achieved" },
-       { 0x28, "Throughput range cannot be achieved" },
-       { 0x29, "Temporary failure" },
-       { 0x2A, "Switching equipment congestion" },
-       { 0x2B, "Access information discarded" },
-       { 0x2C, "Requested circuit/channel not available" },
-       { 0x2D, "Pre-empted" },
-       { 0x2E, "Precedence call blocked" },
-       { 0x2F, "Resources unavailable, unspecified" },
-       { 0x31, "Quality of service unavailable" },
-       { 0x32, "Requested facility not subscribed" },
-       { 0x33, "Reverse charging not allowed" },
-       { 0x34, "Outgoing calls barred" },
-       { 0x35, "Outgoing calls barred within CUG" },
-       { 0x36, "Incoming calls barred" },
-       { 0x37, "Incoming calls barred within CUG" },
-       { 0x38, "Call waiting not subscribed" },
-       { 0x39, "Bearer capability not authorized" },
-       { 0x3A, "Bearer capability not presently available" },
-       { 0x3E, "Inconsistency in designated outgoing access information and subscriber class" },
-       { 0x3F, "Service or option not available, unspecified" },
-       { 0x41, "Bearer capability not implemented" },
-       { 0x42, "Channel type not implemented" },
-       { 0x43, "Transit network selection not implemented" },
-       { 0x44, "Message not implemented" },
-       { 0x45, "Requested facility not implemented" },
-       { 0x46, "Only restricted digital information bearer capability is available" },
-       { 0x4F, "Service or option not implemented, unspecified" },
-       { 0x51, "Invalid call reference value" },
-       { 0x52, "Identified channel does not exist" },
-       { 0x53, "Call identity does not exist for suspended call" },
-       { 0x54, "Call identity in use" },
-       { 0x55, "No call suspended" },
-       { 0x56, "Call having the requested call identity has been cleared" },
-       { 0x57, "Called user not member of CUG" },
-       { 0x58, "Incompatible destination" },
-       { 0x59, "Non-existent abbreviated address entry" },
-       { 0x5A, "Destination address missing, and direct call not subscribed" },
-       { 0x5B, "Invalid transit network selection (national use)" },
-       { 0x5C, "Invalid facility parameter" },
-       { 0x5D, "Mandatory information element is missing" },
-       { 0x5F, "Invalid message, unspecified" },
-       { 0x60, "Mandatory information element is missing" },
-       { 0x61, "Message type non-existent or not implemented" },
-       { 0x62, "Message not compatible with call state or message type non-existent or not implemented" },
-       { 0x63, "Information element nonexistant or not implemented" },
-       { 0x64, "Invalid information element contents" },
-       { 0x65, "Message not compatible with call state" },
-       { 0x66, "Recovery on timer expiry" },
-       { 0x67, "Parameter non-existent or not implemented - passed on" },
-       { 0x6E, "Message with unrecognized parameter discarded" },
-       { 0x6F, "Protocol error, unspecified" },
-       { 0x7F, "Internetworking, unspecified" },
-       { 0,    NULL }
+#define        Q931_CAUSE_UNALLOC_NUMBER       0x01
+#define        Q931_CAUSE_NO_ROUTE_TO_DEST     0x03
+#define        Q931_CAUSE_CALL_REJECTED        0x15
+#define        Q931_CAUSE_NUMBER_CHANGED       0x16
+#define        Q931_CAUSE_ACCESS_INFO_DISC     0x2B
+#define        Q931_CAUSE_QOS_UNAVAILABLE      0x31
+#define        Q931_CAUSE_CHAN_NONEXISTENT     0x52
+#define        Q931_CAUSE_INCOMPATIBLE_DEST    0x58
+#define        Q931_CAUSE_MAND_IE_MISSING      0x60
+#define        Q931_CAUSE_MT_NONEX_OR_UNIMPL   0x61
+#define        Q931_CAUSE_IE_NONEX_OR_UNIMPL   0x63
+#define        Q931_CAUSE_INVALID_IE_CONTENTS  0x64
+#define        Q931_CAUSE_MSG_INCOMPAT_W_CS    0x65
+#define        Q931_CAUSE_REC_TIMER_EXP        0x66
+
+const value_string q931_cause_code_vals[] = {
+       { 0x00,                         "Valid cause code not yet received" },
+       { Q931_CAUSE_UNALLOC_NUMBER,    "Unallocated (unassigned) number" },
+       { 0x02,                         "No route to specified transit network" },
+       { Q931_CAUSE_NO_ROUTE_TO_DEST,  "No route to destination" },
+       { 0x04,                         "Send special information tone" },
+       { 0x05,                         "Misdialled trunk prefix" },
+       { 0x06,                         "Channel unacceptable" },
+       { 0x07,                         "Call awarded and being delivered in an established channel" },
+       { 0x08,                         "Prefix 0 dialed but not allowed" },
+                                       /* Q.850 - "Preemption" */
+       { 0x09,                         "Prefix 1 dialed but not allowed" },
+                                       /* Q.850 - "Preemption - circuit reserved for reuse" */
+       { 0x0A,                         "Prefix 1 dialed but not required" },
+       { 0x0B,                         "More digits received than allowed, call is proceeding" },
+       { 0x0E,                         "QoR: ported number" },
+       { 0x10,                         "Normal call clearing" },
+       { 0x11,                         "User busy" },
+       { 0x12,                         "No user responding" },
+       { 0x13,                         "No answer from user (user alerted)" },
+       { 0x14,                         "Subscriber absent" },
+       { Q931_CAUSE_CALL_REJECTED,     "Call rejected" },
+       { Q931_CAUSE_NUMBER_CHANGED,    "Number changed" },
+       { 0x17,                         "Reverse charging rejected" },
+                                       /* Q.850 - "Redirection to new destination" */
+       { 0x18,                         "Call suspended" },
+                                       /* Q.850 Amendment 1 - "Call rejected due to feature at the destination" */
+       { 0x19,                         "Call resumed" },
+                                       /* Q.850 - "Exchange routing error */
+       { 0x1A,                         "Non-selected user clearing" },
+       { 0x1B,                         "Destination out of order" },
+       { 0x1C,                         "Invalid number format (incomplete number)" },
+       { 0x1D,                         "Facility rejected" },
+       { 0x1E,                         "Response to STATUS ENQUIRY" },
+       { 0x1F,                         "Normal unspecified" },
+       { 0x21,                         "Circuit out of order" },
+       { 0x22,                         "No circuit/channel available" },
+       { 0x23,                         "Destination unattainable" },
+       { 0x25,                         "Degraded service" },
+       { 0x26,                         "Network out of order" },
+       { 0x27,                         "Transit delay range cannot be achieved" },
+                                       /* Q.850 - "Permanent frame mode connection out of service" */
+       { 0x28,                         "Throughput range cannot be achieved" },
+                                       /* Q.850 - "Permanent frame mode connection operational" */
+       { 0x29,                         "Temporary failure" },
+       { 0x2A,                         "Switching equipment congestion" },
+       { Q931_CAUSE_ACCESS_INFO_DISC,  "Access information discarded" },
+       { 0x2C,                         "Requested circuit/channel not available" },
+       { 0x2D,                         "Pre-empted" },
+       { 0x2E,                         "Precedence call blocked" },
+       { 0x2F,                         "Resources unavailable, unspecified" },
+       { Q931_CAUSE_QOS_UNAVAILABLE,   "Quality of service unavailable" },
+       { 0x32,                         "Requested facility not subscribed" },
+       { 0x33,                         "Reverse charging not allowed" },
+       { 0x34,                         "Outgoing calls barred" },
+       { 0x35,                         "Outgoing calls barred within CUG" },
+       { 0x36,                         "Incoming calls barred" },
+       { 0x37,                         "Incoming calls barred within CUG" },
+       { 0x38,                         "Call waiting not subscribed" },
+       { 0x39,                         "Bearer capability not authorized" },
+       { 0x3A,                         "Bearer capability not presently available" },
+       { 0x3E,                         "Inconsistency in designated outgoing access information and subscriber class" },
+       { 0x3F,                         "Service or option not available, unspecified" },
+       { 0x41,                         "Bearer capability not implemented" },
+       { 0x42,                         "Channel type not implemented" },
+       { 0x43,                         "Transit network selection not implemented" },
+       { 0x44,                         "Message not implemented" },
+       { 0x45,                         "Requested facility not implemented" },
+       { 0x46,                         "Only restricted digital information bearer capability is available" },
+       { 0x4F,                         "Service or option not implemented, unspecified" },
+       { 0x51,                         "Invalid call reference value" },
+       { Q931_CAUSE_CHAN_NONEXISTENT,  "Identified channel does not exist" },
+       { 0x53,                         "Call identity does not exist for suspended call" },
+       { 0x54,                         "Call identity in use" },
+       { 0x55,                         "No call suspended" },
+       { 0x56,                         "Call having the requested call identity has been cleared" },
+       { 0x57,                         "Called user not member of CUG" },
+       { Q931_CAUSE_INCOMPATIBLE_DEST, "Incompatible destination" },
+       { 0x59,                         "Non-existent abbreviated address entry" },
+       { 0x5A,                         "Destination address missing, and direct call not subscribed" },
+                                       /* Q.850 - "Non-existent CUG" */
+       { 0x5B,                         "Invalid transit network selection (national use)" },
+       { 0x5C,                         "Invalid facility parameter" },
+       { 0x5D,                         "Mandatory information element is missing" },
+       { 0x5F,                         "Invalid message, unspecified" },
+       { Q931_CAUSE_MAND_IE_MISSING,   "Mandatory information element is missing" },
+       { Q931_CAUSE_MT_NONEX_OR_UNIMPL,"Message type non-existent or not implemented" },
+       { 0x62,                         "Message not compatible with call state or message type non-existent or not implemented" },
+       { Q931_CAUSE_IE_NONEX_OR_UNIMPL,"Information element nonexistant or not implemented" },
+       { Q931_CAUSE_INVALID_IE_CONTENTS,"Invalid information element contents" },
+       { Q931_CAUSE_MSG_INCOMPAT_W_CS, "Message not compatible with call state" },
+       { Q931_CAUSE_REC_TIMER_EXP,     "Recovery on timer expiry" },
+       { 0x67,                         "Parameter non-existent or not implemented - passed on" },
+       { 0x6E,                         "Message with unrecognized parameter discarded" },
+       { 0x6F,                         "Protocol error, unspecified" },
+       { 0x7F,                         "Internetworking, unspecified" },
+       { 0,                            NULL }
 };
 
-static void
+static const value_string q931_cause_condition_vals[] = {
+       { 0x00, "Unknown" },
+       { 0x01, "Permanent" },
+       { 0x02, "Transient" },
+       { 0x00, NULL }
+};
+
+#define        Q931_REJ_USER_SPECIFIC          0x00
+#define        Q931_REJ_IE_MISSING             0x04
+#define        Q931_REJ_IE_INSUFFICIENT        0x08
+
+static const value_string q931_rejection_reason_vals[] = {
+       { 0x00, "User specific" },
+       { 0x04, "Information element missing" },
+       { 0x08, "Information element contents are not sufficient" },
+       { 0x00, NULL }
+};
+
+void
 dissect_q931_cause_ie(tvbuff_t *tvb, int offset, int len,
-    proto_tree *tree)
+    proto_tree *tree, int hf_cause_value)
 {
        guint8 octet;
+       guint8 cause_value;
        guint8 coding_standard;
+       guint8 rejection_reason;
 
        if (len == 0)
                return;
        octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Coding standard: %s",
-           val_to_str(coding_standard, q931_cause_coding_standard_vals, NULL));
        if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
                /*
                 * We don't know how the cause is encoded,
                 * so just dump it as data and be done with it.
                 */
+               proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
                proto_tree_add_text(tree, tvb, offset,
                    len, "Data: %s",
                    tvb_bytes_to_str(tvb, offset, len));
                return;
        }
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Location: %s",
-           val_to_str(octet & 0x0F, q931_cause_location_vals,
-             "Unknown (0x%X)"));
+       proto_tree_add_uint(tree, hf_q931_cause_location, tvb, offset, 1, octet);
+       proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
+       proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
        offset += 1;
        len -= 1;
 
@@ -1055,6 +1202,7 @@ dissect_q931_cause_ie(tvbuff_t *tvb, int offset, int len,
                    "Recommendation: %s",
                    val_to_str(octet & 0x7F, q931_cause_recommendation_vals,
                      "Unknown (0x%02X)"));
+               proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
                offset += 1;
                len -= 1;
        }
@@ -1062,31 +1210,117 @@ dissect_q931_cause_ie(tvbuff_t *tvb, int offset, int len,
        if (len == 0)
                return;
        octet = tvb_get_guint8(tvb, offset);
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Cause value: %s",
-           val_to_str(octet & 0x7F, q931_cause_code_vals,
-             "Unknown (0x%02X)"));
+       cause_value = octet & 0x7F;
+       proto_tree_add_uint(tree, hf_cause_value, tvb, offset, 1, cause_value);
+       proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
        offset += 1;
        len -= 1;
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, tvb, offset, len,
-           "Diagnostics: %s",
-           tvb_bytes_to_str(tvb, offset, len));
+       switch (cause_value) {
+
+       case Q931_CAUSE_UNALLOC_NUMBER:
+       case Q931_CAUSE_NO_ROUTE_TO_DEST:
+       case Q931_CAUSE_QOS_UNAVAILABLE:
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
+                   "Network service: %s",
+                   (octet & 0x80) ? "User" : "Provider");
+               proto_tree_add_text(tree, tvb, offset, 1,
+                   "%s",
+                   (octet & 0x40) ? "Abnormal" : "Normal");
+               proto_tree_add_text(tree, tvb, offset, 1,
+                   "Condition: %s",
+                   val_to_str(octet & 0x03, q931_cause_condition_vals,
+                     "Unknown (0x%X)"));
+               break;
+
+       case Q931_CAUSE_CALL_REJECTED:
+               rejection_reason = octet & 0x7C;
+               proto_tree_add_text(tree, tvb, offset, 1,
+                   "Rejection reason: %s",
+                   val_to_str(octet & 0x7C, q931_rejection_reason_vals,
+                     "Unknown (0x%X)"));
+               proto_tree_add_text(tree, tvb, offset, 1,
+                   "Condition: %s",
+                   val_to_str(octet & 0x03, q931_cause_condition_vals,
+                     "Unknown (0x%X)"));
+               offset += 1;
+               len -= 1;
+
+               if (len == 0)
+                       return;
+               switch (rejection_reason) {
+
+               case Q931_REJ_USER_SPECIFIC:
+                       proto_tree_add_text(tree, tvb, offset, len,
+                           "User specific diagnostic: %s",
+                           tvb_bytes_to_str(tvb, offset, len));
+                       break;
+
+               case Q931_REJ_IE_MISSING:
+                       proto_tree_add_text(tree, tvb, offset, 1,
+                           "Missing information element: %s",
+                           val_to_str(tvb_get_guint8(tvb, offset), q931_info_element_vals0,
+                             "Unknown (0x%02X)"));
+                       break;
+
+               case Q931_REJ_IE_INSUFFICIENT:
+                       proto_tree_add_text(tree, tvb, offset, 1,
+                           "Insufficient information element: %s",
+                           val_to_str(tvb_get_guint8(tvb, offset), q931_info_element_vals0,
+                             "Unknown (0x%02X)"));
+                       break;
+
+               default:
+                       proto_tree_add_text(tree, tvb, offset, len,
+                           "Diagnostic: %s",
+                           tvb_bytes_to_str(tvb, offset, len));
+                       break;
+               }
+               break;
+
+       case Q931_CAUSE_ACCESS_INFO_DISC:
+       case Q931_CAUSE_INCOMPATIBLE_DEST:
+       case Q931_CAUSE_MAND_IE_MISSING:
+       case Q931_CAUSE_IE_NONEX_OR_UNIMPL:
+       case Q931_CAUSE_INVALID_IE_CONTENTS:
+               do {
+                       proto_tree_add_text(tree, tvb, offset, 1,
+                           "Information element: %s",
+                           val_to_str(tvb_get_guint8(tvb, offset), q931_info_element_vals0,
+                             "Unknown (0x%02X)"));
+                       offset += 1;
+                       len -= 1;
+               } while (len != 0);
+               break;
+
+       case Q931_CAUSE_MT_NONEX_OR_UNIMPL:
+       case Q931_CAUSE_MSG_INCOMPAT_W_CS:
+               proto_tree_add_text(tree, tvb, offset, 1,
+                   "Message type: %s",
+                   val_to_str(tvb_get_guint8(tvb, offset), q931_message_type_vals,
+                     "Unknown (0x%02X)"));
+               break;
+
+       case Q931_CAUSE_REC_TIMER_EXP:
+               if (len < 3)
+                       return;
+               proto_tree_add_text(tree, tvb, offset, 3,
+                   "Timer: %.3s", tvb_get_ptr(tvb, offset, 3));
+               break;
+
+       default:
+               proto_tree_add_text(tree, tvb, offset, len,
+                   "Diagnostics: %s",
+                   tvb_bytes_to_str(tvb, offset, len));
+       }
 }
 
 /*
  * Dissect a Call state information element.
  */
-static const value_string q931_coding_standard_vals[] = {
-       { 0x00, "ITU-T standardized coding" },
-       { 0x20, "ISO/IEC standard" },
-       { 0x40, "National standard" },
-       { 0x60, "Standard defined for the network" },
-       { 0,    NULL }
-};
-       
 static const value_string q931_call_state_vals[] = {
        { 0x00, "Null" },
        { 0x01, "Call initiated" },
@@ -1095,14 +1329,15 @@ static const value_string q931_call_state_vals[] = {
        { 0x04, "Call delivered" },
        { 0x06, "Call present" },
        { 0x07, "Call received" },
-       { 0x09, "Connect request" },
-       { 0x0A, "Incoming call proceeding" },
-       { 0x0B, "Active" },
-       { 0x0C, "Disconnect request" },
-       { 0x0F, "Disconnect indication" },
-       { 0x11, "Suspend request" },
-       { 0x13, "Resume request" },
-       { 0x16, "Release request" },
+       { 0x08, "Connect request" },
+       { 0x09, "Incoming call proceeding" },
+       { 0x0A, "Active" },
+       { 0x0B, "Disconnect request" },
+       { 0x0C, "Disconnect indication" },
+       { 0x0F, "Suspend request" },
+       { 0x12, "Resume request" },
+       { 0x13, "Release request" },
+       { 0x16, "Call abort"},
        { 0x19, "Overlap receiving" },
        { 0x3D, "Restart request" },
        { 0x3E, "Restart" },
@@ -1120,9 +1355,7 @@ dissect_q931_call_state_ie(tvbuff_t *tvb, int offset, int len,
                return;
        octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Coding standard: %s",
-           val_to_str(coding_standard, q931_coding_standard_vals, NULL));
+       proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
        if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
                /*
                 * We don't know how the call state is encoded,
@@ -1161,7 +1394,7 @@ static const value_string q931_not_basic_channel_selection_vals[] = {
 };
 
 #define        Q931_IS_SLOT_MAP                0x10
-       
+
 static const value_string q931_element_type_vals[] = {
        { 0x03, "B-channel units" },
        { 0x06, "H0-channel units" },
@@ -1238,10 +1471,7 @@ dissect_q931_channel_identification_ie(tvbuff_t *tvb, int offset, int len,
                        return;
                octet = tvb_get_guint8(tvb, offset);
                coding_standard = octet & 0x60;
-               proto_tree_add_text(tree, tvb, offset, 1,
-                   "Coding standard: %s",
-                   val_to_str(coding_standard, q931_coding_standard_vals,
-                     NULL));
+               proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
                if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
                        /*
                         * We don't know how the channel identifier is
@@ -1292,9 +1522,7 @@ dissect_q931_progress_indicator_ie(tvbuff_t *tvb, int offset, int len,
                return;
        octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Coding standard: %s",
-           val_to_str(coding_standard, q931_cause_coding_standard_vals, NULL));
+       proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
        if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
                /*
                 * We don't know how the progress indicator is encoded,
@@ -1427,19 +1655,24 @@ static void
 dissect_q931_date_time_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
-       if (len != 6) {
+       if (len == 6) {
+               /*
+                * XXX - what is "year" relative to?  Is "month" 0-origin or
+                * 1-origin?  Q.931 doesn't say....
+                */
+               proto_tree_add_text(tree, tvb, offset, 6,
+                   "Date/time: %02u-%02u-%02u %02u:%02u:%02u",
+                   tvb_get_guint8(tvb, offset + 0), tvb_get_guint8(tvb, offset + 1), tvb_get_guint8(tvb, offset + 2),
+                   tvb_get_guint8(tvb, offset + 3), tvb_get_guint8(tvb, offset + 4), tvb_get_guint8(tvb, offset + 5));
+       } else if (len == 5) {
+               proto_tree_add_text(tree, tvb, offset, 5,
+                   "Date/time: %02u-%02u-%02u %02u:%02u:00",
+                   tvb_get_guint8(tvb, offset + 0), tvb_get_guint8(tvb, offset + 1), tvb_get_guint8(tvb, offset + 2),
+                   tvb_get_guint8(tvb, offset + 3), tvb_get_guint8(tvb, offset + 4));
+       } else {
                proto_tree_add_text(tree, tvb, offset, len,
-                   "Date/time: length is %d, should be 6\n", len);
-               return;
+                   "Date/time: length is %d, should be 5 or 6", len);
        }
-       /*
-        * XXX - what is "year" relative to?  Is "month" 0-origin or
-        * 1-origin?  Q.931 doesn't say....
-        */
-       proto_tree_add_text(tree, tvb, offset, 6,
-           "Date/time: %02u-%02u-%02u %02u:%02u:%02u",
-           tvb_get_guint8(tvb, offset + 0), tvb_get_guint8(tvb, offset + 1), tvb_get_guint8(tvb, offset + 2),
-           tvb_get_guint8(tvb, offset + 3), tvb_get_guint8(tvb, offset + 4), tvb_get_guint8(tvb, offset + 5));
 }
 
 /*
@@ -1454,7 +1687,7 @@ static const value_string q931_signal_vals[] = {
        { 0x05, "Confirm tone on" },
        { 0x06, "Answer tone on" },
        { 0x07, "Call waiting tone on" },
-       { 0x08, "Off-hoke warning tone on" },
+       { 0x08, "Off-hook warning tone on" },
        { 0x09, "Preemption tone on" },
        { 0x3F, "Tones off" },
        { 0x40, "Alerting on - pattern 0" },
@@ -1475,7 +1708,7 @@ dissect_q931_signal_ie(tvbuff_t *tvb, int offset, int len,
 {
        if (len != 1) {
                proto_tree_add_text(tree, tvb, offset, len,
-                   "Signal: length is %d, should be 1\n", len);
+                   "Signal: length is %d, should be 1", len);
                return;
        }
        proto_tree_add_text(tree, tvb, offset, 1,
@@ -1508,7 +1741,7 @@ dissect_q931_information_rate_ie(tvbuff_t *tvb, int offset, int len,
 {
        if (len != 4) {
                proto_tree_add_text(tree, tvb, offset, len,
-                   "Information rate: length is %d, should be 4\n", len);
+                   "Information rate: length is %d, should be 4", len);
                return;
        }
        proto_tree_add_text(tree, tvb, offset + 0, 1,
@@ -1774,12 +2007,12 @@ dissect_q931_reverse_charge_ind_ie(tvbuff_t *tvb, int offset, int len,
  * Dissect a (phone) number information element.
  */
 static const value_string q931_number_type_vals[] = {
-       { 0x00, "Unknown" },
-       { 0x10, "International number" },
-       { 0x20, "National number" },
-       { 0x30, "Network specific number" },
-       { 0x40, "Subscriber number" },
-       { 0x60, "Abbreviated number" },
+       { 0x0, "Unknown" },
+       { 0x1, "International number" },
+       { 0x2, "National number" },
+       { 0x3, "Network specific number" },
+       { 0x4, "Subscriber number" },
+       { 0x6, "Abbreviated number" },
        { 0,    NULL }
 };
 
@@ -1795,8 +2028,8 @@ static const value_string q931_numbering_plan_vals[] = {
 
 static const value_string q931_presentation_indicator_vals[] = {
        { 0x00, "Presentation allowed" },
-       { 0x20, "Presentation restricted" },
-       { 0x40, "Number not available due to interworking" },
+       { 0x01, "Presentation restricted" },
+       { 0x02, "Number not available due to interworking" },
        { 0,    NULL }
 };
 
@@ -1821,21 +2054,20 @@ static const value_string q931_redirection_reason_vals[] = {
 
 static void
 dissect_q931_number_ie(tvbuff_t *tvb, int offset, int len,
-    proto_tree *tree)
+    proto_tree *tree, int hfindex, e164_info_t e164_info)
 {
        guint8 octet;
+       gint number_plan;
 
        if (len == 0)
                return;
        octet = tvb_get_guint8(tvb, offset);
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Type of number: %s",
-           val_to_str(octet & 0x70, q931_number_type_vals,
-             "Unknown (0x%02X)"));
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Numbering plan: %s",
-           val_to_str(octet & 0x0F, q931_numbering_plan_vals,
-             "Unknown (0x%02X)"));
+       number_plan = octet & 0x0f;
+       e164_info.nature_of_address = ( octet & 0x70 ) >> 4;
+       proto_tree_add_uint(tree, hf_q931_numbering_plan, tvb, offset, 1, octet);
+       proto_tree_add_uint(tree, hf_q931_number_type, tvb, offset, 1, octet);
+       proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
+       
        offset += 1;
        len -= 1;
 
@@ -1843,14 +2075,9 @@ dissect_q931_number_ie(tvbuff_t *tvb, int offset, int len,
                if (len == 0)
                        return;
                octet = tvb_get_guint8(tvb, offset);
-               proto_tree_add_text(tree, tvb, offset, 1,
-                   "Presentation indicator: %s",
-                   val_to_str(octet & 0x60, q931_presentation_indicator_vals,
-                     "Unknown (0x%X)"));
-               proto_tree_add_text(tree, tvb, offset, 1,
-                   "Screening indicator: %s",
-                   val_to_str(octet & 0x03, q931_screening_indicator_vals,
-                     "Unknown (0x%X)"));
+               proto_tree_add_uint(tree, hf_q931_screening_ind, tvb, offset, 1, octet);
+               proto_tree_add_uint(tree, hf_q931_presentation_ind, tvb, offset, 1, octet);
+               proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
                offset += 1;
                len -= 1;
        }
@@ -1872,8 +2099,18 @@ dissect_q931_number_ie(tvbuff_t *tvb, int offset, int len,
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, tvb, offset, len, "Number: %s",
-           tvb_format_text(tvb, offset, len));
+       proto_tree_add_item(tree, hfindex, tvb, offset, len, FALSE);
+       proto_item_append_text(proto_tree_get_parent(tree), ": '%s'", tvb_format_text(tvb, offset, len));
+
+       if ( number_plan == 1 ) {
+               if ( e164_info.e164_number_type != NONE ){
+
+                       e164_info.E164_number_str = tvb_get_string(tvb, offset, len);
+                       e164_info.E164_number_length = len;
+                       dissect_e164_number(tvb, tree, offset, len, e164_info);
+               }
+       }
+
 }
 
 /*
@@ -1933,7 +2170,7 @@ dissect_q931_restart_indicator_ie(tvbuff_t *tvb, int offset, int len,
 {
        if (len != 1) {
                proto_tree_add_text(tree, tvb, offset, len,
-                   "Restart indicator: length is %d, should be 1\n", len);
+                   "Restart indicator: length is %d, should be 1", len);
                return;
        }
        proto_tree_add_text(tree, tvb, offset, 1,
@@ -1986,9 +2223,9 @@ dissect_q931_high_layer_compat_ie(tvbuff_t *tvb, int offset, int len,
                return;
        octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, tvb, offset, 1,
-           "Coding standard: %s",
-           val_to_str(coding_standard, q931_coding_standard_vals, NULL));
+       proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
+       offset += 1;
+       len -= 1;
        if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
                /*
                 * We don't know how the call state is encoded,
@@ -2035,20 +2272,21 @@ dissect_q931_high_layer_compat_ie(tvbuff_t *tvb, int offset, int len,
 /*
  * Dissect a User-user information element.
  */
-#define        Q931_PROTOCOL_DISCRIMINATOR_IA5 0x04
+#define        Q931_PROTOCOL_DISCRIMINATOR_IA5         0x04
+#define Q931_PROTOCOL_DISCRIMINATOR_ASN1       0x05
 
 static const value_string q931_protocol_discriminator_vals[] = {
        { 0x00,                                 "User-specific protocol" },
        { 0x01,                                 "OSI high layer protocols" },
        { 0x02,                                 "X.244" },
        { Q931_PROTOCOL_DISCRIMINATOR_IA5,      "IA5 characters" },
-       { 0x05,                                 "X.208 and X.209 coded user information" },
+       { Q931_PROTOCOL_DISCRIMINATOR_ASN1,     "X.208 and X.209 coded user information" },
        { 0x07,                                 "V.120 rate adaption" },
        { 0x08,                                 "Q.931/I.451 user-network call control messages" },
        { 0,                                    NULL }
 };
 
-static void
+void
 dissect_q931_user_user_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
@@ -2090,158 +2328,65 @@ dissect_q931_ia5_ie(tvbuff_t *tvb, int offset, int len, proto_tree *tree,
        if (len != 0) {
                proto_tree_add_text(tree, tvb, offset, len, "%s: %s", label,
                    tvb_format_text(tvb, offset, len));
+               proto_item_append_text(proto_tree_get_parent(tree), "  '%s'", tvb_format_text(tvb, offset, len));
        }
 }
 
-static const value_string q931_codeset_vals[] = {
-       { 0x00, "Q.931 information elements" },
-       { 0x04, "Information elements for ISO/IEC use" },
-       { 0x05, "Information elements for national use" },
-       { 0x06, "Information elements specific to the local network" },
-       { 0x07, "User-specific information elements" },
-       { 0x00, NULL },
-};
-
-static gboolean
-q931_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
-    gboolean started_heuristic)
+static void
+dissect_q931_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
+    gboolean is_tpkt)
 {
        int             offset = 0;
-       guint           reported_length;
        proto_tree      *q931_tree = NULL;
-       proto_item      *ti;
-       proto_tree      *ie_tree;
+       proto_tree      *ie_tree = NULL;
+       proto_item      *ti, *ti_ie;
        guint8          call_ref_len;
        guint8          call_ref[15];
-       guint8          message_type;
+       guint32         call_ref_val;
+       guint8          message_type, segmented_message_type;
        guint8          info_element;
        guint16         info_element_len;
-       int             codeset; 
-       gboolean        non_locking_shift;
-       guint8          protocol_discriminator;
-
-#ifdef H323
-       tvbuff_t        *h225_tvb;
-       gboolean        is_h323_h225 = FALSE;
-       /*
-        * It is very much possible to find a TPKT header here
-        * TPKT is defined in RFC 1009 as a wrapper around ISO
-        * defined protocols. There could even be several TPKT
-        * wrapped messages in one TCP data field...
-        * XXXXXXXX THIS IS NOT IMPLEMENTED YET!!! XXXXXXXXXXX
-        *
-        * For Q.931 related messages this is easy. If a 
-        * protocol discriminator is found with a value of 3
-        * and it's the first discriminator in the Q.931 
-        * message one can safely assume it to be a TPKT 
-        * header. See also Q.931 Table 4-1/Q.931 
-        */
-#endif
-
-       protocol_discriminator = tvb_get_guint8( tvb, offset );
-           /* Keep the protocol discriminator for later use */
-
-#ifdef H323
-       if ( started_heuristic ) {
-               /*
-                * The heuristic Q.931 message should conform to this
-                */
-               if ( protocol_discriminator != NLPID_Q_931 ) 
-                       return FALSE;
-
-               if ( ! is_tpkt( tvb, &offset ) ) 
-                       return FALSE;
-
-               if ( tvb_length_remaining( tvb, offset ) <= 3 ) 
-                       return FALSE;
-       }
+       gboolean        more_frags; 
+       guint32         frag_len;
+       fragment_data *fd_head;
+       tvbuff_t *next_tvb = NULL;
 
-       /* 
-        * The first byte should be < 8 (3 is TPKT, rest is Q.931)
-        */
-       if ( protocol_discriminator < 8 ) {
-               /*
-                * The minimum length of a Q.931 message is 3:
-                * 1 byte for the protocol discriminator,
-                * 1 for the call_reference length,
-                * and one for the message type.
-                */
-               if ( tvb_length_remaining( tvb, offset ) <= 3 ) 
-                       return FALSE;
-
-               /* 
-                * OK, there are a couple of bytes available, but is there 
-                * also a protocol discriminator?
-                */
-               if ( tvb_length_remaining( tvb, offset ) > 3 ) {
-                       /* Reread the protocol discriminator */
-                       protocol_discriminator =
-                           tvb_get_guint8( tvb, offset + 4);
-               } else {
-                     /* No discriminator available */
-                     protocol_discriminator = 0;
-               }
-
-               /*
-                * If it's not H.323 related Q.931 no heuristic action needed
-                * Dangerous, there might be other uses for this code.....
-                */
-               if ( ( started_heuristic ) && (protocol_discriminator != 8 ) ) 
-                       return FALSE;
-
-               /*
-                * Always check if it's a real TPKT message
-                */
-               if ( ! is_tpkt( tvb, &offset ) ) 
-                       return FALSE;
-
-               dissect_tpkt( tvb, &offset, pinfo, tree );
-
-               /*
-                * Reset the current_proto variable because dissect_tpkt
-                * messed with it
-                */
-               if ( started_heuristic )
-                       pinfo->current_proto = "Q.931 HEUR";
-               else 
-                       pinfo->current_proto = "Q.931";
-       }
-#endif
-
-       /*
-        * The minimum length of a Q.931 message is
-        * 3, 1 byte for the protocol discr. 1 for the call_reference length,
-        * and one for the message type.
-        */
-       if ( tvb_length_remaining( tvb, offset ) <= 3 ) {
-               return FALSE;
-       }
-
-       if (check_col(pinfo->fd, COL_PROTOCOL))
-               col_add_str(pinfo->fd, COL_PROTOCOL, "Q.931");
+       if (check_col(pinfo->cinfo, COL_PROTOCOL))
+               col_set_str(pinfo->cinfo, COL_PROTOCOL, "Q.931");
 
        if (tree) {
-               ti = proto_tree_add_item(tree, proto_q931, tvb, offset,
-                   tvb_length(tvb), FALSE);
+               ti = proto_tree_add_item(tree, proto_q931, tvb, offset, -1,
+                   FALSE);
                q931_tree = proto_item_add_subtree(ti, ett_q931);
 
-               dissect_q931_protocol_discriminator( tvb, offset, q931_tree );
+               dissect_q931_protocol_discriminator(tvb, offset, q931_tree);
        }
        offset += 1;
        call_ref_len = tvb_get_guint8(tvb, offset) & 0xF;       /* XXX - do as a bit field? */
        if (q931_tree != NULL)
                proto_tree_add_uint(q931_tree, hf_q931_call_ref_len, tvb, offset, 1, call_ref_len);
        offset += 1;
+       switch (call_ref_len) {
+               case 0: call_ref_val = 0; break;
+               case 1: call_ref_val = tvb_get_guint8(tvb, offset);     break;
+               case 2: call_ref_val = tvb_get_ntohs(tvb, offset); break;
+               case 3: call_ref_val = tvb_get_ntoh24(tvb, offset); break;
+               default: call_ref_val = tvb_get_ntohl(tvb, offset);
+       } 
        if (call_ref_len != 0) {
-               /* XXX - split this into flag and value */
                tvb_memcpy(tvb, call_ref, offset, call_ref_len);
-               if (q931_tree != NULL)
-                       proto_tree_add_bytes(q931_tree, hf_q931_call_ref, tvb, offset, call_ref_len, call_ref);
+               if (q931_tree != NULL) {
+                       proto_tree_add_boolean(q931_tree, hf_q931_call_ref_flag,
+                           tvb, offset, 1, (call_ref[0] & 0x80) != 0);
+                       call_ref[0] &= 0x7F;
+                       proto_tree_add_bytes(q931_tree, hf_q931_call_ref,
+                           tvb, offset, call_ref_len, call_ref);
+               }
                offset += call_ref_len;
        }
        message_type = tvb_get_guint8(tvb, offset);
-       if (check_col(pinfo->fd, COL_INFO)) {
-               col_add_str(pinfo->fd, COL_INFO,
+       if (check_col(pinfo->cinfo, COL_INFO)) {
+               col_add_str(pinfo->cinfo, COL_INFO,
                    val_to_str(message_type, q931_message_type_vals,
                      "Unknown message type (0x%02X)"));
        }
@@ -2252,21 +2397,96 @@ q931_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        /*
         * And now for the information elements....
         */
-       codeset = 0;    /* start out in codeset 0 */
+       if ((message_type != Q931_SEGMENT) || !q931_reassembly || 
+                       (tvb_reported_length_remaining(tvb, offset) <= 4)) {
+               dissect_q931_IEs(tvb, pinfo, tree, q931_tree, is_tpkt, offset, 0);
+               return;
+       }
+       info_element = tvb_get_guint8(tvb, offset);
+       info_element_len = tvb_get_guint8(tvb, offset + 1);
+       if ((info_element != Q931_IE_SEGMENTED_MESSAGE) || (info_element_len < 2)) {
+               dissect_q931_IEs(tvb, pinfo, tree, q931_tree, is_tpkt, offset, 0);
+               return;
+       }
+       /* Segmented message IE */
+       ti_ie = proto_tree_add_text(q931_tree, tvb, offset, 1+1+info_element_len, "%s",
+                                   val_to_str(info_element, q931_info_element_vals[0], "Unknown information element (0x%02X)"));
+       ie_tree = proto_item_add_subtree(ti_ie, ett_q931_ie);
+       proto_tree_add_text(ie_tree, tvb, offset, 1, "Information element: %s",
+                                   val_to_str(info_element, q931_info_element_vals[0], "Unknown (0x%02X)"));
+       proto_tree_add_text(ie_tree, tvb, offset + 1, 1, "Length: %u", info_element_len);
+       dissect_q931_segmented_message_ie(tvb, offset + 2, info_element_len, ie_tree);
+       more_frags = (tvb_get_guint8(tvb, offset + 2) & 0x7F) != 0;
+       segmented_message_type = tvb_get_guint8(tvb, offset + 3);
+       if (check_col(pinfo->cinfo, COL_INFO)) {
+               col_append_fstr(pinfo->cinfo, COL_INFO, " of %s",
+                   val_to_str(segmented_message_type, q931_message_type_vals, "Unknown message type (0x%02X)"));
+       }
+       offset += 1 + 1 + info_element_len;
+       /* Reassembly */
+       frag_len = tvb_length_remaining(tvb, offset);
+       fd_head = fragment_add_seq_next(tvb, offset, pinfo, call_ref_val,
+                                                                       q931_fragment_table, q931_reassembled_table,
+                                                                       frag_len, more_frags);
+       if (fd_head) {
+               if (pinfo->fd->num == fd_head->reassembled_in) {  /* last fragment */
+                       if (fd_head->next != NULL) {  /* 2 or more segments */
+                               next_tvb = tvb_new_real_data(fd_head->data, fd_head->len, fd_head->len);
+                               tvb_set_child_real_data_tvbuff(tvb, next_tvb);
+                               add_new_data_source(pinfo, next_tvb, "Reassembled Q.931 IEs");
+                               /* Show all fragments. */
+                               if (tree) show_fragment_seq_tree(fd_head, &q931_frag_items, q931_tree, pinfo, next_tvb);
+                       } else {  /* only 1 segment */
+                               next_tvb = tvb_new_subset(tvb, offset, -1, -1);
+                       }
+                       if (check_col(pinfo->cinfo, COL_INFO)) {
+                               col_add_fstr(pinfo->cinfo, COL_INFO, "%s [reassembled]",
+                                   val_to_str(segmented_message_type, q931_message_type_vals, "Unknown message type (0x%02X)"));
+                       }
+               } else {
+                       if (tree) proto_tree_add_uint(q931_tree, hf_q931_reassembled_in, tvb, offset, frag_len, fd_head->reassembled_in);
+               }
+       }
+       if (next_tvb)
+               dissect_q931_IEs(next_tvb, pinfo, tree, q931_tree, is_tpkt, 0, 0);
+}
+
+static const value_string q931_codeset_vals[] = {
+       { 0x00, "Q.931 information elements" },
+       { 0x04, "Information elements for ISO/IEC use" },
+       { 0x05, "Information elements for national use" },
+       { 0x06, "Information elements specific to the local network" },
+       { 0x07, "User-specific information elements" },
+       { 0x00, NULL },
+};
+
+static void
+dissect_q931_IEs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *root_tree,
+    proto_tree *q931_tree, gboolean is_tpkt, int offset, int initial_codeset)
+{
+       proto_item      *ti;
+       proto_tree      *ie_tree = NULL;
+       guint8          info_element;
+       guint16         info_element_len;
+       int             codeset, locked_codeset;
+       gboolean        non_locking_shift, first_segment;
+       tvbuff_t        *h225_tvb, *next_tvb;
+       e164_info_t e164_info;
+       e164_info.e164_number_type = NONE;
+
+       codeset = locked_codeset = initial_codeset;
        non_locking_shift = TRUE;
-       reported_length = tvb_reported_length(tvb);
-       while (offset < reported_length) {
+       first_segment = FALSE;
+       while (tvb_reported_length_remaining(tvb, offset) > 0) {
                info_element = tvb_get_guint8(tvb, offset);
 
-               /*
-                * Check for the single-octet IEs.
-                */
-               switch (info_element & Q931_IE_SO_IDENTIFIER_MASK) {
-
-               case Q931_IE_SHIFT:
-                       non_locking_shift =
-                           !(info_element & Q931_IE_SHIFT_LOCKING);
+               /* Check for the codeset shift */
+               if ((info_element & Q931_IE_SO_MASK) &&
+                   ((info_element & Q931_IE_SO_IDENTIFIER_MASK) == Q931_IE_SHIFT)) {
+                       non_locking_shift = info_element & Q931_IE_SHIFT_NON_LOCKING;
                        codeset = info_element & Q931_IE_SHIFT_CODESET;
+                       if (!non_locking_shift)
+                               locked_codeset = codeset;
                        if (q931_tree != NULL) {
                                proto_tree_add_text(q931_tree, tvb, offset, 1,
                                    "%s shift to codeset %u: %s",
@@ -2277,335 +2497,491 @@ q931_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                        }
                        offset += 1;
                        continue;
+               }
 
-               case Q931_IE_MORE_DATA_OR_SEND_COMP:
-                       switch (info_element) {
+               /*
+                * Check for the single-octet IEs.
+                */
+               if (info_element & Q931_IE_SO_MASK) {
+                       /*
+                        * Check for subdissectors for this IE or
+                        * for all IEs in this codeset.
+                        */
+                       if (dissector_get_port_handle(codeset_dissector_table, codeset) ||
+                           dissector_get_port_handle(ie_dissector_table, (codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK))) {
+                               next_tvb = tvb_new_subset (tvb, offset, 1, 1);
+                               if (dissector_try_port(ie_dissector_table, (codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK), next_tvb, pinfo, q931_tree) ||
+                                   dissector_try_port(codeset_dissector_table, codeset, next_tvb, pinfo, q931_tree)) {
+                                       offset += 1;
+                                       codeset = locked_codeset;
+                                       continue;
+                               }
+                       }
+
+                       switch ((codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK)) {
+
+                       case CS0 | Q931_IE_MORE_DATA_OR_SEND_COMP:
+                               switch (info_element) { 
+
+                               case Q931_IE_MORE_DATA:
+                                       if (q931_tree != NULL) {
+                                               proto_tree_add_text(q931_tree, tvb, offset, 1,
+                                                   "More data");
+                                       }
+                                       break;
+
+                               case Q931_IE_SENDING_COMPLETE:
+                                       if (q931_tree != NULL) {
+                                               proto_tree_add_text(q931_tree, tvb, offset, 1,
+                                                   "Sending complete");
+                                       }
+                                       break;
+
+                               default:
+                                       if (q931_tree != NULL) {
+                                               proto_tree_add_text(q931_tree, tvb, offset, 1,
+                                                   "Unknown information element (0x%02X)",
+                                                   info_element);
+                                       }
+                                       break;
+                               }
+                               break;
 
-                       case Q931_IE_MORE_DATA:
+                       case CS0 | Q931_IE_CONGESTION_LEVEL:
                                if (q931_tree != NULL) {
                                        proto_tree_add_text(q931_tree, tvb, offset, 1,
-                                           "More data");
+                                           "Congestion level: %s",
+                                           val_to_str(info_element & Q931_IE_SO_IE_MASK,
+                                             q931_congestion_level_vals,
+                                             "Unknown (0x%X)"));
                                }
                                break;
 
-                       case Q931_IE_SENDING_COMPLETE:
+                       case CS0 | Q931_IE_REPEAT_INDICATOR:
                                if (q931_tree != NULL) {
                                        proto_tree_add_text(q931_tree, tvb, offset, 1,
-                                           "Sending complete");
+                                           "Repeat indicator: %s",
+                                           val_to_str(info_element & Q931_IE_SO_IE_MASK,
+                                         q931_repeat_indication_vals,
+                                             "Unknown (0x%X)"));
                                }
                                break;
 
                        default:
                                if (q931_tree != NULL) {
                                        proto_tree_add_text(q931_tree, tvb, offset, 1,
-                                           "Unknown information element (0x%02X",
+                                           "Unknown information element (0x%02X)",
                                            info_element);
                                }
                                break;
                        }
                        offset += 1;
-                       if (non_locking_shift)
-                               codeset = 0;
+                       codeset = locked_codeset;
                        continue;
-
-               case Q931_IE_CONGESTION_LEVEL:
-                       if (q931_tree != NULL) {
-                               proto_tree_add_text(q931_tree, tvb, offset, 1,
-                                   "Congestion level: %s",
-                                   val_to_str(info_element & Q931_IE_SO_IE_MASK,
-                                     q931_congestion_level_vals,
-                                     "Unknown (0x%X)"));
-                       }               
-                       offset += 1;
-                       if (non_locking_shift)
-                               codeset = 0;
-                       continue;
-
-               case Q931_IE_REPEAT_INDICATOR:
-                       if (q931_tree != NULL) {
-                               proto_tree_add_text(q931_tree, tvb, offset, 1,
-                                   "Repeat indicator: %s",
-                                   val_to_str(info_element & Q931_IE_SO_IE_MASK,
-                                     q931_repeat_indication_vals,
-                                     "Unknown (0x%X)"));
-                       }               
-                       offset += 1;
-                       if (non_locking_shift)
-                               codeset = 0;
-                       continue;
-
-               default:
-                       break;
                }
 
                /*
                 * Variable-length IE.
-                */
-#ifndef H323
-               info_element_len = tvb_get_guint8(tvb, offset + 1);
-#else
-               /* 
+                *
                 * According to page 18 from Recommendation H.225.0 :
                 * " Length of user-user contents contents
                 * - Shall be 2 octets instead of 1 (as in Figure 4-36/Q.931)"
-                * 
-                * This will be true for all messages going to / from TCP port
-                * 1720 and with the first and fourth octet of the user-user 
-                * IE having the values 0x7E and 0x05 resp.
-                * See http://www.mbuf.org/~moto/h323/h323decoder.html
                 *
+                * We assume that if this is Q.931-over-TPKT, it might
+                * be H.225 traffic, and check for the IE being a user-user
+                * IE with ASN.1 encoding of the user information.
                 */
-
-               if ( ( tvb_get_guint8( tvb, offset ) == 0x7E ) && 
-                  ( tvb_get_guint8( tvb, offset + 3 ) == 0x05 ) && 
-                  /* ( ( pi.srcport == 1720 ) || ( pi.destport == 1720 ) ) && */
-                  ( protocol_discriminator == NLPID_Q_931 ) )  {
-                       info_element_len = tvb_get_ntohs( tvb, offset + 1 );
-                       is_h323_h225 = TRUE;
-                       if ( tree == NULL ) {
-                               h225_tvb = tvb_new_subset( tvb, offset + 4, info_element_len - 1, info_element_len - 1 );
-                               dissect_h225_cs( h225_tvb, pinfo, tree );
-                               /*
-                                * Skip the 4 bytes of the element header and then the element itself
-                                */
-                               offset += 4;
-                               offset += info_element_len - 1;
-                       }
-               } else {
-                       info_element_len = tvb_get_guint8( tvb, offset + 1 );
-               }
-#endif
-               if (q931_tree != NULL) {
-#ifdef H323
-                       if (is_h323_h225) {
+               if (is_tpkt && tvb_bytes_exist(tvb, offset, 4) &&
+                   codeset == 0 && tvb_get_guint8(tvb, offset) == Q931_IE_USER_USER &&
+                   tvb_get_guint8(tvb, offset + 3) == Q931_PROTOCOL_DISCRIMINATOR_ASN1)  {
+                       info_element_len = tvb_get_ntohs(tvb, offset + 1);
+                       if (q931_tree != NULL) {
                                ti = proto_tree_add_text(q931_tree, tvb, offset,
-                                   1+1+1, "%s",
+                                   1+2+info_element_len, "%s",
                                    val_to_str(info_element,
-                                     q931_info_element_vals,
+                                     q931_info_element_vals[codeset],
                                      "Unknown information element (0x%02X)"));
                                ie_tree = proto_item_add_subtree(ti,
                                    ett_q931_ie);
                                proto_tree_add_text(ie_tree, tvb, offset, 1,
                                    "Information element: %s",
                                    val_to_str(info_element,
-                                     q931_info_element_vals, "Unknown (0x%02X)"));
+                                     q931_info_element_vals[codeset], "Unknown (0x%02X)"));
                                proto_tree_add_text(ie_tree, tvb, offset + 1,
                                    2, "Length: %u", info_element_len);
-                       } else {
-#endif
-                               ti = proto_tree_add_text(q931_tree, tvb, offset,
-                                   1+1+info_element_len, "%s",
-                                   val_to_str(info_element, q931_info_element_vals,
-                                     "Unknown information element (0x%02X)"));
-                                       ie_tree = proto_item_add_subtree(ti, ett_q931_ie);
-                               proto_tree_add_text(ie_tree, tvb, offset, 1,
-                                   "Information element: %s",
-                                   val_to_str(info_element, q931_info_element_vals,
-                                     "Unknown (0x%02X)"));
-                               proto_tree_add_text(ie_tree, tvb, offset + 1, 1,
-                                   "Length: %u", info_element_len);
-#ifdef H323
+                               proto_tree_add_text(ie_tree, tvb, offset + 3,
+                                   1, "Protocol discriminator: %s",
+                                   val_to_str(tvb_get_guint8(tvb, offset + 3),
+                                     q931_protocol_discriminator_vals,
+                                     "Unknown (0x%02x)"));
                        }
-#endif
-                       switch (info_element) {
 
-                       case Q931_IE_SEGMENTED_MESSAGE:
-                               dissect_q931_segmented_message_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                       if (info_element_len > 1) {
+                               /*
+                                * Do we have a handle for the H.225
+                                * dissector?
+                                */
+                               if (h225_handle != NULL) {
+                                       /*
+                                        * Yes - call it, regardless of
+                                        * whether we're building a
+                                        * protocol tree or not.
+                                        */
+                                       h225_tvb = tvb_new_subset(tvb,
+                                           offset + 4, info_element_len - 1,
+                                           info_element_len - 1);
+                                       call_dissector(h225_handle, h225_tvb,
+                                           pinfo, root_tree);
+                               } else {
+                                       /*
+                                        * No - just show it as "User
+                                        * information" (if "ie_tree" is
+                                        * null, this won't add anything).
+                                        */
+                                       proto_tree_add_text(ie_tree, tvb,
+                                           offset + 4, info_element_len - 1,
+                                           "User information: %s",
+                                           tvb_bytes_to_str(tvb, offset + 4,
+                                             info_element_len - 1));
+                               }
+                       }
+                       offset += 1 + 2 + info_element_len;
+               } else {
+                       info_element_len = tvb_get_guint8(tvb, offset + 1);
 
-                       case Q931_IE_BEARER_CAPABILITY:
-                       case Q931_IE_LOW_LAYER_COMPAT:
-                               dissect_q931_bearer_capability_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
+                       if (first_segment && (tvb_reported_length_remaining(tvb, offset + 2) < info_element_len)) {  /* incomplete IE at the end of the 1st segment */
+                               proto_tree_add_text(q931_tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "Incomplete IE in the 1st segment");
                                break;
+                       }
 
-                       case Q931_IE_CAUSE:
-                               dissect_q931_cause_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                       /*
+                        * Check for subdissectors for this IE or
+                        * for all IEs in this codeset.
+                        */
+                       if (dissector_get_port_handle(codeset_dissector_table, codeset) ||
+                           dissector_get_port_handle(ie_dissector_table, (codeset << 8) | info_element)) {
+                               next_tvb = tvb_new_subset (tvb, offset, info_element_len + 2, info_element_len + 2);
+                               if (dissector_try_port(ie_dissector_table, (codeset << 8) | info_element, next_tvb, pinfo, q931_tree) ||
+                                   dissector_try_port(codeset_dissector_table, codeset, next_tvb, pinfo, q931_tree)) {
+                                       offset += 2 + info_element_len;
+                                       codeset = locked_codeset;
+                                       continue;
+                               }
+                       }
 
-                       case Q931_IE_CALL_STATE:
-                               dissect_q931_call_state_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                       ti = proto_tree_add_text(q931_tree, tvb, offset, 1+1+info_element_len, "%s",
+                                   val_to_str(info_element, q931_info_element_vals[codeset], "Unknown information element (0x%02X)"));
+                       ie_tree = proto_item_add_subtree(ti, ett_q931_ie);
+                       proto_tree_add_text(ie_tree, tvb, offset, 1, "Information element: %s",
+                                   val_to_str(info_element, q931_info_element_vals[codeset], "Unknown (0x%02X)"));
+                       proto_tree_add_text(ie_tree, tvb, offset + 1, 1, "Length: %u", info_element_len);
+
+                       if (((codeset << 8) | info_element) == (CS0 | Q931_IE_SEGMENTED_MESSAGE)) {
+                               dissect_q931_segmented_message_ie(tvb, offset + 2, info_element_len, ie_tree);
+                               if (check_col(pinfo->cinfo, COL_INFO)) {
+                                       col_append_fstr(pinfo->cinfo, COL_INFO, " of %s",
+                                           val_to_str(tvb_get_guint8(tvb, offset + 3), q931_message_type_vals, "Unknown message type (0x%02X)"));
+                               }
+                               if (tvb_get_guint8(tvb, offset + 2) & 0x80) {  /* the 1st segment */
+                                       first_segment = TRUE;
+                               } else {  /* not the 1st segment */
+                                       proto_tree_add_text(q931_tree, tvb, offset + 4, tvb_reported_length_remaining(tvb, offset + 4), "Message segment");
+                                       info_element_len += tvb_reported_length_remaining(tvb, offset + 4);
+                               }
+                       } else if (q931_tree != NULL) {
+                               switch ((codeset << 8) | info_element) {
 
-                       case Q931_IE_CHANNEL_IDENTIFICATION:
-                               dissect_q931_channel_identification_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_BEARER_CAPABILITY:
+                               case CS0 | Q931_IE_LOW_LAYER_COMPAT:
+                                       dissect_q931_bearer_capability_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_PROGRESS_INDICATOR:
-                               dissect_q931_progress_indicator_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_CAUSE:
+                                       dissect_q931_cause_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree,
+                                           hf_q931_cause_value);
+                                       break;
 
-                       case Q931_IE_NETWORK_SPECIFIC_FACIL:
-                       case Q931_IE_TRANSIT_NETWORK_SEL:
-                               dissect_q931_ns_facilities_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_CALL_STATE:
+                                       dissect_q931_call_state_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_NOTIFICATION_INDICATOR:
-                               dissect_q931_notification_indicator_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_CHANNEL_IDENTIFICATION:
+                                       dissect_q931_channel_identification_ie(
+                                           tvb, offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_DISPLAY:
-                               dissect_q931_ia5_ie(tvb, offset + 2,
-                                   info_element_len, ie_tree,
-                                   "Display information");
-                               break;
+                               case CS0 | Q931_IE_PROGRESS_INDICATOR:
+                                       dissect_q931_progress_indicator_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_DATE_TIME:
-                               dissect_q931_date_time_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_NETWORK_SPECIFIC_FACIL:
+                               case CS0 | Q931_IE_TRANSIT_NETWORK_SEL:
+                                       dissect_q931_ns_facilities_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_KEYPAD_FACILITY:
-                               dissect_q931_ia5_ie(tvb, offset + 2,
-                                   info_element_len, ie_tree,
-                                   "Keypad facility");
-                               break;
+                               case CS0 | Q931_IE_NOTIFICATION_INDICATOR:
+                                       dissect_q931_notification_indicator_ie(
+                                           tvb, offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_SIGNAL:
-                               dissect_q931_signal_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_DISPLAY:
+                                       dissect_q931_ia5_ie(tvb, offset + 2,
+                                           info_element_len, ie_tree,
+                                           "Display information");
+                                       break;
 
-                       case Q931_IE_INFORMATION_RATE:
-                               dissect_q931_information_rate_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_DATE_TIME:
+                                       dissect_q931_date_time_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_E2E_TRANSIT_DELAY:
-                               dissect_q931_e2e_transit_delay_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_KEYPAD_FACILITY:
+                                       dissect_q931_ia5_ie(tvb, offset + 2,
+                                           info_element_len, ie_tree,
+                                           "Keypad facility");
+                                       break;
 
-                       case Q931_IE_TD_SELECTION_AND_INT:
-                               dissect_q931_td_selection_and_int_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_SIGNAL:
+                                       dissect_q931_signal_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_PL_BINARY_PARAMETERS:
-                               dissect_q931_pl_binary_parameters_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_INFORMATION_RATE:
+                                       dissect_q931_information_rate_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_PL_WINDOW_SIZE:
-                               dissect_q931_pl_window_size_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_E2E_TRANSIT_DELAY:
+                                       dissect_q931_e2e_transit_delay_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_PACKET_SIZE:
-                               dissect_q931_packet_size_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_TD_SELECTION_AND_INT:
+                                       dissect_q931_td_selection_and_int_ie(
+                                           tvb, offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_CUG:
-                               dissect_q931_cug_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_PL_BINARY_PARAMETERS:
+                                       dissect_q931_pl_binary_parameters_ie(
+                                           tvb, offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_REVERSE_CHARGE_IND:
-                               dissect_q931_reverse_charge_ind_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_PL_WINDOW_SIZE:
+                                       dissect_q931_pl_window_size_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_CALLING_PARTY_NUMBER:
-                       case Q931_IE_CALLED_PARTY_NUMBER:
-                       case Q931_IE_REDIRECTING_NUMBER:
-                               dissect_q931_number_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_PACKET_SIZE:
+                                       dissect_q931_packet_size_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_CALLING_PARTY_SUBADDR:
-                       case Q931_IE_CALLED_PARTY_SUBADDR:
-                               dissect_q931_party_subaddr_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_CUG:
+                                       dissect_q931_cug_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_RESTART_INDICATOR:
-                               dissect_q931_restart_indicator_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_REVERSE_CHARGE_IND:
+                                       dissect_q931_reverse_charge_ind_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
 
-                       case Q931_IE_HIGH_LAYER_COMPAT:
-                               dissect_q931_high_layer_compat_ie(tvb,
-                                   offset + 2, info_element_len, ie_tree);
-                               break;
+                               case CS0 | Q931_IE_CALLING_PARTY_NUMBER:
+                                       e164_info.e164_number_type = CALLING_PARTY_NUMBER;
+                                       dissect_q931_number_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree,
+                                           hf_q931_calling_party_number, e164_info);
+                                       break;
 
-                       case Q931_IE_USER_USER:
-#ifdef H323
-                               if (is_h323_h225) {
-                                       h225_tvb = tvb_new_subset(tvb,
-                                           offset + 4, info_element_len - 1,
-                                           info_element_len - 1);
-                                       dissect_h225_cs(h225_tvb, pinfo, tree);
-                                       offset += 3;
-                                       proto_tree_add_text(ie_tree, tvb,
-                                           offset, 1,
-                                           "Protocol discriminator: %s",
-                                           val_to_str(tvb_get_guint8(tvb, offset),
-                                             q931_protocol_discriminator_vals,
-                                             "Unknown (0x%02x)"));
-                                       offset += info_element_len;
-                               } else {
-#endif
+                               case CS0 | Q931_IE_CONNECTED_NUMBER_DEFAULT:
+                                       dissect_q931_number_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree,
+                                           hf_q931_connected_number, e164_info);
+                                       break;
+
+                               case CS0 | Q931_IE_CALLED_PARTY_NUMBER:
+                                       e164_info.e164_number_type = CALLED_PARTY_NUMBER;
+                                       dissect_q931_number_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree,
+                                           hf_q931_called_party_number, e164_info);
+                                       break;
+
+                               case CS0 | Q931_IE_REDIRECTING_NUMBER:
+                                       dissect_q931_number_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree,
+                                           hf_q931_redirecting_number, e164_info);
+                                       break;
+
+                               case CS0 | Q931_IE_CALLING_PARTY_SUBADDR:
+                               case CS0 | Q931_IE_CALLED_PARTY_SUBADDR:
+                                       dissect_q931_party_subaddr_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
+
+                               case CS0 | Q931_IE_RESTART_INDICATOR:
+                                       dissect_q931_restart_indicator_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
+
+                               case CS0 | Q931_IE_HIGH_LAYER_COMPAT:
+                                       dissect_q931_high_layer_compat_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+                                       break;
+
+                               case CS0 | Q931_IE_USER_USER:
                                        dissect_q931_user_user_ie(tvb,
                                            offset + 2, info_element_len,
                                            ie_tree);
-#ifdef H323
-                               }
-#endif
-                               break;
+                                       break;
 
-                       default:
-                               proto_tree_add_text(ie_tree, tvb, offset + 2,
-                                   info_element_len, "Data: %s",
-                                   bytes_to_str(
-                                     tvb_get_ptr(tvb, offset + 2, info_element_len),
-                                     info_element_len));
-                               break;
+                               default:
+                                       proto_tree_add_text(ie_tree, tvb,
+                                           offset + 2, info_element_len,
+                                           "Data: %s",
+                                           bytes_to_str(
+                                             tvb_get_ptr(tvb, offset + 2,
+                                                 info_element_len),
+                                                 info_element_len));
+                                       break;
+                               }
                        }
+                       offset += 1 + 1 + info_element_len;
                }
-               offset += 1 + 1 + info_element_len;
-               if (non_locking_shift)
-                       codeset = 0;
+               codeset = locked_codeset;
        }
+}
+
+/*
+ * Q.931-over-TPKT-over-TCP.
+ */
+static gboolean
+dissect_q931_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
+       int lv_tpkt_len;
 
        /*
-        * Heuristic should return TRUE if it get's here.
+        * Check whether this looks like a TPKT-encapsulated
+        * Q.931 packet.
+        *
+        * The minimum length of a Q.931 message is 3:
+        * 1 byte for the protocol discriminator,
+        * 1 for the call_reference length,
+        * and one for the message type.
         */
+       lv_tpkt_len = is_tpkt(tvb, 3);
+       if (lv_tpkt_len == -1) {
+               /*
+                * It's not a TPKT packet; reject it.
+                */
+               return FALSE;
+       }
 
-       return TRUE;
-
-}
+       /*
+        * If this segment is *exactly* the length of a TPKT header,
+        * we assume that, as it looks like a TPKT header, it
+        * is one, and that the code put a TPKT header in one
+        * segment and the rest of the PDU in another.
+        */
+       if (tvb_length(tvb) == 4) {
+               /*
+                * It is - call the "dissect TPKT over a TCP stream"
+                * routine.
+                */
+               dissect_tpkt_encap(tvb, pinfo, tree, q931_desegment,
+                   q931_tpkt_pdu_handle);
+               return TRUE;
+       }
 
-#ifdef H323
-gboolean
-dissect_q931_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
-{
        /*
-        * XXX - should this be heuristic, or should we just make
-        * port 1720 do Q.931-inside-TPKT; that port appears to be
-        * intended for H.323 calls, according to
+        * Well, we have more data than just the TPKT header;
+        * check whether it looks like the beginning of a
+        * Q.931 message.
         *
-        *      http://www.isi.edu/in-notes/iana/assignments/port-numbers
+        * The minimum length of a Q.931 message is 3, as per the
+        * above.
         *
-        * which says it's for "h323hostcall"?
+        * Check that we have that many bytes past the TPKT header in
+        * the tvbuff; we already know that the TPKT header says we
+        * have that many bytes (as we passed 3 as the "min_len" argument
+        * to "is_tpkt()").
+        */
+       if (!tvb_bytes_exist(tvb, 4, 3))
+               return FALSE;
+
+       /* Check the protocol discriminator */
+       if (tvb_get_guint8(tvb, 4) != NLPID_Q_931) {
+               /* Doesn't look like Q.931 inside TPKT */
+               return FALSE;
+       }
+
+       /*
+        * OK, it looks like Q.931-over-TPKT.
+        * Call the "dissect TPKT over a TCP stream" routine.
         */
-       pinfo->current_proto = "Q.931 HEUR";
-       return q931_dissector(tvb, pinfo, tree, TRUE);
+       dissect_tpkt_encap(tvb, pinfo, tree, q931_desegment,
+           q931_tpkt_pdu_handle);
+
+       return TRUE;
 }
-#endif
 
-void
+static void
+dissect_q931_tpkt_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
+       dissect_q931_pdu(tvb, pinfo, tree, TRUE);
+}
+
+static void
 dissect_q931(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
-       pinfo->current_proto = "Q.931";
-       q931_dissector(tvb, pinfo, tree, FALSE);
+       dissect_q931_pdu(tvb, pinfo, tree, FALSE);
+}
+
+static void
+dissect_q931_ie_cs0(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
+       dissect_q931_IEs(tvb, pinfo, NULL, tree, FALSE, 0, 0);
+}
+
+static void
+dissect_q931_ie_cs7(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
+       dissect_q931_IEs(tvb, pinfo, NULL, tree, FALSE, 0, 7);
+}
+
+static void 
+q931_init(void) {
+       /* Initialize the fragment and reassembly tables */
+       fragment_table_init(&q931_fragment_table);
+       reassembled_table_init(&q931_reassembled_table);
 }
 
 void
@@ -2613,37 +2989,178 @@ proto_register_q931(void)
 {
        static hf_register_info hf[] = {
                { &hf_q931_discriminator,
-                 { "Protocol discriminator", "q931.disc", FT_UINT8, BASE_HEX, NULL, 0x0, 
-                       "" }},
+                 { "Protocol discriminator", "q931.disc", FT_UINT8, BASE_HEX, NULL, 0x0,
+                       "", HFILL }},
 
                { &hf_q931_call_ref_len,
                  { "Call reference value length", "q931.call_ref_len", FT_UINT8, BASE_DEC, NULL, 0x0,
-                       "" }},
+                       "", HFILL }},
+
+               { &hf_q931_call_ref_flag,
+                 { "Call reference flag", "q931.call_ref_flag", FT_BOOLEAN, BASE_NONE, TFS(&tfs_call_ref_flag), 0x0,
+                       "", HFILL }},
 
                { &hf_q931_call_ref,
                  { "Call reference value", "q931.call_ref", FT_BYTES, BASE_HEX, NULL, 0x0,
-                       "" }},
+                       "", HFILL }},
 
                { &hf_q931_message_type,
                  { "Message type", "q931.message_type", FT_UINT8, BASE_HEX, VALS(q931_message_type_vals), 0x0,
-                       "" }},
-
-           };
+                       "", HFILL }},
+
+               { &hf_q931_segment_type,
+                 { "Segmented message type", "q931.segment_type", FT_UINT8, BASE_HEX, VALS(q931_message_type_vals), 0x0,
+                       "", HFILL }},
+
+               { &hf_q931_coding_standard,
+                 { "Coding standard", "q931.coding_standard", FT_UINT8, BASE_HEX,
+                        VALS(q931_coding_standard_vals), 0x60,"", HFILL }},
+
+               { &hf_q931_information_transfer_capability,
+                 { "Information transfer capability", "q931.information_transfer_capability", FT_UINT8, BASE_HEX,
+                        VALS(q931_information_transfer_capability_vals), 0x1f,"", HFILL }},
+
+               { &hf_q931_transfer_mode,
+                 { "Transfer mode", "q931.transfer_mode", FT_UINT8, BASE_HEX,
+                        VALS(q931_transfer_mode_vals), 0x60,"", HFILL }},
+
+               { &hf_q931_information_transfer_rate,
+                 { "Information transfer rate", "q931.information_transfer_rate", FT_UINT8, BASE_HEX,
+                        VALS(q931_information_transfer_rate_vals), 0x1f,"", HFILL }},
+
+               { &hf_q931_uil1,
+                 { "User information layer 1 protocol", "q931.uil1", FT_UINT8, BASE_HEX,
+                        VALS(q931_uil1_vals), 0x1f,"", HFILL }},
+
+               { &hf_q931_cause_location,
+                 { "Cause location", "q931.cause_location", FT_UINT8, BASE_DEC, VALS(q931_cause_location_vals), 0x0f,
+                       "", HFILL }},
+
+               { &hf_q931_cause_value,
+                 { "Cause value", "q931.cause_value", FT_UINT8, BASE_DEC, VALS(q931_cause_code_vals), 0x7f,
+                       "", HFILL }},
+
+               { &hf_q931_number_type,
+                 { "Number type", "q931.number_type", FT_UINT8, BASE_HEX, VALS(q931_number_type_vals), 0x70,
+                       "", HFILL }},
+
+               { &hf_q931_numbering_plan,
+                 { "Numbering plan", "q931.numbering_plan", FT_UINT8, BASE_HEX, VALS(q931_numbering_plan_vals), 0x0f,
+                       "", HFILL }},
+
+               { &hf_q931_screening_ind,
+                 { "Screening indicator", "q931.screening_ind", FT_UINT8, BASE_HEX, VALS(q931_screening_indicator_vals), 0x03,
+                       "", HFILL }},
+
+               { &hf_q931_presentation_ind,
+                 { "Presentation indicator", "q931.presentation_ind", FT_UINT8, BASE_HEX, VALS(q931_presentation_indicator_vals), 0x60,
+                       "", HFILL }},
+
+               { &hf_q931_extension_ind,
+                 { "Extension indicator",  "q931.extension_ind",
+                       FT_BOOLEAN, 8, TFS(&q931_extension_ind_value), 0x80,
+                       "", HFILL }},
+
+               { &hf_q931_calling_party_number,
+                 { "Calling party number digits", "q931.calling_party_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
+                       "", HFILL }},
+
+               { &hf_q931_called_party_number,
+                 { "Called party number digits", "q931.called_party_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
+                       "", HFILL }},
+
+               { &hf_q931_connected_number,
+                 { "Connected party number digits", "q931.connected_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
+                       "", HFILL }},
+
+               { &hf_q931_redirecting_number,
+                 { "Redirecting party number digits", "q931.redirecting_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
+                       "", HFILL }},
+    /* desegmentation fields */
+               { &hf_q931_segment_overlap,
+                 { "Segment overlap", "q931.segment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
+                       "Fragment overlaps with other fragments", HFILL }},
+
+               { &hf_q931_segment_overlap_conflict,
+                 { "Conflicting data in fragment overlap", "q931.segment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
+                       "Overlapping fragments contained conflicting data", HFILL }},
+
+               { &hf_q931_segment_multiple_tails,
+                 { "Multiple tail fragments found", "q931.segment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
+                       "Several tails were found when defragmenting the packet", HFILL }},
+
+               { &hf_q931_segment_too_long_segment,
+                 { "Segment too long", "q931.segment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
+                       "Segment contained data past end of packet", HFILL }},
+
+               { &hf_q931_segment_error,
+                 { "Defragmentation error", "q931.segment.error", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
+                       "Defragmentation error due to illegal fragments", HFILL }},
+
+               { &hf_q931_segment,
+                 { "Q.931 Segment", "q931.segment", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
+                       "Q.931 Segment", HFILL }},
+
+               { &hf_q931_segments,
+                 { "Q.931 Segments", "q931.segments", FT_NONE, BASE_NONE, NULL, 0x0,
+                       "Q.931 Segments", HFILL }},
+
+               { &hf_q931_reassembled_in,
+                 { "Reassembled Q.931 in frame", "q931.reassembled_in", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
+                       "This Q.931 message is reassembled in this frame", HFILL}}, 
+       };
        static gint *ett[] = {
                &ett_q931,
                &ett_q931_ie,
+               &ett_q931_segments,
+               &ett_q931_segment,
        };
+       module_t *q931_module;
 
-       proto_q931 = proto_register_protocol ("Q.931", "q931");
+       proto_q931 = proto_register_protocol("Q.931", "Q.931", "q931");
        proto_register_field_array (proto_q931, hf, array_length(hf));
        proto_register_subtree_array(ett, array_length(ett));
-
+       register_init_routine(q931_init);
+
+       register_dissector("q931", dissect_q931, proto_q931);
+       q931_tpkt_pdu_handle = create_dissector_handle(dissect_q931_tpkt_pdu,
+           proto_q931);
+       register_dissector("q931.ie", dissect_q931_ie_cs0, proto_q931);
+       register_dissector("q931.ie.cs7", dissect_q931_ie_cs7, proto_q931);
+
+       /* subdissector code */ 
+       codeset_dissector_table = register_dissector_table("q931.codeset", "Q.931 Codeset", FT_UINT8, BASE_HEX);
+       ie_dissector_table = register_dissector_table("q931.ie", "Q.931 IE", FT_UINT16, BASE_HEX);
+
+       q931_module = prefs_register_protocol(proto_q931, NULL);
+       prefs_register_bool_preference(q931_module, "desegment_h323_messages",
+           "Desegment all Q.931 messages spanning multiple TCP segments",
+           "Whether the Q.931 dissector should desegment all messages spanning multiple TCP segments",
+           &q931_desegment);
+       prefs_register_bool_preference(q931_module, "reassembly",
+           "Reassemble segmented Q.931 messages",
+           "Reassemble segmented Q.931 messages (Q.931 - Annex H)",
+           &q931_reassembly);
 }
 
 void
 proto_reg_handoff_q931(void)
 {
-#ifdef H323
-       heur_dissector_add("tcp", dissect_q931_heur);
-#endif
+       dissector_handle_t q931_handle;
+
+       q931_handle = find_dissector("q931");
+       dissector_add("lapd.sapi", LAPD_SAPI_Q931, q931_handle);
+
+       /*
+        * Attempt to get a handle for the H.225 dissector.
+        * If we can't, the handle we get is null, and we'll just
+        * dissect putatively-H.255 Call Signaling stuff as User
+        * Information.
+        */
+       h225_handle = find_dissector("h225");
+
+       /*
+        * For H.323.
+        */
+       heur_dissector_add("tcp", dissect_q931_tpkt, proto_q931);
 }