Make the "Save only marked frames" button in the "Save As..." dialog box
[obnox/wireshark/wip.git] / packet-q931.c
index f8229b65f8a269e26dd3dfcd69641a78d1977052..b126931b10a597f163078f3020e6e190eb92fa62 100644 (file)
@@ -2,12 +2,13 @@
  * Routines for Q.931 frame disassembly
  * Guy Harris <guy@alum.mit.edu>
  *
- * $Id: packet-q931.c,v 1.9 1999/11/25 10:01:16 guy Exp $
+ * $Id: packet-q931.c,v 1.32 2001/09/14 07:10:05 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
 #include <glib.h>
 #include <string.h>
 #include "packet.h"
+#include "strutil.h"
+#include "nlpid.h"
 #include "packet-q931.h"
 
+#include "packet-tpkt.h"
+#ifdef H323
+#include "packet-h225.h"
+#endif
+
 /* Q.931 references:
  *
  * http://www.acacia-net.com/Clarinet/Protocol/q9313svn.htm
@@ -62,13 +70,14 @@ static gint ett_q931_ie = -1;
 /*
  * Q.931 message types.
  */
+#define        Q931_ESCAPE             0x00
 #define        Q931_ALERTING           0x01
 #define        Q931_CALL_PROCEEDING    0x02
 #define        Q931_CONNECT            0x07
 #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
@@ -97,6 +106,7 @@ static gint ett_q931_ie = -1;
 #define        Q931_STATUS_ENQUIRY     0x75
 
 static const value_string q931_message_type_vals[] = {
+  { Q931_ESCAPE,               "ESCAPE" },
        { Q931_ALERTING,                "ALERTING" },
        { Q931_CALL_PROCEEDING,         "CALL PROCEEDING" },
        { Q931_CONNECT,                 "CONNECT" },
@@ -140,7 +150,7 @@ static const value_string q931_message_type_vals[] = {
 /*
  * Single-octet IEs.
  */
-#define        Q931_IE_SO_IDENTIFIER_MASK      0x70    /* IE identifier mask */
+#define        Q931_IE_SO_IDENTIFIER_MASK      0xf0    /* IE identifier mask */
 #define        Q931_IE_SO_IDENTIFIER_SHIFT     4       /* IE identifier shift */
 #define        Q931_IE_SO_IE_MASK              0x0F    /* IE mask */
 
@@ -309,25 +319,25 @@ static const value_string q931_repeat_indication_vals[] = {
  * Dissect a Segmented message information element.
  */
 static void
-dissect_q931_segmented_message_ie(const u_char *pd, int offset, int len,
+dissect_q931_segmented_message_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len != 2) {
-               proto_tree_add_text(tree, offset, len,
+               proto_tree_add_text(tree, tvb, offset, len,
                    "Segmented message: length is %d, should be 2\n", len);
                return;
        }
-       if (pd[offset] & 0x80) {
-               proto_tree_add_text(tree, offset, 1,
+       if (tvb_get_guint8(tvb, offset) & 0x80) {
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "First segment: %u segments remaining",
-                   pd[offset] & 0x7F);
+                   tvb_get_guint8(tvb, offset) & 0x7F);
        } else {
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Not first segment: %u segments remaining",
-                   pd[offset] & 0x7F);
+                   tvb_get_guint8(tvb, offset) & 0x7F);
        }
-       proto_tree_add_text(tree, offset + 1, 1,
-           "Segmented message type: %u\n", pd[offset + 1]);
+       proto_tree_add_text(tree, tvb, offset + 1, 1,
+           "Segmented message type: %u\n", tvb_get_guint8(tvb, offset + 1));
 }
 
 /*
@@ -505,14 +515,40 @@ static const value_string q931_uil3_vals[] = {
        { 0,                    NULL }
 };
 
-static const value_string q931_uil3_tr_9577_vals[] = {
-       { 0xCC, "IP" },
-       { 0xCF, "PPP" },
-       { 0x00, NULL }
-};
+static void 
+dissect_q931_protocol_discriminator(tvbuff_t *tvb, int offset, proto_tree *tree)
+{
+       unsigned int discriminator = tvb_get_guint8(tvb, offset);
+
+       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: 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 {
+               proto_tree_add_uint_format(tree, hf_q931_discriminator,
+                   tvb, offset, 1, discriminator,
+                   "Protocol discriminator: Reserved (0x%02X)",
+                   discriminator);
+       }
+}
 
 void
-dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
+dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
@@ -525,9 +561,9 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, offset, 1,
+       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) {
@@ -535,11 +571,12 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
                 * We don't know how the bearer capability is encoded,
                 * so just dump it as data and be done with it.
                 */
-               proto_tree_add_text(tree, offset,
-                   len, "Data: %s", bytes_to_str(&pd[offset], len));
+               proto_tree_add_text(tree, tvb, offset,
+                   len, "Data: %s",
+                   tvb_bytes_to_str(tvb, offset, len));
                return;
        }
-       proto_tree_add_text(tree, offset, 1,
+       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)"));
@@ -552,8 +589,8 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
        if (!(octet & Q931_IE_VL_EXTENSION)) {
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Out-band negotiation %spossible",
                    (octet & 0x40) ? "" : "not ");
                offset += 1;
@@ -562,13 +599,13 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       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)"));
        it_rate = octet & 0x1F;
-       proto_tree_add_text(tree, offset, 1,
+       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)"));
@@ -578,19 +615,19 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
        if (it_rate == Q931_IT_RATE_MULTIRATE) {
                if (len == 0)
                        return;
-               proto_tree_add_text(tree, offset, 1, "Rate multiplier: %u", pd[offset]);
+               proto_tree_add_text(tree, tvb, offset, 1, "Rate multiplier: %u", tvb_get_guint8(tvb, offset));
                offset += 1;
                len -= 1;
        }
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        if ((octet & 0x60) == 0x20) {
                /*
                 * Layer 1 information.
                 */
-               proto_tree_add_text(tree, offset, 1,
+               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)"));
@@ -601,14 +638,14 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
                        goto l1_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Layer 1 is %s",
                    (octet & 0x40) ? "Asynchronous" : "Synchronous");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Layer 1 in-band negotiation is %spossible",
                    (octet & 0x20) ? "" : "not ");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "User rate: %s",
                    val_to_str(octet & 0x1F, q931_l1_user_rate_vals,
                      "Unknown (0x%02X)"));
@@ -619,21 +656,21 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
                        goto l1_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Intermediate rate: %s",
                      val_to_str(octet & 0x60, q931_l1_intermediate_rate_vals,
                       "Unknown (0x%X)"));
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "%s to send data with network independent clock",
                    (octet & 0x10) ? "Required" : "Not required");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "%s accept data with network independent clock",
                    (octet & 0x08) ? "Can" : "Cannot");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "%s to send data with flow control mechanism",
                    (octet & 0x04) ? "Required" : "Not required");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "%s accept data with flow control mechanism",
                    (octet & 0x02) ? "Can" : "Cannot");
                offset += 1;
