aboutsummaryrefslogtreecommitdiff
path: root/src/core/message.c
blob: 346570c92c0833c2d7d2c292b77807374ad7b6ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// Copyright 2016 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "core/nng_impl.h"

// Message API.

// Message chunk, internal to the message implementation.
typedef struct {
	size_t		ch_cap;         // allocated size
	size_t		ch_len;         // length in use
	uint8_t *	ch_buf;         // underlying buffer
	uint8_t *	ch_ptr;         // pointer to actual data
} nni_chunk;

// Underlying message structure.
struct nng_msg {
	nni_chunk	m_header;
	nni_chunk	m_body;
	nni_time	m_expire;       // usec
	nni_list	m_options;
};

typedef struct {
	int		mo_num;
	size_t		mo_sz;
	void *		mo_val;
	nni_list_node	mo_node;
} nni_msgopt;

static void
nni_chunk_dump(const nni_chunk *chunk, char *prefix)
{
	size_t i, j;
	uint8_t x;
	char buf[128];

	(void) snprintf(buf, sizeof (buf),
	    " %s (cap %d, len %d, offset %d ptr %p):", prefix,
	    (int) chunk->ch_cap, (int) chunk->ch_len,
	    (int) (chunk->ch_ptr - chunk->ch_buf), chunk->ch_ptr);
	nni_println(buf);

	buf[0] = 0;
	for (i = 0, j = 0; i < chunk->ch_len; i++) {
		if ((i % 16) == 0) {
			if (j > 0) {
				buf[j++] = '\0';
				nni_println(buf);
				j = 0;
			}
			snprintf(buf, sizeof (buf), " %4x: ", (unsigned) i);
			j += strlen(buf);
		}
		buf[j++] = ' ';
		x = (chunk->ch_ptr[i] >> 4);
		buf[j++] = x > 9 ? ('A' + (x - 10)) : '0' + x;
		x = (chunk->ch_ptr[i] & 0x0f);
		buf[j++] = x > 9 ? ('A' + (x - 10)) : '0' + x;
	}
	if (j > 0) {
		buf[j++] = '\0';
		nni_println(buf);
	}
}


void
nni_msg_dump(const char *banner, const nni_msg *msg)
{
	char buf[128];

	(void) snprintf(buf, sizeof (buf), "--- %s BEGIN ---", banner);
	nni_println(buf);
	nni_chunk_dump(&msg->m_header, "HEADER");
	nni_chunk_dump(&msg->m_body, "BODY");
	nni_println("--- END ---");
}


// nni_chunk_grow increases the underlying space for a chunk.  It ensures
// that the desired amount of trailing space (including the length)
// and headroom (excluding the length) are available.  It also copies
// any extant referenced data.  Note that the capacity will increase,
// but not the length.  To increase the length of the referenced data,
// use either chunk_append or chunk_prepend.
//
// Note that having some headroom is useful when data must be prepended
// to a message - it avoids having to perform extra data copies, so we
// encourage initial allocations to start with sufficient room.
static int
nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted)
{
	size_t headroom = 0;
	uint8_t *newbuf;

	// We assume that if the pointer is a valid pointer, and inside
	// the backing store, then the entire data length fits.  In this
	// case we perform a logical realloc, except we don't copy any
	// unreferenced data.  We do preserve the headroom of the previous
	// use, since that may be there for a reason.
	//
	// The test below also covers the case where the pointers are both
	// NULL, or the capacity is zero.

	if ((ch->ch_ptr >= ch->ch_buf) &&
	    (ch->ch_ptr < (ch->ch_buf + ch->ch_cap))) {
		headroom = (size_t) (ch->ch_ptr - ch->ch_buf);
		if (headwanted < headroom) {
			headwanted = headroom; // Never shrink this.
		}
		if (((newsz + headwanted) < ch->ch_cap) &&
		    (headwanted <= headroom)) {
			// We have enough space at the ends already.
			return (0);
		}
		if ((newbuf = nni_alloc(newsz + headwanted)) == NULL) {
			return (NNG_ENOMEM);
		}
		// Copy all the data, but not header or trailer.
		memcpy(newbuf + headwanted, ch->ch_ptr, ch->ch_len);
		nni_free(ch->ch_buf, ch->ch_cap);
		ch->ch_buf = newbuf;
		ch->ch_ptr = newbuf + headwanted;
		ch->ch_cap = newsz + headwanted;
		return (0);
	}

	// We either don't have a data pointer yet, or it doesn't reference
	// the backing store.  In this case, we just check against the
	// allocated capacity and grow, or don't grow.
	if ((newsz + headwanted) >= ch->ch_cap) {
		if ((newbuf = nni_alloc(newsz + headwanted)) == NULL) {
			return (NNG_ENOMEM);
		}
		nni_free(ch->ch_buf, ch->ch_cap);
		ch->ch_cap = newsz + headwanted;
		ch->ch_buf = newbuf;
	}

	ch->ch_ptr = ch->ch_buf + headwanted;
	return (0);
}


