Exemple #1
0
 def _encode(self, outstream):
     fld_size = IntField(32, False)
     fld_data = RawBytes(0)
     if len(self.section.interfaces) > 1:
         # Spec is a bit ambiguous here. Section 4.4 says "it MUST
         # be assumed that all the Simple Packet Blocks have been captured
         # on the interface previously specified in the first Interface
         # Description Block." but later adds "A Simple Packet Block cannot
         # be present in a Section that has more than one interface because
         # of the impossibility to refer to the correct one (it does not
         # contain any Interface ID field)." Why would it say "the first"
         # IDB and not "the only" IDB if this was really forbidden?
         strictness.problem(
             "writing SimplePacket for section with multiple interfaces")
         if strictness.should_fix():
             # Can't fix this. The IDBs have already been written.
             pass
     snap_len = self.interface.snaplen
     if snap_len > 0 and snap_len < self.captured_len:
         # This isn't a strictness issue, it *will* break other readers
         # if we write more bytes than the snaplen says to expect.
         fld_size.encode(self.packet_len,
                         outstream,
                         endianness=self.section.endianness)
         fld_data.encode(self.packet_data[:snap_len], outstream)
     else:
         fld_size.encode(self.packet_len,
                         outstream,
                         endianness=self.section.endianness)
         fld_data.encode(self.packet_data, outstream)
Exemple #2
0
def write_options(stream, options):
    """
    Each option is composed by:

    - option_code (uint16)
    - value_length (uint16)
    - value (value_length-sized binary data)

    The end marker is simply an option with code ``0x0000``, length 0,
    and no payload
    """

    if not options:
        # Options are optional; if there are none we don't need opt_endofopt
        return

    for key in options:
        code = options._field_names[key]
        values = options.get_all_raw(key)
        if len(values) > 1 and not options.schema[code].multiple:
            strictness.problem(
                "writing repeated option {} '{}' not permitted by pcapng spec".
                format(code, options._get_name_alias(code)))
            if strictness.should_fix():
                values = values[:1]
        for value in values:
            write_int(code, stream, 16, False, options.endianness)
            write_int(len(value), stream, 16, False, options.endianness)
            write_bytes_padded(stream, value)
    # Write the end marker
    write_int(0, stream, 32, False, options.endianness)
Exemple #3
0
 def _check_multiples(self, code):
     """Check if a non-repeatable option is repeated"""
     if len(self.data[code]) > 1 and not self.schema[code].multiple:
         strictness.problem(
             "repeated option {} '{}' not permitted by pcapng spec".format(
                 code, self._get_name_alias(code)))
         if strictness.should_fix():
             self.data[code] = self.data[code][:1]
Exemple #4
0
 def write(self, outstream):
     if len(self.section.interfaces) < 1:
         strictness.problem(
             "writing {cls} for section with no interfaces".format(
                 cls=self.__class__.__name__))
         if strictness.should_fix():
             # Only way to "fix" is to not write the block
             return
     super(BlockWithInterfaceMixin, self).write(outstream)
Exemple #5
0
 def write(self, outstream):
     strictness.problem("Packet Block is obsolete and must not be used")
     if strictness.should_fix():
         self.enhanced().write(outstream)
     else:
         super(ObsoletePacket, self).write(outstream)