@@ -643,23 +680,23 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
                        goto l1_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Rate adaption header %sincluded",
                    (octet & 0x40) ? "" : "not ");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Multiple frame establishment %ssupported",
                    (octet & 0x20) ? "" : "not ");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "%s mode of operation",
                    (octet & 0x10) ? "Protocol sensitive" : "Bit transparent");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    (octet & 0x08) ?
                      "Full protocol negotiation" : "LLI = 256 only");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Message originator is %s",
                    (octet & 0x04) ? "Assignor only" : "Default assignee");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Negotiation is done %s",
                    (octet & 0x02) ? "in-band" : "out-of-band");
                offset += 1;
@@ -669,16 +706,16 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
                        goto l1_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Stop bits: %s",
                      val_to_str(octet & 0x60, q931_l1_stop_bits_vals,
                       "Unknown (0x%X)"));
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Data bits: %s",
                      val_to_str(octet & 0x18, q931_l1_data_bits_vals,
                       "Unknown (0x%X)"));
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Parity: %s",
                      val_to_str(octet & 0x08, q931_l1_parity_vals,
                       "Unknown (0x%X)"));
@@ -687,23 +724,23 @@ dissect_q931_bearer_capability_ie(const u_char *pd, int offset, int len,
                        goto l1_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "%s duplex",
                    (octet & 0x40) ? "Full" : "Half");
                modem_type = octet & 0x3F;
                if (modem_type <= 0x5 ||
                    (modem_type >= 0x20 && modem_type <= 0x2F)) {
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Modem type: National use 0x%02X", modem_type);
                } else if (modem_type >= 0x30) {
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Modem type: User specified 0x%02X", modem_type);
                } else {
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Modem type: %s",
                              val_to_str(modem_type, q931_l1_modem_type_vals,
-                             NULL));
+                             "Unknown (0x%02X)"));
                }
                offset += 1;
                len -= 1;
@@ -713,13 +750,13 @@ l1_done:
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        if ((octet & 0x60) == 0x40) {
                /*
                 * Layer 2 information.
                 */
                uil2_protocol = octet & 0x1F;
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "User information layer 2 protocol: %s",
                    val_to_str(uil2_protocol, q931_uil2_vals,
                      "Unknown (0x%02X)"));
@@ -733,13 +770,13 @@ l1_done:
                        goto l2_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
+               octet = tvb_get_guint8(tvb, offset);
                if (uil2_protocol == Q931_UIL2_USER_SPEC) {
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "User-specified layer 2 protocol information: 0x%02X",
                            octet & 0x7F);
                } else {
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Mode: %s",
                            val_to_str(octet & 0x60, q931_mode_vals,
                              "Unknown (0x%02X)"));
@@ -751,8 +788,8 @@ l1_done:
                        goto l2_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Window size: %u k", octet & 0x7F);
                offset += 1;
                len -= 1;
@@ -762,13 +799,13 @@ l2_done:
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        if ((octet & 0x60) == 0x60) {
                /*
                 * Layer 3 information.
                 */
                uil3_protocol = octet & 0x1F;
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "User information layer 3 protocol: %s",
                    val_to_str(uil3_protocol, q931_uil3_vals,
                      "Unknown (0x%02X)"));
@@ -783,23 +820,25 @@ l2_done:
                        goto l3_done;
                if (len == 0)
                        return;
-               octet = pd[offset];
+               octet = tvb_get_guint8(tvb, offset);
                switch (uil3_protocol) {
 
                case Q931_UIL3_X25_PL:
                case Q931_UIL3_ISO_8208:
                case Q931_UIL3_X223:
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Mode: %s",
                            val_to_str(octet & 0x60, q931_mode_vals,
                              "Unknown (0x%02X)"));
+                       offset += 1;
+                       len -= 1;
 
                        if (octet & Q931_IE_VL_EXTENSION)
                                goto l3_done;
                        if (len == 0)
                                return;
-                       octet = pd[offset];
-                       proto_tree_add_text(tree, offset, 1,
+                       octet = tvb_get_guint8(tvb, offset);
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Default packet size: %u", octet & 0x0F);
                        offset += 1;
                        len -= 1;
@@ -808,15 +847,15 @@ l2_done:
                                goto l3_done;
                        if (len == 0)
                                return;
-                       octet = pd[offset];
-                       proto_tree_add_text(tree, offset, 1,
+                       octet = tvb_get_guint8(tvb, offset);
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Packet window size: %u", octet & 0x7F);
                        offset += 1;
                        len -= 1;
                        break;
 
                case Q931_UIL3_USER_SPEC:
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Default packet size: %u octets",
                            1 << (octet & 0x0F));
                        offset += 1;
@@ -829,11 +868,11 @@ l2_done:
                                goto l3_done;
                        if (len == 0)
                                return;
-                       octet = pd[offset + 1];
+                       octet = tvb_get_guint8(tvb, offset + 1);
                        add_l3_info |= (octet & 0x0F);
-                       proto_tree_add_text(tree, offset, 2,
+                       proto_tree_add_text(tree, tvb, offset, 2,
                            "Additional layer 3 protocol information: %s",
-                           val_to_str(add_l3_info, q931_uil3_tr_9577_vals,
+                           val_to_str(add_l3_info, nlpid_vals,
                              "Unknown (0x%02X)"));
                        offset += 2;
                        len -= 2;
@@ -969,7 +1008,7 @@ static const value_string q931_cause_code_vals[] = {
 };
 
 static void
-dissect_q931_cause_ie(const u_char *pd, int offset, int len,
+dissect_q931_cause_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
@@ -977,9 +1016,9 @@ dissect_q931_cause_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, offset, 1,
+       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) {
@@ -987,11 +1026,12 @@ dissect_q931_cause_ie(const u_char *pd, int offset, int len,
                 * We don't know how the cause is encoded,
                 * so just dump it as data and be done with it.
                 */
-               proto_tree_add_text(tree, offset,
-                   len, "Data: %s", bytes_to_str(&pd[offset], len));
+               proto_tree_add_text(tree, tvb, offset,
+                   len, "Data: %s",
+                   tvb_bytes_to_str(tvb, offset, len));
                return;
        }
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Location: %s",
            val_to_str(octet & 0x0F, q931_cause_location_vals,
              "Unknown (0x%X)"));
@@ -1001,30 +1041,30 @@ dissect_q931_cause_ie(const u_char *pd, int offset, int len,
        if (!(octet & Q931_IE_VL_EXTENSION)) {
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Recommendation: %s",
                    val_to_str(octet & 0x7F, q931_cause_recommendation_vals,
-                     "Unknown (0x%X)"));
+                     "Unknown (0x%02X)"));
                offset += 1;
                len -= 1;
        }
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       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%X)"));
+             "Unknown (0x%02X)"));
        offset += 1;
        len -= 1;
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, len,
            "Diagnostics: %s",
