name change
[metze/wireshark/wip.git] / epan / reassemble.h
1 /* reassemble.h
2  * Declarations of outines for {fragment,segment} reassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 /* make sure that all flags that are set in a fragment entry is also set for
26  * the flags field of fd_head !!!
27  */
28
29 /* only in fd_head: packet is defragmented */
30 #define FD_DEFRAGMENTED         0x0001
31
32 /* there are overlapping fragments */
33 #define FD_OVERLAP              0x0002
34
35 /* overlapping fragments contain different data */
36 #define FD_OVERLAPCONFLICT      0x0004
37
38 /* more than one fragment which indicates end-of data */
39 #define FD_MULTIPLETAILS        0x0008
40
41 /* fragment contains data past the end of the datagram */
42 #define FD_TOOLONGFRAGMENT      0x0010
43
44 /* fragment data not alloced, fd->data pointing to fd_head->data+fd->offset */
45 #define FD_NOT_MALLOCED         0x0020
46
47 /* this flag is used to request fragment_add to continue the reassembly process */
48 #define FD_PARTIAL_REASSEMBLY   0x0040
49
50 /* fragment offset is indicated by sequence number and not byte offset
51    into the defragmented packet */
52 #define FD_BLOCKSEQUENCE        0x0100
53
54 typedef struct _fragment_data {
55         struct _fragment_data *next;
56         guint32 frame;
57         guint32 offset;
58         guint32 len;
59         guint32 datalen; /*Only valid in first item of list */
60         guint32 reassembled_in; /* frame where this PDU was reassembled,
61                                    only valid in the first item of the list
62                                    and when FD_DEFRAGMENTED is set*/
63         guint32 flags;
64         unsigned char *data;
65 } fragment_data;
66
67 /*
68  * Initialize a fragment table.
69  */
70 extern void fragment_table_init(GHashTable **fragment_table);
71 extern void dcerpc_fragment_table_init(GHashTable **fragment_table);
72
73 /*
74  * Initialize a reassembled-packet table.
75  */
76 extern void reassembled_table_init(GHashTable **reassembled_table);
77
78 /*
79  * Free up all space allocated for fragment keys and data.
80  */
81 void reassemble_init(void);
82
83 /*
84  * This function adds a new fragment to the fragment hash table.
85  * If this is the first fragment seen for this datagram, a new entry
86  * is created in the hash table, otherwise this fragment is just added
87  * to the linked list of fragments for this packet.
88  * The list of fragments for a specific datagram is kept sorted for
89  * easier handling.
90  *
91  * Returns a pointer to the head of the fragment data list if we have all the
92  * fragments, NULL otherwise.
93  */
94 extern fragment_data *fragment_add(tvbuff_t *tvb, int offset, packet_info *pinfo,
95     guint32 id, GHashTable *fragment_table, guint32 frag_offset,
96     guint32 frag_data_len, gboolean more_frags);
97 extern fragment_data *fragment_add_multiple_ok(tvbuff_t *tvb, int offset,
98     packet_info *pinfo, guint32 id, GHashTable *fragment_table,
99     guint32 frag_offset, guint32 frag_data_len, gboolean more_frags);
100
101 extern fragment_data *fragment_add_check(tvbuff_t *tvb, int offset,
102     packet_info *pinfo, guint32 id, GHashTable *fragment_table,
103     GHashTable *reassembled_table, guint32 frag_offset,
104     guint32 frag_data_len, gboolean more_frags);
105
106 /* same as fragment_add() but this one assumes frag_number is a block
107    sequence number. note that frag_number is 0 for the first fragment. */
108 extern fragment_data *fragment_add_seq(tvbuff_t *tvb, int offset, packet_info *pinfo,
109     guint32 id, GHashTable *fragment_table, guint32 frag_number,
110     guint32 frag_data_len, gboolean more_frags);
111
112 extern fragment_data *
113 fragment_add_dcerpc_dg(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
114         void *act_id,
115         GHashTable *fragment_table, guint32 frag_number,
116         guint32 frag_data_len, gboolean more_frags);
117
118 /*
119  * These functions add a new fragment to the fragment hash table.
120  * If this is the first fragment seen for this datagram, a new
121  * "fragment_data" structure is allocated to refer to the reassembled,
122  * packet, and:
123  *
124  *      in "fragment_add_seq_802_11()", if "more_frags" is false,
125  *      the structure is not added to the hash table, and not given
126  *      any fragments to refer to, but is just returned;
127  *
128  *      otherwise, this fragment is added to the linked list of fragments
129  *      for this packet, and the "fragment_data" structure is put into
130  *      the hash table.
131  *
132  * Otherwise, this fragment is just added to the linked list of fragments
133  * for this packet.
134  *
135  * If, after processing this fragment, we have all the fragments, they
136  * remove that from the fragment hash table if necessary and add it
137  * to the table of reassembled fragments, and return a pointer to the
138  * head of the fragment list.
139  *
140  * If this is the first fragment we've seen, and "more_frags" is false,
141  * "fragment_add_seq_802_11()" does nothing to the fragment data list,
142  * and returns a pointer to the head of that (empty) list.  The other
143  * routines return NULL.
144  *
145  * Otherwise, they return NULL.
146  *
147  * "fragment_add_seq_check()" and "fragment_add_seq_802_11()" assume
148  * frag_number is a block sequence number.
149  * The bsn for the first block is 0.
150  *
151  * "fragment_add_seq_next()" is for protocols with no sequence number,
152  * and assumes fragments always appear in sequence.
153  */
154 extern fragment_data *
155 fragment_add_seq_check(tvbuff_t *tvb, int offset, packet_info *pinfo,
156              guint32 id, GHashTable *fragment_table,
157              GHashTable *reassembled_table, guint32 frag_number,
158              guint32 frag_data_len, gboolean more_frags);
159
160 extern fragment_data *
161 fragment_add_seq_802_11(tvbuff_t *tvb, int offset, packet_info *pinfo,
162              guint32 id, GHashTable *fragment_table,
163              GHashTable *reassembled_table, guint32 frag_number,
164              guint32 frag_data_len, gboolean more_frags);
165
166 extern fragment_data *
167 fragment_add_seq_next(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
168              GHashTable *fragment_table, GHashTable *reassembled_table,
169              guint32 frag_data_len, gboolean more_frags);
170
171 /* to specify how much to reassemble, for fragmentation where last fragment can not be
172  * identified by flags or such.
173  * note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
174  * i.e. since the block numbers start at 0, if we specify tot_len==2, that
175  * actually means we want to defragment 3 blocks, block 0, 1 and 2.
176  *
177  */
178 extern void
179 fragment_set_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table,
180                      guint32 tot_len);
181
182 /* to resad whatever totlen previously set */
183 extern guint32
184 fragment_get_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table);
185
186 /*
187  * This function will set the partial reassembly flag(FD_PARTIAL_REASSEMBLY) for a fh.
188  * When this function is called, the fh MUST already exist, i.e.
189  * the fh MUST be created by the initial call to fragment_add() before
190  * this function is called. Also note that this function MUST be called to indicate
191  * a fh will be extended (increase the already stored data). After calling this function,
192  * and if FD_DEFRAGMENTED is set, the reassembly process will be continued.
193  */
194 extern void
195 fragment_set_partial_reassembly(packet_info *pinfo, guint32 id, GHashTable *fragment_table);
196
197 /* This function is used to check if there is partial or completed reassembly state
198  * matching this packet. I.e. Are there reassembly going on or not for this packet?
199  */
200 extern fragment_data *
201 fragment_get(packet_info *pinfo, guint32 id, GHashTable *fragment_table);
202
203 /* The same for the reassemble table */
204 /* id *must* be the frame number for this to work! */
205 extern fragment_data *
206 fragment_get_reassembled(packet_info *pinfo, guint32 id, GHashTable *reassembled_table);
207
208 extern fragment_data *
209 fragment_get_reassembled_id(packet_info *pinfo, guint32 id, GHashTable *reassembled_table);
210
211 /* This will free up all resources and delete reassembly state for this PDU.
212  * Except if the PDU is completely reassembled, then it would NOT deallocate the
213  * buffer holding the reassembled data but instead return the pointer to that
214  * buffer.
215  *
216  * So, if you call fragment_delete and it returns non-NULL, YOU are responsible to
217  * g_free() that buffer.
218  */
219 extern unsigned char *
220 fragment_delete(packet_info *pinfo, guint32 id, GHashTable *fragment_table);
221
222 /* hf_fragment, hf_fragment_error, and hf_reassembled_in should be
223    FT_FRAMENUM, the others should be FT_BOOLEAN
224 */
225 typedef struct _fragment_items {
226         gint    *ett_fragment;
227         gint    *ett_fragments;
228
229         int     *hf_fragments;
230         int     *hf_fragment;
231         int     *hf_fragment_overlap;
232         int     *hf_fragment_overlap_conflict;
233         int     *hf_fragment_multiple_tails;
234         int     *hf_fragment_too_long_fragment;
235         int     *hf_fragment_error;
236         int     *hf_reassembled_in;
237
238         const char      *tag;
239 } fragment_items;
240
241 extern tvbuff_t *
242 process_reassembled_data(tvbuff_t *tvb, int offset, packet_info *pinfo,
243     const char *name, fragment_data *fd_head, const fragment_items *fit,
244     gboolean *update_col_infop, proto_tree *tree);
245
246 extern gboolean
247 show_fragment_tree(fragment_data *ipfd_head, const fragment_items *fit,
248     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi);
249
250 extern gboolean
251 show_fragment_seq_tree(fragment_data *ipfd_head, const fragment_items *fit,
252     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi);