Browse docspacket.copy_to
On this page
API method

packet.copy_to

Decodes payload bytes into the fields of a struct variable following a layout string like "u8 kind; u16 sequence; i16 sticks[4]", starting at a payload-relative offset (0 by default).

Syntax

Atlas Script
packet.copy_to(record, layout, offset?)

Parameters

record
Struct variable whose fields the layout string names. packet.copy_to fills those fields from the packet; packet.copy_from writes them into the packet.
layout
Layout string listing struct fields in byte order, like "u8 kind; u16 sequence; i16 sticks[4]".
offset Optional
Byte offset into the packet. Payload-relative for packet.* methods, and absolute (whole IP packet) for the raw_* variants.

Return value

See the remarks for the returned value and runtime behavior.

Remarks

Decodes payload bytes into the fields of a struct variable following a layout string like "u8 kind; u16 sequence; i16 sticks[4]", starting at a payload-relative offset (0 by default). Every layout name must be a field of the struct; an array entry stores a new array of the listed count. Returns false, leaving every struct field unchanged, when the layout is invalid, names a field the struct does not have, or would run past the end of the packet.

Examples

Decode a payload into a struct.atlas
struct Message {
    var kind;
    var sequence;
    var flags;
};

const string LAYOUT = "u8 kind; u16be sequence; u16be flags";
Message message;

event on_start() {
    if (!network.capture("udp and udp.DstPort == 9000")) {
        system.log("Network capture could not start");
    }
}

event on_network_send() {
    if (packet.copy_to(message, LAYOUT) && message.kind == 0x02) {
        message.flags = math.bit_or(message.flags, 0x0004);
        if (!packet.copy_from(message, LAYOUT)) {
            system.log("The payload no longer fits the layout");
        }
    }
}
Reference details are generated from the Atlas Script schema in this documentation source snapshot. Confirm release-specific behavior in the Atlas for Windows build you use.