How to pack a struct with a struct member in lua? - struct

I'm writing a lua script for Nginx to communicate with a server which provides a protocol I must comply with. And the protocol accepts a struct with a struct member, I can't find a way to pack it in lua.
require "struct"
-- struct udp_packet_header {
-- uint32_t head_token;
-- uint64_t uuid;
-- uint64_t timestamp;
-- uint32_t order;
-- uint32_t trans_size;
-- uint32_t https_size;
-- uint16_t head_crc;
-- };
local udp_packet_header_def = '<ILLIIIH'
-- struct udp_packet {
-- struct udp_packet_header header;
-- char data[];
-- };
local udp_packet_struct_def = ?
As defined above, how can I pack the struct udp_packet with a struct member header?
I checked https://github.com/iryont/lua-struct but found nothing useful.
Is doing this even possible? If not, is there a better way to do this? Maybe write C code and call it from lua?

Related

Is it possible to export a struct symbols?

in linux kernel driver is it possible to export a struct as a symbol? In other if there are 2 drivers i.e. Driver_A and Driver_B. And Driver_A has a struct test_a defined that is required to be exported so that Driver_B can directly reference struct test_a. Something like following
Driver_A has following structure defined
struct test_a {
int a,
long b,
int (*mem_write)(void *buf_priv, uint32_t size);
}
EXPORT_SYMBOL(test_a)
Driver_B
extern struct test_a test;
And method in Driver_B is able to access a,b and

How to publish a constant string in the Rust FFI?

I want to have a Rust library expose a const char * static string to C, to be compatible with an existing interface (specifically librsync). That is, the C header file has
extern char const *my_string;
In C, the library would simply have
char const *my_string = "hi";
In Rust I've tried something like
pub static my_string: *const libc::c_char = unsafe { "hi\0" as *const libc::c_char };
but this complains
error: casting `&'static str` as `*const i8` is invalid
It seems like I can't use CString etc because they won't be a compile-time constant expression.
We need a public, static, unmangled pointer to some zero-terminated bytes:
#[export_name = "CONST_C_STR"] // or #[no_mangle]
pub static CONST_C_STR: &[u8; 20] = b"a constant c string\0";
This worked with a simple C program:
#include <stdio.h>
extern char * CONST_C_STR;
int main(int argc, char *argv[]) {
printf("%s\n", CONST_C_STR);
}
The crate c_str_macro provides a convenience macro c_str!, which appends a 0 byte to a Rust string literal and presents it as a CStr reference.
Disclaimer: I'm the author of the crate.

Struct->Union-Struct

I have this struct:
struct foo {
int a;
union {
struct {
int b;
struct bar
{
int c;
int d;
} *aBar;
} in;
} u;
};
How I need to declare a variable of type bar, in Visual C++ ?
When you declare an structure like this:
struct
{
int b;
} in;
You are actually creating an object with name in, having unnamed-data type. This data-type would be named internally by compiler, and depends on compiler. The style given above does not declare in to be a type, but a variable!
If you want to make it a type, use either of given approaches:
// Approach 1
struct in{...};
// Approach 2
typedef struct {..} in; // in is now a type, because of `typedef`
If you have compiler that supports C++0x, and specifically type decltype keyword, you can use it against the first style (which makes in a variable). Example:
decltype(in) in_var;
in_var.b = 10;
Thanks Ajay, I solved that way:
foo *k;
decltype(k->u.in.aBar) j;
j->c = 1;
j->d = 1;

Marshalling nested Structs in C to C#

I'm again stuck with marshalling C structs into C#. Here is what I want to marshal to C#.
The C function signature is as below
int GetParametersDescription(unsigned int *numParams,
OP_PARAMETER_INFO **parameterInfo,
unsigned int *numVars,
OP_VARIABLE_INFO **variableInfo);
Output Arguments
numParams: number of model’s parameters.
parameterInfo: List of parameters’ structures returned.Size of the list is numParams.
numVars: number of model’s variables.
variableInfo: List of variables’ structures returned.Size of the list is numVars.
OP_PARAMETER_INFO struct signature in C
typedef struct OP_PARAMETER_INFO {
unsigned id; // Parameter Id
char *path; // Parameter's path in the model
char *name; // Parameter's label if any
char *alias; // Parameter's alias if any.
struct OP_VARIABLE_INFO *varInfo; // Pointer on OP_VARIABLE_INFO if variable exist
unsigned nbRows; // Number of rows for this parameter
unsigned nbCols; // Number of cols for this parameter
double *values; // List of values, nbValue = nbRows * nbCols
OP_SEARCH_RESULTS exist; // This is an enum
unsigned newParamId; // ID of the estimated parameter if does not exist
} OP_PARAMETER_INFO;
OP_VARIABLE_INFO struct signature in C
typedef struct OP_VARIABLE_INFO {
unsigned id; // Variable Id
char *name; // Variable name
unsigned int nbParams; // Number of parameters associated with the variable
struct OP_PARAMETER_INFO** param; // List of pointers of OP_PARAMETER_INFO structure
} OP_VARIABLE_INFO;
OP_SEARCH_RESULTS is an enum and the signature look as below
typedef enum {
OP_SEARCH_NOT_FOUND,
OP_SEARCH_FOUND_EXACT,
OP_SEARCH_FOUND_DIFFERENT,
OP_SEARCH_FOUND_ESTIMATION
} OP_SEARCH_RESULTS;
I did try out making structs in C#, but none of them seem to work.
Could you pls help me defining these structs in C# and calling them in P/invoke methods and to extract the structs information.....

Linux kernel rb tree

Is it valid to do the following
struct foo {
int data;
struct rb_node node
};
struct rb_root root;
/* Filling tree with kalloc'ed foo nodes */
struct rb_node *node=rb_first(&root);
while (node)
{
struct rb_node *next=rb_next(node);
kfree(node);
node = next;
}
root=RB_ROOT;
In fact, I just want do foreach and clear at same time with linear time.
Explored rb_next implementation. It returns parent before right children.
So, It is impossible to clear list this way.

Resources