static void
nni_chunk_free(nni_chunk *ch)
{
	if ((ch->ch_cap != 0) && (ch->ch_buf != NULL)) {
		nni_free(ch->ch_buf, ch->ch_cap);
	}
	ch->ch_ptr = NULL;
	ch->ch_buf = NULL;
	ch->ch_len = 0;
	ch->ch_cap = 0;
}


// nni_chunk_trunc truncates bytes from the end of the chunk.
static int
nni_chunk_trunc(nni_chunk *ch, size_t len)
{
	if (ch->ch_len < len) {
		return (NNG_EINVAL);
	}
	ch->ch_len -= len;
	return (0);
}


// nni_chunk_trim removes bytes from the beginning of the chunk.
static int
nni_chunk_trim(nni_chunk *ch, size_t len)
{
	if (ch->ch_len < len) {
		return (NNG_EINVAL);
	}
	ch->ch_len -= len;
	// Don't advance the pointer if we are just removing the whole content
	if (ch->ch_len != 0) {
		ch->ch_ptr += len;
	}
	return (0);
}


// nni_chunk_dup allocates storage for a new chunk, and copies
// the contents of the source to the destination.  The new chunk will
// have the same size, headroom, and capacity as the original.
static int
nni_chunk_dup(nni_chunk *dst, const nni_chunk *src)
{
	if ((dst->ch_buf = nni_alloc(src->ch_cap)) == NULL) {
		return (NNG_ENOMEM);
	}
	dst->ch_cap = src->ch_cap;
	dst->ch_len = src->ch_len;
	dst->ch_ptr = dst->ch_buf + (src->ch_ptr - src->ch_buf);
	memcpy(dst->ch_ptr, src->ch_ptr, dst->ch_len);
	return (0);
}


// nni_chunk_append appends the data to the chunk, growing as necessary.
// If the data pointer is NULL, then the chunk data region is allocated,
// but uninitialized.
static int
nni_chunk_append(nni_chunk *ch, const void *data, size_t len)
{
	int rv;

	if (len == 0) {
		return (0);
	}
	if ((rv = nni_chunk_grow(ch, len + ch->ch_len, 0)) != 0) {
		return (rv);
	}
	if (ch->ch_ptr == NULL) {
		ch->ch_ptr = ch->ch_buf;
	}
	if (data != NULL) {
		memcpy(ch->ch_ptr + ch->ch_len, data, len);
	}
	ch->ch_len += len;
	return (0);
}


