Find the BitTorrent dissector immediately after registering it, and save
authorGuy Harris <guy@alum.mit.edu>
Sat, 9 Jul 2005 00:44:44 +0000 (00:44 -0000)
committerGuy Harris <guy@alum.mit.edu>
Sat, 9 Jul 2005 00:44:44 +0000 (00:44 -0000)
the handle, rather than finding it every time we need it.

Just make a guint8 array with the BitTorrent magic string length
followed by the string, and use "tvb_memeql()" to test against that - it
handles checking to make sure there's enough data in the tvbuff (if
not, it returns -1 as a "no match" indication) and then checks all of
the first 20 bytes in one operation.

Clean up white space.

svn path=/trunk/; revision=14882

epan/dissectors/packet-bittorrent.c

index 9aa9472c5e10166f066f0b61cb7561074b0a70da..e57d13928e27c3750976454d7a7b33ddd405534d 100644 (file)
@@ -90,6 +90,8 @@ static gint ett_bittorrent_msg = -1;
 
 static gboolean bittorrent_desegment = TRUE;
 
+static dissector_handle_t bittorrent_handle;
+
 static guint get_bittorrent_pdu_length(tvbuff_t *tvb, int offset)
 {
        if (tvb_get_guint8(tvb, offset) == 19) {
@@ -166,6 +168,7 @@ static void dissect_bittorrent_message (tvbuff_t *tvb, packet_info *pinfo, proto
 static void dissect_bittorrent_welcome (tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
 {
        int offset = 0;
+
        if (check_col(pinfo->cinfo, COL_INFO)) {
            col_set_str(pinfo->cinfo, COL_INFO, "BitTorrent Handshake");
        }
@@ -181,11 +184,11 @@ static void dissect_bittorrent_tcp_pdu (tvbuff_t *tvb, packet_info *pinfo, proto
 {
        proto_item *ti;
        
-    if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
-           col_set_str(pinfo->cinfo, COL_PROTOCOL, "BitTorrent");
-    }
+       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
+               col_set_str(pinfo->cinfo, COL_PROTOCOL, "BitTorrent");
+       }
 
-    if (check_col(pinfo->cinfo, COL_INFO)) {
+       if (check_col(pinfo->cinfo, COL_INFO)) {
            col_set_str(pinfo->cinfo, COL_INFO, "BitTorrent Peer-To-Peer connection");
        }
 
@@ -205,27 +208,24 @@ static void dissect_bittorrent (tvbuff_t *tvb, packet_info *pinfo, proto_tree *t
        tcp_dissect_pdus(tvb, pinfo, tree, bittorrent_desegment, 4, get_bittorrent_pdu_length, dissect_bittorrent_tcp_pdu);
 }
 
+static const guint8 bittorrent_magic[20] = {
+       19,
+       'B', 'i', 't', 'T', 'o', 'r', 'r', 'e', 'n', 't',
+       ' ', 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l'
+};
+
 static gboolean test_bittorrent_packet (tvbuff_t *tvb, packet_info *pinfo,
                                                                                     proto_tree *tree)
 {
        conversation_t *conversation;
 
-    if (!tvb_bytes_exist(tvb, 0, 20))
-               return FALSE;
-
-       if (tvb_get_guint8(tvb, 0) != 19) {
-               return FALSE;
-       }
-
-       if (tvb_memeql(tvb, 1, "BitTorrent protocol", 19) == -1) {
+       if (tvb_memeql(tvb, 0, bittorrent_magic, sizeof bittorrent_magic) == -1) {
                return FALSE;
        }
 
        conversation = conversation_new (pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
 
-       DISSECTOR_ASSERT(find_dissector("bittorrent"));
-
-       conversation_set_dissector(conversation, find_dissector("bittorrent"));
+       conversation_set_dissector(conversation, bittorrent_handle);
        
        dissect_bittorrent(tvb, pinfo, tree);
 
@@ -304,5 +304,6 @@ void
 proto_reg_handoff_bittorrent(void)
 {
        register_dissector("bittorrent", dissect_bittorrent, proto_bittorrent);
+       bittorrent_handle = find_dissector("bittorrent");
        heur_dissector_add("tcp", test_bittorrent_packet, proto_bittorrent);
 }