-           bytes_to_str(&pd[offset], len));
+           tvb_bytes_to_str(tvb, offset, len));
 }
 
 /*
@@ -1046,14 +1086,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" },
@@ -1061,7 +1102,7 @@ static const value_string q931_call_state_vals[] = {
 };
 
 static void
-dissect_q931_call_state_ie(const u_char *pd, int offset, int len,
+dissect_q931_call_state_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
@@ -1069,9 +1110,9 @@ dissect_q931_call_state_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Coding standard: %s",
            val_to_str(coding_standard, q931_coding_standard_vals, NULL));
        if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
@@ -1079,11 +1120,12 @@ dissect_q931_call_state_ie(const u_char *pd, int offset, int len,
                 * We don't know how the call state is encoded,
                 * so just dump it as data and be done with it.
                 */
-               proto_tree_add_text(tree, offset,
-                   len, "Data: %s", bytes_to_str(&pd[offset], len));
+               proto_tree_add_text(tree, tvb, offset,
+                   len, "Data: %s",
+                   tvb_bytes_to_str(tvb, offset, len));
                return;
        }
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Call state: %s",
            val_to_str(octet & 0x3F, q931_call_state_vals,
              "Unknown (0x%02X)"));
@@ -1121,7 +1163,7 @@ static const value_string q931_element_type_vals[] = {
 };
 
 static void
-dissect_q931_channel_identification_ie(const u_char *pd, int offset, int len,
+dissect_q931_channel_identification_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
@@ -1131,26 +1173,26 @@ dissect_q931_channel_identification_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       octet = tvb_get_guint8(tvb, offset);
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Interface %s identified",
            (octet & Q931_INTERFACE_IDENTIFIED) ? "explicitly" : "implicitly");
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "%s interface",
            (octet & Q931_NOT_BASIC_CHANNEL) ? "Not basic" : "Basic");
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Indicated channel is %s",
            (octet & 0x08) ? "required" : "preferred");
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Indicated channel is %sthe D-channel",
            (octet & 0x04) ? "" : "not ");
        if (octet & Q931_NOT_BASIC_CHANNEL) {
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Channel selection: %s",
                    val_to_str(octet & 0x03, q931_not_basic_channel_selection_vals,
-                     NULL));
+                     "Unknown (0x%X)"));
        } else {
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Channel selection: %s",
                    val_to_str(octet & 0x03, q931_basic_channel_selection_vals,
                      NULL));
@@ -1164,7 +1206,7 @@ dissect_q931_channel_identification_ie(const u_char *pd, int offset, int len,
                do {
                        if (len == 0)
                                break;
-                       octet = pd[offset];
+                       octet = tvb_get_guint8(tvb, offset);
                        offset += 1;
                        len -= 1;
                        identifier_len++;
@@ -1175,9 +1217,10 @@ dissect_q931_channel_identification_ie(const u_char *pd, int offset, int len,
                 * last octet of the interface identifier?
                 */
                if (identifier_len != 0) {
-                       proto_tree_add_text(tree, identifier_offset,
+                       proto_tree_add_text(tree, tvb, identifier_offset,
                            identifier_len, "Interface identifier: %s",
-                           bytes_to_str(&pd[identifier_offset],
+                           bytes_to_str(
+                             tvb_get_ptr(tvb, identifier_offset, identifier_len),
                              identifier_len));
                }
        }
@@ -1185,9 +1228,9 @@ dissect_q931_channel_identification_ie(const u_char *pd, int offset, int len,
        if (octet & Q931_NOT_BASIC_CHANNEL) {
                if (len == 0)
                        return;
-               octet = pd[offset];
+               octet = tvb_get_guint8(tvb, offset);
                coding_standard = octet & 0x60;
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Coding standard: %s",
                    val_to_str(coding_standard, q931_coding_standard_vals,
                      NULL));
@@ -1197,18 +1240,19 @@ dissect_q931_channel_identification_ie(const u_char *pd, int offset, int len,
                         * encoded, so just dump it as data and be done
                         * with it.
                         */
-                       proto_tree_add_text(tree, offset,
-                           len, "Data: %s", bytes_to_str(&pd[offset], len));
+                       proto_tree_add_text(tree, tvb, offset,
+                           len, "Data: %s",
+                           tvb_bytes_to_str(tvb, offset, len));
                        return;
                }
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Channel is indicated by %s",
                    (octet & Q931_IS_SLOT_MAP) ? "slot map" : "number");
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "%s type: %s",
                    (octet & Q931_IS_SLOT_MAP) ? "Map element" : "Channel",
                    val_to_str(octet & 0x0F, q931_element_type_vals,
-                   "Unknown (0x%02X)"));
+                       "Unknown (0x%02X)"));
 
                /*
                 * XXX - dump the channel number or slot map.
@@ -1230,7 +1274,7 @@ static const value_string q931_progress_description_vals[] = {
 };
 
 void
-dissect_q931_progress_indicator_ie(const u_char *pd, int offset, int len,
+dissect_q931_progress_indicator_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
@@ -1238,9 +1282,9 @@ dissect_q931_progress_indicator_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, offset, 1,
+       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) {
@@ -1248,11 +1292,12 @@ dissect_q931_progress_indicator_ie(const u_char *pd, int offset, int len,
                 * We don't know how the progress indicator is encoded,
                 * so just dump it as data and be done with it.
                 */
-               proto_tree_add_text(tree, offset,
-                   len, "Data: %s", bytes_to_str(&pd[offset], len));
+               proto_tree_add_text(tree, tvb, offset,
+                   len, "Data: %s",
+                   tvb_bytes_to_str(tvb, offset, len));
                return;
        }
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Location: %s",
            val_to_str(octet & 0x0F, q931_cause_location_vals,
              "Unknown (0x%X)"));
@@ -1261,8 +1306,8 @@ dissect_q931_progress_indicator_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       octet = tvb_get_guint8(tvb, offset);
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Progress description: %s",
            val_to_str(octet & 0x7F, q931_progress_description_vals,
              "Unknown (0x%02X)"));
@@ -1287,7 +1332,7 @@ static const value_string q931_netid_plan_vals[] = {
 };
 
 static void
-dissect_q931_ns_facilities_ie(const u_char *pd, int offset, int len,
+dissect_q931_ns_facilities_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
@@ -1295,9 +1340,9 @@ dissect_q931_ns_facilities_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        netid_len = octet & 0x7F;
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Network identification length: %u",
            netid_len);
        offset += 1;