// nni_chunk_prepend prepends data to the chunk, as efficiently as possible.
// If the data pointer is NULL, then no data is actually copied, but the
// data region will have "grown" in the beginning, with uninitialized data.
static int
nni_chunk_prepend(nni_chunk *ch, const void *data, size_t len)
{
	int rv;

	if (ch->ch_ptr == NULL) {
		ch->ch_ptr = ch->ch_buf;
	}

	if ((ch->ch_ptr >= ch->ch_buf) &&
	    (ch->ch_ptr < (ch->ch_buf + ch->ch_cap)) &&
	    (len <= (size_t) (ch->ch_ptr - ch->ch_buf))) {
		// There is already enough room at the beginning.
		ch->ch_ptr -= len;
	} else if ((ch->ch_len + len) <= ch->ch_cap) {
		// We had enough capacity, just shuffle data down.
		memmove(ch->ch_ptr + len, ch->ch_ptr, ch->ch_len);
	} else if ((rv = nni_chunk_grow(ch, 0, len)) == 0) {
		// We grew the chunk, so adjust.
		ch->ch_ptr -= len;
	} else {
		// Couldn't grow the chunk either.  Error.
		return (rv);
	}

	ch->ch_len += len;
	if (data) {
		memcpy(ch->ch_ptr, data, len);
	}

	return (0);
}


int
nni_msg_alloc(nni_msg **mp, size_t sz)
{
	nni_msg *m;
	int rv;

	if ((m = NNI_ALLOC_STRUCT(m)) == NULL) {
		return (NNG_ENOMEM);
	}

	// 64-bytes of header, including room for 32 bytes
	// of headroom and 32 bytes of trailer.
	if ((rv = nni_chunk_grow(&m->m_header, 32, 32)) != 0) {
		NNI_FREE_STRUCT(m);
		return (rv);
	}

	// If the message is less than 1024 bytes, or is not power
	// of two aligned, then we insert a 32 bytes of headroom
	// to allow for inlining backtraces, etc.  We also allow the
	// amount of space at the end for the same reason.  Large aligned
	// allocations are unmolested to avoid excessive overallocation.
	if ((sz < 1024) || ((sz & (sz-1)) != 0)) {
		rv = nni_chunk_grow(&m->m_body, sz + 32, 32);
	} else {
		rv = nni_chunk_grow(&m->m_body, sz, 0);
	}
	if (rv != 0) {
		nni_chunk_free(&m->m_header);
		NNI_FREE_STRUCT(m);
	}
	if ((rv = nni_chunk_append(&m->m_body, NULL, sz)) != 0) {
		// Should not happen since we just grew it to fit.
		nni_panic("chunk_append failed");
	}

	NNI_LIST_INIT(&m->m_options, nni_msgopt, mo_node);
	*mp = m;
	return (0);
}


int
nni_msg_dup(nni_msg **dup, const nni_msg *src)
{
	nni_msg *m;
	nni_msgopt *mo;
	nni_msgopt *newmo;
	int rv;

	if ((m = NNI_ALLOC_STRUCT(m)) == NULL) {
		return (NNG_ENOMEM);
	}
	memset(m, 0, sizeof (*m));
	NNI_LIST_INIT(&m->m_options, nni_msgopt, mo_node);


	if ((rv = nni_chunk_dup(&m->m_header, &src->m_header)) != 0) {
		NNI_FREE_STRUCT(m);
		return (rv);
	}
	if ((rv = nni_chunk_dup(&m->m_body, &src->m_body)) != 0) {
		nni_chunk_free(&m->m_header);
		NNI_FREE_STRUCT(m);
		return (rv);
	}

	NNI_LIST_FOREACH (&src->m_options, mo) {
		newmo = nni_alloc(sizeof (*newmo) + mo->mo_sz);
		if (newmo == NULL) {
			nni_msg_free(m);
			return (NNG_ENOMEM);
		}
		newmo->mo_val = ((char *) newmo + sizeof (*newmo));
		newmo->mo_sz = mo->mo_sz;
		newmo->mo_num = mo->mo_num;
		memcpy(newmo->mo_val, mo->mo_val, mo->mo_sz);
		nni_list_append(&m->m_options, newmo);
	}

	*dup = m;
	return (0);
}


