From Heikki Vatiainen: make "get_host_ipaddr()" require dotted-quad IP
[obnox/wireshark/wip.git] / reassemble.c
1 /* reassemble.c
2  * Routines for {fragment,segment} reassembly
3  *
4  * $Id: reassemble.c,v 1.9 2002/02/03 23:28:38 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <epan/packet.h>
32
33 #include "reassemble.h"
34
35 typedef struct _fragment_key {
36         address src;
37         address dst;
38         guint32 id;
39 } fragment_key;
40
41 static GMemChunk *fragment_key_chunk = NULL;
42 static GMemChunk *fragment_data_chunk = NULL;
43 static int fragment_init_count = 200;
44
45 #define LINK_FRAG(fd_head,fd)                                   \
46         {       fragment_data *fd_i;                            \
47                 /* add fragment to list, keep list sorted */            \
48                 for(fd_i=(fd_head);fd_i->next;fd_i=fd_i->next){ \
49                         if( ((fd)->offset) < (fd_i->next->offset) )     \
50                                 break;                                  \
51                 }                                                       \
52                 (fd)->next=fd_i->next;                          \
53                 fd_i->next=(fd);                                        \
54         }
55
56 static gint
57 fragment_equal(gconstpointer k1, gconstpointer k2)
58 {
59         fragment_key* key1 = (fragment_key*) k1;
60         fragment_key* key2 = (fragment_key*) k2;
61
62         /*key.id is the first item to compare since item is most
63           likely to differ between sessions, thus shortcircuiting
64           the comparasion of addresses.
65         */
66         return ( ( (key1->id    == key2->id) &&
67                    (ADDRESSES_EQUAL(&key1->src, &key2->src)) &&
68                    (ADDRESSES_EQUAL(&key1->dst, &key2->dst))
69                  ) ?
70                  TRUE : FALSE);
71 }
72
73 static guint
74 fragment_hash(gconstpointer k)
75 {
76         fragment_key* key = (fragment_key*) k;
77         guint hash_val;
78 /*
79         int i;
80 */
81
82         hash_val = 0;
83
84 /*      More than likely: in most captures src and dst addresses are the
85         same, and would hash the same.
86         We only use id as the hash as an optimization.
87
88         for (i = 0; i < key->src.len; i++)
89                 hash_val += key->src.data[i];
90         for (i = 0; i < key->dst.len; i++)
91                 hash_val += key->dst.data[i];
92 */
93
94         hash_val += key->id;
95
96         return hash_val;
97 }
98
99 /*
100  * For a hash table entry, free the address data to which the key refers
101  * and the fragment data to which the value refers.
102  * (The actual key and value structures get freed by "reassemble_init()".)
103  */
104 static gboolean
105 free_all_fragments(gpointer key_arg, gpointer value, gpointer user_data)
106 {
107         fragment_key *key = key_arg;
108         fragment_data *fd_head;
109
110         /*
111          * Grr.  I guess the theory here is that freeing
112          * something sure as heck modifies it, so you
113          * want to ban attempts to free it, but, alas,
114          * if we make the "data" field of an "address"
115          * structure not a "const", the compiler whines if
116          * we try to make it point into the data for a packet,
117          * as that's a "const" array (and should be, as dissectors
118          * shouldn't trash it).
119          *
120          * So we cast the complaint into oblivion, and rely on
121          * the fact that these addresses are known to have had
122          * their data mallocated, i.e. they don't point into,
123          * say, the middle of the data for a packet.
124          */
125         g_free((gpointer)key->src.data);
126         g_free((gpointer)key->dst.data);
127
128         for (fd_head = value; fd_head != NULL; fd_head = fd_head->next) {
129                 if(fd_head->data && !(fd_head->flags&FD_NOT_MALLOCED))
130                         g_free(fd_head->data);
131         }
132
133         return TRUE;
134 }
135
136 /*
137  * Initialize a fragment table.
138  */
139 void
140 fragment_table_init(GHashTable **fragment_table)
141 {
142         if (*fragment_table != NULL) {
143                 /*
144                  * The fragment hash table exists.
145                  * 
146                  * Remove all entries and free fragment data for
147                  * each entry.  (The key and value data is freed
148                  * by "reassemble_init()".)
149                  */
150                 g_hash_table_foreach_remove(*fragment_table,
151                                 free_all_fragments, NULL);
152         } else {
153                 /* The fragment table does not exist. Create it */
154                 *fragment_table = g_hash_table_new(fragment_hash,
155                                 fragment_equal);
156         }
157 }
158
159 /*
160  * Free up all space allocated for fragment keys and data.
161  */
162 void
163 reassemble_init(void)
164 {
165         if (fragment_key_chunk != NULL)
166                 g_mem_chunk_destroy(fragment_key_chunk);
167         if (fragment_data_chunk != NULL)
168                 g_mem_chunk_destroy(fragment_data_chunk);
169         fragment_key_chunk = g_mem_chunk_new("fragment_key_chunk",
170             sizeof(fragment_key),
171             fragment_init_count * sizeof(fragment_key),
172             G_ALLOC_ONLY);
173         fragment_data_chunk = g_mem_chunk_new("fragment_data_chunk",
174             sizeof(fragment_data),
175             fragment_init_count * sizeof(fragment_data),
176             G_ALLOC_ONLY);
177 }
178
179 /* This function cleans up the stored state and removes the reassembly data and
180  * (with one exception) all allocated memory for matching reassembly.
181  * 
182  * The exception is :
183  * If the PDU was already completely reassembled, then the buffer containing the 
184  * reassembled data WILL NOT be free()d, and the pointer to that buffer will be
185  * returned.
186  * Othervise the function will return NULL.
187  *
188  * So, if you call fragment_delete and it returns non-NULL, YOU are responsible to 
189  * g_free() that buffer.
190  */
191 unsigned char *
192 fragment_delete(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
193 {
194         fragment_data *fd_head, *fd;
195         fragment_key key;
196         unsigned char *data=NULL;
197
198         /* create key to search hash with */
199         key.src = pinfo->src;
200         key.dst = pinfo->dst;
201         key.id  = id;
202
203         fd_head = g_hash_table_lookup(fragment_table, &key);
204
205         if(fd_head==NULL){
206                 /* We do not recognize this as a PDU we have seen before. return*/
207                 return NULL;
208         }
209
210         data=fd_head->data;
211         /* loop over all partial fragments and free any buffers */
212         for(fd=fd_head->next;fd;){
213                 fragment_data *tmp_fd;
214                 tmp_fd=fd->next;
215
216                 if( !(fd->flags&FD_NOT_MALLOCED) )
217                         g_free(fd->data);
218                 g_mem_chunk_free(fragment_data_chunk, fd);
219                 fd=tmp_fd;
220         }
221         g_mem_chunk_free(fragment_data_chunk, fd_head);
222         g_hash_table_remove(fragment_table, &key);
223
224         return data;
225 }
226
227 /* This function is used to check if there is partial or completed reassembly state
228  * matching this packet. I.e. Are there reassembly going on or not for this packet?
229  */
230 fragment_data *
231 fragment_get(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
232 {
233         fragment_data *fd_head;
234         fragment_key key;
235
236         /* create key to search hash with */
237         key.src = pinfo->src;
238         key.dst = pinfo->dst;
239         key.id  = id;
240
241         fd_head = g_hash_table_lookup(fragment_table, &key);
242         
243         return fd_head;
244 }
245
246 /* This function can be used to explicitely set the total length (if known)
247  * for reassembly of a PDU.
248  * This is useful for reassembly of PDUs where one may have the total length specified
249  * in the first fragment instead of as for, say, IPv4 where a flag indicates which
250  * is the last fragment.
251  *
252  * Such protocols might fragment_add with a more_frags==TRUE for every fragment
253  * and just tell the reassembly engine the expected total length of the reassembled data
254  * using fragment_set_tot_len immediately after doing fragment_add for the first packet.
255  *
256  * note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
257  * i.e. since the block numbers start at 0, if we specify tot_len==2, that 
258  * actually means we want to defragment 3 blocks, block 0, 1 and 2.
259  */
260 void
261 fragment_set_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table, 
262                      guint32 tot_len)
263 {
264         fragment_data *fd_head;
265         fragment_key key;
266
267         /* create key to search hash with */
268         key.src = pinfo->src;
269         key.dst = pinfo->dst;
270         key.id  = id;
271
272         fd_head = g_hash_table_lookup(fragment_table, &key);
273
274         if(fd_head){
275                 fd_head->datalen = tot_len;
276         }
277
278         return;
279 }
280
281
282 /* This function will set the partial reassembly flag for a fh.
283    When this function is called, the fh MUST already exist, i.e.
284    the fh MUST be created by the initial call to fragment_add() before
285    this function is called.
286    Also note that this function MUST be called to indicate a fh will be 
287    extended (increase the already stored data)
288 */
289
290 void
291 fragment_set_partial_reassembly(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
292 {
293         fragment_data *fd_head;
294         fragment_key key;
295
296         /* create key to search hash with */
297         key.src = pinfo->src;
298         key.dst = pinfo->dst;
299         key.id  = id;
300
301         fd_head = g_hash_table_lookup(fragment_table, &key);
302
303         if(fd_head){
304                 fd_head->flags |= FD_PARTIAL_REASSEMBLY;
305         }
306 }
307 /*
308  * This function adds a new fragment to the fragment hash table.
309  * If this is the first fragment seen for this datagram, a new entry
310  * is created in the hash table, otherwise this fragment is just added
311  * to the linked list of fragments for this packet.
312  * The list of fragments for a specific datagram is kept sorted for
313  * easier handling.
314  *
315  * Returns a pointer to the head of the fragment data list if we have all the
316  * fragments, NULL otherwise.
317  *
318  * This function assumes frag_offset being a byte offset into the defragment
319  * packet.
320  *
321  * 01-2002
322  * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be
323  * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set 
324  * using fragment_set_partial_reassembly() before calling fragment_add
325  * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags
326  * are lowered when a new extension process is started.
327  */
328 fragment_data *
329 fragment_add(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
330              GHashTable *fragment_table, guint32 frag_offset,
331              guint32 frag_data_len, gboolean more_frags)
332 {
333         fragment_key key, *new_key;
334         fragment_data *fd_head;
335         fragment_data *fd;
336         fragment_data *fd_i;
337         guint32 max, dfpos;
338         unsigned char *old_data;
339
340         /* create key to search hash with */
341         key.src = pinfo->src;
342         key.dst = pinfo->dst;
343         key.id  = id;
344
345         fd_head = g_hash_table_lookup(fragment_table, &key);
346
347         /* have we already seen this frame ?*/
348         if (pinfo->fd->flags.visited) {
349                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
350                         return fd_head;
351                 } else {
352                         return NULL;
353                 }
354         }
355
356         if (fd_head==NULL){
357                 /* not found, this must be the first snooped fragment for this
358                  * packet. Create list-head.
359                  */
360                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
361                 /* head/first structure in list only holds no other data than
362                  * 'datalen' then we don't have to change the head of the list
363                  * even if we want to keep it sorted
364                  */
365                 fd_head->next=NULL;
366                 fd_head->datalen=0;
367                 fd_head->offset=0;
368                 fd_head->len=0;
369                 fd_head->flags=0;
370                 fd_head->data=NULL;
371
372                 /*
373                  * We're going to use the key to insert the fragment,
374                  * so allocate a structure for it, and copy the
375                  * addresses, allocating new buffers for the address
376                  * data.
377                  */
378                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
379                 COPY_ADDRESS(&new_key->src, &key.src);
380                 COPY_ADDRESS(&new_key->dst, &key.dst);
381                 new_key->id = key.id;
382                 g_hash_table_insert(fragment_table, new_key, fd_head);
383         }
384
385         /* create new fd describing this fragment */
386         fd = g_mem_chunk_alloc(fragment_data_chunk);
387         fd->next = NULL;
388         fd->flags = 0;
389         fd->frame = pinfo->fd->num;
390         fd->offset = frag_offset;
391         fd->len  = frag_data_len;
392         fd->data = NULL;
393
394         /*
395          * If it was already defragmented and this new fragment goes beyond
396          * data limits, set flag in already empty fds & point old fds to malloc'ed data.
397          */
398         if(fd_head->flags & FD_DEFRAGMENTED && (frag_offset+frag_data_len) >= fd_head->datalen &&
399                 fd_head->flags & FD_PARTIAL_REASSEMBLY){
400                 for(fd_i=fd_head->next; fd_i; fd_i=fd_i->next){
401                         if( !fd_i->data ) {
402                                 fd_i->data = fd_head->data + fd_i->offset;
403                                 fd_i->flags |= FD_NOT_MALLOCED;
404                         }
405                         fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
406                 }
407                 fd_head->flags ^= FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY;
408                 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
409                 fd_head->datalen=0;
410         }
411
412         if (!more_frags) {  
413                 /*
414                  * This is the tail fragment in the sequence.
415                  */
416                 if (fd_head->datalen) {
417                         /* ok we have already seen other tails for this packet
418                          * it might be a duplicate.
419                          */
420                         if (fd_head->datalen != (fd->offset + fd->len) ){
421                                 /* Oops, this tail indicates a different packet
422                                  * len than the previous ones. Somethings wrong
423                                  */
424                                 fd->flags      |= FD_MULTIPLETAILS;
425                                 fd_head->flags |= FD_MULTIPLETAILS;
426                         }
427                 } else {
428                         /* this was the first tail fragment, now we know the
429                          * length of the packet
430                          */
431                         fd_head->datalen = fd->offset + fd->len;
432                 }
433         }
434
435
436
437
438         /* If the packet is already defragmented, this MUST be an overlap.
439          * The entire defragmented packet is in fd_head->data
440          * Even if we have previously defragmented this packet, we still check
441          * check it. Someone might play overlap and TTL games.
442          */
443         if (fd_head->flags & FD_DEFRAGMENTED) {
444                 fd->flags      |= FD_OVERLAP;
445                 fd_head->flags |= FD_OVERLAP;
446                 /* make sure its not too long */
447                 if (fd->offset + fd->len > fd_head->datalen) {
448                         fd->flags      |= FD_TOOLONGFRAGMENT;
449                         fd_head->flags |= FD_TOOLONGFRAGMENT;
450                         LINK_FRAG(fd_head,fd);
451                         return (fd_head);
452                 }
453                 /* make sure it doesnt conflict with previous data */
454                 if ( memcmp(fd_head->data+fd->offset,
455                         tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
456                         fd->flags      |= FD_OVERLAPCONFLICT;
457                         fd_head->flags |= FD_OVERLAPCONFLICT;
458                         LINK_FRAG(fd_head,fd);
459                         return (fd_head);
460                 }
461                 /* it was just an overlap, link it and return */
462                 LINK_FRAG(fd_head,fd);
463                 return (fd_head);
464         }
465
466
467
468         /* If we have reached this point, the packet is not defragmented yet.
469          * Save all payload in a buffer until we can defragment.
470          * XXX - what if we didn't capture the entire fragment due
471          * to a too-short snapshot length?
472          */
473         fd->data = g_malloc(fd->len);
474         tvb_memcpy(tvb, fd->data, offset, fd->len);
475         LINK_FRAG(fd_head,fd);
476
477
478         if( !(fd_head->datalen) ){
479                 /* if we dont know the datalen, there are still missing
480                  * packets. Cheaper than the check below.
481                  */
482                 return NULL;
483         }
484
485
486         /* check if we have received the entire fragment
487          * this is easy since the list is sorted and the head is faked.
488          */
489         max = 0;
490         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
491                 if ( ((fd_i->offset)<=max) && 
492                     ((fd_i->offset+fd_i->len)>max) ){
493                         max = fd_i->offset+fd_i->len;
494                 }
495         }
496
497         if (max < (fd_head->datalen)) {
498                 /* we have not received all packets yet */
499                 return NULL;
500         }
501
502
503         if (max > (fd_head->datalen)) {
504                 /*XXX not sure if current fd was the TOOLONG*/
505                 /*XXX is it fair to flag current fd*/
506                 /* oops, too long fragment detected */
507                 fd->flags      |= FD_TOOLONGFRAGMENT;
508                 fd_head->flags |= FD_TOOLONGFRAGMENT;
509         }
510
511
512         /* we have received an entire packet, defragment it and
513          * free all fragments 
514          */
515         /* store old data just in case */
516         old_data=fd_head->data;
517         fd_head->data = g_malloc(max);
518
519         /* add all data fragments */
520         for (dfpos=0,fd_i=fd_head;fd_i;fd_i=fd_i->next) {
521                 if (fd_i->len) {
522                         if (fd_i->offset < dfpos) {
523                                 fd_i->flags    |= FD_OVERLAP;
524                                 fd_head->flags |= FD_OVERLAP;
525                                 if ( memcmp(fd_head->data+fd_i->offset,
526                                         fd_i->data,
527                                         MIN(fd_i->len,(dfpos-fd_i->offset))
528                                         ) ){
529                                         fd_i->flags    |= FD_OVERLAPCONFLICT;
530                                         fd_head->flags |= FD_OVERLAPCONFLICT;
531                                 }
532                         } 
533                         /* dfpos is always >= than fd_i->offset */
534                         /* No gaps can exist here, max_loop(above) does this */
535                         if( fd_i->offset+fd_i->len > dfpos )
536                                 memcpy(fd_head->data+dfpos, fd_i->data+(dfpos-fd_i->offset),
537                                         fd_i->len-(dfpos-fd_i->offset));
538                         if( fd_i->flags & FD_NOT_MALLOCED )
539                                 fd_i->flags ^= FD_NOT_MALLOCED;
540                         else
541                                 g_free(fd_i->data);
542                         fd_i->data=NULL;
543
544                         dfpos=MAX(dfpos,(fd_i->offset+fd_i->len));
545                 }
546         }
547
548         if( old_data )
549                 g_free(old_data);
550         /* mark this packet as defragmented.
551            allows us to skip any trailing fragments */
552         fd_head->flags |= FD_DEFRAGMENTED;
553
554         return fd_head;
555 }
556
557
558
559 /*
560  * This function adds a new fragment to the fragment hash table.
561  * If this is the first fragment seen for this datagram, a new entry
562  * is created in the hash table, otherwise this fragment is just added
563  * to the linked list of fragments for this packet.
564  * The list of fragments for a specific datagram is kept sorted for
565  * easier handling.
566  *
567  * Returns a pointer to the head of the fragment data list if we have all the
568  * fragments, NULL otherwise.
569  *
570  * This function assumes frag_offset being a block sequence number.
571  * the bsn for the first block is 0.
572  */
573 fragment_data *
574 fragment_add_seq(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
575              GHashTable *fragment_table, guint32 frag_offset,
576              guint32 frag_data_len, gboolean more_frags)
577 {
578         fragment_key key, *new_key;
579         fragment_data *fd_head;
580         fragment_data *fd;
581         fragment_data *fd_i;
582         fragment_data *last_fd;
583         guint32 max, dfpos, size;
584
585         /* create key to search hash with */
586         key.src = pinfo->src;
587         key.dst = pinfo->dst;
588         key.id  = id;
589
590         fd_head = g_hash_table_lookup(fragment_table, &key);
591
592         /* have we already seen this frame ?*/
593         if (pinfo->fd->flags.visited) {
594                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
595                         return fd_head;
596                 } else {
597                         return NULL;
598                 }
599         }
600
601         if (fd_head==NULL){
602                 /* not found, this must be the first snooped fragment for this
603                  * packet. Create list-head.
604                  */
605                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
606                 /* head/first structure in list only holds no other data than
607                  * 'datalen' then we don't have to change the head of the list
608                  * even if we want to keep it sorted
609                  */
610                 fd_head->next=NULL;
611                 fd_head->datalen=0;
612                 fd_head->offset=0;
613                 fd_head->len=0;
614                 fd_head->flags=FD_BLOCKSEQUENCE;
615                 fd_head->data=NULL;
616
617                 /*
618                  * We're going to use the key to insert the fragment,
619                  * so allocate a structure for it, and copy the
620                  * addresses, allocating new buffers for the address
621                  * data.
622                  */
623                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
624                 COPY_ADDRESS(&new_key->src, &key.src);
625                 COPY_ADDRESS(&new_key->dst, &key.dst);
626                 new_key->id = key.id;
627                 g_hash_table_insert(fragment_table, new_key, fd_head);
628         }
629
630         /* create new fd describing this fragment */
631         fd = g_mem_chunk_alloc(fragment_data_chunk);
632         fd->next = NULL;
633         fd->flags = 0;
634         fd->frame = pinfo->fd->num;
635         fd->offset = frag_offset;
636         fd->len  = frag_data_len;
637         fd->data = NULL;
638
639         if (!more_frags) {  
640                 /*
641                  * This is the tail fragment in the sequence.
642                  */
643                 if (fd_head->datalen) {
644                         /* ok we have already seen other tails for this packet
645                          * it might be a duplicate.
646                          */
647                         if (fd_head->datalen != fd->offset ){
648                                 /* Oops, this tail indicates a different packet
649                                  * len than the previous ones. Somethings wrong
650                                  */
651                                 fd->flags      |= FD_MULTIPLETAILS;
652                                 fd_head->flags |= FD_MULTIPLETAILS;
653                         }
654                 } else {
655                         /* this was the first tail fragment, now we know the
656                          * length of the packet
657                          */
658                         fd_head->datalen = fd->offset;
659                 }
660         }
661
662         /* If the packet is already defragmented, this MUST be an overlap.
663          * The entire defragmented packet is in fd_head->data
664          * Even if we have previously defragmented this packet, we still check
665          * check it. Someone might play overlap and TTL games.
666          */
667         if (fd_head->flags & FD_DEFRAGMENTED) {
668                 fd->flags      |= FD_OVERLAP;
669                 fd_head->flags |= FD_OVERLAP;
670
671                 /* make sure its not too long */
672                 if (fd->offset > fd_head->datalen) {
673                         fd->flags      |= FD_TOOLONGFRAGMENT;
674                         fd_head->flags |= FD_TOOLONGFRAGMENT;
675                         LINK_FRAG(fd_head,fd);
676                         return (fd_head);
677                 }
678                 /* make sure it doesnt conflict with previous data */
679                 dfpos=0;
680                 for (fd_i=fd_head->next;fd_i->offset!=fd->offset;fd_i=fd_i->next) {
681                   dfpos += fd_i->len;
682                 }
683                 if(fd_i->datalen!=fd->datalen){
684                         fd->flags      |= FD_OVERLAPCONFLICT;
685                         fd_head->flags |= FD_OVERLAPCONFLICT;
686                         LINK_FRAG(fd_head,fd);
687                         return (fd_head);
688                 }
689                 if ( memcmp(fd_head->data+dfpos,
690                         tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
691                         fd->flags      |= FD_OVERLAPCONFLICT;
692                         fd_head->flags |= FD_OVERLAPCONFLICT;
693                         LINK_FRAG(fd_head,fd);
694                         return (fd_head);
695                 }
696                 /* it was just an overlap, link it and return */
697                 LINK_FRAG(fd_head,fd);
698                 return (fd_head);
699         }
700
701         /* If we have reached this point, the packet is not defragmented yet.
702          * Save all payload in a buffer until we can defragment.
703          * XXX - what if we didn't capture the entire fragment due
704          * to a too-short snapshot length?
705          */
706         fd->data = g_malloc(fd->len);
707         tvb_memcpy(tvb, fd->data, offset, fd->len);
708         LINK_FRAG(fd_head,fd);
709
710
711         if( !(fd_head->datalen) ){
712                 /* if we dont know the datalen, there are still missing
713                  * packets. Cheaper than the check below.
714                  */
715                 return NULL;
716         }
717
718
719         /* check if we have received the entire fragment
720          * this is easy since the list is sorted and the head is faked.
721          */
722         max = 0;
723         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
724           if ( fd_i->offset==max ){
725             max++;
726           }
727         }
728         /* max will now be datalen+1 if all fragments have been seen */
729
730         if (max <= fd_head->datalen) {
731                 /* we have not received all packets yet */
732                 return NULL;
733         }
734
735
736         if (max > (fd_head->datalen+1)) {
737                 /* oops, too long fragment detected */
738                 fd->flags      |= FD_TOOLONGFRAGMENT;
739                 fd_head->flags |= FD_TOOLONGFRAGMENT;
740         }
741
742
743         /* we have received an entire packet, defragment it and
744          * free all fragments 
745          */
746         size=0;
747         last_fd=NULL;
748         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
749           if(!last_fd || last_fd->offset!=fd_i->offset){
750             size+=fd_i->len;
751           }
752           last_fd=fd_i;
753         }
754         fd_head->data = g_malloc(size);
755         fd_head->len = size;            /* record size for caller       */
756
757         /* add all data fragments */
758         last_fd=NULL;
759         for (dfpos=0,fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
760           if (fd_i->len) {
761             if(!last_fd || last_fd->offset!=fd_i->offset){
762               memcpy(fd_head->data+dfpos,fd_i->data,fd_i->len);
763               dfpos += fd_i->len;
764             } else {
765               /* duplicate/retransmission/overlap */
766               if( (last_fd->len!=fd_i->datalen)
767                   || memcmp(last_fd->data, fd_i->data, last_fd->len) ){
768                         fd->flags      |= FD_OVERLAPCONFLICT;
769                         fd_head->flags |= FD_OVERLAPCONFLICT;
770               }
771             }
772             last_fd=fd_i;
773           }
774         }
775
776         /* we have defragmented the pdu, now free all fragments*/
777         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
778           if(fd_i->data){
779             g_free(fd_i->data);
780             fd_i->data=NULL;
781           }
782         }
783
784         /* mark this packet as defragmented.
785            allows us to skip any trailing fragments */
786         fd_head->flags |= FD_DEFRAGMENTED;
787
788         return fd_head;
789 }