@@ -1305,12 +1350,12 @@ dissect_q931_ns_facilities_ie(const u_char *pd, int offset, int len,
        if (netid_len != 0) {
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Type of network identification: %s",
                    val_to_str(octet & 0x70, q931_netid_type_vals,
                      "Unknown (0x%02X)"));
-               proto_tree_add_text(tree, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Network identification plan: %s",
                    val_to_str(octet & 0x0F, q931_netid_plan_vals,
                      "Unknown (0x%02X)"));
@@ -1323,9 +1368,9 @@ dissect_q931_ns_facilities_ie(const u_char *pd, int offset, int len,
                if (netid_len > len)
                        netid_len = len;
                if (netid_len != 0) {
-                       proto_tree_add_text(tree, offset, netid_len,
-                           "Network identification: %.*s",
-                           netid_len, &pd[offset]);
+                       proto_tree_add_text(tree, tvb, offset, netid_len,
+                           "Network identification: %s",
+                           tvb_format_text(tvb, offset, netid_len));
                        offset += netid_len;
                        len -= netid_len;
                }
@@ -1337,9 +1382,9 @@ dissect_q931_ns_facilities_ie(const u_char *pd, int offset, int len,
         */
         if (len == 0)
                return;
-       proto_tree_add_text(tree, offset,
+       proto_tree_add_text(tree, tvb, offset,
            len, "Network-specific facility specification: %s",
-           bytes_to_str(&pd[offset], len));
+           tvb_bytes_to_str(tvb, offset, len));
 }
 
 /*
@@ -1353,15 +1398,15 @@ static const value_string q931_notification_description_vals[] = {
 };
 
 static void
-dissect_q931_notification_indicator_ie(const u_char *pd, int offset, int len,
+dissect_q931_notification_indicator_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       octet = tvb_get_guint8(tvb, offset);
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Notification description: %s",
            val_to_str(octet & 0x7F, q931_notification_description_vals,
              "Unknown (0x%02X)"));
@@ -1371,11 +1416,11 @@ dissect_q931_notification_indicator_ie(const u_char *pd, int offset, int len,
  * Dissect a Date/time information element.
  */
 static void
-dissect_q931_date_time_ie(const u_char *pd, int offset, int len,
+dissect_q931_date_time_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len != 6) {
-               proto_tree_add_text(tree, offset, len,
+               proto_tree_add_text(tree, tvb, offset, len,
                    "Date/time: length is %d, should be 6\n", len);
                return;
        }
@@ -1383,10 +1428,10 @@ dissect_q931_date_time_ie(const u_char *pd, int offset, int len,
         * XXX - what is "year" relative to?  Is "month" 0-origin or
         * 1-origin?  Q.931 doesn't say....
         */
-       proto_tree_add_text(tree, offset, 6,
-           "Date/time: %u-%u-%u %u:%u:%u",
-           pd[offset + 0], pd[offset + 1], pd[offset + 2],
-           pd[offset + 3], pd[offset + 4], pd[offset + 5]);
+       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));
 }
 
 /*
@@ -1401,7 +1446,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" },
@@ -1417,17 +1462,18 @@ static const value_string q931_signal_vals[] = {
 };
 
 static void
-dissect_q931_signal_ie(const u_char *pd, int offset, int len,
+dissect_q931_signal_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len != 1) {
-               proto_tree_add_text(tree, offset, len,
+               proto_tree_add_text(tree, tvb, offset, len,
                    "Signal: length is %d, should be 1\n", len);
                return;
        }
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Signal: %s",
-           val_to_str(pd[offset], q931_signal_vals, "Unknown (0x%02X)"));
+           val_to_str(tvb_get_guint8(tvb, offset), q931_signal_vals,
+               "Unknown (0x%02X)"));
 }
 
 /*
@@ -1449,34 +1495,34 @@ static const value_string q931_throughput_class_vals[] = {
 };
 
 static void
-dissect_q931_information_rate_ie(const u_char *pd, int offset, int len,
+dissect_q931_information_rate_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len != 4) {
-               proto_tree_add_text(tree, offset, len,
+               proto_tree_add_text(tree, tvb, offset, len,
                    "Information rate: length is %d, should be 4\n", len);
                return;
        }
-       proto_tree_add_text(tree, offset + 0, 1,
+       proto_tree_add_text(tree, tvb, offset + 0, 1,
            "Incoming information rate: %s",
-           val_to_str(pd[offset + 0] & 0x1F, q931_throughput_class_vals,
-             "Unknown (0x%02X)"));
-       proto_tree_add_text(tree, offset + 1, 1,
+           val_to_str(tvb_get_guint8(tvb, offset + 0) & 0x1F,
+             q931_throughput_class_vals, "Unknown (0x%02X)"));
+       proto_tree_add_text(tree, tvb, offset + 1, 1,
            "Outgoing information rate: %s",
-           val_to_str(pd[offset + 1] & 0x1F, q931_throughput_class_vals,
-             "Unknown (0x%02X)"));
-       proto_tree_add_text(tree, offset + 2, 1,
+           val_to_str(tvb_get_guint8(tvb, offset + 1) & 0x1F,
+             q931_throughput_class_vals, "Unknown (0x%02X)"));
+       proto_tree_add_text(tree, tvb, offset + 2, 1,
            "Minimum incoming information rate: %s",
-           val_to_str(pd[offset + 2] & 0x1F, q931_throughput_class_vals,
-             "Unknown (0x%02X)"));
-       proto_tree_add_text(tree, offset + 3, 1,
+           val_to_str(tvb_get_guint8(tvb, offset + 2) & 0x1F,
+             q931_throughput_class_vals, "Unknown (0x%02X)"));
+       proto_tree_add_text(tree, tvb, offset + 3, 1,
            "Minimum outgoing information rate: %s",
-           val_to_str(pd[offset + 3] & 0x1F, q931_throughput_class_vals,
-             "Unknown (0x%02X)"));
+           val_to_str(tvb_get_guint8(tvb, offset + 3) & 0x1F,
+             q931_throughput_class_vals, "Unknown (0x%02X)"));
 }
 
 static int
-dissect_q931_guint16_value(const u_char *pd, int offset, int len,
+dissect_q931_guint16_value(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree, char *label)
 {
        guint8 octet;
@@ -1485,7 +1531,7 @@ dissect_q931_guint16_value(const u_char *pd, int offset, int len,
 
        value_len = 0;
 
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        if (octet & Q931_IE_VL_EXTENSION) {
                /*
                 * Only one octet long - error.
@@ -1503,7 +1549,7 @@ dissect_q931_guint16_value(const u_char *pd, int offset, int len,
                 */
                goto past_end;
        }
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        if (octet & Q931_IE_VL_EXTENSION) {
                /*
                 * Only two octets long - error.
@@ -1521,7 +1567,7 @@ dissect_q931_guint16_value(const u_char *pd, int offset, int len,
                 */
                goto past_end;
        }
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        if (!(octet & Q931_IE_VL_EXTENSION)) {
                /*
                 * More than three octets long - error.
@@ -1533,17 +1579,17 @@ dissect_q931_guint16_value(const u_char *pd, int offset, int len,
        len -= 1;
        value_len++;
 
-       proto_tree_add_text(tree, offset, value_len, "%s: %u ms", label,
+       proto_tree_add_text(tree, tvb, offset, value_len, "%s: %u ms", label,
            value);
        return value_len;
 
 past_end:
-       proto_tree_add_text(tree, offset, len,
+       proto_tree_add_text(tree, tvb, offset, len,
            "%s goes past end of information element", label);
        return -1;
 
 bad_length:
-       proto_tree_add_text(tree, offset, len, "%s isn't 3 octets long",
+       proto_tree_add_text(tree, tvb, offset, len, "%s isn't 3 octets long",
            label);
        return -1;
 }
@@ -1552,14 +1598,14 @@ bad_length:
  * Dissect an End-to-end transit delay information element.
  */
 static void
-dissect_q931_e2e_transit_delay_ie(const u_char *pd, int offset, int len,
+dissect_q931_e2e_transit_delay_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        int value_len;
 
        if (len == 0)
                return;
-       value_len = dissect_q931_guint16_value(pd, offset, len, tree,
+       value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
            "Cumulative transit delay");
        if (value_len < 0)
                return; /* error */
@@ -1568,7 +1614,7 @@ dissect_q931_e2e_transit_delay_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       value_len = dissect_q931_guint16_value(pd, offset, len, tree,
+       value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
            "Requested end-to-end transit delay");
        if (value_len < 0)
                return; /* error */
@@ -1577,7 +1623,7 @@ dissect_q931_e2e_transit_delay_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       value_len = dissect_q931_guint16_value(pd, offset, len, tree,
+       value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
            "Maximum end-to-end transit delay");
 }
 
@@ -1585,12 +1631,12 @@ dissect_q931_e2e_transit_delay_ie(const u_char *pd, int offset, int len,
  * Dissect a Transit delay selection and indication information element.
  */
 static void
-dissect_q931_td_selection_and_int_ie(const u_char *pd, int offset, int len,
+dissect_q931_td_selection_and_int_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len == 0)
                return;
-       dissect_q931_guint16_value(pd, offset, len, tree,
+       dissect_q931_guint16_value(tvb, offset, len, tree,
            "Transit delay");
 }
 
@@ -1606,26 +1652,25 @@ static const value_string q931_fast_selected_vals[] = {
 };
 
 static void
-dissect_q931_pl_binary_parameters_ie(const u_char *pd, int offset, int len,
+dissect_q931_pl_binary_parameters_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       octet = tvb_get_guint8(tvb, offset);
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Fast select: %s",
-           val_to_str(octet & 0x18, q931_fast_selected_vals,
-             NULL));
-       proto_tree_add_text(tree, offset, 1,
+           val_to_str(octet & 0x18, q931_fast_selected_vals, NULL));
+       proto_tree_add_text(tree, tvb, offset, 1,
            "%s",
            (octet & 0x04) ? "No request/request denied" :
                             "Request indicated/request accepted");
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "%s confirmation",
            (octet & 0x02) ? "Link-by-link" : "End-to-end");
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Modulus %u sequencing",
            (octet & 0x01) ? 8 : 128);
 }