void
nni_msg_free(nni_msg *m)
{
	nni_msgopt *mo;

	if (m != NULL) {
		nni_chunk_free(&m->m_header);
		nni_chunk_free(&m->m_body);
		while ((mo = nni_list_first(&m->m_options)) != NULL) {
			nni_list_remove(&m->m_options, mo);
			nni_free(mo, sizeof (*mo) + mo->mo_sz);
		}
		NNI_FREE_STRUCT(m);
	}
}


int
nni_msg_setopt(nni_msg *m, int opt, const void *val, size_t sz)
{
	// Find the existing option if present.  Note that if we alter
	// a value, we can wind up trashing old data due to ENOMEM.
	nni_msgopt *oldmo, *newmo;

	NNI_LIST_FOREACH (&m->m_options, oldmo) {
		if (oldmo->mo_num == opt) {
			if (sz == oldmo->mo_sz) {
				// nice! we can just overwrite old value
				memcpy(oldmo->mo_val, val, sz);
				return (0);
			}
			break;
		}
	}
	if ((newmo = nni_alloc(sizeof (*newmo) + sz)) == NULL) {
		return (NNG_ENOMEM);
	}
	newmo->mo_val = ((char *) newmo + sizeof (*newmo));
	newmo->mo_sz = sz;
	newmo->mo_num = opt;
	memcpy(newmo->mo_val, val, sz);
	if (oldmo != NULL) {
		nni_list_remove(&m->m_options, oldmo);
		nni_free(oldmo, sizeof (*oldmo) + oldmo->mo_sz);
	}
	nni_list_append(&m->m_options, newmo);
	return (0);
}


int
nni_msg_getopt(nni_msg *m, int opt, void *val, size_t *szp)
{
	nni_msgopt *mo;

	NNI_LIST_FOREACH (&m->m_options, mo) {
		if (mo->mo_num == opt) {
			size_t sz = *szp;
			if (sz > mo->mo_sz) {
				sz = mo->mo_sz;
				memcpy(val, mo->mo_val, sz);
				*szp = mo->mo_sz;
				return (0);
			}
		}
	}
	return (NNG_ENOTSUP);
}


int
nni_msg_realloc(nni_msg *m, size_t sz)
{
	int rv = 0;

	if (m->m_body.ch_len < sz) {
		rv = nni_chunk_append(&m->m_body, NULL, sz - m->m_body.ch_len);
		if (rv != 0) {
			return (rv);
		}
	} else {
		// "Shrinking", just mark bytes at end usable again.
		nni_chunk_trunc(&m->m_body, m->m_body.ch_len - sz);
	}
	return (0);
}


void *
nni_msg_header(nni_msg *m)
{
	return (m->m_header.ch_ptr);
}


size_t
nni_msg_header_len(nni_msg *m)
{
	return (m->m_header.ch_len);
}


void *
nni_msg_body(nni_msg *m)
{
	return (m->m_body.ch_ptr);
}


size_t
nni_msg_len(nni_msg *m)
{
	return (m->m_body.ch_len);
}


int
nni_msg_append(nni_msg *m, const void *data, size_t len)
{
	return (nni_chunk_append(&m->m_body, data, len));
}


int
nni_msg_prepend(nni_msg *m, const void *data, size_t len)
{
	return (nni_chunk_prepend(&m->m_body, data, len));
}


int
nni_msg_trim(nni_msg *m, size_t len)
{
	return (nni_chunk_trim(&m->m_body, len));
}


int
nni_msg_trunc(nni_msg *m, size_t len)
{
	return (nni_chunk_trunc(&m->m_body, len));
}


int
nni_msg_append_header(nni_msg *m, const void *data, size_t len)
{
	return (nni_chunk_append(&m->m_header, data, len));
}


int
nni_msg_prepend_header(nni_msg *m, const void *data, size_t len)
{
	return (nni_chunk_prepend(&m->m_header, data, len));
}


int
nni_msg_trim_header(nni_msg *m, size_t len)
{
	return (nni_chunk_trim(&m->m_header, len));
}


int
nni_msg_trunc_header(nni_msg *m, size_t len)
{
	return (nni_chunk_trunc(&m->m_header, len));
}