c1c3ed077ab44f93676eff06ef4c15b827f84af3
[obnox/wireshark/wip.git] / epan / dissectors / packet-edonkey.c
1 /* packet-edonkey.c
2  * Routines for edonkey dissection
3  * Copyright 2003, Xuan Zhang <xz@aemail4u.com>
4  * Copyright 2007, Stefano Picerno <stefano.picerno@gmail.com>
5  * Copyright 2008, Stefan Monhof <stefan.monhof@stud.uni-due.de>
6  *
7  * eDonkey dissector based on protocol descriptions from mldonkey:
8  *  http://savannah.nongnu.org/download/mldonkey/docs/Edonkey-Overnet/edonkey-protocol.txt
9  *  http://savannah.nongnu.org/download/mldonkey/docs/Edonkey-Overnet/overnet-protocol.txt
10  *
11  * Kademlia dissector based on source code inspection of aMule 2.1.3 and eMule 0.48a
12  * Modified and added on the basis of information and names from the eMule 0.49a source code
13  *
14  * $Id$
15  *
16  * Wireshark - Network traffic analyzer
17  * By Gerald Combs <gerald@wireshark.org>
18  * Copyright 1998 Gerald Combs
19  *
20  * This program is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU General Public License
22  * as published by the Free Software Foundation; either version 2
23  * of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <stdlib.h>
40
41 #include <glib.h>
42
43 #include <epan/packet.h>
44 #include <epan/prefs.h>
45 #include <epan/emem.h>
46 #include "packet-edonkey.h"
47 #include "packet-tcp.h"
48
49 static int proto_edonkey = -1;
50
51 static int hf_edonkey_message  = -1;
52 static int hf_edonkey_protocol = -1;
53 static int hf_edonkey_message_length = -1;
54 static int hf_edonkey_message_type = -1;
55 static int hf_edonkey_client_hash = -1;
56 static int hf_edonkey_server_hash = -1;
57 static int hf_edonkey_file_hash = -1;
58 static int hf_edonkey_client_id = -1;
59 static int hf_edonkey_metatag_namesize = -1;
60 static int hf_edonkey_metatag_type = -1;
61 static int hf_edonkey_metatag = -1;
62 static int hf_edonkey_metatag_name = -1;
63 static int hf_edonkey_metatag_id = -1;
64 static int hf_edonkey_search = -1;
65 static int hf_edonkey_ip = -1;
66 static int hf_edonkey_port = -1;
67 static int hf_edonkey_hash = -1;
68 static int hf_edonkey_part_count = -1;
69 static int hf_edonkey_file_status = -1;
70 static int hf_edonkey_directory = -1;
71 static int hf_edonkey_string = -1;
72 static int hf_edonkey_string_length = -1;
73 static int hf_edonkey_fileinfo = -1;
74 static int hf_edonkey_clientinfo = -1;
75 static int hf_edonkey_serverinfo = -1;
76 static int hf_emule_aich_partnum = -1;
77 static int hf_emule_aich_root_hash = -1;
78 static int hf_emule_aich_hash_entry = -1;
79 static int hf_emule_aich_hash_id = -1;
80 static int hf_emule_aich_hash = -1;
81 static int hf_emule_multipacket_entry = -1;
82 static int hf_emule_multipacket_opcode = -1;
83 static int hf_emule_source_count = -1;
84 static int hf_emule_zlib = -1;
85 static int hf_emule_public_key = -1;
86 static int hf_emule_signature = -1;
87 static int hf_emule_sourceOBFU = -1;
88 static int hf_overnet_peer = -1;
89
90 static int hf_edonkey_unparsed_data_length = -1;
91 static int hf_kademlia = -1;
92 static int hf_kademlia_search_condition = -1;
93 static int hf_kademlia_search_condition_argument_uint32 = -1;
94 static int hf_kademlia_search_condition_argument_uint64 = -1;
95 static int hf_kademlia_unparsed_data_length = -1;
96 static int hf_kademlia_peer = -1;
97 static int hf_kademlia_peer_id = -1;
98 static int hf_kademlia_hash = -1;
99 static int hf_kademlia_file_id = -1;
100 static int hf_kademlia_keyword_hash = -1;
101 static int hf_kademlia_recipients_id = -1;
102 static int hf_kademlia_sender_id = -1;
103 static int hf_kademlia_target_id = -1;
104 static int hf_kademlia_distance = -1;
105 static int hf_kademlia_version = -1;
106 static int hf_kademlia_peertype = -1;
107 static int hf_kademlia_tag_float = -1;
108 static int hf_kademlia_tag_uint64 = -1;
109 static int hf_kademlia_tag_uint32 = -1;
110 static int hf_kademlia_tag_ipv4 = -1;
111 static int hf_kademlia_tag_uint16 = -1;
112 static int hf_kademlia_tag_uint8 = -1;
113 static int hf_kademlia_tag_hash = -1;
114 static int hf_kademlia_tag_bsob = -1;
115 static int hf_kademlia_tag_string = -1;
116 static int hf_kademlia_udp_port = -1;
117 static int hf_kademlia_tcp_port = -1;
118 static int hf_kademlia_ip = -1;
119 static int hf_kademlia_tag_name = -1;
120 static int hf_kademlia_tag_name_length = -1;
121 static int hf_kademlia_tag_type = -1;
122 static int hf_kademlia_request_type = -1;
123
124 static gint ett_kademlia_tag = -1;
125 static gint ett_edonkey_listitem = -1;
126 static gint ett_kademlia_search_expression = -1;
127 static gint ett_edonkey = -1;
128 static gint ett_edonkey_message = -1;
129 static gint ett_edonkey_metatag = -1;
130 static gint ett_edonkey_search = -1;
131 static gint ett_edonkey_fileinfo = -1;
132 static gint ett_edonkey_serverinfo = -1;
133 static gint ett_edonkey_clientinfo = -1;
134 static gint ett_emule_aichhash = -1;
135 static gint ett_emule_multipacket = -1;
136 static gint ett_emule_zlib = -1;
137 static gint ett_overnet_peer = -1;
138 static gint ett_emule_sourceOBFU = -1;
139
140 /* desegmentation of eDonkey over TCP */
141 static gboolean edonkey_desegment = TRUE;
142
143 static const value_string kademlia_msgs[] = {
144     { KADEMLIA2_BOOTSTRAP_REQ        ,"KADEMLIA2_BOOTSTRAP_REQ"      },
145     { KADEMLIA2_BOOTSTRAP_RES        ,"KADEMLIA2_BOOTSTRAP_RES"      },
146     { KADEMLIA2_HELLO_REQ            ,"KADEMLIA2_HELLO_REQ"          },
147     { KADEMLIA2_HELLO_RES            ,"KADEMLIA2_HELLO_RES"          },
148     { KADEMLIA2_PUBLISH_KEY_REQ      ,"KADEMLIA2_PUBLISH_KEY_REQ"    },
149     { KADEMLIA2_PUBLISH_NOTES_REQ    ,"KADEMLIA2_PUBLISH_NOTES_REQ"  },
150     { KADEMLIA2_PUBLISH_RES          ,"KADEMLIA2_PUBLISH_RES"        },
151     { KADEMLIA2_PUBLISH_SOURCE_REQ   ,"KADEMLIA2_PUBLISH_SOURCE_REQ" },
152     { KADEMLIA2_REQ                  ,"KADEMLIA2_REQ"                },
153     { KADEMLIA2_RES                  ,"KADEMLIA2_RES"                },
154     { KADEMLIA2_SEARCH_KEY_REQ       ,"KADEMLIA2_SEARCH_KEY_REQ"     },
155     { KADEMLIA2_SEARCH_NOTES_REQ     ,"KADEMLIA2_SEARCH_NOTES_REQ"   },
156     { KADEMLIA2_SEARCH_RES           ,"KADEMLIA2_SEARCH_RES"         },
157     { KADEMLIA2_SEARCH_SOURCE_REQ    ,"KADEMLIA2_SEARCH_SOURCE_REQ"  },
158     { KADEMLIA_BOOTSTRAP_REQ         ,"KADEMLIA_BOOTSTRAP_REQ"       },
159     { KADEMLIA_BOOTSTRAP_RES         ,"KADEMLIA_BOOTSTRAP_RES"       },
160     { KADEMLIA_CALLBACK_REQ          ,"KADEMLIA_CALLBACK_REQ"        },
161     { KADEMLIA_FINDBUDDY_REQ         ,"KADEMLIA_FINDBUDDY_REQ"       },
162     { KADEMLIA_FINDBUDDY_RES         ,"KADEMLIA_FINDBUDDY_RES"       },
163     { KADEMLIA_FIREWALLED_ACK_RES    ,"KADEMLIA_FIREWALLED_ACK_RES"  },
164     { KADEMLIA_FIREWALLED_REQ        ,"KADEMLIA_FIREWALLED_REQ"      },
165     { KADEMLIA_FIREWALLED_RES        ,"KADEMLIA_FIREWALLED_RES"      },
166     { KADEMLIA_HELLO_REQ             ,"KADEMLIA_HELLO_REQ"           },
167     { KADEMLIA_HELLO_RES             ,"KADEMLIA_HELLO_RES"           },
168     { KADEMLIA_PUBLISH_NOTES_REQ     ,"KADEMLIA_PUBLISH_NOTES_REQ"   },
169     { KADEMLIA_PUBLISH_NOTES_RES     ,"KADEMLIA_PUBLISH_NOTES_RES"   },
170     { KADEMLIA_PUBLISH_REQ           ,"KADEMLIA_PUBLISH_REQ"         },
171     { KADEMLIA_PUBLISH_RES           ,"KADEMLIA_PUBLISH_RES"         },
172     { KADEMLIA_REQ                   ,"KADEMLIA_REQ"                 },
173     { KADEMLIA_RES                   ,"KADEMLIA_RES"                 },
174     { KADEMLIA_SEARCH_NOTES_REQ      ,"KADEMLIA_SEARCH_NOTES_REQ"    },
175     { KADEMLIA_SEARCH_NOTES_RES      ,"KADEMLIA_SEARCH_NOTES_RES"    },
176     { KADEMLIA_SEARCH_REQ            ,"KADEMLIA_SEARCH_REQ"          },
177     { KADEMLIA_SEARCH_RES            ,"KADEMLIA_SEARCH_RES"          },
178     { 0, NULL }
179 };
180
181 static const value_string kademlia_tag_types[] = {
182     { KADEMLIA_TAGTYPE_HASH          ,"TAGTYPE_HASH"      },
183     { KADEMLIA_TAGTYPE_STRING        ,"TAGTYPE_STRING"    },
184     { KADEMLIA_TAGTYPE_UINT32        ,"TAGTYPE_UINT32"    },
185     { KADEMLIA_TAGTYPE_FLOAT32       ,"TAGTYPE_FLOAT32"   },
186     { KADEMLIA_TAGTYPE_BOOL          ,"TAGTYPE_BOOL"      },
187     { KADEMLIA_TAGTYPE_BOOLARRAY     ,"TAGTYPE_BOOLARRAY" },
188     { KADEMLIA_TAGTYPE_BLOB          ,"TAGTYPE_BLOB"      },
189     { KADEMLIA_TAGTYPE_UINT16        ,"TAGTYPE_UINT16"    },
190     { KADEMLIA_TAGTYPE_UINT8         ,"TAGTYPE_UINT8"     },
191     { KADEMLIA_TAGTYPE_BSOB          ,"TAGTYPE_BSOB"      },
192     { KADEMLIA_TAGTYPE_UINT64        ,"TAGTYPE_UINT64"    },
193
194     { KADEMLIA_TAGTYPE_STR1          ,"TAGTYPE_STR1"      },
195     { KADEMLIA_TAGTYPE_STR2          ,"TAGTYPE_STR2"      },
196     { KADEMLIA_TAGTYPE_STR3          ,"TAGTYPE_STR3"      },
197     { KADEMLIA_TAGTYPE_STR4          ,"TAGTYPE_STR4"      },
198     { KADEMLIA_TAGTYPE_STR5          ,"TAGTYPE_STR5"      },
199     { KADEMLIA_TAGTYPE_STR6          ,"TAGTYPE_STR6"      },
200     { KADEMLIA_TAGTYPE_STR7          ,"TAGTYPE_STR7"      },
201     { KADEMLIA_TAGTYPE_STR8          ,"TAGTYPE_STR8"      },
202     { KADEMLIA_TAGTYPE_STR9          ,"TAGTYPE_STR9"      },
203     { KADEMLIA_TAGTYPE_STR10         ,"TAGTYPE_STR10"     },
204     { KADEMLIA_TAGTYPE_STR11         ,"TAGTYPE_STR11"     },
205     { KADEMLIA_TAGTYPE_STR12         ,"TAGTYPE_STR12"     },
206     { KADEMLIA_TAGTYPE_STR13         ,"TAGTYPE_STR13"     },
207     { KADEMLIA_TAGTYPE_STR14         ,"TAGTYPE_STR14"     },
208     { KADEMLIA_TAGTYPE_STR15         ,"TAGTYPE_STR15"     },
209     { KADEMLIA_TAGTYPE_STR16         ,"TAGTYPE_STR16"     },
210     { KADEMLIA_TAGTYPE_STR17         ,"TAGTYPE_STR17"     },
211     { KADEMLIA_TAGTYPE_STR18         ,"TAGTYPE_STR18"     },
212     { KADEMLIA_TAGTYPE_STR19         ,"TAGTYPE_STR19"     },
213     { KADEMLIA_TAGTYPE_STR20         ,"TAGTYPE_STR20"     },
214     { KADEMLIA_TAGTYPE_STR21         ,"TAGTYPE_STR21"     },
215     { KADEMLIA_TAGTYPE_STR22         ,"TAGTYPE_STR22"     },
216     { 0, NULL }
217 };
218
219 static const value_string kademlia_tags[] = {
220     { KADEMLIA_TAG_BUDDYHASH         ,"TAG_BUDDYHASH"    },
221     { KADEMLIA_TAG_CLIENTLOWID       ,"TAG_CLIENTLOWID"  },
222     { KADEMLIA_TAG_COLLECTION        ,"TAG_COLLECTION"   },
223     { KADEMLIA_TAG_COPIED            ,"TAG_COPIED"       },
224     { KADEMLIA_TAG_DESCRIPTION       ,"TAG_DESCRIPTION"  },
225     { KADEMLIA_TAG_ENCRYPTION        ,"TAG_ENCRYPTION"   },
226     { KADEMLIA_TAG_FAIL              ,"TAG_FAIL"         },
227     { KADEMLIA_TAG_FILECOMMENT       ,"TAG_FILECOMMENT"  },
228     { KADEMLIA_TAG_FILE_COUNT        ,"TAG_FILE_COUNT"   },
229     { KADEMLIA_TAG_FILEFORMAT        ,"TAG_FILEFORMAT"   },
230     { KADEMLIA_TAG_FILENAME          ,"TAG_FILENAME"     },
231     { KADEMLIA_TAG_FILERATING        ,"TAG_FILERATING"   },
232     { KADEMLIA_TAG_FILESIZE_HI       ,"TAG_FILESIZE_HI"  },
233     { KADEMLIA_TAG_FILESIZE          ,"TAG_FILESIZE"     },
234     { KADEMLIA_TAG_FILETYPE          ,"TAG_FILETYPE"     },
235     { KADEMLIA_TAG_GAP_END           ,"TAG_GAP_END"      },
236     { KADEMLIA_TAG_GAP_START         ,"TAG_GAP_START"    },
237     { KADEMLIA_TAG_IP_ADDRESS        ,"TAG_IP_ADDRESS"   },
238     { KADEMLIA_TAG_MEDIA_ALBUM       ,"TAG_MEDIA_ALBUM"  },
239     { KADEMLIA_TAG_MEDIA_ARTIST      ,"TAG_MEDIA_ARTIST" },
240     { KADEMLIA_TAG_MEDIA_BITRATE     ,"TAG_MEDIA_BITRATE"},
241     { KADEMLIA_TAG_MEDIA_CODEC       ,"TAG_MEDIA_CODEC"  },
242     { KADEMLIA_TAG_MEDIA_LENGTH      ,"TAG_MEDIA_LENGTH" },
243     { KADEMLIA_TAG_MEDIA_TITLE       ,"TAG_MEDIA_TITLE"  },
244     { KADEMLIA_TAG_PART_HASH         ,"TAG_PART_HASH"    },
245     { KADEMLIA_TAG_PART_PATH         ,"TAG_PART_PATH"    },
246     { KADEMLIA_TAG_PARTS             ,"TAG_PARTS"        },
247     { KADEMLIA_TAG_PERMISSIONS       ,"TAG_PERMISSIONS"  },
248     { KADEMLIA_TAG_PING              ,"TAG_PING"         },
249     { KADEMLIA_TAG_PORT              ,"TAG_PORT"         },
250     { KADEMLIA_TAG_PREFERENCE        ,"TAG_PREFERENCE"   },
251     { KADEMLIA_TAG_PRIORITY          ,"TAG_PRIORITY"     },
252     { KADEMLIA_TAG_QTIME             ,"TAG_QTIME"        },
253     { KADEMLIA_TAG_SERVERIP          ,"TAG_SERVERIP"     },
254     { KADEMLIA_TAG_SERVERPORT        ,"TAG_SERVERPORT"   },
255     { KADEMLIA_TAG_SOURCEIP          ,"TAG_SOURCEIP"     },
256     { KADEMLIA_TAG_SOURCEPORT        ,"TAG_SOURCEPORT"   },
257     { KADEMLIA_TAG_SOURCES           ,"TAG_SOURCES"      },
258     { KADEMLIA_TAG_SOURCETYPE        ,"TAG_SOURCETYPE"   },
259     { KADEMLIA_TAG_SOURCEUPORT       ,"TAG_SOURCEUPORT"  },
260     { KADEMLIA_TAG_STATUS            ,"TAG_STATUS"       },
261     { KADEMLIA_TAG_TEMPFILE          ,"TAG_TEMPFILE"     },
262     { KADEMLIA_TAG_USER_COUNT        ,"TAG_USER_COUNT"   },
263     { KADEMLIA_TAG_VERSION           ,"TAG_VERSION"      },
264
265     { 0, NULL }
266 };
267
268 static const value_string edonkey_protocols[] = {
269     { EDONKEY_PROTO_EDONKEY,             "eDonkey"                  },
270     { EDONKEY_PROTO_EMULE_EXT,           "eMule Extensions"         },
271     { EDONKEY_PROTO_EMULE_COMP,          "eMule Compressed"         },
272     { EDONKEY_PROTO_KADEMLIA,            "Kademlia"                 },
273     { EDONKEY_PROTO_KADEMLIA_COMP,       "Kademlia Compressed"      },
274     { EDONKEY_PROTO_ADU_KADEMLIA,        "KAdu"                     },
275     { EDONKEY_PROTO_ADU_KADEMLIA_COMP,   "KAdu Compressed"          },
276     { 0,                                 NULL                       }
277 };
278
279 static const value_string edonkey_tcp_msgs[] = {
280     { EDONKEY_MSG_HELLO,                 "Hello"                    },
281     { EDONKEY_MSG_BAD_PROTO,             "Bad Proto"                },
282     { EDONKEY_MSG_GET_SERVER_LIST,       "Get Server List"          },
283     { EDONKEY_MSG_OFFER_FILES,           "Offer Files"              },
284     { EDONKEY_MSG_SEARCH_FILES,          "Search Files"             },
285     { EDONKEY_MSG_DISCONNECT,            "Disconnect"               },
286     { EDONKEY_MSG_GET_SOURCES,           "Get Sources"              },
287     { EDONKEY_MSG_GET_SOURCES_OBFU,      "Get Sources Obfuscation"  },
288     { EDONKEY_MSG_SEARCH_USER,           "Search User"              },
289     { EDONKEY_MSG_CLIENT_CB_REQ,         "Client Callback Request"  },
290     { EDONKEY_MSG_MORE_RESULTS,          "More Results"             },
291     { EDONKEY_MSG_SERVER_LIST,           "Server List"              },
292     { EDONKEY_MSG_SEARCH_FILE_RESULTS,   "Search File Results"      },
293     { EDONKEY_MSG_SERVER_STATUS,         "Server Status"            },
294     { EDONKEY_MSG_SERVER_CB_REQ,         "Server Callback Request"  },
295     { EDONKEY_MSG_CALLBACK_FAIL,         "Callback Fail"            },
296     { EDONKEY_MSG_SERVER_MESSAGE,        "Server Message"           },
297     { EDONKEY_MSG_ID_CHANGE,             "ID Change"                },
298     { EDONKEY_MSG_SERVER_INFO_DATA,      "Server Info Data"         },
299     { EDONKEY_MSG_FOUND_SOURCES,         "Found Sources"            },
300     { EDONKEY_MSG_FOUND_SOURCES_OBFU,    "Found Sources Obfuscation"},
301     { EDONKEY_MSG_SEARCH_USER_RESULTS,   "Search User Results"      },
302     { EDONKEY_MSG_SENDING_PART,          "Sending Part"             },
303     { EDONKEY_MSG_REQUEST_PARTS,         "Request Parts"            },
304     { EDONKEY_MSG_NO_SUCH_FILE,          "No Such File"             },
305     { EDONKEY_MSG_END_OF_DOWNLOAD,       "End of Download"          },
306     { EDONKEY_MSG_VIEW_FILES,            "View Files"               },
307     { EDONKEY_MSG_VIEW_FILES_ANSWER,     "View Files Answer"        },
308     { EDONKEY_MSG_HELLO_ANSWER,          "Hello Answer"             },
309     { EDONKEY_MSG_NEW_CLIENT_ID,         "New Client ID"            },
310     { EDONKEY_MSG_CLIENT_MESSAGE,        "Client Message"           },
311     { EDONKEY_MSG_FILE_STATUS_REQUEST,   "File Status Request"      },
312     { EDONKEY_MSG_FILE_STATUS,           "File Status"              },
313     { EDONKEY_MSG_HASHSET_REQUEST,       "Hashset Request"          },
314     { EDONKEY_MSG_HASHSET_ANSWER,        "Hashset Answer"           },
315     { EDONKEY_MSG_SLOT_REQUEST,          "Slot Request"             },
316     { EDONKEY_MSG_SLOT_GIVEN,            "Slot Given"               },
317     { EDONKEY_MSG_SLOT_RELEASE,          "Slot Release"             },
318     { EDONKEY_MSG_SLOT_TAKEN,            "Slot Taken"               },
319     { EDONKEY_MSG_FILE_REQUEST,          "File Request"             },
320     { EDONKEY_MSG_FILE_REQUEST_ANSWER,   "File Request Answer"      },
321     { EDONKEY_MSG_GET_SHARED_DIRS,       "Get Shared Directories"   },
322     { EDONKEY_MSG_GET_SHARED_FILES,      "Get Shared Files"         },
323     { EDONKEY_MSG_SHARED_DIRS,           "Shared Directories"       },
324     { EDONKEY_MSG_SHARED_FILES,          "Shared Files"             },
325     { EDONKEY_MSG_SHARED_DENIED,         "Shared Denied"            },
326     { 0,                                 NULL                       }
327 };
328
329 static const value_string emule_tcp_msgs[] = {
330     { EMULE_MSG_HELLO,                   "Hello"                    },
331     { EMULE_MSG_HELLO_ANSWER,            "Hello Answer"             },
332     { EMULE_MSG_DATA_COMPRESSED,         "Data Compressed"          },
333     { EMULE_MSG_QUEUE_RANKING,           "Queue Ranking"            },
334     { EMULE_MSG_FILE_DESC,               "File Description"         },
335     { EMULE_MSG_SOURCES_REQUEST,         "Sources Request"          },
336     { EMULE_MSG_SOURCES_ANSWER,          "Sources Answer"           },
337     { EMULE_MSG_SIGNATURE,               "Signature"                },
338     { EMULE_MSG_PUBLIC_KEY,              "Public Key"                  },
339     { EMULE_MSG_SEC_IDENT_STATE,         "Second Identification State" },
340     { EMULE_MSG_MULTIPACKET,             "MultiPacket"              },
341     { EMULE_MSG_MULTIPACKET_EXT,         "MultiPacketExt"           },
342     { EMULE_MSG_MULTIPACKET_ANSWER,      "MultiPacket Answer"       },
343     { EMULE_MSG_CALLBACK,                "Callback"                 },
344     { EMULE_MSG_AICH_REQUEST,            "AICH Hashset Request"     },
345     { EMULE_MSG_AICH_ANSWER,             "AICH Hashset Answer"      },
346     { EMULE_MSG_AICHFILEHASH_ANSWER,     "AICH Master Hash Request" },
347     { EMULE_MSG_AICHFILEHASH_REQUEST,    "AICH Master Hash Answer"  },
348     { EMULE_MSG_DATA_COMPRESSED_64,      "Data Compressed (64bit)"  },
349     { EMULE_MSG_SENDING_PART_64,         "Sending Part (64bit)"     },
350     { EMULE_MSG_REQUEST_PARTS_64,        "Request Parts (64bit)"    },
351     { 0,                                 NULL                       }
352 };
353
354 static const value_string edonkey_udp_msgs[] = {
355     { EDONKEY_MSG_UDP_SERVER_STATUS_REQUEST,    "Server Status Request"    },
356     { EDONKEY_MSG_UDP_SERVER_STATUS,            "Server Status"            },
357     { EDONKEY_MSG_UDP_SEARCH_FILE,              "Search File"              },
358     { EDONKEY_MSG_UDP_SEARCH_FILE_RESULTS,      "Search File Results"      },
359     { EDONKEY_MSG_UDP_GET_SOURCES,              "Get Sources"              },
360     { EDONKEY_MSG_UDP_FOUND_SOURCES,            "Found Sources"            },
361     { EDONKEY_MSG_UDP_CALLBACK_REQUEST,         "Callback Request"         },
362     { EDONKEY_MSG_UDP_CALLBACK_FAIL,            "Callback Fail"            },
363     { EDONKEY_MSG_UDP_SERVER_LIST,              "Server List"              },
364     { EDONKEY_MSG_UDP_GET_SERVER_INFO,          "Get Server Info"          },
365     { EDONKEY_MSG_UDP_SERVER_INFO,              "Server Info"              },
366     { EDONKEY_MSG_UDP_GET_SERVER_LIST,          "Get Server List"          },
367
368     /* eMule Extensions */
369     { EMULE_MSG_UDP_REASKFILEPING,              "Reask File Ping"          },
370     { EMULE_MSG_UDP_REASKACK,                   "Reask ACK"                },
371     { EMULE_MSG_UDP_FILE_NOT_FOUND,             "File not found"           },
372     { EMULE_MSG_UDP_QUEUE_FULL,                 "Queue Full"               },
373
374     /* Overnet Extensions */
375     { OVERNET_MSG_UDP_CONNECT,                  "Connect"                  },
376     { OVERNET_MSG_UDP_CONNECT_REPLY,            "Connect Reply"            },
377     { OVERNET_MSG_UDP_PUBLICIZE,                "Publicize"                },
378     { OVERNET_MSG_UDP_PUBLICIZE_ACK,            "Publicize ACK"            },
379     { OVERNET_MSG_UDP_SEARCH,                   "Search"                   },
380     { OVERNET_MSG_UDP_SEARCH_NEXT,              "Search Next"              },
381     { OVERNET_MSG_UDP_SEARCH_INFO,              "Search Info"              },
382     { OVERNET_MSG_UDP_SEARCH_RESULT,            "Search Result"            },
383     { OVERNET_MSG_UDP_SEARCH_END,               "Search End"               },
384     { OVERNET_MSG_UDP_PUBLISH,                  "Publish"                  },
385     { OVERNET_MSG_UDP_PUBLISH_ACK,              "Publish ACK"              },
386     { OVERNET_MSG_UDP_IDENTIFY_REPLY,           "Identify Reply"           },
387     { OVERNET_MSG_UDP_IDENTIFY_ACK,             "Identify ACK"             },
388     { OVERNET_MSG_UDP_FIREWALL_CONNECTION,      "Firewall Connection"      },
389     { OVERNET_MSG_UDP_FIREWALL_CONNECTION_ACK,  "Firewall Connection ACK"  },
390     { OVERNET_MSG_UDP_FIREWALL_CONNECTION_NACK, "Firewall Connection NACK" },
391     { OVERNET_MSG_UDP_IP_QUERY,                 "IP Query"                 },
392     { OVERNET_MSG_UDP_IP_QUERY_ANSWER,          "IP Query Answer"          },
393     { OVERNET_MSG_UDP_IP_QUERY_END,             "IP Query End"             },
394     { OVERNET_MSG_UDP_IDENTIFY,                 "Identify"                 },
395     { 0,                                        NULL                       }
396 };
397
398 static const value_string edonkey_special_tags[] = {
399     { EDONKEY_STAG_NAME,                "Name"                      },
400     { EDONKEY_STAG_SIZE,                "Size"                      },
401     { EDONKEY_STAG_TYPE,                "Type"                      },
402     { EDONKEY_STAG_FORMAT,              "Format"                    },
403     { EDONKEY_STAG_COLLECTION,          "Collection"                },
404     { EDONKEY_STAG_PART_PATH,           "Part Path"                 },
405     { EDONKEY_STAG_PART_HASH,           "Part Hash"                 },
406     { EDONKEY_STAG_COPIED,              "Copied"                    },
407     { EDONKEY_STAG_GAP_START,           "Gap Start"                 },
408     { EDONKEY_STAG_GAP_END,             "Gap End"                   },
409     { EDONKEY_STAG_DESCRIPTION,         "Description"               },
410     { EDONKEY_STAG_PING,                "Ping"                      },
411     { EDONKEY_STAG_FAIL,                "Fail"                      },
412     { EDONKEY_STAG_PREFERENCE,          "Preference"                },
413     { EDONKEY_STAG_PORT,                "Port"                      },
414     { EDONKEY_STAG_IP,                  "IP"                        },
415     { EDONKEY_STAG_VERSION,             "Version"                   },
416     { EDONKEY_STAG_TEMPFILE,            "Temporary File"            },
417     { EDONKEY_STAG_PRIORITY,            "Priority"                  },
418     { EDONKEY_STAG_STATUS,              "Status"                    },
419     { EDONKEY_STAG_AVAILABILITY,        "Availability"              },
420     { EDONKEY_STAG_QTIME,               "Queue Time"                },
421     { EDONKEY_STAG_PARTS,               "Parts"                     },
422     { EDONKEY_STAG_MOD_VERSION,         "Mod Version"               },
423     { EMULE_STAG_COMPRESSION,           "Compression"               },
424     { EMULE_STAG_UDP_CLIENT_PORT,       "UDP Client Port"           },
425     { EMULE_STAG_UDP_VERSION,           "UDP Version"               },
426     { EMULE_STAG_SOURCE_EXCHANGE,       "Source Exchange"           },
427     { EMULE_STAG_COMMENTS,              "Comments"                  },
428     { EMULE_STAG_EXTENDED_REQUEST,      "Extended Request"          },
429     { EMULE_STAG_COMPATIBLE_CLIENT,     "Compatible Client"         },
430     { EMULE_STAG_COMPLETE_SOURCES,      "Complete Sources"          },
431     { EMULE_STAG_SIZE_HI,               "Size (High Byte)"          },
432     { EMULE_STAG_SERVER_VERSION,        "Server Version"            },
433     { EMULE_STAG_COMPAT_OPTIONS1,       "Compatible Options"        },
434     { EMULE_STAG_UDPPORTS,              "UDP Ports"                 },
435     { EMULE_STAG_MISCOPTIONS1,          "Misc Options 1"            },
436     { EMULE_STAG_VERSION,               "eMule Version"             },
437     { EMULE_STAG_BUDDYIP,               "Buddy IP"                  },
438     { EMULE_STAG_BUDDYUDP,              "Buddy UDP"                 },
439     { EMULE_STAG_MISCOPTIONS2,          "Misc Options 2"            },
440     { 0,                                NULL                        }
441 };
442
443 static const value_string edonkey_search_ops[] = {
444     { EDONKEY_SEARCH_AND,               "AND"                       },
445     { EDONKEY_SEARCH_OR,                "OR"                        },
446     { EDONKEY_SEARCH_ANDNOT,            "AND NOT"                   },
447     { 0,                                NULL                        }
448 };
449
450 static const value_string edonkey_search_conds[] = {
451     { EDONKEY_SEARCH_MIN,               "MIN"                       },
452     { EDONKEY_SEARCH_MAX,               "MAX"                       },
453     { 0,                                NULL                        }
454 };
455
456 static const value_string kademlia_search_conds[] = {
457     { 0, "="  },
458     { 1, ">"  },
459     { 2, "<"  },
460     { 3, ">=" },
461     { 4, "<=" },
462     { 5, "<>" },
463     { 0, NULL }
464 };
465
466 static const value_string kademlia_versions[] = {
467     { KADEMLIA_VERSION1_46c,     " (eMule <= 0.46c or compatibles)"   },
468     { KADEMLIA_VERSION2_47a,     " (eMule 0.47a or compatibles)"      },
469     { KADEMLIA_VERSION3_47b,     " (eMule 0.47b or compatibles)"      },
470     { KADEMLIA_VERSION5_48a,     " (eMule 0.48a or compatibles)"      },
471     { KADEMLIA_VERSION6_49aBETA, " (eMule 0.49aBETA1 or compatibles)" },
472     { KADEMLIA_VERSION7_49a,     " (eMule 0.49a or compatibles)"      },
473     { 0,                         NULL                                 }
474 };
475
476 static const value_string kademlia_parameter[] = {
477     { KADEMLIA_FIND_VALUE,       " (Find Value)"   },
478     { KADEMLIA_STORE,            " (Store)"        },
479     { KADEMLIA_FIND_NODE,        " (Find Node)"    },
480     { 0,                         NULL              }
481 };
482
483 static const value_string kademlia_tag_sourcetype[] = {
484     { 1,                         "HighID Source"                                          },
485     { 3,                         "Firewalled Kad Source"                                  },
486     { 4,                         ">4GB file HighID Source"                                },
487     { 5,                         ">4GB file Firewalled Kad Source"                        },
488     { 6,                         "Firewalled Source with Direct Callback (supports >4GB)" },
489     { 0,                         NULL                                                     }
490 };
491
492 static const value_string kademlia_tag_encryption[] = {
493     { 1,                         "Supports Crypt Layer"                                              },
494     { 2,                         "Requests Crypt Layer"                                              },
495     { 3,                         "Supports & Requests Crypt Layer"                                   },
496     { 4,                         "Requires Crypt Layer"                                              },
497     { 5,                         "Supports & Requires Crypt Layer"                                   },
498     { 6,                         "Requests & Requires Crypt Layer"                                   },
499     { 7,                         "Supports, Requests & Requires Crypt Layer"                         },
500     { 8,                         "Direct UDP Callback"                                               },
501     { 9,                         "Supports Crypt Layer; Direct UDP Callback"                         },
502     { 10,                        "Requests Crypt Layer; Direct UDP Callback"                         },
503     { 11,                        "Supports & Requests Crypt Layer; Direct UDP Callback"              },
504     { 12,                        "Requires Crypt Layer; Direct UDP Callback"                         },
505     { 13,                        "Supports & Requires Crypt Layer; Direct UDP Callback"              },
506     { 14,                        "Requests & Requires Crypt Layer; Direct UDP Callback"              },
507     { 15,                        "Supports, Requests & Requires Crypt Layer; Direct UDP Callback"    },
508     { 0,                         NULL                                                                }
509 };
510
511 /* Dissects a generic eDonkey list */
512 static int dissect_edonkey_list(tvbuff_t *tvb, packet_info *pinfo _U_,
513                                 int offset,  proto_tree *tree,
514                                 int listnum_length, const char* listdesc,
515                                 int  (*item_dissector)(tvbuff_t  *, packet_info *, int, proto_tree *))
516 {
517     /* <List> ::= <List Size> <Item>* */
518     guint32 listnum, i;
519     proto_tree *subtree;
520     proto_item* ti;
521     proto_item* list_ti;
522     int list_start_offset;
523
524     list_start_offset = offset;
525
526     switch (listnum_length) {
527         case -1:
528         case 1:
529             listnum = tvb_get_guint8(tvb, offset);
530             break;
531
532         case -2:
533             listnum = tvb_get_ntohs(tvb, offset);
534             break;
535
536         case 2:
537             listnum = tvb_get_letohs(tvb, offset);
538             break;
539
540         case 4:
541             listnum = tvb_get_letohl(tvb, offset);
542             break;
543
544         case -4:
545             listnum = tvb_get_ntohl(tvb, offset);
546             break;
547
548         default:
549             /* Not Supported */
550             return offset;
551     }
552     if (listnum_length < 0)
553         listnum_length = -listnum_length;
554
555     /* keep the tree item object, its length will be set at the end of the function */
556     list_ti = proto_tree_add_text(tree, tvb, offset, listnum_length, "%s List Size: %u", listdesc, listnum);
557
558     offset+= listnum_length;
559
560     for (i=0; i<listnum; i++)
561     {
562         int item_start_offset;
563
564         item_start_offset = offset;
565         ti = proto_tree_add_text( tree, tvb, item_start_offset, 1, "%s[%u/%u]", listdesc, i+1, listnum);
566
567         subtree = proto_item_add_subtree(ti, ett_edonkey_listitem );
568
569         /* dissect one list element */
570         offset = (*item_dissector)(tvb, pinfo, offset, subtree);
571         /* Set the container node length */
572         proto_item_set_len( ti, offset - item_start_offset );
573     }
574
575     /* Set the container node length */
576     proto_item_set_len(list_ti, offset - list_start_offset );
577     return offset;
578 }
579
580 static gint lookup_str_index(gchar* str, gint length, const value_string *vs)
581 {
582     gint i = 0;
583
584     if (str == NULL) return -1;
585
586     while (vs[i].strptr) {
587         if (g_ascii_strncasecmp(str, vs[i].strptr, length) == 0)
588             return i;
589         i++;
590     }
591
592     return -1;
593 }
594
595 static guint8 edonkey_metatag_name_get_type(tvbuff_t *tvb, gint start, gint length, guint8 special_tagtype)
596 {
597     guint8 *tag_name;
598
599     if (match_strval(special_tagtype, edonkey_special_tags) == NULL) {
600         gint idx;
601         tag_name = tvb_get_ephemeral_string(tvb, start, length);
602         idx = lookup_str_index(tag_name, length, edonkey_special_tags);
603         if (idx < 0)
604             return EDONKEY_STAG_UNKNOWN;
605         else return edonkey_special_tags[idx].value;
606     }
607     else return special_tagtype;
608
609 }
610
611 static proto_item* edonkey_tree_add_metatag_name(proto_tree *tree, tvbuff_t *tvb,
612                                                  gint start, gint length, guint8 special_tagtype)
613 {
614     const gchar *tag_name;
615     tag_name = match_strval(special_tagtype, edonkey_special_tags);
616     if (tag_name == NULL) {
617         return proto_tree_add_item(tree, hf_edonkey_metatag_name, tvb, start, length, ENC_ASCII|ENC_NA);
618     }
619     else {
620         return proto_tree_add_uint_format(tree, hf_edonkey_metatag_id, tvb, start, length,
621                                           special_tagtype, "Meta Tag Name: %s (0x%02x)",
622                                           tag_name, special_tagtype);
623     }
624 }
625
626 static int dissect_kademlia_search_condition_argument_uint64(tvbuff_t *tvb, packet_info *pinfo _U_,
627                                    int offset, proto_tree *tree)
628 {
629     proto_tree_add_item( tree, hf_kademlia_search_condition_argument_uint64, tvb, offset, 8, ENC_LITTLE_ENDIAN );
630     return offset + 8;
631 }
632
633 static int dissect_kademlia_search_condition_argument_uint32(tvbuff_t *tvb, packet_info *pinfo _U_,
634                                    int offset, proto_tree *tree)
635 {
636     proto_tree_add_item( tree, hf_kademlia_search_condition_argument_uint32, tvb, offset, 4, ENC_LITTLE_ENDIAN );
637     return offset + 4;
638 }
639
640 static int dissect_kademlia_search_condition(tvbuff_t *tvb, packet_info *pinfo _U_,
641                                    int offset, proto_tree *tree )
642 {
643     proto_item * ti;
644     guint16 value = tvb_get_guint8(tvb, offset);
645     ti = proto_tree_add_item( tree, hf_kademlia_search_condition, tvb, offset, 1, ENC_BIG_ENDIAN );
646     proto_item_append_text(ti, " [%s]", val_to_str( value, kademlia_search_conds, "Unknown") );
647
648     return offset + 1;
649 }
650
651 /* Dissects the eDonkey meta tag */
652 static int dissect_edonkey_metatag(tvbuff_t *tvb, packet_info *pinfo _U_,
653                                    int offset, proto_tree *tree)
654 {
655     /* <Meta Tag> ::= <Tag Type (guint8)> <Tag Name> <Tag> */
656     /* <Tag Name> ::= <Tag Name Size (guint16)> <Special Tag> || <String> */
657     /* <Tag Name> ::= <Special Tag> iff Tag Type had the top bit set */
658     proto_item *ti;
659     proto_tree *metatag_tree;
660     guint8 real_tag_type, tag_type, special_tagtype, trans_tagtype, tag_value_guint8;
661     guint16 tag_name_size, string_length, array_length, tag_value_guint16;
662     guint32 tag_length, blob_length, tag_value_guint32;
663     int tag_offset;
664
665     real_tag_type = tag_type = tvb_get_guint8(tvb, offset);
666     if (tag_type & EDONKEY_MTAG_SHORTNAME) {
667         real_tag_type &= ~EDONKEY_MTAG_SHORTNAME;
668         tag_name_size = 1;
669         special_tagtype = tvb_get_guint8(tvb, offset+1);
670         tag_length = 2;
671     } else {
672         tag_name_size = tvb_get_letohs(tvb, offset+1);
673         special_tagtype = tvb_get_guint8(tvb, offset+3);
674         tag_length = 3 + tag_name_size;
675     }
676
677     tag_offset = offset + tag_length;
678
679     switch (real_tag_type)
680     {
681         case EDONKEY_MTAG_HASH:
682             /* <Tag> ::= HASH */
683             tag_length += 16;
684             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
685             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
686             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
687             if (tag_type==real_tag_type)
688                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
689             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
690             proto_tree_add_item(metatag_tree, hf_edonkey_hash, tvb, tag_offset, 16, ENC_NA);
691             break;
692
693         case EDONKEY_MTAG_STRING:
694             /* <Tag> ::= <Length (guint16)> <String> */
695             string_length = tvb_get_letohs(tvb, tag_offset);
696             tag_length += 2+string_length;
697             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
698             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
699             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
700             if (tag_type==real_tag_type)
701                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
702             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
703             proto_tree_add_uint(metatag_tree, hf_edonkey_string_length, tvb, tag_offset, 2, string_length);
704             proto_tree_add_item(metatag_tree, hf_edonkey_string, tvb, tag_offset+2, string_length, ENC_ASCII|ENC_NA);
705             break;
706
707         case EDONKEY_MTAG_DWORD:
708             /* <Tag> ::= guint32 */
709             tag_length += 4;
710             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
711             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
712             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
713             if (tag_type==real_tag_type)
714                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
715             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
716             trans_tagtype = edonkey_metatag_name_get_type(tvb, offset+3, tag_name_size, special_tagtype);
717             if (trans_tagtype == EDONKEY_STAG_IP) {
718                 proto_tree_add_item(metatag_tree, hf_edonkey_ip, tvb, tag_offset, 4, ENC_BIG_ENDIAN);
719             }
720             else if (trans_tagtype == EMULE_STAG_SERVER_VERSION) {
721                 guint16 maj, min;
722                 min = tvb_get_letohs(tvb, tag_offset);
723                 maj = tvb_get_letohs(tvb, tag_offset+2);
724                 proto_tree_add_text(metatag_tree, tvb, tag_offset, 4, "Meta Tag Value: %u.%u", maj, min);
725             }
726             else {
727                 tag_value_guint32 = tvb_get_letohl(tvb, tag_offset);
728                 proto_tree_add_text(metatag_tree, tvb, tag_offset, 4, "Meta Tag Value: %u", tag_value_guint32);
729             }
730             break;
731
732         case EDONKEY_MTAG_FLOAT:
733             /* <Tag> ::=  4 byte float */
734             tag_length += 4;
735             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
736             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
737             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
738             if (tag_type==real_tag_type)
739                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
740             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
741             break;
742
743         case EDONKEY_MTAG_WORD:
744             /* <Tag> ::= guint16 */
745             tag_length += 2;
746             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
747             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
748             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
749             if (tag_type==real_tag_type)
750                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
751             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
752             {
753                 tag_value_guint16 = tvb_get_letohs(tvb, tag_offset);
754                 proto_tree_add_text(metatag_tree, tvb, tag_offset, 2, "Meta Tag Value: %u", tag_value_guint16);
755             }
756             break;
757
758         case EDONKEY_MTAG_BYTE:
759             /* <Tag> ::= guint8 */
760             tag_length += 1;
761             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
762             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
763             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
764             if (tag_type==real_tag_type)
765                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
766             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
767             {
768                 tag_value_guint8 = tvb_get_guint8(tvb, tag_offset);
769                 proto_tree_add_text(metatag_tree, tvb, tag_offset, 1, "Meta Tag Value: %u", tag_value_guint8);
770             }
771             break;
772
773         case EDONKEY_MTAG_BOOL:
774             /* <Tag> ::= <Bool (guint8)> */
775             tag_length += 1;
776             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
777             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
778             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
779             if (tag_type==real_tag_type)
780                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
781             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
782             {
783                 tag_value_guint8 = tvb_get_guint8(tvb, tag_offset);
784                 proto_tree_add_text(metatag_tree, tvb, tag_offset, 1, "Meta Tag Value: %u", tag_value_guint8);
785             }
786             break;
787
788         case EDONKEY_MTAG_BOOL_ARRAY:
789             /* <Tag> ::= <Length (guint16)> <BoolArray> */
790             array_length = tvb_get_letohs(tvb, tag_offset);
791             /*
792              * This is allegedly what the protocol uses, rather than the
793              * correct value of (array_length+7)/8
794              * Therefore an extra unused byte is transmitted if the array
795              * is a multiple of 8 longs.
796              */
797             tag_length += 2+(array_length/8)+1;
798             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
799             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
800             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
801             if (tag_type==real_tag_type)
802                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
803             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
804             proto_tree_add_text(metatag_tree, tvb, tag_offset, 2, "Boolean Array Length: %u", array_length);
805             break;
806
807         case EDONKEY_MTAG_BLOB:
808             /* <Tag> ::= <Length (guint32)> <BLOB> */
809             blob_length = tvb_get_letohl(tvb, tag_offset);
810             tag_length += 4+blob_length;
811             ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
812             metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
813             proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
814             if (tag_type==real_tag_type)
815                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
816             edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
817             proto_tree_add_text(metatag_tree, tvb, tag_offset, 2, "BLOB Length: %u", blob_length);
818             break;
819
820         case EDONKEY_MTAG_BSOB:
821             /* This is possibly a Binary Small OBject, like a BLOB but with an 8 or 16-bit length field */
822             /* That's a complete guess though, so don't handle it yet. */
823         case EDONKEY_MTAG_UNKNOWN:
824         default:
825             if (real_tag_type>=EDONKEY_MTAG_STR1 && real_tag_type<=EDONKEY_MTAG_STR16) {
826                 /* <Tag> ::= <String> */
827                 string_length = real_tag_type-EDONKEY_MTAG_STR1+1;
828                 tag_length += string_length;
829                 ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
830                 metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
831                 proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_type, tvb, offset, 1, tag_type);
832                 if (real_tag_type==tag_type)
833                     proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
834                 edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
835                 proto_tree_add_item(metatag_tree, hf_edonkey_string, tvb, tag_offset, string_length, ENC_ASCII|ENC_NA);
836
837             } else {
838                 /* Unknown tag type - actual tag length is also unknown */
839                 ti = proto_tree_add_item(tree, hf_edonkey_metatag, tvb, offset, tag_length, ENC_NA);
840                 metatag_tree = proto_item_add_subtree(ti, ett_edonkey_metatag);
841                 proto_tree_add_text(metatag_tree, tvb, offset, 1, "Unknown Meta Tag Type (0x%02x)", tag_type);
842                 if (real_tag_type==tag_type)
843                     proto_tree_add_uint(metatag_tree, hf_edonkey_metatag_namesize, tvb, offset+1, 2, tag_name_size);
844                 edonkey_tree_add_metatag_name(metatag_tree, tvb, tag_offset-tag_name_size, tag_name_size, special_tagtype);
845             }
846             break;
847
848     }
849
850     return offset + tag_length;
851 }
852
853 /* Dissects the eDonkey address */
854 static int dissect_edonkey_address(tvbuff_t *tvb, packet_info *pinfo _U_,
855                                    int offset, proto_tree *tree)
856 {
857     /* <Address> ::= <IP> <Port> */
858     /*    guint32 ip = tvb_get_letohl(tvb, offset);
859           proto_tree_add_ipv4(tree, hf_edonkey_ip, tvb, offset, 4, ip); */
860     proto_tree_add_item(tree, hf_edonkey_ip, tvb, offset, 4, ENC_BIG_ENDIAN);
861     proto_tree_add_item(tree, hf_edonkey_port, tvb, offset+4, 2, ENC_LITTLE_ENDIAN);
862     return offset+6;
863 }
864
865
866 static int dissect_kademlia_udp_port(tvbuff_t *tvb, packet_info *pinfo _U_,
867                                  int offset, proto_tree *tree)
868 {
869     proto_tree_add_item(tree, hf_kademlia_udp_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
870     return offset +2;
871 }
872
873 static int dissect_kademlia_tcp_port(tvbuff_t *tvb, packet_info *pinfo _U_,
874                                      int offset, proto_tree *tree)
875 {
876     proto_tree_add_item(tree, hf_kademlia_tcp_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
877     return offset +2;
878 }
879
880
881 static int dissect_kademlia_ip_address(tvbuff_t *tvb, packet_info *pinfo _U_,
882                                        int offset, proto_tree *tree)
883 {
884     proto_tree_add_item(tree, hf_kademlia_ip, tvb, offset, 4, ENC_LITTLE_ENDIAN);
885     return offset + 4;
886 }
887
888
889 static int dissect_kademlia_address(tvbuff_t *tvb, packet_info *pinfo _U_,
890                                     int offset, proto_tree *tree)
891 {
892     /* <Address> ::= <IP> <Port> <Tcp Port>*/
893     offset = dissect_kademlia_ip_address( tvb, pinfo, offset, tree );
894
895     offset = dissect_kademlia_udp_port( tvb, pinfo, offset, tree );
896
897     offset = dissect_kademlia_tcp_port( tvb, pinfo, offset, tree );
898
899     return offset;
900 }
901
902 static int dissect_kademlia_tagname(tvbuff_t *tvb, packet_info *pinfo _U_,
903                                     int offset, proto_tree *tree, const gchar** outputTagName, const gchar** outputExtendedTagName)
904 {
905     /* <String> ::= <String length (guint16)> DATA */
906     const gchar * tagname;
907     const gchar * tag_full_name = NULL;
908     guint8 tagname_value;
909     proto_item *ti, *hidden_item;
910
911     guint16 string_length = tvb_get_letohs(tvb, offset);
912
913     proto_tree_add_uint(tree, hf_kademlia_tag_name_length, tvb, offset, 2, string_length);
914
915     hidden_item = proto_tree_add_uint(tree, hf_edonkey_string_length, tvb, offset, 2, string_length);
916     PROTO_ITEM_SET_HIDDEN(hidden_item);
917
918     tagname = tvb_get_ephemeral_string( tvb, offset + 2, string_length );
919
920     tag_full_name = "UnknownTagName";
921
922     if ( tagname && string_length == 1 ) {
923         tagname_value = *(guint8*)tagname;
924         /* lookup tagname */
925         tag_full_name = val_to_str( tagname_value, kademlia_tags, tag_full_name );
926     }
927
928     ti = proto_tree_add_item(tree, hf_kademlia_tag_name, tvb, offset+2, string_length, ENC_BIG_ENDIAN);
929     proto_item_append_text(ti, " [%s]", tag_full_name);
930
931     if (outputTagName)
932         *outputTagName = tagname;
933
934     if (outputExtendedTagName)
935         *outputExtendedTagName = tag_full_name;
936
937     return offset+2+string_length;
938 }
939
940 static int dissect_kademlia_string(tvbuff_t *tvb, packet_info *pinfo _U_,
941                                     int offset, proto_tree *tree)
942 {
943     /* <String> ::= <String length (guint16)> DATA */
944     const gchar * string_value;
945     guint16 string_length;
946     string_length = tvb_get_letohs(tvb, offset);
947
948     proto_tree_add_uint(tree, hf_edonkey_string_length, tvb, offset, 2, string_length);
949
950     string_value = tvb_get_ephemeral_string( tvb, offset + 2, string_length );
951
952     proto_tree_add_text(tree, tvb, offset+2, string_length, "String: %s", string_value);
953
954     return offset+2+string_length;
955 }
956
957 /* Dissects the eDonkey address list */
958 static int dissect_edonkey_address_list(tvbuff_t *tvb, packet_info *pinfo _U_,
959                                         int offset,  proto_tree *tree)
960 {
961     /* <Address List> ::= <List Size (guint8)> <Address>* */
962     return dissect_edonkey_list(tvb, pinfo, offset, tree, 1, "Address", dissect_edonkey_address);
963 }
964
965 /* Dissects the eDonkey hash */
966 static int dissect_edonkey_hash(tvbuff_t *tvb, packet_info *pinfo _U_,
967                                 int offset, proto_tree *tree)
968 {
969     /* <hash> ::= HASH (16 word MD4 digest) */
970     proto_tree_add_item(tree, hf_edonkey_hash, tvb, offset, 16, ENC_NA);
971     return offset+16;
972 }
973
974 /* Dissects the eDonkey file hash */
975 static int dissect_edonkey_file_hash(tvbuff_t *tvb, packet_info *pinfo _U_,
976                                      int offset, proto_tree *tree)
977 {
978     /* <File hash> ::= HASH (16 word MD4 digest) */
979     proto_tree_add_item(tree, hf_edonkey_file_hash, tvb, offset, 16, ENC_NA);
980     return offset+16;
981 }
982
983 /* Dissects the eMule public key */
984 static int dissect_edonkey_public_key(tvbuff_t *tvb, packet_info *pinfo _U_,
985                                      int offset, proto_tree *tree)
986 {
987     guint8 length = tvb_get_guint8(tvb, offset);
988     proto_tree_add_text(tree, tvb, offset, 1, "Public key length: %u", length);
989     offset++;
990     proto_tree_add_item(tree, hf_emule_public_key, tvb, offset, length, ENC_NA);
991     return offset + length;
992 }
993
994 /* Dissects the eMule signature */
995 static int dissect_edonkey_signature(tvbuff_t *tvb, packet_info *pinfo _U_,
996                                      int offset, proto_tree *tree)
997 {
998     guint8 length = tvb_get_guint8(tvb, offset);
999     proto_tree_add_text(tree, tvb, offset, 1, "Signature length: %u", length);
1000     offset++;
1001     proto_tree_add_item(tree, hf_emule_signature, tvb, offset, length, ENC_NA);
1002     return offset + length;
1003 }
1004
1005 static const char *kademlia_hash(tvbuff_t *tvb, int offset) {
1006     guint32 hash[4];
1007     int i;
1008
1009     for (i = 0; i < 4; i++)
1010         hash[i] = tvb_get_letohl(tvb, offset + i*4);
1011
1012     return ep_strdup_printf("%08X%08X%08X%08X", hash[0], hash[1], hash[2], hash[3]);
1013 }
1014
1015 static int dissect_kademlia_hash_hidden(tvbuff_t *tvb, packet_info *pinfo _U_,
1016                                         int offset, proto_tree *tree) {
1017     proto_item *hidden_item;
1018     const char *hash;
1019
1020     hash = kademlia_hash(tvb, offset);
1021
1022     /* <File hash> ::= HASH (16 word MD4 digest) */
1023     hidden_item = proto_tree_add_string_format_value(tree, hf_kademlia_hash, tvb, offset, 16, hash, "%s", hash);
1024     PROTO_ITEM_SET_HIDDEN(hidden_item);
1025
1026     return offset+16;
1027 }
1028
1029 /* Dissects the Kademlia hash*/
1030 static int dissect_kademlia_hash(tvbuff_t *tvb, packet_info *pinfo _U_,
1031                                  int offset, proto_tree *tree, int *value_ptr) {
1032     const char *hash;
1033
1034     hash = kademlia_hash(tvb, offset);
1035
1036     /* <File hash> ::= HASH (16 word MD4 digest) */
1037     proto_tree_add_string_format_value(tree, *value_ptr, tvb, offset, 16, hash, "%s", hash);
1038
1039     return dissect_kademlia_hash_hidden(tvb, pinfo, offset, tree);
1040 }
1041
1042 static int dissect_kademlia_tag_hash_hidden(tvbuff_t *tvb, packet_info *pinfo _U_,
1043                                         int offset, proto_tree *tree) {
1044     proto_item *hidden_item;
1045     const char *hash;
1046
1047     hash = kademlia_hash(tvb, offset);
1048
1049     /* <File hash> ::= HASH (16 word MD4 digest) */
1050     hidden_item = proto_tree_add_string_format_value(tree, hf_kademlia_tag_hash, tvb, offset, 16, hash, "%s", hash);
1051     PROTO_ITEM_SET_HIDDEN(hidden_item);
1052
1053     return offset+16;
1054 }
1055
1056 static int dissect_kademlia_tag_hash(tvbuff_t *tvb, packet_info *pinfo _U_,
1057                                  int offset, proto_tree *tree) {
1058     const char *hash;
1059
1060     hash = kademlia_hash(tvb, offset);
1061
1062     /* <File hash> ::= HASH (16 word MD4 digest) */
1063     proto_tree_add_string_format_value(tree, hf_kademlia_hash, tvb, offset, 16, hash, "%s", hash);
1064     return dissect_kademlia_tag_hash_hidden( tvb, pinfo, offset, tree );
1065 }
1066
1067 static int dissect_kademlia_tag_bsob(tvbuff_t *tvb, packet_info *pinfo _U_,
1068                                  int offset, proto_tree *tree, const gchar** string_value )
1069 {
1070     guint16 bsob_length;
1071
1072     bsob_length = tvb_get_guint8(tvb, offset);
1073     *string_value = tvb_bytes_to_str( tvb, offset + 1, bsob_length );
1074
1075     proto_tree_add_item(tree, hf_kademlia_tag_bsob, tvb, offset + 1, bsob_length, ENC_NA);
1076     return offset + 1 + bsob_length;
1077 }
1078
1079
1080 static int dissect_kademlia_tag_string(tvbuff_t *tvb, packet_info *pinfo _U_,
1081                                  int offset, proto_tree *tree, const gchar** string_value)
1082 {
1083     proto_item *hidden_item;
1084     guint16 string_length = tvb_get_letohs(tvb, offset);
1085
1086     hidden_item = proto_tree_add_uint(tree, hf_edonkey_string_length, tvb, offset, 2, string_length);
1087     PROTO_ITEM_SET_HIDDEN(hidden_item);
1088     hidden_item = proto_tree_add_item(tree, hf_edonkey_string, tvb, offset+2, string_length, ENC_ASCII|ENC_NA);
1089     PROTO_ITEM_SET_HIDDEN(hidden_item);
1090     *string_value = tvb_get_ephemeral_string(tvb, offset + 2, string_length);
1091
1092     proto_tree_add_item(tree, hf_kademlia_tag_string, tvb, offset + 2, string_length, ENC_ASCII|ENC_NA);
1093     return offset + 2 + string_length;
1094 }
1095
1096 /* Dissects the eDonkey hash list */
1097 static int dissect_edonkey_hash_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1098                                      int offset,  proto_tree *tree)
1099 {
1100     /* <Hash List> ::= <File Hash> <List Size (guint16)> <Hash>* */
1101     offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1102     return dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "Hash", dissect_edonkey_hash);
1103 }
1104
1105 /* Dissects the eDonkey meta tag list */
1106 static int dissect_edonkey_metatag_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1107                                         int offset, proto_tree *tree)
1108 {
1109     /* <Meta Tag List> ::= <List Size (guint32)> <Meta tag>* */
1110     return dissect_edonkey_list(tvb, pinfo, offset, tree, 4, "Meta Tag", dissect_edonkey_metatag);
1111 }
1112
1113 /* Dissects the eDonkey String */
1114 static int dissect_edonkey_string(tvbuff_t *tvb, packet_info *pinfo _U_,
1115                                   int offset, proto_tree *tree)
1116 {
1117     /* <String> ::= <String length (guint16)> DATA */
1118     guint16 string_length = tvb_get_letohs(tvb, offset);
1119     proto_tree_add_uint(tree, hf_edonkey_string_length, tvb, offset, 2, string_length);
1120     proto_tree_add_item(tree, hf_edonkey_string, tvb, offset+2, string_length, ENC_ASCII|ENC_NA);
1121     return offset+2+string_length;
1122 }
1123
1124 /* Dissects the eDonkey Directory */
1125 static int dissect_edonkey_directory(tvbuff_t *tvb, packet_info *pinfo _U_,
1126                                      int offset, proto_tree *tree)
1127 {
1128     /* <Directory> ::= <String> */
1129     guint16 string_length = tvb_get_letohs(tvb, offset);
1130     proto_tree_add_uint(tree, hf_edonkey_string_length, tvb, offset, 2, string_length);
1131     proto_tree_add_item(tree, hf_edonkey_directory, tvb, offset+2, string_length, ENC_ASCII|ENC_NA);
1132     return offset+2+string_length;
1133 }
1134
1135 /* Dissects the eDonkey Filename */
1136 static int dissect_edonkey_file_name(tvbuff_t *tvb, packet_info *pinfo _U_,
1137                                      int offset, proto_tree *tree)
1138 {
1139     /* <Filename> ::= <String> */
1140     return dissect_edonkey_string(tvb, pinfo, offset, tree);
1141 }
1142
1143 /* Dissects the eDonkey File Status */
1144 static int dissect_edonkey_file_status(tvbuff_t *tvb, packet_info *pinfo _U_,
1145                                        int offset, proto_tree *tree)
1146 {
1147     guint16 partcount, arrlen;
1148
1149     /* <File Status> ::= <Part Count> <Part Status> */
1150     partcount = tvb_get_letohs(tvb, offset);
1151     arrlen = (partcount+7)/8;
1152
1153     proto_tree_add_uint(tree, hf_edonkey_part_count, tvb, offset, 2, partcount);
1154     if (partcount>0) {
1155         proto_tree_add_item(tree, hf_edonkey_file_status, tvb, offset+2, arrlen, ENC_NA);
1156     }
1157     return offset+2+arrlen;
1158 }
1159
1160
1161 /* Dissects the eDonkey directory list */
1162 static int dissect_edonkey_directory_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1163                                           int offset,  proto_tree *tree)
1164 {
1165     /* <Directory List> ::= <List Size (guint32)> <Directory>* */
1166     return dissect_edonkey_list(tvb, pinfo, offset, tree, 4, "Directory", dissect_edonkey_directory);
1167 }
1168
1169 /* Dissects the eDonkey server hash */
1170 static int dissect_edonkey_server_hash(tvbuff_t *tvb, packet_info *pinfo _U_,
1171                                        int offset, proto_tree *tree)
1172 {
1173     /* <Server hash> ::= HASH (16 word MD4 digest) */
1174     proto_tree_add_item(tree, hf_edonkey_server_hash, tvb, offset, 16, ENC_NA);
1175     return offset+16;
1176 }
1177
1178 /* Dissects the eDonkey client hash */
1179 static int dissect_edonkey_client_hash(tvbuff_t *tvb, packet_info *pinfo _U_,
1180                                        int offset, proto_tree *tree)
1181 {
1182     /* <Client hash> ::= HASH (16 word MD4 digest) */
1183     proto_tree_add_item(tree, hf_edonkey_client_hash, tvb, offset, 16, ENC_NA);
1184     return offset+16;
1185 }
1186
1187 /* Dissects the eMule sourceOBFU */
1188 static int dissect_emule_sourceOBFU(tvbuff_t *tvb, packet_info *pinfo _U_,
1189                                    int offset, proto_tree *tree)
1190 {
1191
1192     proto_item *ti;
1193     proto_tree *sourceOBFU_tree;
1194     guint8 settings = tvb_get_guint8(tvb, offset+6);
1195     /* Add subtree for client info */
1196     ti = proto_tree_add_item(tree, hf_emule_sourceOBFU, tvb, offset, 7 + ((settings & 0x80) ? 16 : 0), ENC_NA);
1197     sourceOBFU_tree = proto_item_add_subtree(ti, ett_emule_sourceOBFU);
1198
1199     proto_tree_add_item(sourceOBFU_tree, hf_edonkey_ip, tvb, offset, 4, ENC_BIG_ENDIAN);
1200     proto_tree_add_item(sourceOBFU_tree, hf_edonkey_port, tvb, offset+4, 2, ENC_LITTLE_ENDIAN);
1201     proto_tree_add_text(sourceOBFU_tree, tvb, offset+6, 1, "Obfuscation Settings: %u", settings);
1202     offset += 7;
1203     if (settings & 0x80)
1204         offset = dissect_edonkey_client_hash(tvb, pinfo, offset, sourceOBFU_tree);
1205     return offset;
1206 }
1207
1208
1209 /* Dissects the eMule sourceOBFU list */
1210 static int dissect_emule_sourceOBFU_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1211                                         int offset,  proto_tree *tree)
1212 {
1213     return dissect_edonkey_list(tvb, pinfo, offset, tree, 1, "Sources Obfuscation", dissect_emule_sourceOBFU);
1214 }
1215
1216
1217 /* Dissects the eDonkey client ID */
1218 static int dissect_edonkey_client_id(tvbuff_t *tvb, packet_info *pinfo _U_,
1219                                      int offset, proto_tree *tree, gboolean fileinfo)
1220 {
1221     proto_item *ti;
1222     /* <Client ID> ::= guint32 */
1223     /*    guint32 ip = tvb_get_letohl(tvb, offset);
1224           proto_tree_add_ipv4(tree, hf_edonkey_client_id, tvb, offset, 4, ip); */
1225     ti = proto_tree_add_item(tree, hf_edonkey_client_id, tvb, offset, 4, ENC_BIG_ENDIAN);
1226     if (fileinfo) {
1227         guint32 ip = tvb_get_letohl(tvb, offset);
1228         guint16 port = tvb_get_letohs(tvb, offset+4);
1229         if (ip==0xfcfcfcfc && port==0xfcfc) {
1230             proto_item_append_text(ti, " (myself, incomplete file)");
1231         } else if (ip==0xfbfbfbfb && port==0xfbfb) {
1232             proto_item_append_text(ti, " (myself, complete file)");
1233         }
1234     }
1235     return offset+4;
1236 }
1237
1238 /* Dissects the eDonkey port */
1239 static int dissect_edonkey_port(tvbuff_t *tvb, packet_info *pinfo _U_,
1240                                 int offset, proto_tree *tree)
1241 {
1242     /* <Port> ::= guint16 */
1243     proto_tree_add_item(tree, hf_edonkey_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1244     return offset+2;
1245 }
1246
1247 /* Dissects the eDonkey start offset */
1248 static int dissect_edonkey_start_offset(tvbuff_t *tvb, packet_info *pinfo _U_,
1249                                         int offset, proto_tree *tree)
1250 {
1251     /* <Start Offset> ::= guint32 */
1252     guint32 start = tvb_get_letohl(tvb, offset);
1253     proto_tree_add_text(tree, tvb, offset, 4, "Start Offset: %u", start);
1254     return offset+4;
1255 }
1256
1257 static int dissect_edonkey_start_offset_64(tvbuff_t *tvb, packet_info *pinfo _U_,
1258                                            int offset, proto_tree *tree)
1259 {
1260     /* <Start Offset> ::= guint64 */
1261     guint64 start = tvb_get_letoh64(tvb, offset);
1262     proto_tree_add_text(tree, tvb, offset, 8, "Start Offset: %" G_GINT64_MODIFIER "u", start);
1263     return offset+8;
1264 }
1265
1266 /* Dissects the eDonkey end offset */
1267 static int dissect_edonkey_end_offset(tvbuff_t *tvb, packet_info *pinfo _U_,
1268                                       int offset, proto_tree *tree)
1269 {
1270     /* <End Offset> ::= guint32 */
1271     guint32 end = tvb_get_letohl(tvb, offset);
1272     proto_tree_add_text(tree, tvb, offset, 4, "End Offset: %u", end);
1273     return offset+4;
1274 }
1275
1276 static int dissect_edonkey_end_offset_64(tvbuff_t *tvb, packet_info *pinfo _U_,
1277                                          int offset, proto_tree *tree)
1278 {
1279     /* <End Offset> ::= guint64 */
1280     guint64 end = tvb_get_letoh64(tvb, offset);
1281     proto_tree_add_text(tree, tvb, offset, 8, "End Offset: %" G_GINT64_MODIFIER "u", end);
1282     return offset+8;
1283 }
1284
1285 /* Dissects the eDonkey client info */
1286 static int dissect_edonkey_client_info(tvbuff_t *tvb, packet_info *pinfo _U_,
1287                                        int offset,  proto_tree *tree)
1288 {
1289     /* <Client info> ::= <Client hash> <Client ID> <Port> <Meta tag list> */
1290     proto_item *ti;
1291     proto_tree *clientinfo_tree;
1292     /* Add subtree for client info */
1293     ti = proto_tree_add_item(tree, hf_edonkey_clientinfo, tvb, offset, 0, ENC_NA);
1294     clientinfo_tree = proto_item_add_subtree(ti, ett_edonkey_clientinfo);
1295     offset = dissect_edonkey_client_hash(tvb, pinfo, offset, clientinfo_tree);
1296     offset = dissect_edonkey_client_id(tvb, pinfo, offset, clientinfo_tree, FALSE);
1297     offset = dissect_edonkey_port(tvb, pinfo, offset, clientinfo_tree);
1298     offset = dissect_edonkey_metatag_list(tvb, pinfo, offset, clientinfo_tree);
1299     return offset;
1300 }
1301
1302 /* Dissects the eDonkey client info list */
1303 static int dissect_edonkey_client_info_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1304                                             int offset,  proto_tree *tree)
1305 {
1306     /* <Client Info List> ::= <List Size (guint32)> <Client Info>* */
1307     return dissect_edonkey_list(tvb, pinfo, offset, tree, 4, "Client Info", dissect_edonkey_client_info);
1308 }
1309
1310 /* Dissects the eDonkey server info */
1311 static int dissect_edonkey_server_info(tvbuff_t *tvb, packet_info *pinfo _U_,
1312                                        int offset,  proto_tree *tree)
1313 {
1314     /* <Server info> ::= <Server hash> <Server Address> <Meta tag list> */
1315     proto_item *ti;
1316     proto_tree *serverinfo_tree;
1317     /* Add subtree for server info */
1318     ti = proto_tree_add_item(tree, hf_edonkey_serverinfo, tvb, offset, 0, ENC_NA);
1319     serverinfo_tree = proto_item_add_subtree(ti, ett_edonkey_serverinfo);
1320     offset = dissect_edonkey_server_hash(tvb, pinfo, offset, serverinfo_tree);
1321     offset = dissect_edonkey_address(tvb, pinfo, offset, serverinfo_tree);
1322     offset = dissect_edonkey_metatag_list(tvb, pinfo, offset, serverinfo_tree);
1323     return offset;
1324 }
1325
1326 /* Dissects the eDonkey file info */
1327 static int dissect_edonkey_file_info(tvbuff_t *tvb, packet_info *pinfo _U_,
1328                                      int offset,  proto_tree *tree)
1329 {
1330     /* <File info> ::= <File hash> <Client ID> <Port> <Meta tag list> */
1331     proto_item *ti;
1332     proto_tree *fileinfo_tree;
1333     int startoff;
1334     /* Add subtree for file info */
1335     ti = proto_tree_add_item(tree, hf_edonkey_fileinfo, tvb, offset, 0, ENC_NA);
1336     startoff = offset;
1337     fileinfo_tree = proto_item_add_subtree(ti, ett_edonkey_fileinfo);
1338     offset = dissect_edonkey_file_hash(tvb, pinfo, offset, fileinfo_tree);
1339     offset = dissect_edonkey_client_id(tvb, pinfo, offset, fileinfo_tree, TRUE);
1340     offset = dissect_edonkey_port(tvb, pinfo, offset, fileinfo_tree);
1341     offset = dissect_edonkey_metatag_list(tvb, pinfo, offset, fileinfo_tree);
1342     proto_item_set_len(ti, offset-startoff);
1343     return offset;
1344 }
1345
1346 /* Dissects the eDonkey file info list */
1347 static int dissect_edonkey_file_info_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1348                                           int offset,  proto_tree *tree)
1349 {
1350     /* <File Info List> ::= <List Size (guint32)> <File Info>* */
1351     return dissect_edonkey_list(tvb, pinfo, offset, tree, 4, "File Info", dissect_edonkey_file_info);
1352 }
1353
1354
1355 /* Dissects the eMule   address list */
1356 static int dissect_emule_address_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1357                                       int offset,  proto_tree *tree)
1358 {
1359     /* <Address List> ::= <List Size (guint16)> <Address>* */
1360     return dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "Address", dissect_edonkey_address);
1361 }
1362
1363 static int dissect_emule_aich_root_hash(tvbuff_t *tvb, packet_info *pinfo _U_,
1364                                         int offset, proto_tree *tree)
1365 {
1366     /* <AICH Root Hash> ::= HASH (20 byte SHA1 digest) */
1367     proto_tree_add_item(tree, hf_emule_aich_root_hash, tvb, offset, 20, ENC_NA);
1368     return offset + 20;
1369 }
1370
1371 static int dissect_emule_aich_hash_list_entry(tvbuff_t *tvb, packet_info *pinfo _U_,
1372                                               int offset, proto_tree *tree)
1373 {
1374     guint16 hashid;
1375     proto_item *ti;
1376     proto_tree *aichhash_tree;
1377     /* <AICH Hash List Entry> ::= <AICH Hash ID> <AICH Hash> */
1378     ti = proto_tree_add_item(tree, hf_emule_aich_hash_entry, tvb, offset, 22, ENC_NA);
1379     aichhash_tree = proto_item_add_subtree(ti, ett_emule_aichhash);
1380
1381     hashid = tvb_get_letohs(tvb, offset);
1382     proto_tree_add_uint(aichhash_tree, hf_emule_aich_hash_id, tvb, offset, 2, hashid);
1383     proto_tree_add_item(aichhash_tree, hf_emule_aich_hash, tvb, offset+2, 20, ENC_NA);
1384     return offset + 22;
1385 }
1386
1387 static int dissect_emule_aich_hash_list(tvbuff_t *tvb, packet_info *pinfo _U_,
1388                                         int offset, proto_tree *tree)
1389 {
1390     /* <AICH Hash List> ::= <List Size (guint16)> < <AICH Hash ID> <AICH Hash> >* */
1391     return dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "AICH Hash", dissect_emule_aich_hash_list_entry);
1392 }
1393
1394 static int dissect_emule_multipacket(tvbuff_t *tvb, packet_info *pinfo _U_,
1395                                      int offset, int eoffset, proto_tree *tree, int isext)
1396 {
1397     guint8 opcode, nextop;
1398     guint16 namelen, partcount, arrlen, oplen;
1399     guint32 sourcecount;
1400     proto_item *ti;
1401     proto_tree *mp_tree;
1402
1403     /* <MultiPacket> ::= <File Hash> <Opcodes>* */
1404     /* <MultiPacketExt> ::= <File Hash> <FileLength> <Opcodes>* */
1405     offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1406
1407     if (isext) {
1408         guint64 filelen = tvb_get_letoh64(tvb, offset);
1409     proto_tree_add_text(tree, tvb, offset, 8, "File Length: %" G_GINT64_MODIFIER "u", filelen);
1410         offset += 8;
1411     }
1412
1413     while (offset<eoffset) {
1414         opcode = tvb_get_guint8(tvb, offset);
1415
1416         switch (opcode) {
1417             case EDONKEY_MSG_FILE_STATUS_REQUEST:
1418                 ti = proto_tree_add_item(tree, hf_emule_multipacket_entry, tvb, offset, 1, ENC_NA);
1419                 mp_tree = proto_item_add_subtree(ti, ett_emule_multipacket);
1420
1421                 proto_tree_add_uint_format(mp_tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1422                                            opcode, "File Status Request (0x%02x)", opcode);
1423                 offset += 1;
1424                 break;
1425             case EDONKEY_MSG_FILE_REQUEST:
1426                 partcount = 443; /* Invalid */
1427                 sourcecount = 65536; /* Out of range */
1428                 arrlen = 0;
1429                 oplen = 1;
1430
1431                 if (offset+2<eoffset) {
1432                     nextop = tvb_get_guint8(tvb, offset+1);
1433                     if (nextop!=EDONKEY_MSG_FILE_STATUS_REQUEST &&
1434                         nextop!=EMULE_MSG_SOURCES_REQUEST &&
1435                         nextop!=EMULE_MSG_AICHFILEHASH_REQUEST) {
1436
1437                         partcount = tvb_get_letohs(tvb, offset+1);
1438                         if (partcount<=442) {
1439                             arrlen = (partcount+7)/8;
1440                             oplen += 2+arrlen;
1441
1442                             if (offset+2+arrlen+2<eoffset) {
1443                                 nextop = tvb_get_guint8(tvb, offset+2+arrlen+1);
1444                                 if (nextop!=EDONKEY_MSG_FILE_STATUS_REQUEST &&
1445                                     nextop!=EMULE_MSG_SOURCES_REQUEST &&
1446                                     nextop!=EMULE_MSG_AICHFILEHASH_REQUEST) {
1447
1448                                     sourcecount = tvb_get_letohs(tvb, offset+2+arrlen+1);
1449                                     oplen += 2;
1450                                 }
1451                             }
1452                         }
1453                     }
1454                 }
1455
1456                 ti = proto_tree_add_item(tree, hf_emule_multipacket_entry, tvb, offset, oplen, ENC_NA);
1457                 mp_tree = proto_item_add_subtree(ti, ett_emule_multipacket);
1458
1459                 proto_tree_add_uint_format(mp_tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1460                                            opcode, "File Name Request (0x%02x)", opcode);
1461                 if (partcount<=442) {
1462                     dissect_edonkey_file_status(tvb, pinfo, offset+1, mp_tree);
1463                     if (sourcecount<65536) {
1464                         proto_tree_add_uint(mp_tree, hf_emule_source_count, tvb, offset+3+arrlen, 2, sourcecount);
1465                     }
1466                 }
1467                 offset += oplen;
1468                 break;
1469             case EMULE_MSG_SOURCES_REQUEST:
1470                 ti = proto_tree_add_item(tree, hf_emule_multipacket_entry, tvb, offset, 1, ENC_NA);
1471                 mp_tree = proto_item_add_subtree(ti, ett_emule_multipacket);
1472
1473                 proto_tree_add_uint_format(mp_tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1474                                            opcode, "Sources Request (0x%02x)", opcode);
1475                 offset += 1;
1476                 break;
1477             case EMULE_MSG_AICHFILEHASH_REQUEST:
1478                 ti = proto_tree_add_item(tree, hf_emule_multipacket_entry, tvb, offset, 1, ENC_NA);
1479                 mp_tree = proto_item_add_subtree(ti, ett_emule_multipacket);
1480
1481                 proto_tree_add_uint_format(mp_tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1482                                            opcode, "AICH Root Hash Request (0x%02x)", opcode);
1483                 offset += 1;
1484                 break;
1485
1486             case EDONKEY_MSG_FILE_STATUS:
1487                 partcount = tvb_get_letohs(tvb, offset+1);
1488                 arrlen = (partcount+7)/8;
1489
1490                 ti = proto_tree_add_item(tree, hf_emule_multipacket_entry, tvb, offset, 3+arrlen, ENC_NA);
1491                 mp_tree = proto_item_add_subtree(ti, ett_emule_multipacket);
1492
1493                 proto_tree_add_uint_format(mp_tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1494                                            opcode, "File Status (0x%02x)", opcode);
1495                 offset = dissect_edonkey_file_status(tvb, pinfo, offset+1, mp_tree);
1496                 break;
1497             case EDONKEY_MSG_FILE_REQUEST_ANSWER:
1498                 namelen = tvb_get_letohs(tvb, offset+1);
1499
1500                 ti = proto_tree_add_item(tree, hf_emule_multipacket_entry, tvb, offset, 3+namelen, ENC_NA);
1501                 mp_tree = proto_item_add_subtree(ti, ett_emule_multipacket);
1502
1503                 proto_tree_add_uint_format(mp_tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1504                                            opcode, "File Name (0x%02x)", opcode);
1505                 offset = dissect_edonkey_file_name(tvb, pinfo, offset+1, mp_tree);
1506                 break;
1507             case EMULE_MSG_AICHFILEHASH_ANSWER:
1508                 ti = proto_tree_add_item(tree, hf_emule_multipacket_entry, tvb, offset, 21, ENC_NA);
1509                 mp_tree = proto_item_add_subtree(ti, ett_emule_multipacket);
1510
1511                 proto_tree_add_uint_format(mp_tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1512                                            opcode, "AICH Root Hash (0x%02x)", opcode);
1513                 proto_tree_add_item(mp_tree, hf_emule_aich_root_hash, tvb, offset+1, 20, ENC_NA);
1514                 offset += 21;
1515                 break;
1516
1517             default:
1518                 /* Unknown opcode means we can't continue parsing the stream */
1519                 proto_tree_add_uint_format(tree, hf_emule_multipacket_opcode, tvb, offset, 1,
1520                                            opcode, "Unknown MultiPacket opcode (0x%02x)", opcode);
1521                 return offset+1;
1522         }
1523     }
1524
1525     return offset;
1526 }
1527
1528 /* Dissects the Overnet peer type */
1529 static int dissect_overnet_peertype(tvbuff_t *tvb, packet_info *pinfo _U_,
1530                                     int offset, proto_tree *tree)
1531 {
1532     /* <Peer type> ::= guint8 */
1533     guint8 peertype = tvb_get_guint8(tvb, offset);
1534     proto_tree_add_text(tree, tvb, offset, 1, "Peer Type: %u", peertype);
1535     return offset+1;
1536 }
1537
1538 /* Dissects the Overnet peer */
1539 static int dissect_overnet_peer(tvbuff_t *tvb, packet_info *pinfo _U_,
1540                                 int offset, proto_tree *tree)
1541 {
1542     /* <Peer> ::= <Hash> <Address> <Peer type> */
1543     proto_item *ti;
1544     proto_tree *peer_tree;
1545     ti = proto_tree_add_item(tree, hf_overnet_peer, tvb, offset, 16 + 6 + 1, ENC_NA);
1546     peer_tree = proto_item_add_subtree(ti, ett_overnet_peer);
1547     offset = dissect_edonkey_hash(tvb, pinfo, offset, peer_tree);
1548     offset = dissect_edonkey_address(tvb, pinfo, offset, peer_tree);
1549     offset = dissect_overnet_peertype(tvb, pinfo, offset, peer_tree);
1550     return offset;
1551 }
1552
1553 static int dissect_kademlia_peertype(tvbuff_t *tvb, packet_info *pinfo _U_,
1554                                      int offset, proto_tree *tree)
1555 {
1556     /* <Peer type> ::= 1bytes */
1557     proto_tree_add_item( tree, hf_kademlia_peertype, tvb, offset, 1, ENC_BIG_ENDIAN );
1558     return offset + 1;
1559 }
1560
1561 /* Dissects the Kademlia peer */
1562 static int dissect_kademlia_peer(tvbuff_t *tvb, packet_info *pinfo _U_,
1563                                  int offset, proto_tree *tree)
1564 {
1565     /* <Peer> ::= <Hash> <Address> <Peer type> */
1566     proto_item *ti;
1567     proto_tree *peer_tree;
1568     int kad_version;
1569
1570     ti = proto_tree_add_item(tree, hf_kademlia_peer, tvb, offset, 16 + 4 + 4  + 1, ENC_NA);
1571
1572     peer_tree = proto_item_add_subtree(ti, ett_overnet_peer);
1573
1574     /* 16 */
1575     offset = dissect_kademlia_hash(tvb, pinfo, offset, peer_tree, &hf_kademlia_peer_id);
1576
1577     /* 8 ( 4 ip + 2 tcp port + 2 udp port ) */
1578     offset = dissect_kademlia_address(tvb, pinfo, offset, peer_tree);
1579
1580     /* 1 */
1581     /* offset = dissect_kademlia_peertype(tvb, pinfo, offset, peer_tree); */
1582     kad_version = tvb_get_guint8(tvb, offset);
1583     ti = proto_tree_add_item(peer_tree, hf_kademlia_version, tvb, offset, 1, ENC_BIG_ENDIAN);
1584     proto_item_append_text(ti, "%s", val_to_str(kad_version, kademlia_versions, " Unknown"));
1585     return offset + 1;
1586 }
1587
1588 /* Dissects the Kademlia2 peer */
1589 static int dissect_kademlia2_peer(tvbuff_t *tvb, packet_info *pinfo _U_,
1590                                  int offset, proto_tree *tree)
1591 {
1592     /* <Peer> ::= <Hash> <Address> <Peer type> */
1593     proto_item *ti;
1594     proto_tree *peer_tree;
1595
1596     ti = proto_tree_add_item(tree, hf_kademlia_peer, tvb, offset, 16 + 4 + 4  + 1, ENC_NA);
1597
1598     peer_tree = proto_item_add_subtree(ti, ett_overnet_peer);
1599
1600     /* 16 */
1601     offset = dissect_kademlia_hash(tvb, pinfo, offset, peer_tree, &hf_kademlia_peer_id);
1602
1603     /* 8 ( 4 ip + 2 tcp port + 2 udp port ) */
1604     offset = dissect_kademlia_address(tvb, pinfo, offset, peer_tree);
1605
1606     /* 1 */
1607     offset = dissect_kademlia_peertype(tvb, pinfo, offset, peer_tree);
1608     return offset;
1609 }
1610
1611
1612 /* Dissects the eDonkey search query */
1613 static int dissect_edonkey_search_query(tvbuff_t *tvb, packet_info *pinfo _U_,
1614                                         int offset, proto_tree *tree)
1615 {
1616     /* <Search Query> ::= <Search Type> <Search> */
1617     proto_item *ti;
1618     proto_tree *search_tree;
1619     guint8 search_type, operator, special_tagtype, limit_type;
1620     guint16 tag_name_size, string_length;
1621     guint32 search_length, limit;
1622     int string_offset, tag_name_offset;
1623
1624     search_type = tvb_get_guint8(tvb, offset);
1625     search_length = 1;
1626
1627     switch (search_type)
1628     {
1629         case EDONKEY_SEARCH_BOOL:
1630             /* <Search> ::=  <Operator> <Search Query> <Search Query> */
1631             search_length += 1;
1632             operator = tvb_get_guint8(tvb, offset+1);
1633
1634             /* Add subtree for search entry */
1635             ti = proto_tree_add_item(tree, hf_edonkey_search, tvb, offset, search_length, ENC_NA);
1636             search_tree = proto_item_add_subtree(ti, ett_edonkey_search);
1637
1638             /* Add query info */
1639             proto_tree_add_text(search_tree, tvb, offset, 2, "Boolean search (0x%02x): %s (0x%02x)",
1640                                 search_type, val_to_str(operator, edonkey_search_ops, "Unknown"), operator);
1641
1642             offset+=2;
1643             offset = dissect_edonkey_search_query(tvb, pinfo, offset, search_tree);
1644             offset = dissect_edonkey_search_query(tvb, pinfo, offset, search_tree);
1645             break;
1646
1647         case EDONKEY_SEARCH_NAME:
1648             /* <Search> ::=  <String> */
1649             string_offset = offset + search_length;
1650             string_length = tvb_get_letohs(tvb, string_offset);
1651             search_length += 2+string_length;
1652
1653             /* Add subtree for search entry */
1654             ti = proto_tree_add_item(tree, hf_edonkey_search, tvb, offset, search_length, ENC_NA);
1655             search_tree = proto_item_add_subtree(ti, ett_edonkey_search);
1656
1657             /* Add query info */
1658             proto_tree_add_text(search_tree, tvb, offset, 1, "Search by name (0x%02x)", search_type);
1659             proto_tree_add_uint(search_tree, hf_edonkey_string_length, tvb, string_offset, 2, string_length);
1660             proto_tree_add_item(search_tree, hf_edonkey_string, tvb, string_offset+2, string_length, ENC_ASCII|ENC_NA);
1661             offset += search_length;
1662             break;
1663
1664         case EDONKEY_SEARCH_META:
1665             /* <Search> ::=  <String> <Meta tag Name> */
1666             string_offset = offset + search_length;
1667             string_length = tvb_get_letohs(tvb, offset+1);
1668             search_length += 2+string_length;
1669
1670             tag_name_offset = offset + search_length;
1671             tag_name_size = tvb_get_letohs(tvb, tag_name_offset);
1672             special_tagtype = tvb_get_guint8(tvb, tag_name_offset+2);
1673             search_length += 2 + tag_name_size;
1674
1675             /* Add subtree for search entry */
1676             ti = proto_tree_add_item(tree, hf_edonkey_search, tvb, offset, search_length, ENC_NA);
1677             search_tree = proto_item_add_subtree(ti, ett_edonkey_search);
1678
1679             /* Add query info */
1680             proto_tree_add_text(search_tree, tvb, offset, 1, "Search by metadata (0x%02x)", search_type);
1681             proto_tree_add_uint(search_tree, hf_edonkey_string_length, tvb, string_offset, 2, string_length);
1682             proto_tree_add_item(search_tree, hf_edonkey_string, tvb, string_offset+2, string_length, ENC_ASCII|ENC_NA);
1683             proto_tree_add_uint(search_tree, hf_edonkey_metatag_namesize, tvb, tag_name_offset, 2, tag_name_size);
1684             edonkey_tree_add_metatag_name(search_tree, tvb, tag_name_offset+2, tag_name_size, special_tagtype);
1685             offset += search_length;
1686             break;
1687
1688         case EDONKEY_SEARCH_LIMIT:
1689             /* <Search> ::=  <Limit (guint32)> <Minmax> <Meta tag Name> */
1690             search_length += 5; /* 4 bytes for the limit, one for the minmax */
1691             limit = tvb_get_letohl(tvb, offset+1);
1692             limit_type = tvb_get_guint8(tvb, offset+5);
1693
1694             tag_name_offset = offset + search_length;
1695             tag_name_size = tvb_get_letohs(tvb, tag_name_offset);
1696             special_tagtype = tvb_get_guint8(tvb, tag_name_offset+2);
1697             search_length += 2 + tag_name_size;
1698
1699             /* Add subtree for search entry */
1700             ti = proto_tree_add_item(tree, hf_edonkey_search, tvb, offset, search_length, ENC_NA);
1701             search_tree = proto_item_add_subtree(ti, ett_edonkey_search);
1702
1703             /* Add query info */
1704             proto_tree_add_text(search_tree, tvb, offset, 6, "Search by limit (0x%02x): %s %u",
1705                                 search_type, val_to_str(limit_type, edonkey_search_conds, "Unknown"), limit);
1706             proto_tree_add_uint(search_tree, hf_edonkey_metatag_namesize, tvb, tag_name_offset, 2, tag_name_size);
1707             edonkey_tree_add_metatag_name(search_tree, tvb, tag_name_offset+2, tag_name_size, special_tagtype);
1708             offset += search_length;
1709             break;
1710
1711         default:
1712             /* Unknown search type - actual search length is also unknown */
1713             ti = proto_tree_add_item(tree, hf_edonkey_search, tvb, offset, search_length, ENC_NA);
1714             search_tree = proto_item_add_subtree(ti, ett_edonkey_search);
1715             proto_tree_add_text(search_tree, tvb, offset, search_length, "Unknown Search (0x%02x)", search_type);
1716             offset += search_length;
1717             break;
1718     }
1719
1720     return offset;
1721 }
1722
1723 static void dissect_edonkey_tcp_message(guint8 msg_type,
1724                                         tvbuff_t *tvb, packet_info *pinfo _U_,
1725                                         int offset, int length, proto_tree *tree)
1726 {
1727     int msg_end, bytes_remaining;
1728     guint8  helloClient, more;
1729     guint32 nusers, nfiles;
1730
1731     if (tree == NULL) return;
1732
1733     bytes_remaining = tvb_reported_length_remaining(tvb, offset);
1734     if ((length < 0) || (length > bytes_remaining)) length = bytes_remaining;
1735     if (length <= 0) return;
1736
1737     msg_end = offset + length;
1738
1739     switch (msg_type) {
1740         case EDONKEY_MSG_HELLO:
1741             /* Client to Server: <Client Info> */
1742             /* Client to Client: 0x10 <Client Info> <Server address> */
1743             /* If Hello is sent to server 0x10 before UserHash is skipped,
1744                but UserHash might starts with 0x10. To decrease posibility
1745                of mistake, we check also 6th and 15h byte of UserHash -
1746                they have constant value. The best way would be to process
1747                whole packet to check it. */
1748             helloClient = (tvb_get_guint8(tvb, offset) == 0x10 && tvb_get_guint8(tvb, offset + 6) == 0x0E && tvb_get_guint8(tvb, offset + 15) == 0x6F);
1749             if (helloClient) {
1750                 proto_tree_add_text(tree, tvb, offset, 1, "User hash length: 16");
1751                 offset += 1;
1752             }
1753             offset = dissect_edonkey_client_info(tvb, pinfo, offset, tree);
1754             if (helloClient)  /* User's server ip is sent only to clients. */
1755                 offset = dissect_edonkey_address(tvb, pinfo, offset, tree);
1756             break;
1757
1758         case EDONKEY_MSG_HELLO_ANSWER: /* Hello Answer: <Client Info> <Server address> */
1759             offset = dissect_edonkey_client_info(tvb, pinfo, offset, tree);
1760             offset = dissect_edonkey_address(tvb, pinfo, offset, tree);
1761             break;
1762
1763         case EDONKEY_MSG_SERVER_CB_REQ: /* Server Callback Request: <Client address> */
1764             offset = dissect_edonkey_address(tvb, pinfo, offset, tree);
1765             break;
1766
1767         case EDONKEY_MSG_SERVER_INFO_DATA: /* Server Info Data: <Server Info> */
1768             offset = dissect_edonkey_server_info(tvb, pinfo, offset, tree);
1769             break;
1770
1771         case EDONKEY_MSG_SERVER_LIST: /* Server List: <Address List> */
1772             offset = dissect_edonkey_address_list(tvb, pinfo, offset, tree);
1773             break;
1774
1775         case EDONKEY_MSG_OFFER_FILES: /* Offer Files: <File info List> */
1776         case EDONKEY_MSG_VIEW_FILES_ANSWER: /* View Files Answer: <File info list> */
1777             offset = dissect_edonkey_file_info_list(tvb, pinfo, offset, tree);
1778             break;
1779
1780         case EDONKEY_MSG_SEARCH_FILE_RESULTS: /* Search File Results: <File Info list> <More> */
1781             offset = dissect_edonkey_file_info_list(tvb, pinfo, offset, tree);
1782             more = tvb_get_guint8(tvb, offset);
1783             proto_tree_add_text(tree, tvb, offset, 1, "More: %s (0x%02x)", (more) ? "TRUE" : "FALSE", more);
1784             break;
1785
1786         case EDONKEY_MSG_SEARCH_FILES: /* Search File: <Search query> */
1787         case EDONKEY_MSG_SEARCH_USER:  /* Search User: <Search query> */
1788             offset = dissect_edonkey_search_query(tvb, pinfo, offset, tree);
1789             break;
1790
1791         case EDONKEY_MSG_GET_SOURCES:         /* Get Sources: <File Hash> <Size> <Size 64> */
1792         case EDONKEY_MSG_GET_SOURCES_OBFU:    /* Get Sources: <File Hash> <Size> <Size 64> */
1793             {
1794                 guint32 fileSize;
1795                 offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1796                 fileSize = tvb_get_letohl(tvb, offset);
1797                 proto_tree_add_text(tree, tvb, offset, 4, "File size: %u %s", fileSize, (fileSize == 0) ? "(64bit file size used)" : "");
1798                 offset += 4;
1799                 /* if fileSize = 0 then 64bit file size comes next */
1800                 if (fileSize == 0) {
1801                     guint64 fileSize64 = tvb_get_letoh64(tvb, offset);
1802                     proto_tree_add_text(tree, tvb, offset, 8, "Large file size : %" G_GINT64_MODIFIER "u", fileSize64);
1803                 }
1804             }
1805             break;
1806
1807         case EDONKEY_MSG_NO_SUCH_FILE:        /* No Such File: <File Hash> */
1808         case EDONKEY_MSG_END_OF_DOWNLOAD:     /* End of Download: <File Hash> */
1809         case EDONKEY_MSG_FILE_STATUS_REQUEST: /* File Status Request: <File Hash> */
1810         case EDONKEY_MSG_HASHSET_REQUEST:     /* Hashset Request: <File Hash> */
1811         case EDONKEY_MSG_SLOT_REQUEST:        /* Slot Request: <File Hash> */
1812         case EDONKEY_MSG_FILE_REQUEST:        /* File Request: <File Hash> */
1813             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1814             break;
1815
1816         case EDONKEY_MSG_FOUND_SOURCES_OBFU:   /* Found Sources: <File Hash> <SourceOBFU List> */
1817             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1818             offset = dissect_emule_sourceOBFU_list(tvb, pinfo, offset, tree);
1819             break;
1820
1821         case EDONKEY_MSG_FOUND_SOURCES: /* Found Sources: <File Hash> <Address List> */
1822             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1823             offset = dissect_edonkey_address_list(tvb, pinfo, offset, tree);
1824             break;
1825
1826         case EDONKEY_MSG_CLIENT_CB_REQ:  /* Client Callback Request: <Client ID> */
1827         case EDONKEY_MSG_CALLBACK_FAIL:  /* Callback Fail:           <Client ID> */
1828         case EDONKEY_MSG_ID_CHANGE:      /* ID Change:               <Client ID> */
1829             offset = dissect_edonkey_client_id(tvb, pinfo, offset, tree, FALSE);
1830             break;
1831
1832         case EDONKEY_MSG_NEW_CLIENT_ID:  /* New Client ID: <Client ID> <Client ID> */
1833             offset = dissect_edonkey_client_id(tvb, pinfo, offset, tree, FALSE);
1834             offset = dissect_edonkey_client_id(tvb, pinfo, offset, tree, FALSE);
1835             break;
1836
1837         case EDONKEY_MSG_SERVER_MESSAGE: /* Server Message: <String> */
1838         case EDONKEY_MSG_CLIENT_MESSAGE: /* Client Message: <String> */
1839             offset = dissect_edonkey_string(tvb, pinfo, offset, tree);
1840             break;
1841
1842         case EDONKEY_MSG_SERVER_STATUS:  /* Server Status: <Nusers> <Nfiles> */
1843             nusers = tvb_get_letohl(tvb, offset);
1844             nfiles = tvb_get_letohl(tvb, offset+4);
1845             proto_tree_add_text(tree, tvb, offset, 4, "Number of Users: %u", nusers);
1846             proto_tree_add_text(tree, tvb, offset+4, 4, "Number of Files: %u", nfiles);
1847             break;
1848
1849         case EDONKEY_MSG_FILE_STATUS: /* File Status: <File hash> <Part Count> <Part Status>? */
1850             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1851             offset = dissect_edonkey_file_status(tvb, pinfo, offset, tree);
1852             break;
1853
1854         case EDONKEY_MSG_FILE_REQUEST_ANSWER: /* File Request Answer: <File hash> <File name> */
1855             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1856             offset = dissect_edonkey_file_name(tvb, pinfo, offset, tree);
1857             break;
1858
1859         case EDONKEY_MSG_REQUEST_PARTS:  /* Request Parts: <File hash> <Start offset>(3) <End offset>(3) */
1860             {
1861               int pairs, count;
1862               offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1863               pairs = (msg_end - offset) / 8;
1864
1865               for (count=0; count < pairs; count++)
1866               {
1867                 offset = dissect_edonkey_start_offset(tvb, pinfo, offset, tree);
1868               }
1869               for (count=0; count < pairs; count++)
1870               {
1871                 offset = dissect_edonkey_end_offset(tvb, pinfo, offset, tree);
1872               }
1873             }
1874             break;
1875
1876         case EDONKEY_MSG_SENDING_PART:  /* Sending Part: <File hash> <Start offset> <End offset> DATA */
1877             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1878             offset = dissect_edonkey_start_offset(tvb, pinfo, offset, tree);
1879             offset = dissect_edonkey_end_offset(tvb, pinfo, offset, tree);
1880             if (msg_end > offset) {
1881                 bytes_remaining = msg_end - offset;
1882                 proto_tree_add_text(tree, tvb, offset, bytes_remaining, "Message Data (%d bytes)", bytes_remaining);
1883             }
1884             break;
1885
1886
1887         case EDONKEY_MSG_SEARCH_USER_RESULTS: /* Search User Results: <Client info list> */
1888             offset = dissect_edonkey_client_info_list(tvb, pinfo, offset, tree);
1889             break;
1890
1891         case EDONKEY_MSG_GET_SHARED_FILES:    /* Get Shared Files: <Directory> */
1892             offset = dissect_edonkey_directory(tvb, pinfo, offset, tree);
1893             break;
1894
1895         case EDONKEY_MSG_SHARED_DIRS: /* Shared Dirs: <Directory List> */
1896             offset = dissect_edonkey_directory_list(tvb, pinfo, offset, tree);
1897             break;
1898
1899         case EDONKEY_MSG_SHARED_FILES: /* Shared Files: <Directory> <File info list> */
1900             offset = dissect_edonkey_directory(tvb, pinfo, offset, tree);
1901             offset = dissect_edonkey_file_info_list(tvb, pinfo, offset, tree);
1902             break;
1903
1904         case EDONKEY_MSG_HASHSET_ANSWER:      /* Hashset Answer: <Hash List>  */
1905             offset = dissect_edonkey_hash_list(tvb, pinfo, offset, tree);
1906             break;
1907
1908         default:
1909             proto_tree_add_text(tree, tvb, offset, length, "Message Data (%d bytes)", length);
1910             break;
1911     }
1912
1913     if (offset < msg_end) {
1914         int extra_bytes = msg_end - offset;
1915         /* trailing garbage or broken packet */
1916         proto_tree_add_uint_format(tree, hf_edonkey_unparsed_data_length, tvb, offset, extra_bytes, extra_bytes,
1917                                    "Trailing/Undecoded data: %d bytes", extra_bytes );
1918     }
1919     return;
1920 }
1921
1922 static void dissect_emule_tcp_message(guint8 msg_type,
1923                                       tvbuff_t *tvb, packet_info *pinfo _U_,
1924                                       int offset, int length, proto_tree *tree)
1925 {
1926     int msg_end, bytes_remaining;
1927     guint32 packed_length;
1928     guint16 version, rank, partnum;
1929
1930     if (tree == NULL) return;
1931
1932     bytes_remaining = tvb_reported_length_remaining(tvb, offset);
1933     if ((length < 0) || (length > bytes_remaining)) length = bytes_remaining;
1934     if (length <= 0) return;
1935
1936     msg_end = offset + length;
1937
1938     switch (msg_type) {
1939         case EMULE_MSG_HELLO:  /* eMule Info: <eMule Version> <Meta tag list> */
1940         case EMULE_MSG_HELLO_ANSWER:  /* eMule Info Answer: <eMule Version> <Meta tag list> */
1941             version = tvb_get_letohs(tvb, offset);
1942             proto_tree_add_text(tree, tvb, offset, 2, "Version: %u", version);
1943             offset = dissect_edonkey_metatag_list(tvb, pinfo, offset+2, tree);
1944             break;
1945
1946         case EMULE_MSG_QUEUE_RANKING: /* eMule Queue Ranking: <eMule Rank (guint16)> */
1947             rank = tvb_get_letohs(tvb, offset);
1948             proto_tree_add_text(tree, tvb, offset, 2, "Queue Ranking: %u", rank);
1949             break;
1950
1951         case EMULE_MSG_SOURCES_REQUEST: /* Sources Request: <File Hash> */
1952             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1953             break;
1954
1955         case EMULE_MSG_SOURCES_ANSWER: /* Sources Answer: <File Hash> <Address List> */
1956             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1957             offset = dissect_emule_address_list(tvb, pinfo, offset, tree);
1958             break;
1959
1960         case EMULE_MSG_SEC_IDENT_STATE: {
1961             guint32 rndchallenge;
1962             guint8 state = tvb_get_guint8(tvb, offset);
1963             proto_tree_add_text(tree, tvb, offset, 1, "State: %u (%s needed)", state, (state == 0) ? "nothing is" : (state == 1) ? "signature is" : "public key and signature are");
1964             offset++;
1965             rndchallenge = tvb_get_letohl(tvb, offset);
1966             proto_tree_add_text(tree, tvb, offset, 4, "Rndchallenge: %u", rndchallenge);
1967             offset += 4;
1968             break;
1969             }
1970
1971
1972         case EMULE_MSG_PUBLIC_KEY: /* Public Key: <1byte : len> <len bytes: pubkey> */
1973             offset = dissect_edonkey_public_key(tvb, pinfo, offset, tree);
1974             /* offset = dissect_emule_publickey(tvb, pinfo, offset, tree); */
1975             break;
1976
1977         case EMULE_MSG_SIGNATURE:
1978             offset = dissect_edonkey_signature(tvb, pinfo, offset, tree);
1979             if (msg_end != offset) {
1980                 guint8 sigIPused = tvb_get_guint8(tvb, offset);
1981                 proto_tree_add_text(tree, tvb, offset, 1, "Sig IP Used: %u", sigIPused);
1982                 offset++;
1983             }
1984             break;
1985
1986
1987         /* case EMULE_MSG_SIGNATURE: Public Key: <1byte : len> <len bytes: pubkey> */
1988             /* offset = dissect_emule_publickey(tvb, pinfo, offset, tree); */
1989             /* break; */
1990
1991         /* case EMULE_MSG_SECIDENTSTATE: Public Key: <1byte : len> <len bytes: pubkey> */
1992             /* offset = dissect_emule_secstate(tvb, pinfo, offset, tree); */
1993             /* offset = dissect_emule_challenge(tvb, pinfo, offset, tree); */
1994             /* break; */
1995
1996         case EMULE_MSG_DATA_COMPRESSED: /* Data Compressed: <File Hash> <Start Offset> <Length (guint32)> <DATA> */
1997             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
1998             offset = dissect_edonkey_start_offset(tvb, pinfo, offset, tree);
1999             packed_length = tvb_get_letohl(tvb, offset);
2000             proto_tree_add_text(tree, tvb, offset, 4, "Packed Length: %u", packed_length);
2001             offset += 4;
2002             if (msg_end > offset) {
2003                 bytes_remaining = msg_end - offset;
2004                 proto_tree_add_text(tree, tvb, offset, bytes_remaining,
2005                                     "Compressed Message Data (%d bytes)", bytes_remaining);
2006             }
2007             break;
2008
2009         case EMULE_MSG_DATA_COMPRESSED_64: /* Data Compressed: <File Hash> <Start Offset (guint64)> <Length (guint32)> <DATA> */
2010             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2011             offset = dissect_edonkey_start_offset_64(tvb, pinfo, offset, tree);
2012             packed_length = tvb_get_letohl(tvb, offset);
2013             proto_tree_add_text(tree, tvb, offset, 4, "Packed Length: %u", packed_length);
2014             offset += 4;
2015             if (msg_end > offset) {
2016                 bytes_remaining = msg_end - offset;
2017                 proto_tree_add_text(tree, tvb, offset, bytes_remaining,
2018                                     "Compressed Message Data (%d bytes)", bytes_remaining);
2019             }
2020             break;
2021
2022         case EMULE_MSG_REQUEST_PARTS_64:  /* Request Parts: <File hash> <Start offset>(3) <End offset>(3) */
2023             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2024             offset = dissect_edonkey_start_offset_64(tvb, pinfo, offset, tree);
2025             offset = dissect_edonkey_start_offset_64(tvb, pinfo, offset, tree);
2026             offset = dissect_edonkey_start_offset_64(tvb, pinfo, offset, tree);
2027             offset = dissect_edonkey_end_offset_64(tvb, pinfo, offset, tree);
2028             offset = dissect_edonkey_end_offset_64(tvb, pinfo, offset, tree);
2029             offset = dissect_edonkey_end_offset_64(tvb, pinfo, offset, tree);
2030             break;
2031
2032         case EMULE_MSG_SENDING_PART_64:  /* Sending Part: <File hash> <Start offset> <End offset> DATA */
2033             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2034             offset = dissect_edonkey_start_offset_64(tvb, pinfo, offset, tree);
2035             offset = dissect_edonkey_end_offset_64(tvb, pinfo, offset, tree);
2036             if (msg_end > offset) {
2037                 bytes_remaining = msg_end - offset;
2038                 proto_tree_add_text(tree, tvb, offset, bytes_remaining, "Message Data (%d bytes)", bytes_remaining);
2039             }
2040             break;
2041
2042         case EMULE_MSG_CALLBACK:  /* Callback: < hash ><hash> <uint16> */
2043             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2044             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2045             /* offset = dissect_edonkey_generic_uint16(tvb, pinfo, offset, tree, "uint16" ); */
2046             break;
2047
2048         case EMULE_MSG_AICH_REQUEST: /* AICH Request: <File Hash> <PartNum> <AICH Hash> */
2049             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2050             partnum = tvb_get_letohs(tvb, offset);
2051             proto_tree_add_uint(tree, hf_emule_aich_partnum, tvb, offset, 2, partnum);
2052             offset += 2;
2053             offset = dissect_emule_aich_root_hash(tvb, pinfo, offset, tree);
2054             break;
2055
2056         case EMULE_MSG_AICH_ANSWER: /* AICH Answer: <File Hash> <PartNum> <AICH Hash> <AICH Hash List> */
2057             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2058             partnum = tvb_get_letohs(tvb, offset);
2059             proto_tree_add_uint(tree, hf_emule_aich_partnum, tvb, offset, 2, partnum);
2060             offset += 2;
2061             offset = dissect_emule_aich_root_hash(tvb, pinfo, offset, tree);
2062             offset = dissect_emule_aich_hash_list(tvb, pinfo, offset, tree);
2063             break;
2064
2065         case EMULE_MSG_MULTIPACKET: /* MultiPacket: <Hash> <Opcodes> */
2066         case EMULE_MSG_MULTIPACKET_ANSWER:
2067         case EMULE_MSG_MULTIPACKET_EXT: /* MultiPacketExt: <Hash> <FileLength> <Opcodes> */
2068             offset = dissect_emule_multipacket(tvb, pinfo, offset, offset+length, tree, msg_type==EMULE_MSG_MULTIPACKET_EXT);
2069             break;
2070
2071         default:
2072             dissect_edonkey_tcp_message(msg_type, tvb, pinfo, offset, length, tree);
2073             break;
2074     }
2075     return;
2076 }
2077
2078 static int dissect_edonkey_udp_message(guint8 msg_type,
2079                                         tvbuff_t *tvb, packet_info *pinfo _U_,
2080                                         int offset, int length, proto_tree *tree)
2081 {
2082     int msg_end, bytes_remaining;
2083     guint8 type;
2084     guint16 min, max;
2085     guint16 ischal;
2086     guint32 chal;
2087     guint32 nusers, nfiles;
2088
2089     if (tree == NULL) return offset;
2090
2091     bytes_remaining = tvb_reported_length_remaining(tvb, offset);
2092     if ((length < 0) || (length > bytes_remaining)) length = bytes_remaining;
2093     if (length <= 0) return offset;
2094
2095     msg_end = offset + length;
2096
2097     switch (msg_type) {
2098         /* EDonkey UDP Messages */
2099         case EDONKEY_MSG_UDP_CALLBACK_REQUEST: /* Callback Request: <Address> <Client ID> */
2100             offset = dissect_edonkey_address(tvb, pinfo, offset, tree);
2101             offset = dissect_edonkey_client_id(tvb, pinfo, offset, tree, FALSE);
2102             break;
2103
2104         case EDONKEY_MSG_UDP_CALLBACK_FAIL: /* Callback Fail: <Client ID> */
2105             offset = dissect_edonkey_client_id(tvb, pinfo, offset, tree, FALSE);
2106             break;
2107
2108         case EDONKEY_MSG_UDP_GET_SERVER_INFO: /* Get Server Info: <Challenge> */
2109             if (length>=2) {
2110                 ischal = tvb_get_letohs(tvb, offset);
2111                 if (ischal==0xf0ff) {
2112                     chal = tvb_get_letohl(tvb, offset);
2113                     proto_tree_add_text(tree, tvb, offset, 4, "Challenge: 0x%08x", chal);
2114                     offset += 4;
2115                 } else {
2116                     offset = dissect_edonkey_string(tvb, pinfo, offset, tree);
2117                 }
2118             }
2119             break;
2120
2121         case EDONKEY_MSG_UDP_SERVER_INFO: /* Server Info: <String> <String>*/
2122             ischal = tvb_get_letohs(tvb, offset);
2123             if (ischal==0xf0ff) {
2124                 chal = tvb_get_letohl(tvb, offset);
2125                 proto_tree_add_text(tree, tvb, offset, 4, "Challenge: 0x%08x", chal);
2126                 offset = dissect_edonkey_metatag_list(tvb, pinfo, offset+4, tree);
2127             } else {
2128                 offset = dissect_edonkey_string(tvb, pinfo, offset, tree);
2129                 offset = dissect_edonkey_string(tvb, pinfo, offset, tree);
2130             }
2131             break;
2132
2133         case EDONKEY_MSG_UDP_SERVER_LIST: /* Server List: <Address List> */
2134             offset = dissect_edonkey_address_list(tvb, pinfo, offset, tree);
2135             break;
2136
2137         case EDONKEY_MSG_UDP_SEARCH_FILE_RESULTS: /* Search File Result: <File Info> */
2138             offset = dissect_edonkey_file_info(tvb, pinfo, offset, tree);
2139             break;
2140
2141         case EDONKEY_MSG_UDP_SEARCH_FILE: /* Search File: <Search query> */
2142             offset = dissect_edonkey_search_query(tvb, pinfo, offset, tree);
2143             break;
2144
2145         case EDONKEY_MSG_UDP_GET_SOURCES:     /* Get Sources: <File Hash> */
2146             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2147             break;
2148
2149         case EDONKEY_MSG_UDP_FOUND_SOURCES: /* Found Sources: <File Hash> <Address List> */
2150             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2151             offset = dissect_edonkey_address_list(tvb, pinfo, offset, tree);
2152             break;
2153
2154         case EDONKEY_MSG_UDP_SERVER_STATUS_REQUEST:  /* Server Status Request: <guint32> */
2155             chal = tvb_get_letohl(tvb, offset);
2156             proto_tree_add_text(tree, tvb, offset, 4, "Challenge: 0x%08x", chal);
2157             offset += 4;
2158             break;
2159
2160         case EDONKEY_MSG_UDP_SERVER_STATUS:  /* Server Status: <guint32> <Nusers> <Nfiles> <Nusersmax> */
2161             chal = tvb_get_letohl(tvb, offset);
2162             proto_tree_add_text(tree, tvb, offset, 4, "Challenge: 0x%08x", chal);
2163             offset += 4;
2164             nusers = tvb_get_letohl(tvb, offset);
2165             nfiles = tvb_get_letohl(tvb, offset+4);
2166             proto_tree_add_text(tree, tvb, offset, 4, "Number of Users: %u", nusers);
2167             proto_tree_add_text(tree, tvb, offset+4, 4, "Number of Files: %u", nfiles);
2168             offset += 8;
2169             if (offset < msg_end) {
2170                 nusers = tvb_get_letohl(tvb, offset);
2171                 proto_tree_add_text(tree, tvb, offset, 4, "Max number of Users: %u", nusers);
2172                 offset += 4;
2173             }
2174             break;
2175
2176             /* Overnet UDP Messages */
2177         case OVERNET_MSG_UDP_CONNECT:    /* Connect:   <Peer (sender) > */
2178         case OVERNET_MSG_UDP_PUBLICIZE:  /* Publicize: <Peer (sender) > */
2179             offset = dissect_overnet_peer(tvb, pinfo, offset, tree);
2180             break;
2181
2182         case OVERNET_MSG_UDP_CONNECT_REPLY:    /* Connect Reply: <guint16 Peer List> */
2183             offset = dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "Overnet Peer", dissect_overnet_peer);
2184             break;
2185
2186         case OVERNET_MSG_UDP_SEARCH:    /* Search: <search type (guint8)> <Hash> */
2187             type = tvb_get_guint8(tvb, offset);
2188             proto_tree_add_text(tree, tvb, offset, 1, "Search Type: %u", type);
2189             offset = dissect_edonkey_hash(tvb, pinfo, offset+1, tree);
2190             break;
2191
2192         case OVERNET_MSG_UDP_SEARCH_INFO:
2193             /* Search Info: <Hash> <search type (guint8)> <min (guint16)> <max (guint16)>*/
2194             offset = dissect_edonkey_hash(tvb, pinfo, offset, tree);
2195             type = tvb_get_guint8(tvb, offset);
2196             min = tvb_get_letohs(tvb, offset+1);
2197             max = tvb_get_letohs(tvb, offset+3);
2198             proto_tree_add_text(tree, tvb, offset, 1, "Search Type: %u", type);
2199             proto_tree_add_text(tree, tvb, offset+1, 4, "Search Range: Min=%u Max=%u", min, max);
2200             break;
2201
2202         case OVERNET_MSG_UDP_SEARCH_NEXT:    /* Search Next: <Hash> <guint8 Peer List> */
2203             offset = dissect_edonkey_hash(tvb, pinfo, offset, tree);
2204             offset = dissect_edonkey_list(tvb, pinfo, offset, tree, 1, "Overnet Peer", dissect_overnet_peer);
2205             break;
2206
2207         case OVERNET_MSG_UDP_SEARCH_RESULT:  /* Search Result: <Hash> <Hash> <Meta tag List> */
2208         case OVERNET_MSG_UDP_PUBLISH:        /* Publish: <Hash> <Hash> <Meta tag List> */
2209             offset = dissect_edonkey_hash(tvb, pinfo, offset, tree);
2210             offset = dissect_edonkey_hash(tvb, pinfo, offset, tree);
2211             offset = dissect_edonkey_metatag_list(tvb, pinfo, offset, tree);
2212             break;
2213
2214         case OVERNET_MSG_UDP_SEARCH_END:  /* Search End: <Hash> */
2215             offset = dissect_edonkey_hash(tvb, pinfo, offset, tree);
2216             break;
2217
2218         case OVERNET_MSG_UDP_PUBLISH_ACK:  /* Publish ACK: <File Hash> */
2219             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2220             break;
2221
2222         case OVERNET_MSG_UDP_IP_QUERY:  /* IP Query: <TCP Port> */
2223             proto_tree_add_item(tree, hf_edonkey_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2224             break;
2225
2226         case OVERNET_MSG_UDP_IP_QUERY_ANSWER:  /* IP Query Answer: <IP> */
2227             offset = dissect_edonkey_client_id(tvb, pinfo, offset, tree, FALSE);
2228             break;
2229
2230         case OVERNET_MSG_UDP_IDENTIFY_REPLY:  /* Identify Reply: <Contact (sender)> */
2231             /* <Contact> ::= <Hash> <Address> */
2232             offset = dissect_edonkey_hash(tvb, pinfo, offset, tree);
2233             offset = dissect_edonkey_address(tvb, pinfo, offset, tree);
2234             break;
2235
2236         case OVERNET_MSG_UDP_IDENTIFY_ACK:  /* Identify Reply: <TCP Port (sender)> */
2237             proto_tree_add_item(tree, hf_edonkey_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2238             break;
2239
2240         case OVERNET_MSG_UDP_FIREWALL_CONNECTION:      /* Firewall Connnection  Ack: <Hash> <TCP Port> */
2241             offset = dissect_edonkey_client_hash(tvb, pinfo, offset, tree);
2242             proto_tree_add_item(tree, hf_edonkey_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2243             break;
2244
2245         case OVERNET_MSG_UDP_FIREWALL_CONNECTION_ACK:  /* Firewall Connnection  Ack: <Hash> */
2246         case OVERNET_MSG_UDP_FIREWALL_CONNECTION_NACK: /* Firewall Connnection NAck: <Hash> */
2247             offset = dissect_edonkey_client_hash(tvb, pinfo, offset, tree);
2248             break;
2249
2250         default:
2251             proto_tree_add_text(tree, tvb, offset, length, "Message Data (%d bytes)", length);
2252             offset+=length;
2253             break;
2254     }
2255
2256     return offset;
2257 }
2258
2259 static int dissect_emule_udp_message(guint8 msg_type,
2260                                       tvbuff_t *tvb, packet_info *pinfo _U_,
2261                                       int offset, int length, proto_tree *tree)
2262 {
2263     int msg_end, bytes_remaining;
2264     guint16 rank;
2265
2266     bytes_remaining = tvb_reported_length_remaining(tvb, offset);
2267     if ((length < 0) || (length > bytes_remaining)) length = bytes_remaining;
2268     if (length <= 0) return offset;
2269
2270     msg_end = offset + length;
2271
2272     switch (msg_type) {
2273         case EMULE_MSG_UDP_REASKFILEPING:     /* Reask File Ping: <File Hash> */
2274             offset = dissect_edonkey_file_hash(tvb, pinfo, offset, tree);
2275             if ( offset + 2 < msg_end ) {
2276                 /* Udp version > 3 */
2277                 offset = dissect_edonkey_file_status( tvb, pinfo, offset, tree );
2278             }
2279
2280             if ( msg_end == offset + 2 ) {
2281                 /* Udp version > 2 */
2282                 proto_tree_add_item( tree, hf_emule_source_count, tvb, offset, 2, ENC_LITTLE_ENDIAN );
2283                 offset += 2;
2284             }
2285             break;
2286
2287         case EMULE_MSG_UDP_REASKACK:          /* Reask ACK:     <eMule Rank>  */
2288             if ( offset + 2 < msg_end ) {
2289                 /* Udp version > 3 */
2290                 offset = dissect_edonkey_file_status( tvb, pinfo, offset, tree );
2291             }
2292
2293             rank = tvb_get_letohs(tvb, offset);
2294             proto_tree_add_text(tree, tvb, offset, 2, "Queue Ranking: %u", rank);
2295             offset += 2;
2296             break;
2297
2298         default:
2299             offset = dissect_edonkey_udp_message(msg_type, tvb, pinfo, offset, length,tree);
2300             break;
2301     }
2302     return offset;
2303 }
2304
2305 static int dissect_kademlia_peer_list_2byte(tvbuff_t *tvb, packet_info *pinfo _U_,
2306                                       int offset, proto_tree *tree)
2307 {
2308     return dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "Peer", dissect_kademlia_peer );
2309 }
2310
2311
2312 static int dissect_kademlia_peer_list_1byte(tvbuff_t *tvb, packet_info *pinfo _U_,
2313                                       int offset, proto_tree *tree)
2314 {
2315     return dissect_edonkey_list(tvb, pinfo, offset, tree, 1, "Peer", dissect_kademlia_peer );
2316 }
2317
2318 static int dissect_kademlia2_peer_list_2byte(tvbuff_t *tvb, packet_info *pinfo _U_,
2319                                       int offset, proto_tree *tree)
2320 {
2321     return dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "Peer", dissect_kademlia2_peer );
2322 }
2323
2324 static int dissect_kademlia2_peer_list_1byte(tvbuff_t *tvb, packet_info *pinfo _U_,
2325                                       int offset, proto_tree *tree)
2326 {
2327     return dissect_edonkey_list(tvb, pinfo, offset, tree, 1, "Peer", dissect_kademlia2_peer );
2328 }
2329
2330 static int dissect_kademlia_tag(tvbuff_t *tvb, packet_info *pinfo _U_,
2331                                 int offset, proto_tree *tree)
2332 {
2333     guint8 type;
2334     guint8 tag_type;
2335     const gchar *str_type;
2336     proto_item *ti;
2337     proto_item* tag_node;
2338     proto_tree *subtree;
2339     int item_start_offset;
2340
2341     item_start_offset = offset;
2342     /* tag_node length is adjusted at the end of this function */
2343     tag_node = proto_tree_add_text( tree, tvb, offset, 1, "Tag " );
2344     subtree = proto_item_add_subtree( tag_node, ett_kademlia_tag );
2345
2346     type = tvb_get_guint8( tvb, offset );
2347     str_type = val_to_str(type, kademlia_tag_types, "Unknown" );
2348
2349     {
2350         proto_item * ti_tagtype;
2351         ti_tagtype = proto_tree_add_item( subtree, hf_kademlia_tag_type, tvb, offset, 1, ENC_LITTLE_ENDIAN );
2352         proto_item_append_text( ti_tagtype, " [%s]", str_type );
2353     }
2354
2355     offset += 1;
2356
2357     {
2358         const gchar *tagname_string;
2359         const gchar *tagname_extended_string;
2360
2361         /* Read tagname */
2362         tag_type = tvb_get_guint8( tvb, offset+2 );
2363         offset = dissect_kademlia_tagname( tvb, pinfo, offset, subtree, &tagname_string, &tagname_extended_string );
2364         if ( strlen( tagname_string ) == 1 ) {
2365             const guint8 tagname_guint = *(guint8*)tagname_string;
2366
2367             proto_item_append_text( tag_node, " 0x%02X [%s] = ", tagname_guint, tagname_extended_string );
2368         }
2369         else
2370             proto_item_append_text( tag_node, " \"%s\" [%s] = ", tagname_string, tagname_extended_string );
2371     }
2372
2373     /* Switch on type */
2374     switch( type )
2375     {
2376         case KADEMLIA_TAGTYPE_HASH:
2377             proto_item_append_text( tag_node, "%s", tvb_bytes_to_str( tvb, offset, 16 ));
2378             offset = dissect_kademlia_tag_hash( tvb, pinfo, offset, subtree );
2379             break;
2380         case KADEMLIA_TAGTYPE_STRING:
2381             {
2382                 const gchar* value;
2383                 offset = dissect_kademlia_tag_string( tvb, pinfo, offset, subtree, &value );
2384
2385                 proto_item_append_text( tag_node, "\"%s\"", value );
2386             }
2387             break;
2388         case KADEMLIA_TAGTYPE_UINT8:
2389             {
2390                 guint8 value;
2391                 ti = proto_tree_add_item( subtree, hf_kademlia_tag_uint8, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2392
2393                 value = tvb_get_guint8( tvb, offset );
2394                 proto_item_append_text( tag_node, "%u (0x%02X)", value, value );
2395                 switch (tag_type) {
2396                     case KADEMLIA_TAG_SOURCETYPE:
2397                         {
2398                             proto_item_append_text(ti," (%s)", val_to_str(value, kademlia_tag_sourcetype, "Unknown"));
2399                         }
2400                         break;
2401                     case KADEMLIA_TAG_ENCRYPTION:
2402                         proto_item_append_text(ti, " (%s)", val_to_str(value, kademlia_tag_encryption, "Unknown"));
2403                         break;
2404                 }
2405                 offset += 1;
2406             }
2407             break;
2408         case KADEMLIA_TAGTYPE_UINT16:
2409             {
2410                 guint16 value;
2411                 proto_tree_add_item( subtree, hf_kademlia_tag_uint16, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2412
2413                 value = tvb_get_letohs( tvb, offset );
2414                 proto_item_append_text( tag_node, "%u (0x%04X)", value, value );
2415
2416                 offset += 2;
2417             }
2418             break;
2419         case KADEMLIA_TAGTYPE_UINT64:
2420             {
2421                 guint64 value;
2422                 proto_tree_add_item( subtree, hf_kademlia_tag_uint64, tvb, offset, 8, ENC_LITTLE_ENDIAN);
2423
2424                 value = tvb_get_letoh64( tvb, offset );
2425                 proto_item_append_text( tag_node, "%" G_GINT64_MODIFIER "u (0x%08" G_GINT64_MODIFIER "X)", value, value );
2426
2427                 offset += 8;
2428             }
2429             break;
2430         case KADEMLIA_TAGTYPE_UINT32:
2431            {
2432                 guint32 value;
2433                 /* show ip as dotted decimal */
2434                 switch( tag_type) {
2435                     case KADEMLIA_TAG_SERVERIP:
2436                     case KADEMLIA_TAG_SOURCEIP:
2437                     {
2438                         int ipa = 0, ipb = 0, ipc = 0, ipd = 0;
2439                         proto_tree_add_item( subtree, hf_kademlia_tag_ipv4, tvb, offset, 4, ENC_LITTLE_ENDIAN);
2440                         value = tvb_get_letohl( tvb, offset );
2441                         ipa = (value / (256*256*256)) % 256;
2442                         ipb = (value / (256*256)) % 256;
2443                         ipc = (value / 256) % 256;
2444                         ipd = value % 256;
2445                         proto_item_append_text( tag_node, "%u.%u.%u.%u (0x%02X) ", ipa, ipb, ipc, ipd, value );
2446                     }
2447                     break;
2448                     default:
2449                         proto_tree_add_item( subtree, hf_kademlia_tag_uint32, tvb, offset, 4, ENC_LITTLE_ENDIAN);
2450                         value = tvb_get_letohl( tvb, offset );
2451                         proto_item_append_text( tag_node, "%u (0x%02X) ", value, value );
2452                 }
2453
2454                 offset += 4;
2455             }
2456             break;
2457         case KADEMLIA_TAGTYPE_FLOAT32:
2458             {
2459                 float value;
2460                 proto_tree_add_item( subtree, hf_kademlia_tag_float, tvb, offset, 4, ENC_LITTLE_ENDIAN);
2461
2462                 value = tvb_get_letohieee_float( tvb, offset );
2463                 proto_item_append_text( tag_node, "%f", value );
2464
2465                 offset += 4;
2466             }
2467             break;
2468         case KADEMLIA_TAGTYPE_BSOB:
2469             {
2470                 const gchar* value;
2471                 offset = dissect_kademlia_tag_bsob( tvb, pinfo, offset, subtree, &value );
2472                 proto_item_append_text( tag_node, "%s", value );
2473             }
2474             break;
2475         default:
2476             proto_tree_add_text(tree, tvb, offset, 1, "Tag value not decoded for type: 0x%02X", type );
2477     }
2478
2479     proto_item_append_text( tag_node, " (Type: %s)", str_type );
2480
2481     proto_item_set_len( tag_node, offset - item_start_offset );
2482
2483     return offset;
2484 }
2485
2486 static int dissect_kademlia_taglist(tvbuff_t *tvb, packet_info *pinfo _U_,
2487                                 int offset, proto_tree *tree)
2488 {
2489     return dissect_edonkey_list(tvb, pinfo, offset, tree, 1, "Tag", dissect_kademlia_tag );
2490 }
2491
2492
2493 static int dissect_kademlia_publish_req_entry_file(tvbuff_t *tvb, packet_info *pinfo _U_,
2494                                           int offset, proto_tree *tree)
2495 {
2496     /* Get the hash */
2497     offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_file_id);
2498     /* Read all the kad tags */
2499     return dissect_kademlia_taglist( tvb, pinfo, offset, tree );
2500 }
2501
2502 static int dissect_kademlia_publish_req_entry_peer(tvbuff_t *tvb, packet_info *pinfo _U_,
2503                                           int offset, proto_tree *tree)
2504 {
2505     /* Get the hash */
2506     offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_peer_id);
2507     /* Read all the kad tags */
2508     return dissect_kademlia_taglist( tvb, pinfo, offset, tree );
2509 }
2510
2511
2512 static int dissect_kademlia_search_result(tvbuff_t *tvb, packet_info *pinfo _U_,
2513                                           int offset, proto_tree *tree)
2514 {
2515     /* Get the hash */
2516     offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_hash);
2517     /* Read all the kad tags */
2518     return dissect_kademlia_taglist( tvb, pinfo, offset, tree );
2519 }
2520
2521 static int dissect_kademlia_search_expression_tree(tvbuff_t *tvb, packet_info *pinfo _U_,
2522                                           int offset, proto_tree *tree)
2523 {
2524     int op, item_start_offset;
2525     proto_item* ti;
2526
2527     item_start_offset = offset;
2528     op = tvb_get_guint8(tvb, offset);
2529     ti = proto_tree_add_text(tree, tvb, offset, 1, "SearchExp type: %x ", op);
2530     ++offset;
2531     tree = proto_item_add_subtree( ti, ett_kademlia_search_expression );
2532     switch( op ) {
2533         case 0: /* Bool op */
2534             {
2535                 int boolop;
2536                 const char* bool_op_label;
2537
2538                 proto_item_append_text( ti, "[BoolOp]" );
2539
2540                 boolop = tvb_get_guint8(tvb, offset);
2541
2542                 switch( boolop ) {
2543                     case 0: /* AND */
2544                         bool_op_label = "AND";
2545                         break;
2546                     case 1: /* OR */
2547                         bool_op_label = "OR";
2548                         break;
2549                     case 2: /* NAND */
2550                         bool_op_label = "NAND";
2551                         break;
2552                     default:
2553                         bool_op_label = "Unknown";
2554                 };
2555                 proto_tree_add_text(tree, tvb, offset, 1, "Bool op: %x [%s]", boolop, bool_op_label );
2556                 ++offset;
2557
2558                 /* Left */
2559                 offset = dissect_kademlia_search_expression_tree( tvb, pinfo, offset, tree );
2560                 /* Right */
2561                 offset = dissect_kademlia_search_expression_tree( tvb, pinfo, offset, tree );
2562
2563                 break;
2564             }
2565
2566         case 1: /* String */
2567             proto_item_append_text( ti, "[String]" );
2568             offset = dissect_kademlia_string( tvb, pinfo, offset, tree );
2569             break;
2570         case 2: /* Meta Tag */
2571             proto_item_append_text( ti, "[MetaTag]" );
2572             /* tag value */
2573             offset = dissect_edonkey_string( tvb, pinfo, offset, tree );
2574             /* tag name */
2575             offset = dissect_kademlia_tagname( tvb, pinfo, offset, tree, NULL, NULL );
2576             break;
2577         case 3: /* Min/Max - 32bit! */
2578             proto_item_append_text( ti, "[32bitOp]" );
2579             offset = dissect_kademlia_search_condition_argument_uint32( tvb, pinfo, offset, tree );
2580             offset = dissect_kademlia_search_condition( tvb, pinfo, offset, tree );
2581             /* tag name */
2582             offset = dissect_kademlia_tagname( tvb, pinfo, offset, tree, NULL, NULL );
2583             break;
2584         case 8: /* Min/Max - 64bit! */
2585             proto_item_append_text( ti, "[64bitOp]" );
2586             offset = dissect_kademlia_search_condition_argument_uint64( tvb, pinfo, offset, tree );
2587             offset = dissect_kademlia_search_condition( tvb, pinfo, offset, tree );
2588             /* tag name */
2589             offset = dissect_kademlia_tagname( tvb, pinfo, offset, tree, NULL, NULL );
2590             break;
2591         default:
2592             proto_tree_add_text(tree, tvb, offset, 1, "NOT DECODED op %x", op );
2593     }
2594     proto_item_set_len( ti, offset - item_start_offset );
2595     return offset;
2596 }
2597
2598 static int dissect_kademlia2_prolog( tvbuff_t *tvb, packet_info *pinfo _U_,
2599                                          int offset, proto_tree *tree)
2600 {
2601     int kad_version;
2602     proto_item* ti;
2603
2604     offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_peer_id);
2605     offset = dissect_kademlia_tcp_port(tvb, pinfo, offset, tree);
2606
2607     kad_version = tvb_get_guint8(tvb, offset);
2608     /* ti = proto_tree_add_text(tree, tvb, offset, 1, "Kad Version: %d", kad_version ); */
2609     ti = proto_tree_add_item(tree, hf_kademlia_version, tvb, offset, 1, ENC_BIG_ENDIAN);
2610     proto_item_append_text(ti, "%s", val_to_str(kad_version, kademlia_versions, " Unknown"));
2611     offset++;
2612
2613     return offset;
2614 }
2615
2616 static int dissect_kademlia_uload( tvbuff_t *tvb, packet_info *pinfo _U_,
2617                                          int offset, proto_tree *tree)
2618 {
2619     int uLoad;
2620     uLoad = tvb_get_guint8(tvb, offset);
2621     proto_tree_add_text(tree, tvb, offset, 1, "uLoad: %d", uLoad );
2622     return offset +1;
2623
2624 }
2625 static int dissect_kademlia_udp_message(guint8 msg_type,
2626                                          tvbuff_t *tvb, packet_info *pinfo _U_,
2627                                          int offset, int length, proto_tree *tree)
2628 {
2629     int msg_end, bytes_remaining;
2630     proto_item *hidden_item;
2631
2632     bytes_remaining = tvb_reported_length_remaining(tvb, offset);
2633     if ((length < 0) || (length > bytes_remaining)) length = bytes_remaining;
2634     if (length <= 0) return offset;
2635
2636     hidden_item = proto_tree_add_item(tree, hf_kademlia, tvb, offset, 1, ENC_BIG_ENDIAN);
2637     PROTO_ITEM_SET_HIDDEN(hidden_item);
2638
2639     msg_end = offset + length;
2640
2641     switch (msg_type) {
2642         case KADEMLIA_BOOTSTRAP_REQ:/* <PEER [25]> */
2643         case KADEMLIA_HELLO_REQ:
2644         case KADEMLIA_HELLO_RES:
2645             offset = dissect_kademlia_peer(tvb, pinfo, offset, tree);
2646             break;
2647         case KADEMLIA2_BOOTSTRAP_REQ:
2648             offset = dissect_kademlia2_prolog( tvb, pinfo, offset, tree );
2649             break;
2650
2651         case KADEMLIA2_HELLO_REQ:
2652         case KADEMLIA2_HELLO_RES:
2653             offset = dissect_kademlia2_prolog( tvb, pinfo, offset, tree );
2654             offset = dissect_kademlia_taglist( tvb, pinfo, offset, tree );
2655             break;
2656         case KADEMLIA_BOOTSTRAP_RES:  /* <CNT [2]> <PEER [25]>*(CNT) */
2657             offset = dissect_kademlia_peer_list_2byte( tvb, pinfo, offset, tree );
2658             break;
2659         case KADEMLIA2_BOOTSTRAP_RES:
2660             offset = dissect_kademlia2_prolog( tvb, pinfo, offset, tree );
2661             offset = dissect_kademlia2_peer_list_2byte( tvb, pinfo, offset, tree );
2662             break;
2663
2664         case KADEMLIA2_SEARCH_SOURCE_REQ:
2665             {
2666                 guint16 startPos;
2667                 guint64 filesize;
2668                 offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2669                 /* start pos */
2670                 startPos = tvb_get_letohs( tvb, offset );
2671                 proto_tree_add_text(tree, tvb, offset, 2, "Start position: %d", startPos );
2672                 offset +=2;
2673                 /* filesize */
2674                 filesize = tvb_get_letoh64( tvb, offset );
2675                 proto_tree_add_text(tree, tvb, offset, 8, "Filesize: %" G_GINT64_MODIFIER "u", filesize );
2676                 offset +=8;
2677             }
2678             break;
2679
2680         case KADEMLIA_SEARCH_NOTES_REQ: /* <HASH (key) [16]> */
2681             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_hash);
2682             break;
2683
2684         case KADEMLIA2_SEARCH_KEY_REQ:
2685             {
2686               guint16 startPos;
2687               offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2688               /* start pos */
2689               startPos = tvb_get_letohs( tvb, offset );
2690               proto_tree_add_text(tree, tvb, offset, 2, "Start position: %d", startPos );
2691               offset += 2;
2692             }
2693             break;
2694         case KADEMLIA2_SEARCH_NOTES_REQ:
2695
2696         case KADEMLIA_PUBLISH_RES:
2697             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2698             if (offset<msg_end) {
2699                 offset = dissect_kademlia_uload( tvb, pinfo, offset, tree );
2700             }
2701             break;
2702         case KADEMLIA2_PUBLISH_RES:
2703             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2704             offset = dissect_kademlia_uload( tvb, pinfo, offset, tree );
2705             break;
2706
2707         case KADEMLIA_REQ: /* <TYPE [1]> <HASH (target) [16]> <HASH (receiver) 16> */
2708         case KADEMLIA2_REQ:
2709             {
2710                 int type;
2711                 guint8 target_id[16];
2712                 guint8 recipients_id[16];
2713                 proto_item *ti;
2714                 int i, j, k, l;
2715                 char binarray[129];
2716
2717                 type = tvb_get_guint8(tvb, offset);
2718                 ti = proto_tree_add_uint_format_value(tree, hf_kademlia_request_type, tvb, offset, 1, type, "0x%02x", type );
2719                 proto_item_append_text(ti, "%s", val_to_str(type, kademlia_parameter, " Unknown"));
2720                 offset +=1;
2721
2722                 /* get target id */
2723                 for (i=0; i<4; i++) {
2724                   for (j=3; j>=0; j--) {
2725                     l = (j+4*i);
2726                     target_id[l] = tvb_get_guint8(tvb, offset + abs(8*i-(l-3)));
2727                   }
2728                 }
2729
2730                 offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2731
2732                 /* get recipient's id */
2733                 for (i=0; i<4; i++) {
2734                   for (j=3; j>=0; j--) {
2735                     l = (j+4*i);
2736                     recipients_id[l] = tvb_get_guint8(tvb, offset + abs(8*i-(l-3)));
2737                   }
2738                 }
2739
2740                 offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_recipients_id);
2741
2742                 /* target_id XOR recipients_id */
2743                 for (i=0; i<16; i++) {
2744                   k = 128;
2745                   l = target_id[i]^recipients_id[i];
2746                   for(j=8*i; j<8*i+8; j++) {
2747                     if (l >= k) {
2748                       binarray[j] = '1';
2749                       l = l-k;
2750                     }
2751                     else
2752                       binarray[j] = '0';
2753                     k = k/2;
2754                   }
2755                 }
2756                 binarray[128] = '\0';
2757                 proto_tree_add_string_format_value(tree, hf_kademlia_distance, tvb, offset, 0, binarray, "%s", binarray);
2758             }
2759             break;
2760
2761         case KADEMLIA_RES:     /* <HASH (target) [16]> <CNT> <PEER [25]>*(CNT) */
2762             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2763             offset = dissect_kademlia_peer_list_1byte( tvb, pinfo, offset, tree );
2764             break;
2765         case KADEMLIA2_RES:
2766             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2767             offset = dissect_kademlia2_peer_list_1byte( tvb, pinfo, offset, tree );
2768             break;
2769
2770         case KADEMLIA_FIREWALLED_RES: /* <IP (sender) [4]> */
2771             offset = dissect_kademlia_ip_address( tvb, pinfo, offset, tree );
2772             break;
2773
2774         case KADEMLIA_FIREWALLED_REQ: /* <TCPPORT (sender) [2]> */
2775             offset = dissect_kademlia_tcp_port( tvb, pinfo, offset, tree );
2776             break;
2777         case KADEMLIA_CALLBACK_REQ:
2778         case KADEMLIA_FINDBUDDY_REQ:
2779         case KADEMLIA_FINDBUDDY_RES:
2780             /* buddy id */
2781             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_hash);
2782             /* userid */
2783             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_hash);
2784             offset = dissect_kademlia_tcp_port( tvb, pinfo, offset, tree );
2785             break;
2786         case KADEMLIA2_PUBLISH_SOURCE_REQ:
2787             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_file_id);
2788             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_peer_id);
2789             offset = dissect_kademlia_taglist( tvb, pinfo, offset, tree );
2790             break;
2791         case KADEMLIA_SEARCH_REQ:
2792             {
2793                 int restrictive;
2794                 /* Target (16bytes) */
2795                 offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2796                 /* Restrictive (1 byte) 0/1 */
2797                 restrictive = tvb_get_guint8(tvb, offset);
2798                 proto_tree_add_text(tree, tvb, offset, 1, "Restrictive: %x", restrictive );
2799                 offset +=1;
2800
2801                 if ( offset < msg_end && restrictive )
2802                     offset = dissect_kademlia_search_expression_tree( tvb, pinfo, offset, tree );
2803             }
2804             break;
2805         case KADEMLIA_SEARCH_RES:
2806             /* Target */
2807             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2808             /* Results list */
2809             offset = dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "Result", dissect_kademlia_search_result );
2810             break;
2811         case KADEMLIA2_SEARCH_RES:
2812             /* Sender */
2813             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_sender_id);
2814             /* Target */
2815             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_target_id);
2816             /* Results list */
2817             offset = dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "Result", dissect_kademlia_search_result );
2818             break;
2819         case KADEMLIA2_PUBLISH_KEY_REQ:
2820             {
2821                 /* Keyword Hash */
2822                 offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_keyword_hash);
2823                 /* Results list */
2824                 offset = dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "StuffToPublish", dissect_kademlia_publish_req_entry_file );
2825                 break;
2826             }
2827         case KADEMLIA_PUBLISH_REQ: /*   0x40    // <HASH (key) [16]> <CNT1 [2]> (<HASH (target) [16]> <CNT2 [2]> <META>*(CNT2))*(CNT1) */
2828             {
2829                 guint8 tagname_value=0, taglist_size, type;
2830                 int i=1, j=34;
2831
2832                 /* check if TAG_SOURCETYPE is set */
2833                 taglist_size = tvb_get_guint8(tvb, offset + j);
2834                 j++;
2835
2836                 while(i <= taglist_size) {
2837                   type = tvb_get_guint8(tvb, offset + j);
2838                   j +=3;
2839                   tagname_value = tvb_get_guint8(tvb, offset + j);
2840                   if (tagname_value == 0xff)
2841                     i = taglist_size;
2842                   j++;
2843                   switch(type) {
2844                     case KADEMLIA_TAGTYPE_HASH:
2845                         j += 16;
2846                         break;
2847                     case KADEMLIA_TAGTYPE_STRING:
2848                     {
2849                         guint16 string_length = tvb_get_letohs(tvb, offset+j);
2850                         j += 2 + string_length;
2851                         break;
2852                     }
2853                     case KADEMLIA_TAGTYPE_UINT8:
2854                         j += 1;
2855                         break;
2856                     case KADEMLIA_TAGTYPE_UINT16:
2857                         j += 2;
2858                         break;
2859                     case KADEMLIA_TAGTYPE_UINT32:
2860                     case KADEMLIA_TAGTYPE_FLOAT32:
2861                         j += 4;
2862                         break;
2863                     case KADEMLIA_TAGTYPE_UINT64:
2864                         j += 8;
2865                         break;
2866                     case KADEMLIA_TAGTYPE_BSOB:
2867                     {
2868                       guint16 bsob_length = tvb_get_guint8(tvb, offset);
2869                       j += 1 + bsob_length;
2870                       break;
2871                     }
2872                   }
2873                   i++;
2874                 }
2875
2876                 switch (tagname_value) {
2877                     case KADEMLIA_TAG_SOURCETYPE:
2878                         {
2879                             /* Target */
2880                             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_file_id);
2881                             /* Results list */
2882                             offset = dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "StuffToPublish", dissect_kademlia_publish_req_entry_peer);
2883                         }
2884                         break;
2885                     default:
2886                         {
2887                             /* Target */
2888                             offset = dissect_kademlia_hash(tvb, pinfo, offset, tree, &hf_kademlia_keyword_hash);
2889                             /* Results list */
2890                             offset = dissect_edonkey_list(tvb, pinfo, offset, tree, 2, "StuffToPublish", dissect_kademlia_publish_req_entry_file);
2891                         }
2892                 }
2893             }
2894     }
2895
2896     return offset;
2897 }
2898
2899 static int dissect_kademlia_udp_compressed_message(guint8 msg_type,
2900                                                     tvbuff_t *tvb, packet_info *pinfo _U_,
2901                                                     int offset, int length, proto_tree *tree)
2902 {
2903     tvbuff_t *tvbraw = NULL;
2904
2905
2906     tvbraw = tvb_child_uncompress(tvb, tvb, offset, length);
2907
2908     if (tvbraw) {
2909         guint32 raw_length;
2910
2911         raw_length = tvb_length( tvbraw );
2912         add_new_data_source(pinfo, tvbraw, "Decompressed Data");
2913
2914         dissect_kademlia_udp_message( msg_type, tvbraw, pinfo, 0, raw_length, tree );
2915         offset += length;
2916     } else {
2917         proto_tree_add_text( tree, tvb, offset, length, "Broken Compressed data (%d bytes)", length);
2918     }
2919     return offset;
2920 }
2921
2922
2923 static guint get_edonkey_tcp_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
2924 {
2925     guint32 msg_len;
2926
2927     /*
2928      * Get the length of the eDonkey packet.
2929      */
2930     msg_len = tvb_get_letohl(tvb, offset+1);
2931
2932     /*
2933      * That length doesn't include the header; add that in.
2934      * XXX - what if it overflows?
2935      */
2936     return msg_len + EDONKEY_TCP_HEADER_LENGTH;
2937 }
2938
2939 static void dissect_edonkey_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2940 {
2941     proto_item *ti;
2942     proto_tree *edonkey_tree = NULL, *edonkey_msg_tree = NULL, *emule_zlib_tree = NULL;
2943     int offset, bytes;
2944     guint8 protocol, msg_type;
2945     guint32 msg_len;
2946     const gchar *protocol_name, *message_name;
2947     void  (*dissector)(guint8, tvbuff_t*, packet_info*, int, int, proto_tree*);
2948     tvbuff_t *tvbraw = NULL;
2949
2950     col_set_str(pinfo->cinfo, COL_PROTOCOL, "eDonkey");
2951
2952     if (tree) {
2953         ti = proto_tree_add_item(tree, proto_edonkey, tvb, 0, -1, FALSE);
2954         edonkey_tree = proto_item_add_subtree(ti, ett_edonkey);
2955     }
2956
2957     offset = 0;
2958     protocol = tvb_get_guint8(tvb, offset);
2959     msg_len = tvb_get_letohl(tvb, offset+1);
2960
2961     protocol_name = match_strval(protocol, edonkey_protocols);
2962     if (protocol_name == NULL) {
2963         /* Not a recognized eDonkey protocol - probably a continuation */
2964         col_set_str(pinfo->cinfo, COL_INFO, "eDonkey Continuation");
2965         if (edonkey_tree) {
2966             bytes = tvb_length_remaining(tvb, offset);
2967             proto_tree_add_text(edonkey_tree, tvb, 0, -1, "Continuation data (%d bytes)", bytes);
2968         }
2969         return;
2970     }
2971
2972     /* Add edonkey message tree */
2973     if (edonkey_tree) {
2974         ti = proto_tree_add_item(edonkey_tree, hf_edonkey_message, tvb,
2975                                  offset, EDONKEY_TCP_HEADER_LENGTH + msg_len, ENC_NA);
2976         edonkey_msg_tree = proto_item_add_subtree(ti, ett_edonkey_message);
2977
2978         proto_tree_add_uint_format(edonkey_msg_tree, hf_edonkey_protocol, tvb, offset, 1, protocol,
2979                                    "Protocol: %s (0x%02x)", protocol_name, protocol);
2980         proto_tree_add_uint(edonkey_msg_tree, hf_edonkey_message_length, tvb, offset+1, 4, msg_len);
2981     }
2982
2983
2984     /* Skip past the EDONKEY Header */
2985     offset += EDONKEY_TCP_HEADER_LENGTH;
2986
2987     if(tvb_reported_length_remaining(tvb, offset) <= 0) {
2988         /* There is not enough space for the msg_type - mark as fragment */
2989         if (check_col(pinfo->cinfo, COL_INFO)) {
2990             col_append_fstr(pinfo->cinfo, COL_INFO, "%s TCP Message Fragment", protocol_name);
2991         }
2992         return;
2993     }
2994
2995     if (check_col(pinfo->cinfo, COL_INFO)) {
2996         col_append_sep_fstr(pinfo->cinfo, COL_INFO, ", ", "%s TCP", protocol_name);
2997     }
2998
2999     msg_type = tvb_get_guint8(tvb, offset);
3000     switch (protocol) {
3001         case EDONKEY_PROTO_EDONKEY:
3002             message_name =  val_to_str(msg_type, edonkey_tcp_msgs, "Unknown");
3003             dissector = dissect_edonkey_tcp_message;
3004             break;
3005
3006         case EDONKEY_PROTO_EMULE_EXT:
3007             message_name = val_to_str(msg_type, emule_tcp_msgs,
3008                                       val_to_str(msg_type, edonkey_tcp_msgs, "Unknown"));
3009             dissector = dissect_emule_tcp_message;
3010             break;
3011
3012         case EDONKEY_PROTO_EMULE_COMP:
3013             /*
3014              * These ought to be exactly the same as standard eDonkey (0xe5) messages,
3015              * except that the payload (after the type byte) is a zlib compressed
3016              * stream.
3017              */
3018             message_name = val_to_str(msg_type, edonkey_tcp_msgs, "Unknown");
3019             tvbraw = tvb_child_uncompress(tvb, tvb, offset+1, msg_len-1);
3020             if (tvbraw) {
3021               dissector = dissect_edonkey_tcp_message;
3022               break;
3023             }
3024
3025         default:
3026             message_name = "Unknown";
3027             dissector = NULL;
3028             break;
3029     }
3030
3031     if (check_col(pinfo->cinfo, COL_INFO)) {
3032         col_append_fstr(pinfo->cinfo, COL_INFO, ": %s", message_name);
3033     }
3034
3035
3036     if (edonkey_msg_tree) {
3037         proto_tree_add_uint_format(edonkey_msg_tree, hf_edonkey_message_type, tvb, offset, 1, msg_type,
3038                                    "Message Type: %s (0x%02x)", message_name, msg_type);
3039         if (dissector && (msg_len > 1)) {
3040           if (!tvbraw) {
3041             (*dissector)(msg_type, tvb, pinfo, offset+1, msg_len-1, edonkey_msg_tree);
3042           } else {
3043             ti = proto_tree_add_item(edonkey_msg_tree, hf_emule_zlib, tvb,
3044                                      offset+1, msg_len-1, ENC_NA);
3045             emule_zlib_tree = proto_item_add_subtree(ti, ett_emule_zlib);
3046             add_new_data_source(pinfo, tvbraw, "Decompressed Data");
3047             (*dissector)(msg_type, tvbraw, pinfo, 0, tvb_length(tvbraw), emule_zlib_tree);
3048           }
3049         }
3050     }
3051 }
3052
3053 static void dissect_edonkey_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3054 {
3055     col_clear(pinfo->cinfo, COL_INFO);
3056
3057     tcp_dissect_pdus(tvb, pinfo, tree, edonkey_desegment,
3058                      EDONKEY_TCP_HEADER_LENGTH, get_edonkey_tcp_pdu_len,
3059                      dissect_edonkey_tcp_pdu);
3060 }
3061
3062 static void dissect_edonkey_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3063 {
3064     proto_item *ti;
3065     proto_tree *edonkey_tree = NULL, *edonkey_msg_tree = NULL;
3066     int offset;
3067     guint8 protocol, msg_type;
3068     const gchar *protocol_name, *message_name;
3069
3070     col_set_str(pinfo->cinfo, COL_PROTOCOL, "eDonkey");
3071
3072     col_set_str(pinfo->cinfo, COL_INFO, "eDonkey UDP Message");
3073
3074     if (tree) {
3075         ti = proto_tree_add_item(tree, proto_edonkey, tvb, 0, -1, FALSE);
3076         edonkey_tree = proto_item_add_subtree(ti, ett_edonkey);
3077     }
3078
3079     offset = 0;
3080     /* eDonkey UDP message - Assume that there is one message per packet */
3081     if (tvb_length_remaining(tvb, offset) >= EDONKEY_UDP_HEADER_LENGTH) {
3082         protocol = tvb_get_guint8(tvb, offset);
3083         msg_type = tvb_get_guint8(tvb, offset+1);
3084         protocol_name = val_to_str(protocol, edonkey_protocols, "Unknown");
3085
3086         if (protocol == EDONKEY_PROTO_KADEMLIA || protocol == EDONKEY_PROTO_KADEMLIA_COMP
3087             || protocol == EDONKEY_PROTO_ADU_KADEMLIA || protocol == EDONKEY_PROTO_ADU_KADEMLIA_COMP)
3088             message_name = val_to_str( msg_type, kademlia_msgs, "Unknown");
3089         else
3090             message_name = val_to_str(msg_type, edonkey_udp_msgs, "Unknown");
3091
3092         if (check_col(pinfo->cinfo, COL_INFO)) {
3093             col_add_fstr(pinfo->cinfo, COL_INFO, "%s UDP: %s", protocol_name, message_name);
3094         }
3095
3096         if (edonkey_tree) {
3097             int remainingLength, extraBytes;
3098
3099             ti = proto_tree_add_item(edonkey_tree, hf_edonkey_message, tvb, offset, -1, ENC_NA);
3100             edonkey_msg_tree = proto_item_add_subtree(ti, ett_edonkey_message);
3101
3102             proto_tree_add_uint_format(edonkey_msg_tree, hf_edonkey_protocol, tvb, offset, 1, protocol,
3103                                        "Protocol: %s (0x%02x)", protocol_name, protocol);
3104             proto_tree_add_uint_format(edonkey_msg_tree, hf_edonkey_message_type, tvb, offset+1, 1, msg_type,
3105                                        "Message Type: %s (0x%02x)", message_name, msg_type);
3106
3107             offset += EDONKEY_UDP_HEADER_LENGTH;
3108             remainingLength = tvb_length_remaining( tvb, offset );
3109
3110             switch (protocol) {
3111                 case EDONKEY_PROTO_EDONKEY:
3112                     offset = dissect_edonkey_udp_message(msg_type, tvb, pinfo, offset, remainingLength, edonkey_msg_tree);
3113                     break;
3114
3115                 case EDONKEY_PROTO_EMULE_EXT:
3116                     offset = dissect_emule_udp_message(msg_type, tvb, pinfo, offset, remainingLength, edonkey_msg_tree);
3117                     break;
3118
3119                 case EDONKEY_PROTO_ADU_KADEMLIA:
3120                 case EDONKEY_PROTO_KADEMLIA:
3121                     offset = dissect_kademlia_udp_message(msg_type, tvb, pinfo, offset, remainingLength, edonkey_msg_tree);
3122                     break;
3123
3124                 case EDONKEY_PROTO_ADU_KADEMLIA_COMP:
3125                 case EDONKEY_PROTO_KADEMLIA_COMP:
3126                     offset = dissect_kademlia_udp_compressed_message(msg_type, tvb, pinfo, offset, remainingLength, edonkey_msg_tree);
3127                     break;
3128
3129                 default:
3130                     break;
3131             }
3132
3133             extraBytes = tvb_length_remaining( tvb, offset );
3134
3135             if ( extraBytes > 0 ) {
3136                 /* trailing garbage or broken packet */
3137                 proto_tree_add_uint_format(tree, hf_edonkey_unparsed_data_length, tvb, offset, extraBytes, extraBytes,
3138                                            "Trailing/Undecoded data: %d bytes", extraBytes );
3139             }
3140         }
3141     }
3142 }
3143
3144 void proto_register_edonkey(void) {
3145
3146     static hf_register_info hf[] = {
3147         { &hf_edonkey_message,
3148             { "eDonkey Message", "edonkey.message",
3149                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3150         { &hf_edonkey_protocol,
3151             { "Protocol", "edonkey.protocol",
3152                 FT_UINT8, BASE_HEX, VALS(edonkey_protocols), 0, "eDonkey Protocol", HFILL } },
3153         { &hf_edonkey_message_length,
3154             { "Message Length", "edonkey.message.length",
3155                 FT_UINT32, BASE_DEC, NULL, 0, "eDonkey Message Length", HFILL } },
3156         { &hf_edonkey_message_type,
3157             { "Message Type", "edonkey.message.type",
3158                 FT_UINT8, BASE_HEX, NULL, 0, "eDonkey Message Type", HFILL } },
3159         { &hf_edonkey_client_hash,
3160             { "Client Hash", "edonkey.client_hash",
3161                 FT_BYTES, BASE_NONE, NULL, 0, "eDonkey Client Hash", HFILL } },
3162         { &hf_edonkey_server_hash,
3163             { "Server Hash", "edonkey.server_hash",
3164                 FT_BYTES, BASE_NONE, NULL, 0, "eDonkey Server Hash", HFILL } },
3165         { &hf_edonkey_file_hash,
3166             { "File Hash", "edonkey.file_hash",
3167                 FT_BYTES, BASE_NONE, NULL, 0, "eDonkey File Hash", HFILL } },
3168         { &hf_edonkey_client_id,
3169             { "Client ID", "edonkey.clientid",
3170                 FT_IPv4, BASE_NONE, NULL, 0, "eDonkey Client ID", HFILL } },
3171         { &hf_edonkey_ip,
3172             { "IP", "edonkey.ip",
3173                 FT_IPv4, BASE_NONE, NULL, 0, "eDonkey IP", HFILL } },
3174         { &hf_edonkey_port,
3175             { "Port", "edonkey.port",
3176                 FT_UINT16, BASE_DEC, NULL, 0, "eDonkey Port", HFILL } },
3177         { &hf_edonkey_metatag,
3178             { "eDonkey Meta Tag", "edonkey.metatag",
3179                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3180         { &hf_edonkey_metatag_type,
3181             { "Meta Tag Type", "edonkey.metatag.type",
3182                 FT_UINT8, BASE_HEX, NULL, 0, "eDonkey Meta Tag Type", HFILL } },
3183         { &hf_edonkey_metatag_id,
3184             { "Meta Tag ID", "edonkey.metatag.id",
3185                 FT_UINT8, BASE_HEX, NULL, 0, "eDonkey Meta Tag ID", HFILL } },
3186         { &hf_edonkey_metatag_name,
3187             { "Meta Tag Name", "edonkey.metatag.name",
3188                 FT_STRING, BASE_NONE, NULL, 0, "eDonkey Meta Tag Name", HFILL } },
3189         { &hf_edonkey_metatag_namesize,
3190             { "Meta Tag Name Size", "edonkey.metatag.namesize",
3191                 FT_UINT16, BASE_DEC, NULL, 0, "eDonkey Meta Tag Name Size", HFILL } },
3192         { &hf_edonkey_search,
3193             { "eDonkey Search", "edonkey.search",
3194                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3195         { &hf_edonkey_hash,
3196             { "Hash", "edonkey.hash",
3197                 FT_BYTES, BASE_NONE, NULL, 0, "eDonkey Hash", HFILL } },
3198         { &hf_edonkey_string,
3199             { "String", "edonkey.string",
3200                 FT_STRING, BASE_NONE, NULL, 0, "eDonkey String", HFILL } },
3201         { &hf_edonkey_string_length,
3202             { "String Length", "edonkey.string_length",
3203                 FT_UINT16, BASE_DEC, NULL, 0, "eDonkey String Length", HFILL } },
3204         { &hf_edonkey_part_count,
3205             { "Part Count", "edonkey.part_count",
3206                 FT_UINT16, BASE_DEC, NULL, 0, "eDonkey Part Count", HFILL } },
3207         { &hf_edonkey_file_status,
3208             { "File Status", "edonkey.file_status",
3209                 FT_BYTES, BASE_NONE, NULL, 0, "eDonkey File Status", HFILL } },
3210         { &hf_edonkey_directory,
3211             { "Directory", "edonkey.directory",
3212                 FT_STRING, BASE_NONE, NULL, 0, "eDonkey Directory", HFILL } },
3213         { &hf_edonkey_fileinfo,
3214             { "eDonkey File Info", "edonkey.fileinfo",
3215                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3216         { &hf_edonkey_serverinfo,
3217             { "eDonkey Server Info", "edonkey.serverinfo",
3218                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3219         { &hf_edonkey_clientinfo,
3220             { "eDonkey Client Info", "edonkey.clientinfo",
3221                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3222         { &hf_emule_public_key,
3223             { "Public Key", "edonkey.emule.public_key",
3224                 FT_BYTES, BASE_NONE, NULL, 0, "eMule Public Key", HFILL } },
3225         { &hf_emule_signature,
3226             { "Signature", "edonkey.emule.signature",
3227                 FT_BYTES, BASE_NONE, NULL, 0, "eMule Signature", HFILL } },
3228         { &hf_emule_aich_partnum,
3229             { "Part Number", "edonkey.emule.aich_partnum",
3230                 FT_UINT16, BASE_DEC, NULL, 0, "eMule AICH Part Number", HFILL } },
3231         { &hf_emule_aich_root_hash,
3232             { "AICH Root Hash", "edonkey.emule.aich_root_hash",
3233                 FT_BYTES, BASE_NONE, NULL, 0, "eMule AICH Root Hash", HFILL } },
3234         { &hf_emule_aich_hash_entry,
3235             { "AICH Hash Entry", "edonkey.emule.aich_hash_entry",
3236                 FT_NONE, BASE_NONE, NULL, 0, "eMule AICH Hash Entry", HFILL } },
3237         { &hf_emule_aich_hash_id,
3238             { "AICH Hash ID", "edonkey.emule.aich_hash_id",
3239                 FT_UINT16, BASE_HEX, NULL, 0, "eMule AICH Hash ID", HFILL } },
3240         { &hf_emule_aich_hash,
3241             { "AICH Hash", "edonkey.emule.aich_hash",
3242                 FT_BYTES, BASE_NONE, NULL, 0, "eMule AICH Hash", HFILL } },
3243         { &hf_emule_multipacket_entry,
3244             { "eMule MultiPacket Entry", "edonkey.emule.multipacket_entry",
3245                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3246         { &hf_emule_multipacket_opcode,
3247             { "MultiPacket Opcode", "edonkey.emule.multipacket_opcode",
3248                 FT_UINT8, BASE_HEX, NULL, 0, "eMule MultiPacket Opcode", HFILL } },
3249         { &hf_emule_sourceOBFU,
3250             {"Source", "edonkey.source",
3251                 FT_NONE, BASE_NONE, NULL, 0, "eDonkey File Source", HFILL } },
3252         { &hf_emule_source_count,
3253             { "Completed Sources Count", "edonkey.emule.source_count",
3254                 FT_UINT16, BASE_DEC, NULL, 0, "eMule Completed Sources Count", HFILL } },
3255         { &hf_emule_zlib,
3256             { "Compressed Data", "edonkey.emule.zlib",
3257                 FT_NONE, BASE_NONE, NULL, 0, "eMule Compressed Data", HFILL } },
3258         { &hf_overnet_peer,
3259             { "Overnet Peer", "edonkey.overnet.peer",
3260                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3261         { &hf_kademlia,
3262             { "Kademlia Packet", "edonkey.kademlia",
3263                 FT_UINT8, BASE_HEX, NULL, 0, "Kademlia Packet Type", HFILL } },
3264         { &hf_kademlia_peertype,
3265             { "Peer Type", "edonkey.kademlia.peer.type",
3266                 FT_UINT8, BASE_DEC_HEX, NULL, 0, "Kademlia Peer Type", HFILL } },
3267         { &hf_kademlia_peer,
3268             { "Kademlia Peer", "edonkey.kademlia.peer",
3269                 FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL } },
3270         { &hf_kademlia_peer_id,
3271             { "Peer ID", "edonkey.kademlia.peer.id",
3272                 FT_STRING, BASE_NONE, NULL, 0, "Kademlia Peer ID", HFILL } },
3273         { &hf_kademlia_hash,
3274             { "Kademlia Hash", "edonkey.kademlia.hash",
3275                 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
3276         { &hf_kademlia_file_id,
3277             { "File ID", "edonkey.kademlia.file.id",
3278                 FT_STRING, BASE_NONE, NULL, 0, "Kademlia File ID", HFILL } },
3279         { &hf_kademlia_keyword_hash,
3280             { "Keyword Hash", "edonkey.kademlia.keyword.hash",
3281                 FT_STRING, BASE_NONE, NULL, 0, "Kademlia Keyword Hash", HFILL } },
3282         { &hf_kademlia_recipients_id,
3283             { "Recipient's ID", "edonkey.kademlia.recipients.id",
3284                 FT_STRING, BASE_NONE, NULL, 0, "Kademlia Recipient's ID", HFILL } },
3285         { &hf_kademlia_sender_id,
3286             { "Sender ID", "edonkey.kademlia.sender.id",
3287                 FT_STRING, BASE_NONE, NULL, 0, "Kademlia Sender ID", HFILL } },
3288         { &hf_kademlia_target_id,
3289             { "Target ID", "edonkey.kademlia.target.id",
3290                 FT_STRING, BASE_NONE, NULL, 0, "Kademlia Target ID", HFILL } },
3291         { &hf_kademlia_distance,
3292             { "XOR Distance", "edonkey.kademlia.distance",
3293                 FT_STRING, BASE_NONE, NULL, 0, "Kademlia XOR Distance", HFILL } },
3294         { &hf_kademlia_version,
3295             { "Kad Version", "edonkey.kademlia.version",
3296                 FT_UINT8, BASE_DEC_HEX, NULL, 0, NULL, HFILL } },
3297         { &hf_kademlia_tag_float,
3298             { "Tag Value (Float)", "edonkey.kademlia.tag.value.float",
3299                 FT_FLOAT, BASE_NONE, NULL, 0, "Float Tag Value", HFILL } },
3300         { &hf_kademlia_tag_uint64,
3301             { "Tag Value (UINT64)", "edonkey.kademlia.tag.value.uint64",
3302                 FT_UINT64, BASE_DEC_HEX, NULL, 0, "UINT64 Tag Value", HFILL } },
3303         { &hf_kademlia_tag_uint32,
3304             { "Tag Value (UINT32)", "edonkey.kademlia.tag.value.uint32",
3305                 FT_UINT32, BASE_DEC_HEX, NULL, 0, "UINT32 Tag Value", HFILL } },
3306         { &hf_kademlia_tag_ipv4,
3307             { "Tag Value (IPv4)", "edonkey.kademlia.tag.value.ipv4",
3308                 FT_IPv4, BASE_NONE, NULL, 0, "UINT32 Tag Value (IPv4)", HFILL } },
3309         { &hf_kademlia_tag_uint16,
3310             { "Tag Value (UINT16)", "edonkey.kademlia.tag.value.uint16",
3311                 FT_UINT16, BASE_DEC_HEX, NULL, 0, "UINT16 Tag Value", HFILL } },
3312         { &hf_kademlia_tag_uint8,
3313             { "Tag Value (UINT8)", "edonkey.kademlia.tag.value.uint8",
3314                 FT_UINT8, BASE_DEC_HEX, NULL, 0, "UINT8 Tag Value", HFILL } },
3315         { &hf_kademlia_tag_hash,
3316             { "Tag Value (HASH)", "edonkey.kademlia.tag.value.hash",
3317                 FT_BYTES, BASE_NONE, NULL, 0, "HASH Tag Value", HFILL } },
3318         { &hf_kademlia_tag_string,
3319             { "Tag Value (String)", "edonkey.kademlia.tag.value.string",
3320                 FT_STRING, BASE_NONE, NULL, 0, "String Tag Value", HFILL } },
3321         { &hf_kademlia_tag_bsob,
3322             { "Tag Value (BSOB)", "edonkey.kademlia.tag.value.bsob",
3323                 FT_BYTES, BASE_NONE, NULL, 0, "BSOB Tag Value", HFILL } },
3324         { &hf_kademlia_udp_port,
3325             { "UDP Port", "edonkey.kademlia.udp_port",
3326                 FT_UINT16, BASE_DEC, NULL, 0, "Kademlia UDP Port", HFILL } },
3327         { &hf_kademlia_ip,
3328             { "IP", "edonkey.kademlia.ip",
3329                 FT_IPv4, BASE_NONE, NULL, 0, "eDonkey IP", HFILL } },
3330         { &hf_kademlia_tcp_port,
3331             { "TCP Port", "edonkey.kademlia.tcp_port",
3332                 FT_UINT16, BASE_DEC, NULL, 0, "Kademlia TCP Port", HFILL } },
3333         { &hf_kademlia_unparsed_data_length,
3334             { "Kademlia unparsed data length", "edonkey.kademlia.unparsed",
3335                 FT_UINT16, BASE_DEC, NULL, 0, "Kademlia trailing data length", HFILL } },
3336         { &hf_kademlia_tag_name,
3337             { "Tag Name", "edonkey.kademlia.tag.name",
3338                 FT_UINT8, BASE_HEX, NULL, 0, "Kademlia Tag Name String", HFILL } },
3339         { &hf_kademlia_tag_name_length,
3340             { "Tag Name Length", "edonkey.kademlia.tag.name.length",
3341                 FT_UINT16, BASE_DEC, NULL, 0, "Kademlia Tag Name String Length", HFILL } },
3342         { &hf_kademlia_tag_type,
3343             { "Tag Type", "edonkey.kademlia.tag.type",
3344                 FT_UINT8, BASE_HEX, NULL, 0, "Kademlia Tag Type", HFILL } },
3345         { &hf_kademlia_request_type,
3346             { "Request Type", "edonkey.kademlia.request.type",
3347                 FT_UINT8, BASE_HEX, NULL, 0, "Kademlia Request Type", HFILL } },
3348         { &hf_kademlia_search_condition,
3349             { "Search Condition", "edonkey.kademlia.search.condition",
3350                 FT_UINT8, BASE_HEX, NULL, 0, "Kademlia Search Condition", HFILL } },
3351         { &hf_kademlia_search_condition_argument_uint32,
3352             { "32bit Argument", "edonkey.kademlia.search.condition.argument.uint32",
3353                 FT_UINT32, BASE_DEC_HEX, NULL, 0, "Kademlia Search Condition Argument 32bit Value", HFILL } },
3354         { &hf_kademlia_search_condition_argument_uint64,
3355             { "64bit Argument", "edonkey.kademlia.search.condition.argument.uint64",
3356                 FT_UINT64, BASE_DEC_HEX, NULL, 0, "Kademlia Search Condition Argument 64bit Value", HFILL } },
3357         { &hf_edonkey_unparsed_data_length,
3358             { "eDonkey unparsed data length", "edonkey.unparsed",
3359                 FT_UINT32, BASE_DEC_HEX, NULL, 0, "eDonkey trailing or unparsed data length", HFILL } },
3360     };
3361
3362     static gint *ett[] = {
3363         &ett_edonkey,
3364         &ett_edonkey_message,
3365         &ett_edonkey_metatag,
3366         &ett_edonkey_search,
3367         &ett_edonkey_fileinfo,
3368         &ett_edonkey_serverinfo,
3369         &ett_edonkey_clientinfo,
3370         &ett_emule_aichhash,
3371         &ett_emule_multipacket,
3372         &ett_emule_zlib,
3373         &ett_overnet_peer,
3374         &ett_emule_sourceOBFU,
3375         &ett_edonkey_listitem,
3376         &ett_kademlia_search_expression,
3377         &ett_kademlia_tag
3378     };
3379     module_t *edonkey_module;
3380
3381     proto_edonkey = proto_register_protocol("eDonkey Protocol", "EDONKEY", "edonkey");
3382
3383     proto_register_field_array(proto_edonkey, hf, array_length(hf));
3384
3385     proto_register_subtree_array(ett, array_length(ett));
3386     register_dissector("edonkey.tcp", dissect_edonkey_tcp, proto_edonkey);
3387     register_dissector("edonkey.udp", dissect_edonkey_udp, proto_edonkey);
3388
3389     edonkey_module = prefs_register_protocol(proto_edonkey, NULL);
3390     prefs_register_bool_preference(edonkey_module, "desegment",
3391                                    "Reassemble eDonkey messages spanning multiple TCP segments",
3392                                    "Whether the eDonkey dissector should reassemble messages spanning multiple TCP segments."
3393                                    " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
3394                                    &edonkey_desegment);
3395 }
3396
3397 void proto_reg_handoff_edonkey(void) {
3398
3399     dissector_handle_t edonkey_tcp_handle;
3400     dissector_handle_t edonkey_udp_handle;
3401
3402     edonkey_tcp_handle = find_dissector("edonkey.tcp");
3403     edonkey_udp_handle = find_dissector("edonkey.udp");
3404
3405     dissector_add_uint("tcp.port", 4661, edonkey_tcp_handle);
3406     dissector_add_uint("tcp.port", 4662, edonkey_tcp_handle);
3407     dissector_add_uint("tcp.port", 4663, edonkey_tcp_handle);
3408     dissector_add_uint("udp.port", 4665, edonkey_udp_handle);
3409     dissector_add_uint("udp.port", 4672, edonkey_udp_handle);
3410
3411 }
3412
3413 /* vim:set expandtab ts=2 cino=(0 :*/