FieldListField usage example

FieldListField is a list assembled and dissected with many times the same field type. The "many times" must be determined by another numerical field, which should be a FieldLenField.

The constructor's parameter are :

  • name
  • default value (most of the time, an empty list)
  • instance of the field that will be used to assemble and disassemble a list item
  • name of the field hodling the list length

For example, if you have a protocol that have a list of IP whose length is precised in another field (coded on 4 bytes), you would do it like this:

class MyProto(Packet):
     name = "MyProto example"
     fields_desc = [ FieldLenField("listlen",None,"iplist","I"),
                     FieldListField("iplist", [],
                                    IPField("noname", "0.0.0.0"),
                                    "listlen")
                   ]

When used, you should have this result :

>>> z=MyProto()
>>> hexdump(z)
0000   00 00 00 00                                        ....
>>> z.iplist=["1.2.3.4","4.3.2.1"]
>>> hexdump(z)
0000   00 00 00 02 01 02 03 04  04 03 02 01               ............
>>> MyProto(str(z))
<MyProto  listlen=2L iplist=['1.2.3.4', '4.3.2.1'] |>