diff options
author | Bill Meier <wmeier@newsguy.com> | 2010-02-07 23:51:56 +0000 |
---|---|---|
committer | Bill Meier <wmeier@newsguy.com> | 2010-02-07 23:51:56 +0000 |
commit | e74e2a598c5d461e1048328c15609a4e9d5701c5 (patch) | |
tree | 810e4623c986f076ef13e48648e268276535f068 /epan/dissectors/packet-applemidi.c | |
parent | 7a4031e31d6f3666b5830628dccecebee59d4965 (diff) |
Fix and rework some code a bit:
-No prefs so prefs calback not req'd;
-Remove (seemingly) not needed dissector registration on UDP port 0;
-Rework dissect_applemidi_heur() and related so that once dissect_applemidi_heur()
determines that a pair of UDP addr/ports are being used for the AppleMIDI protocol,
dissect_apple_midi() will always be called directly for UDP packets with that pair
(rather than dissect_applemidi_heur() always be called for every packet).
svn path=/trunk/; revision=31824
Diffstat (limited to 'epan/dissectors/packet-applemidi.c')
-rw-r--r-- | epan/dissectors/packet-applemidi.c | 139 |
1 files changed, 62 insertions, 77 deletions
diff --git a/epan/dissectors/packet-applemidi.c b/epan/dissectors/packet-applemidi.c index 5d158e933f..20e25cba04 100644 --- a/epan/dissectors/packet-applemidi.c +++ b/epan/dissectors/packet-applemidi.c @@ -44,7 +44,6 @@ #include <glib.h> #include <epan/packet.h> -#include <epan/prefs.h> #include <epan/conversation.h> /* Definitions for protocol name during dissector-register */ @@ -94,29 +93,22 @@ static const value_string applemidi_commands[] = { }; -void proto_reg_handoff_applemidi( void ); - -static guint udp_port = 0; - static int proto_applemidi = -1; -static dissector_handle_t applemidi_handle = NULL; -static dissector_handle_t rtp_handle = NULL; - - +static dissector_handle_t applemidi_handle; +static dissector_handle_t rtp_handle; static void -dissect_applemidi( tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree ) { +dissect_applemidi_common( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint16 command ) { - guint16 command; guint16 seq_num; guint8 count; guint8 *name; unsigned int offset = 0; - proto_tree *applemidi_tree = NULL; - proto_tree *applemidi_tree_seq_num = NULL; + proto_tree *applemidi_tree; + proto_tree *applemidi_tree_seq_num; col_set_str( pinfo->cinfo, COL_PROTOCOL, APPLEMIDI_DISSECTOR_SHORTNAME ); @@ -124,12 +116,10 @@ dissect_applemidi( tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree ) { /* Clear out stuff in the info column */ col_clear( pinfo->cinfo, COL_INFO ); - command = tvb_get_ntohs( tvb, offset + 2 ); - col_add_fstr( pinfo->cinfo, COL_INFO, "%s", val_to_str( command, applemidi_commands, "Unknown Command: 0x%04x" ) ); if ( tree ) { - proto_item *ti = NULL; + proto_item *ti; ti = proto_tree_add_item( tree, proto_applemidi, tvb, 0, -1, FALSE ); applemidi_tree = proto_item_add_subtree( ti, ett_applemidi ); @@ -140,7 +130,7 @@ dissect_applemidi( tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree ) { offset += 2; /* the format of packets for "IN", "NO", "OK" and "BY" is identical and contains - * the protocol version, a random number generated by the initiator of the session + * the protocol version, a random number generated by the initiator of the session, * the SSRC that is used by the respective sides RTP-entity and optionally the * name of the participant */ if ( ( APPLEMIDI_COMMAND_INVITATION == command ) || ( APPLEMIDI_COMMAND_INVITATION_REJECTED == command ) || ( APLLEMIDI_COMMAND_INVITATION_ACCEPTED == command ) || ( APPLEMIDI_COMMAND_ENDSESSION == command ) ) { @@ -155,7 +145,7 @@ dissect_applemidi( tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree ) { proto_tree_add_item( applemidi_tree, hf_applemidi_ssrc, tvb, offset, 4, FALSE ); offset += 4; - len = tvb->length - offset; + len = tvb_reported_length(tvb) - offset; /* Name is optional */ if ( len > 0 ) { @@ -187,14 +177,14 @@ dissect_applemidi( tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree ) { proto_tree_add_item( applemidi_tree, hf_applemidi_timestamp3, tvb, offset, 8, FALSE ); offset += 8; /* With the receiver feedback packet, the recipient can tell the sender up to what sequence - * number in the RTP-stream the packets have been received, this can be used to shorten the + * number in the RTP-stream the packets have been received; this can be used to shorten the * recovery-journal-section in the RTP-session */ } else if ( APPLEMIDI_COMMAND_RECEIVER_FEEDBACK == command ) { proto_tree_add_item( applemidi_tree, hf_applemidi_ssrc, tvb, offset, 4, FALSE ); offset += 4; ti = proto_tree_add_item( applemidi_tree, hf_applemidi_sequence_num, tvb, offset, 4, FALSE ); - /* Apple includes a 32bit sequence-number, but the RTP-packet only specifies 16bit + /* Apple includes a 32bit sequence-number, but the RTP-packet only specifies 16bit. * this subtree and subitem are added to be able to associate the sequence-number * here easier with the one specified in the corresponding RTP-packet */ applemidi_tree_seq_num = proto_item_add_subtree( ti, ett_applemidi_seq_num ); @@ -207,73 +197,74 @@ dissect_applemidi( tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree ) { } } - static gboolean -dissect_applemidi_heur( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree ) { +test_applemidi(tvbuff_t *tvb, guint16 *command_p) { - unsigned int offset = 0; - guint16 signature; - guint16 command; - conversation_t *p_conv; - gboolean result = FALSE; + *command_p = 0xffff; + /* An applemidi session protocol UDP-packet must start with the "magic value" of 0xffff ... */ + if ( APPLEMIDI_PROTOCOL_SIGNATURE != tvb_get_ntohs( tvb, 0 ) ) + return FALSE; + *command_p = tvb_get_ntohs( tvb, 2 ); - /* Get the two octets making up the magic value */ - signature = tvb_get_ntohs( tvb, offset ); + /* ... followed by packet-command: "IN", "NO", "OK", "BY", "CK" and "RS" */ + if ( ( APPLEMIDI_COMMAND_INVITATION == *command_p ) || + ( APPLEMIDI_COMMAND_INVITATION_REJECTED == *command_p ) || + ( APLLEMIDI_COMMAND_INVITATION_ACCEPTED == *command_p ) || + ( APPLEMIDI_COMMAND_ENDSESSION == *command_p ) || + ( APPLEMIDI_COMMAND_SYNCHRONIZATION == *command_p ) || + ( APPLEMIDI_COMMAND_RECEIVER_FEEDBACK == *command_p ) ) + return TRUE; + return FALSE; +} - /* An applemidi session protocol UDP-packet must start with the "magic value" of 0xffff ... */ - if ( APPLEMIDI_PROTOCOL_SIGNATURE == signature ) { - - command = tvb_get_ntohs( tvb, offset + 2 ); - - /* ... followed by packet-command: "IN", "NO", "OK", "BY", "CK" and "RS" */ - if( APPLEMIDI_COMMAND_INVITATION == command ) - result = TRUE; - else if( APPLEMIDI_COMMAND_INVITATION_REJECTED == command ) - result = TRUE; - else if( APLLEMIDI_COMMAND_INVITATION_ACCEPTED == command ) - result = TRUE; - else if( APPLEMIDI_COMMAND_ENDSESSION == command ) - result = TRUE; - else if( APPLEMIDI_COMMAND_SYNCHRONIZATION == command ) - result = TRUE; - else if( APPLEMIDI_COMMAND_RECEIVER_FEEDBACK == command ) - result = TRUE; - } +/* dissect_applemidi() is called when a packet is seen from a previously identified applemidi conversation */ +/* If the packet isn't a valid applemidi packet, assume it's an RTP-MIDI packet. */ - p_conv=find_conversation( pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport, 0 ); +static void +dissect_applemidi( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree ) { + guint16 command; - if( result ) { - if( !p_conv ) { - p_conv = conversation_new( 0, &pinfo->src, &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport, 0 ); - } - if( p_conv ) { - conversation_set_dissector( p_conv, applemidi_handle ); - } + if ( test_applemidi( tvb, &command ) ) + dissect_applemidi_common( tvb, pinfo, tree, command ); + else + call_dissector( rtp_handle, tvb, pinfo, tree ); +} - dissect_applemidi( tvb, pinfo, tree ); - } else { - if( p_conv ) { - if( p_conv->dissector_handle == applemidi_handle ) { - result = TRUE; - /* punt packets not containing valid AppleMIDI-commands to RTP (or in - * case it exists, the more specialized RTP-MIDI-dissector */ - call_dissector( rtp_handle, tvb, pinfo, tree ); - } - } +static gboolean +dissect_applemidi_heur( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree ) { + + guint16 command; + conversation_t *p_conv; + + if ( tvb_length( tvb ) < 4) + return FALSE; /* not enough bytes to check */ + + if ( !test_applemidi( tvb, &command ) ) { + return FALSE; + } + + /* call dissect_applemidi() from now on for UDP packets on this "connection" */ + p_conv=find_conversation( pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, + pinfo->srcport, pinfo->destport, 0 ); + if( !p_conv ) { + p_conv = conversation_new( pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, + pinfo->srcport, pinfo->destport, 0 ); } - return result; + conversation_set_dissector( p_conv, applemidi_handle ); + + dissect_applemidi_common( tvb, pinfo, tree, command ); + return TRUE; + } void proto_register_applemidi( void ) { - module_t *applemidi_module; - static hf_register_info hf[] = { { &hf_applemidi_signature, @@ -439,21 +430,17 @@ proto_register_applemidi( void ) &ett_applemidi_seq_num }; - proto_applemidi = proto_register_protocol( APPLEMIDI_DISSECTOR_NAME, APPLEMIDI_DISSECTOR_SHORTNAME, APPLEMIDI_DISSECTOR_ABBREVIATION ); + proto_applemidi = proto_register_protocol( APPLEMIDI_DISSECTOR_NAME, APPLEMIDI_DISSECTOR_SHORTNAME, APPLEMIDI_DISSECTOR_ABBREVIATION ); proto_register_field_array( proto_applemidi, hf, array_length( hf ) ); proto_register_subtree_array( ett, array_length( ett ) ); - applemidi_module = prefs_register_protocol( proto_applemidi, proto_reg_handoff_applemidi ); - } void proto_reg_handoff_applemidi( void ) { - if ( !applemidi_handle ) { - applemidi_handle = create_dissector_handle( dissect_applemidi, proto_applemidi ); - } + applemidi_handle = create_dissector_handle( dissect_applemidi, proto_applemidi ); /* If we cannot decode the data it will be RTP-MIDI since the Apple session protocol uses * two ports: the control-port and the MIDI-port. On both ports an invitation is being sent. @@ -461,8 +448,6 @@ proto_reg_handoff_applemidi( void ) { * packets, it will be most likely RTP-MIDI... */ rtp_handle = find_dissector( "rtp" ); - dissector_add( "udp.port" , udp_port, applemidi_handle ); heur_dissector_add( "udp", dissect_applemidi_heur, proto_applemidi ); - applemidi_handle = find_dissector( "applemidi" ); } |