23 struct nmsg_container {
32 struct nmsg_container *
34 struct nmsg_container *c;
36 c = calloc(1,
sizeof(*c));
40 c->nmsg = calloc(1,
sizeof(Nmsg__Nmsg));
41 if (c->nmsg == NULL) {
45 nmsg__nmsg__init(c->nmsg);
60 nmsg__nmsg__free_unpacked((*c)->nmsg, NULL);
68 c->do_sequence = do_sequence;
73 Nmsg__NmsgPayload *np;
79 res = _nmsg_message_serialize(msg);
82 assert(msg->np != NULL);
85 np_len = _nmsg_payload_size(msg->np);
88 if (c->estsz !=
NMSG_HDRLSZ_V2 && c->estsz + np_len + 32 >= c->bufsz)
89 return (nmsg_res_container_full);
92 tmp = c->nmsg->payloads;
93 c->nmsg->payloads = realloc(c->nmsg->payloads,
94 ++(c->nmsg->n_payloads) *
sizeof(
void *));
95 if (c->nmsg->payloads == NULL) {
96 c->nmsg->payloads = tmp;
105 c->nmsg->payloads[c->nmsg->n_payloads - 1] = np;
111 c->estsz += ((np_len >= (1 << 7)) ? 1 : 0);
112 c->estsz += ((np_len >= (1 << 14)) ? 1 : 0);
113 c->estsz += ((np_len >= (1 << 21)) ? 1 : 0);
117 c->estsz += (c->do_sequence ? (6+12) : 0);
120 if (c->estsz > c->bufsz)
121 return (nmsg_res_container_overfull);
128 return (c->nmsg->n_payloads);
133 uint8_t **pbuf,
size_t *buf_len,
134 bool do_header,
bool do_zlib,
135 uint32_t sequence, uint64_t sequence_id)
141 uint8_t *len_wire = NULL;
144 *pbuf = buf = malloc((do_zlib) ? (2 * c->estsz) : (c->estsz));
150 memcpy(buf, magic,
sizeof(magic));
151 buf +=
sizeof(magic);
154 version = htons(version);
155 memcpy(buf, &version,
sizeof(version));
156 buf +=
sizeof(version);
160 buf +=
sizeof(uint32_t);
164 _nmsg_payload_calc_crcs(c->nmsg);
166 if (c->do_sequence) {
167 c->nmsg->sequence = sequence;
168 c->nmsg->sequence_id = sequence_id;
169 c->nmsg->has_sequence =
true;
170 c->nmsg->has_sequence_id =
true;
174 if (do_zlib ==
false) {
175 len = nmsg__nmsg__pack(c->nmsg, buf);
182 zb_tmp = malloc(c->estsz);
183 if (zb_tmp == NULL) {
195 ulen = nmsg__nmsg__pack(c->nmsg, zb_tmp);
206 store_net32(len_wire, len);
212 _nmsg_dprintf(6,
"%s: buf= %p len= %zd\n", __func__, buf, len);
219 nmsg_message_t **msgarray,
size_t *n_msg)
227 res = _input_nmsg_deserialize_header(buf, buf_len, &msgsize, &flags);
234 if ((
size_t) msgsize != buf_len)
238 res = _input_nmsg_unpack_container2(buf, buf_len, flags, &nmsg);
243 *msgarray = malloc(nmsg->n_payloads *
sizeof(
void *));
244 if (*msgarray == NULL) {
245 nmsg__nmsg__free_unpacked(nmsg, NULL);
248 *n_msg = nmsg->n_payloads;
250 for (
unsigned i = 0; i < nmsg->n_payloads; i++) {
251 Nmsg__NmsgPayload *np;
255 np = nmsg->payloads[i];
256 nmsg->payloads[i] = NULL;
259 msg = _nmsg_message_from_payload(np);
264 nmsg__nmsg__free_unpacked(nmsg, NULL);
267 (*msgarray)[i] = msg;
269 nmsg->n_payloads = 0;
270 free(nmsg->payloads);
271 nmsg->payloads = NULL;
272 nmsg__nmsg__free_unpacked(nmsg, NULL);
void nmsg_zbuf_destroy(nmsg_zbuf_t *zb)
Destroy all resources associated with an nmsg_zbuf_t object.
nmsg_res nmsg_container_serialize(nmsg_container_t c, uint8_t **pbuf, size_t *buf_len, bool do_header, bool do_zlib, uint32_t sequence, uint64_t sequence_id)
Serialize an NMSG container object, allocating memory as needed and returning a free()able buffer con...
#define NMSG_MAGIC
Four-octet magic sequence seen at the beginning of a serialized NMSG.
#define NMSG_WBUFSZ_MIN
Minimum number of octets that an nmsg wbuf must hold.
#define NMSG_HDRLSZ_V2
Number of octets in an NMSG header (magic + version + length).
nmsg_res nmsg_zbuf_deflate(nmsg_zbuf_t zb, size_t len, u_char *buf, size_t *z_len, u_char *z_buf)
Deflate a buffer.
void nmsg_container_set_sequence(nmsg_container_t c, bool do_sequence)
Enable or disable NMSG container sequence tracking.
nmsg_zbuf_t nmsg_zbuf_deflate_init(void)
Initialize an nmsg_zbuf_t object for deflation.
nmsg_res nmsg_container_deserialize(const uint8_t *buf, size_t buf_len, nmsg_message_t **msgarray, size_t *n_msg)
Deserialize a collection of NMSG messages contained in a buffer containing a serialized NMSG containe...
#define NMSG_FLAG_ZLIB
NMSG container is zlib compressed.
nmsg_res nmsg_container_add(nmsg_container_t c, nmsg_message_t msg)
Add an NMSG message object to an NMSG container object.
void nmsg_container_destroy(nmsg_container_t *c)
Deallocate the resources associated with an nmsg_container_t object.
size_t nmsg_container_get_num_payloads(nmsg_container_t c)
Get the current number of payloads in the NMSG container object.
nmsg_container_t nmsg_container_init(size_t bufsz)
Initialize a new NMSG container object.
#define NMSG_VERSION
Current version number of the NMSG serialization format.