@conf.commands.register
def defragment(plist):
 frags = defaultdict((lambda : []))
 final = []
 pos = 0
 for p in plist:
  p._defrag_pos = pos
  pos += 1
  if (IP in p):
   ip = p[IP]
   if ((ip.frag != 0) or (ip.flags & 1)):
    ip = p[IP]
    uniq = (ip.id, ip.src, ip.dst, ip.proto)
    frags[uniq].append(p)
    continue
  final.append(p)
 defrag = []
 missfrag = []
 for lst in frags.itervalues():
  lst.sort(key=(lambda x: x.frag))
  p = lst[0]
  lastp = lst[(-1)]
  if ((p.frag > 0) or ((lastp.flags & 1) != 0)):
   missfrag += lst
   continue
  p = p.copy()
  if (conf.padding_layer in p):
   del p[conf.padding_layer].underlayer.payload
  ip = p[IP]
  if ((ip.len is None) or (ip.ihl is None)):
   clen = len(ip.payload)
  else:
   clen = (ip.len - (ip.ihl << 2))
  txt = conf.raw_layer()
  for q in lst[1:]:
   if (clen != (q.frag << 3)):
    if (clen > (q.frag << 3)):
     warning(('Fragment overlap (%i > %i) %r || %r ||  %r' % (clen, (q.frag << 3), p, txt, q)))
    missfrag += lst
    break
   if ((q[IP].len is None) or (q[IP].ihl is None)):
    clen += len(q[IP].payload)
   else:
    clen += (q[IP].len - (q[IP].ihl << 2))
   if (conf.padding_layer in q):
    del q[conf.padding_layer].underlayer.payload
   txt.add_payload(q[IP].payload.copy())
  else:
   ip.flags &= (~ 1)
   del ip.chksum
   del ip.len
   p = (p / txt)
   p._defrag_pos = max((x._defrag_pos for x in lst))
   defrag.append(p)
 defrag2 = []
 for p in defrag:
  q = p.__class__(str(p))
  q._defrag_pos = p._defrag_pos
  defrag2.append(q)
 final += defrag2
 final += missfrag
 final.sort(key=(lambda x: x._defrag_pos))
 for p in final:
  del p._defrag_pos
 if hasattr(plist, 'listname'):
  name = ('Defragmented %s' % plist.listname)
 else:
  name = 'Defragmented'
 return PacketList(final, name=name)

@ conf . commands . register def defragment ( plist ) : frags = defaultdict ( ( lambda : [ ] ) ) final = [ ] pos = 0 for p in plist : p . _defrag_pos = pos pos += 1 if ( IP in p ) : ip = p [ IP ] if ( ( ip . frag != 0 ) or ( ip . flags & 1 ) ) : ip = p [ IP ] uniq = ( ip . id , ip . src , ip . dst , ip . proto ) frags [ uniq ] . append ( p ) continue final . append ( p ) defrag = [ ] missfrag = [ ] for lst in frags . itervalues ( ) : lst . sort ( key = ( lambda x : x . frag ) ) p = lst [ 0 ] lastp = lst [ ( - 1 ) ] if ( ( p . frag > 0 ) or ( ( lastp . flags & 1 ) != 0 ) ) : missfrag += lst continue p = p . copy ( ) if ( conf . padding_layer in p ) : del p [ conf . padding_layer ] . underlayer . payload ip = p [ IP ] if ( ( ip . len is None ) or ( ip . ihl is None ) ) : clen = len ( ip . payload ) else : clen = ( ip . len - ( ip . ihl << 2 ) ) txt = conf . raw_layer ( ) for q in lst [ 1 : ] : if ( clen != ( q . frag << 3 ) ) : if ( clen > ( q . frag << 3 ) ) : warning ( ( 'Fragment overlap (%i > %i) %r || %r ||  %r' % ( clen , ( q . frag << 3 ) , p , txt , q ) ) ) missfrag += lst break if ( ( q [ IP ] . len is None ) or ( q [ IP ] . ihl is None ) ) : clen += len ( q [ IP ] . payload ) else : clen += ( q [ IP ] . len - ( q [ IP ] . ihl << 2 ) ) if ( conf . padding_layer in q ) : del q [ conf . padding_layer ] . underlayer . payload txt . add_payload ( q [ IP ] . payload . copy ( ) ) else : ip . flags &= ( ~ 1 ) del ip . chksum del ip . len p = ( p / txt ) p . _defrag_pos = max ( ( x . _defrag_pos for x in lst ) ) defrag . append ( p ) defrag2 = [ ] for p in defrag : q = p . __class__ ( str ( p ) ) q . _defrag_pos = p . _defrag_pos defrag2 . append ( q ) final += defrag2 final += missfrag final . sort ( key = ( lambda x : x . _defrag_pos ) ) for p in final : del p . _defrag_pos if hasattr ( plist , 'listname' ) : name = ( 'Defragmented %s' % plist . listname ) else : name = 'Defragmented' return PacketList ( final , name = name )
defragment mounted btrfs filesystem .