Copy a record structures into another variable of same type

Hi,

I have a simple requirement of copying type OPCUA_Binary::XXX record structures into another variable of same type. I don’t want to copy all the fields inside the structure one by one. Is it possible? icsnpp-opcua-binary

example:

type Info: record {
        bin_info: OPCUA_Binary::Info &optional;
};

redef record connection += {
        opcua_bin: Info &optional;
};

event opcua_binary_event(c: connection, info: OPCUA_Binary::Info)
   {
       set_service(c, "opcua-binary");
       info$ts  = network_time();
       info$uid = c$uid;
       info$id  = c$id;

       c$opcua_bin$bin_info = info;

       Log::write(ICSNPP_OPCUA_Binary::LOG, info);
    }

c$opcua_bin$bin_info = info; this assignement is giving expression error in main.zeek, line XXX: field value missing (ICSNPP_OPCUA_Binary::c$opcua_bin)

I am using Zeek 6.1.1.

Thanks
Biswa

The $opcua_bin field is &optional, so it doesn’t exist unless you assign to it (not to an element within it). So try c$opcua_bin = Info($bin_info = info). That creates a new Info record to assign to $opcua_bin, and populates its one field with the value of info.

Thanks a lot @Vern . It’s working. Hope it will work if I have multiple different structures inside my Info: record structure.

type Info: record {
        bin_info1: OPCUA_Binary::Info2 &optional;
        bin_info2: OPCUA_Binary::Info2 &optional;
        bin_info3: OPCUA_Binary::Info3 &optional;
        bin_info4: OPCUA_Binary::Info4 &optional;
};

redef record connection += {
        opcua_bin: Info &optional;
};

I have tested, it seems working, just to confirm if it’s theoretically correct.

Thanks
Biswa

Yes having multiple fields is fine, and if they’re optional then you can leave them off from the constructor if they’re not available at that point.

1 Like