@@ -1634,40 +1679,40 @@ dissect_q931_pl_binary_parameters_ie(const u_char *pd, int offset, int len,
  * Dissect a Packet layer window size information element.
  */
 static void
-dissect_q931_pl_window_size_ie(const u_char *pd, int offset, int len,
+dissect_q931_pl_window_size_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, 1,
-           "Forward value: %u", pd[offset] & 0x7F);
+       proto_tree_add_text(tree, tvb, offset, 1,
+           "Forward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
        offset += 1;
        len -= 1;
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, 1,
-           "Backward value: %u", pd[offset] & 0x7F);
+       proto_tree_add_text(tree, tvb, offset, 1,
+           "Backward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
 }
 
 /*
  * Dissect a Packet size information element.
  */
 static void
-dissect_q931_packet_size_ie(const u_char *pd, int offset, int len,
+dissect_q931_packet_size_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, 1,
-           "Forward value: %u", pd[offset] & 0x7F);
+       proto_tree_add_text(tree, tvb, offset, 1,
+           "Forward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
        offset += 1;
        len -= 1;
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, 1,
-           "Backward value: %u", pd[offset] & 0x7F);
+       proto_tree_add_text(tree, tvb, offset, 1,
+           "Backward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
 }
 
 /*
@@ -1680,21 +1725,21 @@ static const value_string q931_cug_indication_vals[] = {
 };
 
 static void
-dissect_q931_cug_ie(const u_char *pd, int offset, int len, proto_tree *tree)
+dissect_q931_cug_ie(tvbuff_t *tvb, int offset, int len, proto_tree *tree)
 {
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "CUG indication: %s",
-           val_to_str(pd[offset] & 0x07, q931_cug_indication_vals,
-             "Unknown (0x%02X)"));
+           val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
+             q931_cug_indication_vals, "Unknown (0x%02X)"));
        offset += 1;
        len -= 1;
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, len, "CUG index code: %.*s", len,
-           &pd[offset]);
+       proto_tree_add_text(tree, tvb, offset, len, "CUG index code: %s",
+           tvb_format_text(tvb, offset, len));
 }
 
 /*
@@ -1706,15 +1751,15 @@ static const value_string q931_reverse_charging_indication_vals[] = {
 };
 
 static void
-dissect_q931_reverse_charge_ind_ie(const u_char *pd, int offset, int len,
+dissect_q931_reverse_charge_ind_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Reverse charging indication: %s",
-           val_to_str(pd[offset] & 0x07, q931_reverse_charging_indication_vals,
-             "Unknown (0x%02X)"));
+           val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
+             q931_reverse_charging_indication_vals, "Unknown (0x%02X)"));
 }
 
 /*
@@ -1767,19 +1812,19 @@ static const value_string q931_redirection_reason_vals[] = {
 };
 
 static void
-dissect_q931_number_ie(const u_char *pd, int offset, int len,
+dissect_q931_number_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       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, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Numbering plan: %s",
            val_to_str(octet & 0x0F, q931_numbering_plan_vals,
              "Unknown (0x%02X)"));
@@ -1789,12 +1834,12 @@ dissect_q931_number_ie(const u_char *pd, int offset, int len,
        if (!(octet & Q931_IE_VL_EXTENSION)) {
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               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, offset, 1,
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Screening indicator: %s",
                    val_to_str(octet & 0x03, q931_screening_indicator_vals,
                      "Unknown (0x%X)"));
@@ -1808,8 +1853,8 @@ dissect_q931_number_ie(const u_char *pd, int offset, int len,
        if (!(octet & Q931_IE_VL_EXTENSION)) {
                if (len == 0)
                        return;
-               octet = pd[offset];
-               proto_tree_add_text(tree, offset, 1,
+               octet = tvb_get_guint8(tvb, offset);
+               proto_tree_add_text(tree, tvb, offset, 1,
                    "Reason for redirection: %s",
                    val_to_str(octet & 0x0F, q931_redirection_reason_vals,
                      "Unknown (0x%X)"));
@@ -1819,8 +1864,8 @@ dissect_q931_number_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, len, "Number: %.*s",
-           len, &pd[offset]);
+       proto_tree_add_text(tree, tvb, offset, len, "Number: %s",
+           tvb_format_text(tvb, offset, len));
 }
 
 /*
@@ -1839,19 +1884,19 @@ static const value_string q931_odd_even_indicator_vals[] = {
 };
 
 static void
-dissect_q931_party_subaddr_ie(const u_char *pd, int offset, int len,
+dissect_q931_party_subaddr_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       octet = tvb_get_guint8(tvb, offset);
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Type of subaddress: %s",
            val_to_str(octet & 0x70, q931_subaddress_type_vals,
              "Unknown (0x%02X)"));
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Odd/even indicator: %s",
            val_to_str(octet & 0x10, q931_odd_even_indicator_vals,
              NULL));
@@ -1860,8 +1905,8 @@ dissect_q931_party_subaddr_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       proto_tree_add_text(tree, offset, len, "Subaddress: %s",
-           bytes_to_str(&pd[offset], len));
+       proto_tree_add_text(tree, tvb, offset, len, "Subaddress: %s",
+           tvb_bytes_to_str(tvb, offset, len));
 }
 
 /*
@@ -1875,18 +1920,18 @@ static const value_string q931_restart_indicator_class_vals[] = {
 };
 
 static void
-dissect_q931_restart_indicator_ie(const u_char *pd, int offset, int len,
+dissect_q931_restart_indicator_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        if (len != 1) {
-               proto_tree_add_text(tree, offset, len,
+               proto_tree_add_text(tree, tvb, offset, len,
                    "Restart indicator: length is %d, should be 1\n", len);
                return;
        }
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Restart indicator: %s",
-           val_to_str(pd[offset] & 0x07, q931_restart_indicator_class_vals,
-             "Unknown (0x%02X)"));
+           val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
+             q931_restart_indicator_class_vals, "Unknown (0x%02X)"));
 }
 
 /*
@@ -1922,7 +1967,7 @@ static const value_string q931_audiovisual_characteristics_vals[] = {
 };
 
 void
-dissect_q931_high_layer_compat_ie(const u_char *pd, int offset, int len,
+dissect_q931_high_layer_compat_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
@@ -1931,9 +1976,9 @@ dissect_q931_high_layer_compat_ie(const u_char *pd, int offset, int len,
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        coding_standard = octet & 0x60;
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Coding standard: %s",
            val_to_str(coding_standard, q931_coding_standard_vals, NULL));
        if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
@@ -1941,36 +1986,39 @@ dissect_q931_high_layer_compat_ie(const u_char *pd, int offset, int len,
                 * We don't know how the call state is encoded,
                 * so just dump it as data and be done with it.
                 */
-               proto_tree_add_text(tree, offset,
-                   len, "Data: %s", bytes_to_str(&pd[offset], len));
+               proto_tree_add_text(tree, tvb, offset,
+                   len, "Data: %s",
+                   tvb_bytes_to_str(tvb, offset, len));
                return;
        }
 
        if (len == 0)
                return;
