I was debugging ganglia and came through sending data using XDR through UDP channel.
I found the second argument of the function xdrmem_create ( xdrs, addr, size, op)
strange.
Where the syntax of the arguments are given as:
XDR *xdrs;
char *addr;
u_int size;
enum xdr_op op;
The reference of this function is here.
As you can see, the second argument (xdrs) of this function is a character array. And this is similarly declared in one of the ganglia's function as char msgbuf[GANGLIA_MAX_MESSAGE_LEN];
.
After calling above function as xdrmem_create(&x, msgbuf, GANGLIA_MAX_MESSAGE_LEN, XDR_ENCODE);
in ganglia , appropriate data in the ganglia's specific structure (cb->msg
) is encoded to XDR format by calling the function xdr_Ganglia_value_msg(&x, &(cb->msg))
where x
is an XDR
type of variable.
Later, to send the encoded data through UDP channel, the function Ganglia_udp_send_message( udp_send_channels, msgbuf, len);
is called.
To understand how this XDR data is sent, I tried to print the output of the content of msgbuf
using fprintf
but it always print nothing in spite of the fact that it is a character array . And it is also evident that the encoded data is sent successfully.
So, my question is, How data encoded into XDR format is being sent through UDP channel here?
I have pasted a part of code from ganglia here. You can see from the line 131 to 136 for your reference.
The messages are encoded with XDR, a binary format. The XDR libraries used are old, and a modern version of the API probably would have used uint8_t instead of char. Despite it using char, it is binary data - i.e. you cannot print the data as a string.
If you want to print this data, use a loop where you print each byte as hex e.g. by doing printf("%02X ", msgbuf[i]);
Read RFC4506 to learn about the XDR encoding. The actual messages are described in an XDR language specification, and run through a code generation tool (e.g. rpcgen for C code) to generate code for encoding/decoding. See https://github.com/fastly/ganglia/blob/master/lib/gm_protocol.x for the message definitions that Ganglia defines.