/* packet-slsk.c * Routines for SoulSeek Protocol dissection * Copyright 2003, Christian Wagner * Institute of Telematics - University of Karlsruhe * part of this work supported by * Deutsche Forschungsgemeinschaft (DFG) Grant Number FU448/1 * * SoulSeek Protocol dissector based on protocol descriptions from SoleSeek Project: * http://cvs.sourceforge.net/viewcvs.py/soleseek/SoleSeek/doc/protocol.html?rev=HEAD * Updated for SoulSeek client version 151 * * $Id: packet-slsk.c,v 1.3 2004/03/17 09:01:16 guy Exp $ * * * Ethereal - Network traffic analyzer * By Gerald Combs * Copyright 1998 Gerald Combs * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include #include #include #ifdef HAVE_LIBZ #include #endif #include #include "packet-tcp.h" #include "prefs.h" /* Initialize the protocol and registered fields */ static int proto_slsk = -1; static int hf_slsk_integer = -1; static int hf_slsk_string = -1; static int hf_slsk_byte = -1; static int hf_slsk_message_length = -1; static int hf_slsk_message_code = -1; static int hf_slsk_client_ip = -1; static int hf_slsk_server_ip = -1; static int hf_slsk_string_length = -1; static int hf_slsk_username = -1; static int hf_slsk_password = -1; static int hf_slsk_version = -1; static int hf_slsk_login_successfull = -1; static int hf_slsk_login_message = -1; static int hf_slsk_port = -1; static int hf_slsk_ip = -1; static int hf_slsk_user_exists = -1; static int hf_slsk_status_code = -1; static int hf_slsk_room = -1; static int hf_slsk_chat_message = -1; static int hf_slsk_users_in_room = -1; static int hf_slsk_token = -1; static int hf_slsk_connection_type = -1; static int hf_slsk_chat_message_id = -1; static int hf_slsk_timestamp = -1; static int hf_slsk_search_text = -1; static int hf_slsk_folder_count = -1; static int hf_slsk_file_count = -1; static int hf_slsk_average_speed = -1; static int hf_slsk_download_number = -1; static int hf_slsk_files = -1; static int hf_slsk_directories = -1; static int hf_slsk_slotsfull = -1; static int hf_slsk_place_in_queue = -1; static int hf_slsk_number_of_rooms = -1; static int hf_slsk_filename = -1; static int hf_slsk_directory = -1; static int hf_slsk_size = -1; static int hf_slsk_checksum = -1; static int hf_slsk_code = -1; static int hf_slsk_number_of_users = -1; static int hf_slsk_number_of_days = -1; static int hf_slsk_transfer_direction = -1; static int hf_slsk_user_description = -1; static int hf_slsk_picture_exists = -1; static int hf_slsk_picture = -1; static int hf_slsk_user_uploads = -1; static int hf_slsk_total_uploads = -1; static int hf_slsk_queued_uploads = -1; static int hf_slsk_slots_available = -1; static int hf_slsk_allowed = -1; static int hf_slsk_compr_packet = -1; static int hf_slsk_parent_min_speed = -1; static int hf_slsk_parent_speed_connection_ratio = -1; static int hf_slsk_seconds_parent_inactivity_before_disconnect = -1; static int hf_slsk_seconds_server_inactivity_before_disconnect = -1; static int hf_slsk_nodes_in_cache_before_disconnect = -1; static int hf_slsk_seconds_before_ping_children = -1; static int hf_slsk_recommendation = -1; static int hf_slsk_ranking = -1; /* Initialize the subtree pointers */ static gint ett_slsk = -1; static gint ett_slsk_compr_packet = -1; #define TCP_PORT_SLSK_1 2234 #define TCP_PORT_SLSK_2 5534 #define TCP_PORT_SLSK_3 2240 /* desegmentation of SoulSeek Message over TCP */ static gboolean slsk_desegment = TRUE; #ifdef HAVE_LIBZ static gboolean slsk_decompress = TRUE; #else static gboolean slsk_decompress = FALSE; #endif static const value_string slsk_tcp_msgs[] = { { 1, "Login"}, { 2, "Set Wait Port"}, { 3, "Get Peer Address"}, { 4, "Get Shared File List"}, { 5, "User Exists / Shared File List"}, { 7, "Get User Status"}, { 9, "File Search Result"}, { 13, "Say ChatRoom"}, { 14, "Join Room"}, { 15, "Leave Room / User Info Request"}, { 16, "User Joined Room / User Info Reply"}, { 17, "User Left Room"}, { 18, "Connect To Peer"}, { 22, "Message User"}, { 23, "Message User Ack"}, { 26, "File Search"}, { 28, "Set Status"}, { 32, "Ping"}, { 34, "Update Upload Speed"}, { 35, "Shared Files & Folders"}, { 36, "Get User Stats / Folder Contents Request"}, { 37, "Folder Contents Response"}, { 40, "Queued Downloads / Transfer Request"}, { 41, "Transfer Response"}, { 42, "Placehold Upload"}, { 43, "Queue Upload"}, { 44, "Place In Queue"}, { 46, "Upload Failed"}, { 50, "Queue Failed / Own Recommendation"}, { 51, "Add Things I like / Place In Queue Request"}, { 52, "Remove Things I like"}, { 54, "Get Recommendations"}, { 55, "Type 55"}, { 56, "Get Global Rankings"}, { 57, "Get User Recommendations"}, { 58, "Admin Command"}, { 60, "Place In Line Response"}, { 62, "Room Added"}, { 63, "Room Removed"}, { 64, "Room List"}, { 65, "Exact File Search"}, { 66, "Admin Message"}, { 67, "Global User List"}, { 68, "Tunneled Message"}, { 69, "Privileged User List"}, { 71, "Get Parent List"}, { 73, "Type 73"}, { 83, "Parent Min Speed"}, { 84, "Parent Speed Connection Ratio"}, { 86, "Parent Inactivity Before Disconnect"}, { 87, "Server Inactivity Before Disconnect"}, { 88, "Nodes In Cache Before Disconnect"}, { 90, "Seconds Before Ping Children"}, { 91, "Add To Privileged"}, { 92, "Check Privileges"}, { 93, "Embedded Message"}, { 100, "Become Parent"}, { 102, "Random Parent Addresses"}, { 103, "Send Wishlist Entry"}, { 104, "Type 104"}, { 110, "Get Similar Users"}, { 111, "Get Recommendations for Item"}, { 112, "Get Similar Users for Item"}, { 1001, "Can't Connect To Peer"}, }; static const value_string slsk_status_codes[] = { { -1, "Unknown"}, { 0, "Offline"}, { 1, "Away"}, { 2, "Online"}, }; static const value_string slsk_transfer_direction[] = { { 0, "Download"}, { 1, "Upload"}, }; static const value_string slsk_yes_no[] = { { 0, "No"}, { 1, "Yes"}, }; static const value_string slsk_attr_type[] = { { 0, "Bitrate"}, { 1, "Length"}, { 2, "VBR"}, }; static char* connection_type(char con_type[]) { if (strlen(con_type) != 1) return "Unknown"; if (con_type[0] == 'D') return "Distributed Search"; if (con_type[0] == 'P') return "Peer Connection"; /* "File Search Result / User Info Request / Get Shared File List" */ if (con_type[0] == 'F') return "File Transfer"; return "Unknown"; } static gboolean check_slsk_format(tvbuff_t *tvb, int offset, char format[]){ /* * Returns TRUE if tvbuff beginning at offset matches a certain format * The format is given by an array of characters standing for a special field type * i - integer (4 bytes) * b - byte (1 byte) * s - string (string_length + 4 bytes) * * * - can be used at the end of a format to ignore any following bytes */ switch ( format[0] ) { case 'i': if (tvb_length_remaining(tvb, offset) < 4) return FALSE; offset += 4; break; case 'b': if (tvb_length_remaining(tvb, offset) < 1) return FALSE; offset += 1; break; case 's': if (tvb_length_remaining(tvb, offset) < 4) return FALSE; if (tvb_length_remaining(tvb, offset) < (int)tvb_get_letohl(tvb, offset)+4) return FALSE; offset += tvb_get_letohl(tvb, offset)+4; break; case '*': return TRUE; break; default: return FALSE; break; } if (format[1] == '\0' ) { if (tvb_length_remaining(tvb, offset) != 0) return FALSE; /* Checks for additional bytes at the end */ return TRUE; } return check_slsk_format(tvb, offset, &format[1]); } static char* get_message_type(tvbuff_t *tvb) { /* * Checks if the Message Code is known. * If unknown checks if the Message Code is stored in a byte. * Returns the Message Type. */ int msg_code = tvb_get_letohl(tvb, 4); gchar *message_type = val_to_str(msg_code, slsk_tcp_msgs, "Unknown"); if (strcmp(message_type, "Unknown") == 0) { if (check_slsk_format(tvb, 4, "bisis")) message_type = "Distributed Search"; if (check_slsk_format(tvb, 4, "bssi")) message_type = "Peer Init"; if (check_slsk_format(tvb, 4, "bi")) message_type = "Pierce Fw"; } return message_type; } static guint get_slsk_pdu_len(tvbuff_t *tvb, int offset) { guint32 msg_len; msg_len = tvb_get_letohl(tvb, offset); /* That length doesn't include the length field itself; add that in. */ msg_len += 4; return msg_len; } #ifdef HAVE_LIBZ static tvbuff_t* uncompress_packet(tvbuff_t *tvb, int offset, int comprlen){ /* * Uncompresses a zlib compressed packet inside a message of tvb at offset with length comprlen * Returns an uncompressed tvbuffer if uncompression succeeded * or NULL if uncompression failed */ int err; long uncomprlen = (comprlen*10); guint8 * compr; guint8 * uncompr; tvbuff_t *uncompr_tvb; compr = tvb_memdup(tvb, offset, comprlen); if (!compr) return NULL; uncompr = g_malloc(uncomprlen); if (!uncompr){ g_free(compr); return NULL; } err = uncompress((Bytef *)uncompr, &uncomprlen, (Bytef *)compr, comprlen); g_free(compr); if (err != 0) { g_free(uncompr); return NULL; } uncompr_tvb = tvb_new_real_data((guint8*) uncompr, uncomprlen, uncomprlen); g_free(uncompr); return uncompr_tvb; } #else static tvbuff_t* uncompress_packet(tvbuff_t *tvb _U_, int offset _U_, int comprlen _U_){ return NULL; } #endif /* Code to actually dissect the packets */ static void dissect_slsk_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) { /* Set up structures needed to add the protocol subtree and manage it */ proto_item *ti; proto_tree *slsk_tree; int offset, i, j; guint32 msg_len, msg_code; gchar *message_type; int comprlen = 0; int uncomprlen = 0; int uncompr_tvb_offset = 0; int i2 = 0; int j2 = 0; int i3 = 0; int j3 = 0; offset = 0; msg_len = tvb_get_letohl(tvb, offset); msg_code = tvb_get_letohl(tvb, offset+4); message_type = get_message_type(tvb); /* Make entries in Protocol column and Info column on summary display */ if (check_col(pinfo->cinfo, COL_PROTOCOL)) col_set_str(pinfo->cinfo, COL_PROTOCOL, "slsk"); /* This field shows up as the "Info" column in the display */ if (check_col(pinfo->cinfo, COL_INFO)) col_set_str(pinfo->cinfo, COL_INFO, "SoulSeek Message"); if (check_col(pinfo->cinfo, COL_INFO)) { col_append_fstr(pinfo->cinfo, COL_INFO, ": %s", message_type); } if (tree) { /* create display subtree for the protocol */ ti = proto_tree_add_item(tree, proto_slsk, tvb, 0, -1, FALSE); slsk_tree = proto_item_add_subtree(ti, ett_slsk); /* Continue adding tree items to process the packet here */ proto_tree_add_uint(slsk_tree, hf_slsk_message_length, tvb, offset, 4, msg_len); offset += 4; switch (msg_code) { case 1: if (check_slsk_format(tvb, offset, "issi")) { /* Client-to-Server */ message_type = "Login"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; proto_tree_add_uint(slsk_tree, hf_slsk_string_length, tvb, offset, 4, tvb_get_letohl(tvb, offset)); proto_tree_add_item(slsk_tree, hf_slsk_username, tvb, offset+4, tvb_get_letohl(tvb, offset), FALSE); offset += 4+tvb_get_letohl(tvb, offset); proto_tree_add_uint(slsk_tree, hf_slsk_string_length, tvb, offset, 4, tvb_get_letohl(tvb, offset)); proto_tree_add_item(slsk_tree, hf_slsk_password, tvb, offset+4, tvb_get_letohl(tvb, offset), FALSE); offset += 4+tvb_get_letohl(tvb, offset); proto_tree_add_uint(slsk_tree, hf_slsk_version, tvb, offset, 4, tvb_get_letohl(tvb, offset)); offset += 4; } else if (check_slsk_format(tvb, offset, "ibs") || check_slsk_format(tvb, offset, "ibsi")) { /* Server-to-Client */ message_type = "Login Reply"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; i=tvb_get_guint8(tvb, offset); proto_tree_add_uint_format(slsk_tree, hf_slsk_login_successfull, tvb, offset, 1, tvb_get_guint8(tvb, offset), "Login successfull: %s (Byte: %d)", val_to_str(tvb_get_guint8(tvb, offset), slsk_yes_no, "Unknown"), tvb_get_guint8(tvb, offset)); offset += 1; proto_tree_add_uint(slsk_tree, hf_slsk_string_length, tvb, offset, 4, tvb_get_letohl(tvb, offset)); proto_tree_add_item(slsk_tree, hf_slsk_login_message, tvb, offset+4, tvb_get_letohl(tvb, offset), FALSE); offset += 4+tvb_get_letohl(tvb, offset); if (i == 1){ proto_tree_add_ipv4(slsk_tree, hf_slsk_client_ip, tvb, offset, 4, tvb_get_ntohl(tvb, offset)); offset += 4; } } break; case 2: if (check_slsk_format(tvb, offset, "ii")) { /* Client-to-Server */ message_type = "Set Wait Port"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; proto_tree_add_uint(slsk_tree, hf_slsk_port, tvb, offset, 4, tvb_get_letohl(tvb, offset)); offset += 4; } break; case 3: if (check_slsk_format(tvb, offset, "isii")) { /* Server-to-Client */ message_type = "Get Peer Address Reply"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; proto_tree_add_uint(slsk_tree, hf_slsk_string_length, tvb, offset, 4, tvb_get_letohl(tvb, offset)); proto_tree_add_item(slsk_tree, hf_slsk_username, tvb, offset+4, tvb_get_letohl(tvb, offset), FALSE); offset += 4+tvb_get_letohl(tvb, offset); proto_tree_add_ipv4(slsk_tree, hf_slsk_ip, tvb, offset, 4, tvb_get_ntohl(tvb, offset)); offset += 4; proto_tree_add_uint(slsk_tree, hf_slsk_port, tvb, offset, 4, tvb_get_letohl(tvb, offset)); offset += 4; } else if (check_slsk_format(tvb, offset, "is")) { /* Client-to-Server */ message_type = "Get Peer Address"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; proto_tree_add_uint(slsk_tree, hf_slsk_string_length, tvb, offset, 4, tvb_get_letohl(tvb, offset)); proto_tree_add_item(slsk_tree, hf_slsk_username, tvb, offset+4, tvb_get_letohl(tvb, offset), FALSE); offset += 4+tvb_get_letohl(tvb, offset); } break; case 4: if (check_slsk_format(tvb, offset, "i")) { /* Client-to-Client */ message_type = "Get Shared File List"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; } break; case 5: if (check_slsk_format(tvb, offset, "isb")) { /* Server-to-Client */ message_type = "User Exists Reply"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; proto_tree_add_uint(slsk_tree, hf_slsk_string_length, tvb, offset, 4, tvb_get_letohl(tvb, offset)); proto_tree_add_item(slsk_tree, hf_slsk_username, tvb, offset+4, tvb_get_letohl(tvb, offset), FALSE); offset += 4+tvb_get_letohl(tvb, offset); proto_tree_add_uint_format(slsk_tree, hf_slsk_user_exists, tvb, offset, 1, tvb_get_guint8(tvb, offset), "User exists: %s (Byte: %d)", val_to_str(tvb_get_guint8(tvb, offset), slsk_yes_no, "Unknown"), tvb_get_guint8(tvb, offset)); offset += 1; } else if (check_slsk_format(tvb, offset, "is")) { /* Client-to-Server */ message_type = "User Exists Request"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; proto_tree_add_uint(slsk_tree, hf_slsk_string_length, tvb, offset, 4, tvb_get_letohl(tvb, offset)); proto_tree_add_item(slsk_tree, hf_slsk_username, tvb, offset+4, tvb_get_letohl(tvb, offset), FALSE); offset += 4+tvb_get_letohl(tvb, offset); } else if (check_slsk_format(tvb, offset, "i*")) { /* Client-to-Client */ message_type = "Shared File List"; proto_tree_add_uint_format(slsk_tree, hf_slsk_message_code, tvb, offset, 4, msg_code, "Message Type: %s (Code: %02d)", message_type, msg_code); offset += 4; /* [zlib compressed] */ comprlen = tvb_length_remaining(tvb, offset); if (slsk_decompress == TRUE){ tvbuff_t *uncompr_tvb = uncompress_packet(tvb, offset, comprlen); if (uncompr_tvb == NULL) { proto_tree_add_uint_format(slsk_tree, hf_slsk_integer, tvb, offset, tvb_length_remaining(tvb, offset), 0, "[zlib compressed packet]"); offset += tvb_length_remaining(tvb, offset); proto_tree_add_uint_format(slsk_tree, hf_slsk_integer, tvb, 0, 0, 0, "(uncompression failed !)"); } else { proto_item *ti2 = proto_tree_add_item(slsk_tree, hf_slsk_compr_packet, tvb, offset, -1, FALSE); proto_tree *slsk_compr_packet_tree = proto_item_add_subtree(ti2, ett_slsk_compr_packet); proto_tree_add_uint_format(slsk_tree, hf_slsk_integer, tvb, offset, tvb_length_remaining(tvb, offset), 0, "( compressed packet length: %d)", comprlen); uncomprlen = tvb_length_remaining(uncompr_tvb, 0); proto_tree_add_uint_format(slsk_tree, hf_slsk_integer, tvb, offset, tvb_length_remaining(tvb, offset), 0, "(uncompressed packet length: %d)", uncomprlen); /* Dissects the uncompressed tvbuffer */ uncompr_tvb_offset = 0; if (check_slsk_format(uncompr_tvb, uncompr_tvb_offset, "i*")) { i=0; j = tvb_get_letohl(uncompr_tvb, uncompr_tvb_offset); proto_tree_add_uint_format(slsk_compr_packet_tree, hf_slsk_integer, uncompr_tvb, 0, 0, tvb_get_letohl(uncompr_tvb, uncompr_tvb_offset), "Number of directories: %d", tvb_get_letohl(uncompr_tvb, uncompr_tvb_offset)); uncompr_tvb_offset += 4; while (i