-       octet = pd[offset];
+       octet = tvb_get_guint8(tvb, offset);
        characteristics = octet & 0x7F;
-       proto_tree_add_text(tree, offset, 1,
+       proto_tree_add_text(tree, tvb, offset, 1,
            "High layer characteristics identification: %s",
            val_to_str(characteristics, q931_high_layer_characteristics_vals,
-             NULL));
+            "Unknown (0x%02X)"));
        offset += 1;
        len -= 1;
 
        if (!(octet & Q931_IE_VL_EXTENSION)) {
                if (len == 0)
                        return;
-               octet = pd[offset];
+               octet = tvb_get_guint8(tvb, offset);
                if (characteristics == Q931_AUDIOVISUAL) {
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Extended audiovisual characteristics identification: %s",
-                           val_to_str(octet & 0x7F, q931_audiovisual_characteristics_vals,
-                             NULL));
+                           val_to_str(octet & 0x7F,
+                             q931_audiovisual_characteristics_vals,
+                             "Unknown (0x%02X)"));
                } else {
-                       proto_tree_add_text(tree, offset, 1,
+                       proto_tree_add_text(tree, tvb, offset, 1,
                            "Extended high layer characteristics identification: %s",
-                           val_to_str(octet & 0x7F, q931_high_layer_characteristics_vals,
-                             NULL));
+                           val_to_str(octet & 0x7F,
+                             q931_high_layer_characteristics_vals,
+                             "Unknown (0x%02X)"));
                }
        }
 }
@@ -1993,15 +2041,15 @@ static const value_string q931_protocol_discriminator_vals[] = {
 };
 
 static void
