Main Content

write

Write protocol packet data to PCAP or PCAPNG file

Since R2020b

    Description

    write(pcapObj,packet,timestamp) writes the protocol packet data to the PCAP file specified in the PCAP file writer object, pcapObj. Input packet specifies the protocol packet and input timestamp specifies the packet arrival time.

    write(pcapngObj,packet,timestamp,interfaceID) writes protocol packet data to a PCAPNG file specified in the PCAPNG file writer object, pcapngObj. Input packet, timestamp, and interfaceID specifies the protocol packet, packet arrival time, and interface identifier, respectively.

    example

    write(obj,packet,timestamp) writes the WLAN MAC packet data to the PCAP or PCAPNG file specified in the WLAN PCAP file writer object, obj. Input packet specifies the WLAN MAC packet and input timestamp specifies the packet arrival time.

    example

    write(___,Name,Value) specifies options using one or more name-value pair arguments in addition to the input argument combinations from any of the previous syntaxes. For example, 'PacketFormat','bits' sets the format of the protocol packets to bits.

    Examples

    collapse all

    Create a default PCAP file writer object. Specify the link type for WLAN packet.

    pcapObj = pcapWriter;
    wlanLinkType = 105;

    Write a global header to the PCAP file.

    writeGlobalHeader(pcapObj,wlanLinkType);

    Generate a WLAN packet in bits.

    macConfig = wlanMACFrameConfig;
    [mpdu,frameLength] = wlanMACFrame(macConfig,'OutputFormat','bits');

    Write the WLAN packet to the PCAP file, specifying the packet format as bits.

    timestamp = 124800; % Number of microseconds
    write(pcapObj,mpdu,timestamp,'PacketFormat','bits');

    Create a PCAPNG file writer object, specifying the name of the PCAPNG file.

    pcapngObj = pcapngWriter('FileName','sample');

    Write the interface block for WLAN.

    interfaceName = 'WLAN';
    wlanLinkType = 105;
    interfaceId = writeInterfaceDescriptionBlock(pcapngObj,wlanLinkType, ...
        interfaceName);

    Generate a WLAN packet in bits.

    macConfig = wlanMACFrameConfig;
    [mpdu,frameLength] = wlanMACFrame(macConfig,'OutputFormat','bits');

    Write the WLAN packet to the PCAPNG file, specifying the packet comment and packet format.

    timestamp = 0; % Number of microseconds
    packetComment = 'This is the first packet';
    write(pcapngObj,mpdu,timestamp,interfaceId,'PacketComment', ...
        packetComment,'PacketFormat','bits');

    Create a default WLAN PCAP file writer object.

    obj = wlanPCAPWriter;

    Generate a WLAN MAC packet of type QoS Data. Specify the frame format as HT-Mixed.

    macConfig = wlanMACFrameConfig('FrameType','QoS Data','FrameFormat','HT-Mixed');
    payload ='00576000103afffe80';
    mpdu = wlanMACFrame(payload,macConfig);

    Write the WLAN MAC packet data to the PCAP file.

    timestamp = 0; % Number of microseconds
    write(obj,mpdu,timestamp);

    Input Arguments

    collapse all

    Note

    The pcapWriter, pcapngWriter, and wlanPCAPWriter objects do not overwrite the existing PCAP or PCAPNG files. During each call of these objects, specify a unique PCAP or PCAPNG file name.

    PCAP file writer object, specified as a pcapWriter object.

    Protocol packet, specified as one of these values.

    • Binary-valued vector – This value represents bits.

    • Character vector – This value represents octets in hexadecimal format.

    • String scalar – This value represents octets in hexadecimal format.

    • Numeric vector with each element in the range [0, 255] – This value represents octets in decimal format.

    • n-by-2 character array – In this value, each row represents an octet in hexadecimal format.

    Data Types: char | string | double

    Packet arrival time in POSIX® microseconds elapsed since 1/1/1970, specified as a nonnegative integer.

    Data Types: double

    PCAPNG file writer object, specified as a pcapngWriter object.

    Unique identifier for an interface, specified as a nonnegative scalar.

    Data Types: double

    WLAN PCAP file writer object, specified as a wlanPCAPWriter object.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

    Example: 'PacketFormat','bits' specifies the format of the protocol packet to bits.

    Format of the protocol packet, specified as the comma-separated pair consisting of PacketFormat and 'octets' or 'bits'. If this value is specified as 'octets', packet is specified as one of these values.

    • Binary-valued vector – This value represents bits.

    • Character vector – This value represents octets in hexadecimal format.

    • String scalar – This value represents octets in hexadecimal format.

    • Numeric vector with each element in the range [0, 255] – This value represents octets in decimal format.

    • n-by-2 character array – In this value, each row represents an octet in hexadecimal format.

    Data Types: char | string | double

    Comment for the protocol packet, specified as the comma-separated pair consisting of PacketComment and a character vector or a string scalar.

    Dependencies

    To enable this name-value pair argument, specify the pcapngObj input argument.

    Data Types: char | string

    WLAN packet metadata , specified as the comma-separated pair consisting of Radiotap and one of these values.

    • Binary-valued vector – This value represents bits.

    • Character vector – This value represents octets in hexadecimal format.

    • String scalar – This value represents octets in hexadecimal format.

    • Numeric vector with each element in the range [0, 255] – This value represents octets in decimal format.

    • n-by-2 character array – In this value, each row represents an octet in hexadecimal format.

    Dependencies

    To enable this name-value pair argument, specify the obj input argument.

    Data Types: char | string | double

    Format of the radiotap, specified as the comma-separated pair consisting of RadiotapFormat and 'octets' or 'bits'. If this value is specified as 'octets', Radiotap is specified as one of these values.

    • Binary-valued vector – This value represents bits.

    • Character vector – This value represents octets in hexadecimal format.

    • String scalar – This value represents octets in hexadecimal format.

    • Numeric vector with each element in the range [0, 255] – This value represents octets in decimal format.

    • n-by-2 character array – In this value, each row represents an octet in hexadecimal format.

    Dependencies

    To enable this name-value pair argument, specify the obj input argument.

    Data Types: char | string | double

    References

    [1] Tuexen, M. “PCAP Next Generation (Pcapng) Capture File Format.” 2020. https://www.ietf.org/.

    [2] “Radiotap - Introduction.” Accessed May 20, 2020. https://www.radiotap.org/.

    [3] Group, The Tcpdump. “Tcpdump/Libpcap Public Repository.” Accessed May 20, 2020. https://www.tcpdump.org.

    [4] “Development/LibpcapFileFormat - The Wireshark Wiki.” Accessed May 20, 2020. https://www.wireshark.org.

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    Version History

    Introduced in R2020b