-dissect_q931_user_user_ie(const u_char *pd, int offset, int len,
+dissect_q931_user_user_ie(tvbuff_t *tvb, int offset, int len,
     proto_tree *tree)
 {
        guint8 octet;
 
        if (len == 0)
                return;
-       octet = pd[offset];
-       proto_tree_add_text(tree, offset, 1,
+       octet = tvb_get_guint8(tvb, offset);
+       proto_tree_add_text(tree, tvb, offset, 1,
            "Protocol discriminator: %s",
            val_to_str(octet, q931_protocol_discriminator_vals,
            "Unknown (0x%02x)"));
@@ -2013,13 +2061,13 @@ dissect_q931_user_user_ie(const u_char *pd, int offset, int len,
        switch (octet) {
 
        case Q931_PROTOCOL_DISCRIMINATOR_IA5:
-               proto_tree_add_text(tree, offset, len, "User information: %.*s",
-                   len, &pd[offset]);
+               proto_tree_add_text(tree, tvb, offset, len, "User information: %s",
+                   tvb_format_text(tvb, offset, len));
                break;
 
        default:
-               proto_tree_add_text(tree, offset, len, "User information: %s",
-                   bytes_to_str(&pd[offset], len));
+               proto_tree_add_text(tree, tvb, offset, len, "User information: %s",
+                   tvb_bytes_to_str(tvb, offset, len));
                break;
        }
 }
@@ -2028,12 +2076,12 @@ dissect_q931_user_user_ie(const u_char *pd, int offset, int len,
  * Dissect information elements consisting of ASCII^H^H^H^H^HIA5 text.
  */
 static void
-dissect_q931_ia5_ie(const u_char *pd, int offset, int len, proto_tree *tree,
+dissect_q931_ia5_ie(tvbuff_t *tvb, int offset, int len, proto_tree *tree,
     char *label)
 {
        if (len != 0) {
-               proto_tree_add_text(tree, offset, len, "%s: %.*s", label, len,
-                   &pd[offset]);
+               proto_tree_add_text(tree, tvb, offset, len, "%s: %s", label,
+                   tvb_format_text(tvb, offset, len));
        }
 }
 
@@ -2046,9 +2094,11 @@ static const value_string q931_codeset_vals[] = {
        { 0x00, NULL },
 };
 
-void
-dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
+static gboolean
+q931_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
+    gboolean started_heuristic)
 {
+       int             offset = 0;
        proto_tree      *q931_tree = NULL;
        proto_item      *ti;
        proto_tree      *ie_tree;
@@ -2056,40 +2106,152 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
        guint8          call_ref[15];
        guint8          message_type;
        guint8          info_element;
-       guint         info_element_len;
-       int             codeset;
+       guint16         info_element_len;
+       int             codeset; 
        gboolean        non_locking_shift;
+       guint8          protocol_discriminator;
+       int             lv_tpkt_len;
+
+#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 1006 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 */
+
+       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;
+       }
+
+       /* 
+        * The first byte should be < 8 (3 is TPKT, rest is Q.931)
+        */
+       if ( protocol_discriminator < NLPID_Q_931 ) {
+               /*
+                * 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 ) > 4 ) {
+                       /* 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 != NLPID_Q_931) 
+                       return FALSE;
 
-       if (check_col(fd, COL_PROTOCOL))
-               col_add_str(fd, COL_PROTOCOL, "Q.931");
+               /*
+                * Always check if it's a real TPKT message
+                */
+               if ( ! is_tpkt( tvb, &offset ) ) 
+                       return FALSE;
+
+               lv_tpkt_len = dissect_tpkt_header( tvb, &offset, pinfo, tree );
+               if (lv_tpkt_len == -1) {
+                       /*
+                        * TPKT isn't enabled.
+                        */
+                       return FALSE;
+               }
+
+               /*
+                * Check if it's an empty TPKT message (the next one might be a 
+                * real Q.931 message)
+                * Skip TPKT length!
+                */
+               if ( tvb_length_remaining( tvb, offset ) < lv_tpkt_len - 4 ) {
+                       return TRUE;
+               }
+
+               /*
+                * 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";
+       }
+
+       /*
+        * 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_set_str(pinfo->fd, COL_PROTOCOL, "Q.931");
 
        if (tree) {
-               ti = proto_tree_add_item(tree, proto_q931, offset,
-                   END_OF_FRAME, NULL);
+               ti = proto_tree_add_item(tree, proto_q931, tvb, offset,
+                   tvb_length(tvb), FALSE);
                q931_tree = proto_item_add_subtree(ti, ett_q931);
 
-               proto_tree_add_item(q931_tree, hf_q931_discriminator, offset, 1, pd[offset]);
+               dissect_q931_protocol_discriminator(tvb, offset, q931_tree);
        }
        offset += 1;
-       call_ref_len = pd[offset] & 0xF;        /* XXX - do as a bit field? */
+       call_ref_len = tvb_get_guint8(tvb, offset) & 0xF;       /* XXX - do as a bit field? */
        if (q931_tree != NULL)
-               proto_tree_add_item(q931_tree, hf_q931_call_ref_len, offset, 1, call_ref_len);
+               proto_tree_add_uint(q931_tree, hf_q931_call_ref_len, tvb, offset, 1, call_ref_len);
        offset += 1;
        if (call_ref_len != 0) {
                /* XXX - split this into flag and value */
-               memcpy(call_ref, &pd[offset], call_ref_len);
+               tvb_memcpy(tvb, call_ref, offset, call_ref_len);
                if (q931_tree != NULL)
-                       proto_tree_add_item(q931_tree, hf_q931_call_ref, offset, call_ref_len, call_ref);
+                       proto_tree_add_bytes(q931_tree, hf_q931_call_ref, tvb, offset, call_ref_len, call_ref);
                offset += call_ref_len;
        }
-       message_type = pd[offset];
-       if (check_col(fd, COL_INFO)) {
-               col_add_str(fd, COL_INFO,
+       message_type = tvb_get_guint8(tvb, offset);
+       if (check_col(pinfo->fd, COL_INFO)) {
+               col_add_str(pinfo->fd, COL_INFO,
                    val_to_str(message_type, q931_message_type_vals,
                      "Unknown message type (0x%02X)"));
        }
        if (q931_tree != NULL)
-               proto_tree_add_item(q931_tree, hf_q931_message_type, offset, 1, message_type);
+               proto_tree_add_uint(q931_tree, hf_q931_message_type, tvb, offset, 1, message_type);
        offset += 1;
 
        /*
@@ -2097,8 +2259,8 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
         */
        codeset = 0;    /* start out in codeset 0 */
        non_locking_shift = TRUE;
-       while (IS_DATA_IN_FRAME(offset)) {
-               info_element = pd[offset];
+       while (tvb_reported_length_remaining(tvb, offset) > 0) {
+               info_element = tvb_get_guint8(tvb, offset);
 
                /*
                 * Check for the single-octet IEs.
@@ -2110,7 +2272,7 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
                            !(info_element & Q931_IE_SHIFT_LOCKING);
                        codeset = info_element & Q931_IE_SHIFT_CODESET;
                        if (q931_tree != NULL) {
-                               proto_tree_add_text(q931_tree, offset, 1,
+                               proto_tree_add_text(q931_tree, tvb, offset, 1,
                                    "%s shift to codeset %u: %s",
                                    (non_locking_shift ? "Non-locking" : "Locking"),
                                    codeset,
@@ -2125,21 +2287,21 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
 
                        case Q931_IE_MORE_DATA:
                                if (q931_tree != NULL) {
-                                       proto_tree_add_text(q931_tree, offset, 1,
+                                       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, offset, 1,
+                                       proto_tree_add_text(q931_tree, tvb, offset, 1,
                                            "Sending complete");
                                }
                                break;
 
                        default:
                                if (q931_tree != NULL) {
-                                       proto_tree_add_text(q931_tree, offset, 1,
+                                       proto_tree_add_text(q931_tree, tvb, offset, 1,
                                            "Unknown information element (0x%02X",
                                            info_element);
                                }
@@ -2152,7 +2314,7 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
 
                case Q931_IE_CONGESTION_LEVEL:
                        if (q931_tree != NULL) {
-                               proto_tree_add_text(q931_tree, offset, 1,
+                               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,
@@ -2165,7 +2327,7 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
 
                case Q931_IE_REPEAT_INDICATOR:
                        if (q931_tree != NULL) {
-                               proto_tree_add_text(q931_tree, offset, 1,
+                               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,
@@ -2183,162 +2345,231 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
                /*
                 * Variable-length IE.
                 */
-               if (!BYTES_ARE_IN_FRAME(offset + 1, 1))
-                       break;  /* ran past end of frame */
-               info_element_len = pd[offset + 1];
-               if (!BYTES_ARE_IN_FRAME(offset + 2, info_element_len))
-                       break;  /* ran past end of frame */
-               if (q931_tree != NULL) {
-                       ti = proto_tree_add_text(q931_tree, 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, offset, 1,
-                           "Information element: %s",
-                           val_to_str(info_element, q931_info_element_vals,
-                             "Unknown (0x%02X)"));
-                       proto_tree_add_text(ie_tree, offset + 1, 1,
-                           "Length: %u", info_element_len);
+#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
+                *
+                */
 
+               if ( ( tvb_get_guint8( tvb, offset ) == 0x7E ) && 
+                  ( tvb_get_guint8( tvb, offset + 3 ) == 0x05 ) && 
+                  /* ( ( pinfo->srcport == 1720 ) || ( pinfo->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) {
+                               ti = proto_tree_add_text(q931_tree, tvb, offset,
+                                   1+1+1, "%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,
+                                   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
+                       }
+#endif
                        switch (info_element) {
 
                        case Q931_IE_SEGMENTED_MESSAGE:
-                               dissect_q931_segmented_message_ie(pd,
+                               dissect_q931_segmented_message_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_BEARER_CAPABILITY:
                        case Q931_IE_LOW_LAYER_COMPAT:
-                               dissect_q931_bearer_capability_ie(pd,
+                               dissect_q931_bearer_capability_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_CAUSE:
-                               dissect_q931_cause_ie(pd,
+                               dissect_q931_cause_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_CALL_STATE:
-                               dissect_q931_call_state_ie(pd,
+                               dissect_q931_call_state_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_CHANNEL_IDENTIFICATION:
-                               dissect_q931_channel_identification_ie(pd,
+                               dissect_q931_channel_identification_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_PROGRESS_INDICATOR:
-                               dissect_q931_progress_indicator_ie(pd,
+                               dissect_q931_progress_indicator_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_NETWORK_SPECIFIC_FACIL:
                        case Q931_IE_TRANSIT_NETWORK_SEL:
-                               dissect_q931_ns_facilities_ie(pd,
+                               dissect_q931_ns_facilities_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_NOTIFICATION_INDICATOR:
-                               dissect_q931_notification_indicator_ie(pd,
+                               dissect_q931_notification_indicator_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_DISPLAY:
-                               dissect_q931_ia5_ie(pd, offset + 2,
+                               dissect_q931_ia5_ie(tvb, offset + 2,
                                    info_element_len, ie_tree,
                                    "Display information");
                                break;
 
                        case Q931_IE_DATE_TIME:
-                               dissect_q931_date_time_ie(pd,
+                               dissect_q931_date_time_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_KEYPAD_FACILITY:
-                               dissect_q931_ia5_ie(pd, offset + 2,
+                               dissect_q931_ia5_ie(tvb, offset + 2,
                                    info_element_len, ie_tree,
                                    "Keypad facility");
                                break;
 
                        case Q931_IE_SIGNAL:
-                               dissect_q931_signal_ie(pd,
+                               dissect_q931_signal_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_INFORMATION_RATE:
-                               dissect_q931_information_rate_ie(pd,
+                               dissect_q931_information_rate_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_E2E_TRANSIT_DELAY:
-                               dissect_q931_e2e_transit_delay_ie(pd,
+                               dissect_q931_e2e_transit_delay_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_TD_SELECTION_AND_INT:
-                               dissect_q931_td_selection_and_int_ie(pd,
+                               dissect_q931_td_selection_and_int_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_PL_BINARY_PARAMETERS:
-                               dissect_q931_pl_binary_parameters_ie(pd,
+                               dissect_q931_pl_binary_parameters_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_PL_WINDOW_SIZE:
-                               dissect_q931_pl_window_size_ie(pd,
+                               dissect_q931_pl_window_size_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_PACKET_SIZE:
-                               dissect_q931_packet_size_ie(pd,
+                               dissect_q931_packet_size_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_CUG:
-                               dissect_q931_cug_ie(pd,
+                               dissect_q931_cug_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_REVERSE_CHARGE_IND:
-                               dissect_q931_reverse_charge_ind_ie(pd,
+                               dissect_q931_reverse_charge_ind_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(pd,
+                               dissect_q931_number_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(pd,
+                               dissect_q931_party_subaddr_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_RESTART_INDICATOR:
-                               dissect_q931_restart_indicator_ie(pd,
+                               dissect_q931_restart_indicator_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_HIGH_LAYER_COMPAT:
-                               dissect_q931_high_layer_compat_ie(pd,
+                               dissect_q931_high_layer_compat_ie(tvb,
                                    offset + 2, info_element_len, ie_tree);
                                break;
 
                        case Q931_IE_USER_USER:
-                               dissect_q931_user_user_ie(pd,
-                                   offset + 2, info_element_len, ie_tree);
+#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
+                                       dissect_q931_user_user_ie(tvb,
+                                           offset + 2, info_element_len,
+                                           ie_tree);
+#ifdef H323
+                               }
+#endif
                                break;
 
                        default:
-                               proto_tree_add_text(ie_tree, offset + 2,
+                               proto_tree_add_text(ie_tree, tvb, offset + 2,
                                    info_element_len, "Data: %s",
-                                   bytes_to_str(&pd[offset + 2],
+                                   bytes_to_str(
+                                     tvb_get_ptr(tvb, offset + 2, info_element_len),
                                      info_element_len));
                                break;
                        }
@@ -2347,35 +2578,72 @@ dissect_q931(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
                if (non_locking_shift)
                        codeset = 0;
        }
+
+       /*
+        * Heuristic should return TRUE if it get's here.
+        */
+
+       return TRUE;
+
+}
+
+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
+        *
+        *      http://www.isi.edu/in-notes/iana/assignments/port-numbers
+        *
+        * which says it's for "h323hostcall"?
+        */
+       pinfo->current_proto = "Q.931 HEUR";
+       return q931_dissector(tvb, pinfo, tree, TRUE);
+}
+
+static void
+dissect_q931(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
+       q931_dissector(tvb, pinfo, tree, FALSE);
 }
 
 void
 proto_register_q931(void)
 {
-    static hf_register_info hf[] = {
-       { &hf_q931_discriminator,
-         { "Protocol discriminator", "q931.disc", FT_UINT8, BASE_HEX, NULL, 0x0, 
-               "" }},
-
-       { &hf_q931_call_ref_len,
-         { "Call reference value length", "q931.call_ref_len", FT_UINT8, BASE_DEC, NULL, 0x0,
-               "" }},
-
-       { &hf_q931_call_ref,
-         { "Call reference value", "q931.call_ref", FT_BYTES, BASE_HEX, NULL, 0x0,
-               "" }},
-
-       { &hf_q931_message_type,
-         { "Message type", "q931.message_type", FT_UINT8, BASE_HEX, VALS(q931_message_type_vals), 0x0,
-               "" }},
-
-    };
-    static gint *ett[] = {
-        &ett_q931,
-        &ett_q931_ie,
-    };
-
-    proto_q931 = proto_register_protocol ("Q.931", "q931");
-    proto_register_field_array (proto_q931, hf, array_length(hf));
-    proto_register_subtree_array(ett, array_length(ett));
+       static hf_register_info hf[] = {
+               { &hf_q931_discriminator,
+                 { "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,
+                 { "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 }},
+
+           };
+       static gint *ett[] = {
+               &ett_q931,
+               &ett_q931_ie,
+       };
+
+       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_dissector("q931", dissect_q931, proto_q931);
+}
+
+void
+proto_reg_handoff_q931(void)
+{
+       heur_dissector_add("tcp", dissect_q931_heur, proto_q931);
 }