aboutsummaryrefslogtreecommitdiffstats
path: root/libaxl_send_request.c
blob: b8d9146901162b20a425a42a395239f9aabe0ddf (plain) (blame)
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* See LICENSE file for copyright and license details. */
#include "common.h"

static const char *const req_formats[] = {
	/*
	 * . = 1 unused byte
	 * , = 2 unused bytes
	 * _ = 3 unused bytes
	 * ! = libaxl_bool_t
	 * 1 = uint8_t
	 * 2 = uint16_t
	 * 4 = uint32_t
	 * z = int8_t
	 * s = int16_t
	 * S = int32_t
	 * $ = previous marked length, in units
	 * % = previous marked format, in bits
	 * @ = non-encoded size_t length marked, align input to (size_t)
	 * u = STRING8, align input to (void *) and enter
	 * U = STRING16, align input to (void *) and enter
	 * d = data, uses %, align input to (void *) and enter
	 * e = event, align input to (union libaxl_event)
	 * ? = jump to special encoding routine
	 * * = following are repeated, align input to (void *) and enter
	 * | = following are included and encoded as uint32_t (align output accordingly) if corresponding bit is set in previous
	 * 
	 * The bytes 2–3 (starting at 0) are ignored here because its
	 * the length field and will be filled in by the function
	 */
	[LIBAXL_REQUEST_CREATE_WINDOW]              = "11,44ss222244|4444111.44!!,4444",
	[LIBAXL_REQUEST_CHANGE_WINDOW_ATTRIBUTES]   = "1_44|4444111.44!!,4444",
	[LIBAXL_REQUEST_GET_WINDOW_ATTRIBUTES]      = "1_4",
	[LIBAXL_REQUEST_DESTROY_WINDOW]             = "1_4",
	[LIBAXL_REQUEST_DESTROY_SUBWINDOWS]         = "1_4",
	[LIBAXL_REQUEST_CHANGE_SAVE_SET]            = "11,4",
	[LIBAXL_REQUEST_REPARENT_WINDOW]            = "1_44ss",
	[LIBAXL_REQUEST_MAP_WINDOW]                 = "1_4",
	[LIBAXL_REQUEST_MAP_SUBWINDOWS]             = "1_4",
	[LIBAXL_REQUEST_UNMAP_WINDOW]               = "1_4",
	[LIBAXL_REQUEST_UNMAP_SUBWINDOWS]           = "1_4",
	[LIBAXL_REQUEST_CONFIGURE_WINDOW]           = "1_42|,ss222,41",
	[LIBAXL_REQUEST_CIRCULATE_WINDOW]           = "11,4",
	[LIBAXL_REQUEST_GET_GEOMETRY]               = "1_4",
	[LIBAXL_REQUEST_QUERY_TREE]                 = "1_4",
	[LIBAXL_REQUEST_INTERN_ATOM]                = "1!,2$,u",
	[LIBAXL_REQUEST_GET_ATOM_NAME]              = "1_4",
	[LIBAXL_REQUEST_CHANGE_PROPERTY]            = "11,4441%_4$d",
	[LIBAXL_REQUEST_DELETE_PROPERTY]            = "11,44",
	[LIBAXL_REQUEST_GET_PROPERTY]               = "1_44444",
	[LIBAXL_REQUEST_LIST_PROPERTIES]            = "1_4",
	[LIBAXL_REQUEST_SET_SELECTION_OWNER]        = "1_444",
	[LIBAXL_REQUEST_GET_SELECTION_OWNER]        = "1_4",
	[LIBAXL_REQUEST_CONVERT_SELECTION]          = "1_4444",
	[LIBAXL_REQUEST_SEND_EVENT]                 = "1!,44e",
	[LIBAXL_REQUEST_GRAB_POINTER]               = "1!,4211444",
	[LIBAXL_REQUEST_UNGRAB_POINTER]             = "1_4",
	[LIBAXL_REQUEST_GRAB_BUTTON]                = "1_4211441.2",
	[LIBAXL_REQUEST_UNGRAB_BUTTON]              = "11,42,",
	[LIBAXL_REQUEST_CHANGE_ACTIVE_POINTER_GRAB] = "1_442,",
	[LIBAXL_REQUEST_GRAB_KEYBOARD]              = "1!,4411,",
	[LIBAXL_REQUEST_UNGRAB_KEYBOARD]            = "1_4",
	[LIBAXL_REQUEST_GRAB_KEY]                   = "1!,42111_",
	[LIBAXL_REQUEST_UNGRAB_KEY]                 = "11,42,",
	[LIBAXL_REQUEST_ALLOW_EVENTS]               = "11,4",
	[LIBAXL_REQUEST_GRAB_SERVER]                = "1_",
	[LIBAXL_REQUEST_UNGRAB_SERVER]              = "1_",
	[LIBAXL_REQUEST_QUERY_POINTER]              = "1_4",
	[LIBAXL_REQUEST_GET_MOTION_EVENTS]          = "1_444",
	[LIBAXL_REQUEST_TRANSLATE_COORDINATES]      = "1_44ss",
	[LIBAXL_REQUEST_WARP_POINTER]               = "1_44ss22ss",
	[LIBAXL_REQUEST_SET_INPUT_FOCUS]            = "11,44",
	[LIBAXL_REQUEST_GET_INPUT_FOCUS]            = "1_",
	[LIBAXL_REQUEST_QUERY_KEYMAP]               = "1_",
	[LIBAXL_REQUEST_OPEN_FONT]                  = "1_42$,u",
	[LIBAXL_REQUEST_CLOSE_FONT]                 = "1_4",
	[LIBAXL_REQUEST_QUERY_FONT]                 = "1_4",
	[LIBAXL_REQUEST_QUERY_TEXT_EXTENTS]         = "1_4@?U",
	[LIBAXL_REQUEST_LIST_FONTS]                 = "1_22$u",
	[LIBAXL_REQUEST_LIST_FONTS_WITH_INFO]       = "1_22$u",
	[LIBAXL_REQUEST_SET_FONT_PATH]              = "1_2$,*1$u",
	[LIBAXL_REQUEST_GET_FONT_PATH]              = "1_",
	[LIBAXL_REQUEST_CREATE_PIXMAP]              = "11,4422",
	[LIBAXL_REQUEST_FREE_PIXMAP]                = "11,4",
	[LIBAXL_REQUEST_CREATE_GC]                  = "1_444|1_444211111.44ss41!,ss4211",
	[LIBAXL_REQUEST_CHANGE_GC]                  = "1_44|1_444211111.44ss41!,ss4211",
	[LIBAXL_REQUEST_COPY_GC]                    = "1_444|1_444211111.44ss41!,ss4211",
	[LIBAXL_REQUEST_SET_DASHES]                 = "1_422$*1",
	[LIBAXL_REQUEST_SET_CLIP_RECTANGLES]        = "11,4ss@*ss22",
	[LIBAXL_REQUEST_FREE_GC]                    = "1_4",
	[LIBAXL_REQUEST_CLEAR_AREA]                 = "1!,4ss22",
	[LIBAXL_REQUEST_COPY_AREA]                  = "1_444ssss22",
	[LIBAXL_REQUEST_COPY_PLANE]                 = "1_444ssss224",
	[LIBAXL_REQUEST_POLY_POINT]                 = "11,44@*ss",
	[LIBAXL_REQUEST_POLY_LINE]                  = "11,44@*ss",
	[LIBAXL_REQUEST_POLY_SEGMENT]               = "1_44@*ssss",
	[LIBAXL_REQUEST_POLY_RECTANGLE]             = "1_44@*ss22",
	[LIBAXL_REQUEST_POLY_ARC]                   = "1_44@*ss22ss",
	[LIBAXL_REQUEST_FILL_POLY]                  = "1_4411,@*ss",
	[LIBAXL_REQUEST_POLY_FILL_RECTANGLE]        = "1_44@*ss22",
	[LIBAXL_REQUEST_POLY_FILL_ARC]              = "1_44@*ss22ss",
	[LIBAXL_REQUEST_PUT_IMAGE]                  = "11,4422ss11,@*1",
	[LIBAXL_REQUEST_GET_IMAGE]                  = "11,4ss224",
	[LIBAXL_REQUEST_POLY_TEXT8]                 = "1_44ss@*?1$zu\0""11111",
	[LIBAXL_REQUEST_POLY_TEXT16]                = "1_44ss@*?1$zU\0""11111",
	[LIBAXL_REQUEST_IMAGE_TEXT8]                = "11$,44ssu",
	[LIBAXL_REQUEST_IMAGE_TEXT16]               = "11$,44ssU",
	[LIBAXL_REQUEST_CREATE_COLORMAP]            = "11,444",
	[LIBAXL_REQUEST_FREE_COLORMAP]              = "1_4",
	[LIBAXL_REQUEST_COPY_COLORMAP_AND_FREE]     = "1_44",
	[LIBAXL_REQUEST_INSTALL_COLORMAP]           = "1_4",
	[LIBAXL_REQUEST_UNINSTALL_COLORMAP]         = "1_4",
	[LIBAXL_REQUEST_LIST_INSTALLED_COLORMAPS]   = "1_4",
	[LIBAXL_REQUEST_ALLOC_COLOR]                = "1_4222,",
	[LIBAXL_REQUEST_ALLOC_NAMED_COLOR]          = "1_42$,u",
	[LIBAXL_REQUEST_ALLOC_COLOR_CELLS]          = "1!,422",
	[LIBAXL_REQUEST_ALLOC_COLOR_PLANES]         = "1!,42222",
	[LIBAXL_REQUEST_FREE_COLORS]                = "1_44@*4",
	[LIBAXL_REQUEST_STORE_COLORS]               = "1_4@*42221.",
	[LIBAXL_REQUEST_STORE_NAMED_COLOR]          = "11,442$,u",
	[LIBAXL_REQUEST_QUERY_COLORS]               = "1_4@*4",
	[LIBAXL_REQUEST_LOOKUP_COLOR]               = "1_42$,u",
	[LIBAXL_REQUEST_CREATE_CURSOR]              = "1_4422222222",
	[LIBAXL_REQUEST_CREATE_GLYPH_CURSOR]        = "1_44422222222",
	[LIBAXL_REQUEST_FREE_CURSOR]                = "1_4",
	[LIBAXL_REQUEST_RECOLOR_CURSOR]             = "1_4222222",
	[LIBAXL_REQUEST_QUERY_BEST_SIZE]            = "11,422",
	[LIBAXL_REQUEST_QUERY_EXTENSION]            = "1_2$,u",
	[LIBAXL_REQUEST_LIST_EXTENSIONS]            = "1_",
	[LIBAXL_REQUEST_CHANGE_KEYBOARD_MAPPING]    = "11,11,?*4",
	[LIBAXL_REQUEST_GET_KEYBOARD_MAPPING]       = "1_11,",
	[LIBAXL_REQUEST_CHANGE_KEYBOARD_CONTROL]    = "1_4|zzss1111",
	[LIBAXL_REQUEST_GET_KEYBOARD_CONTROL]       = "1_",
	[LIBAXL_REQUEST_BELL]                       = "1z,",
	[LIBAXL_REQUEST_CHANGE_POINTER_CONTROL]     = "1_sss!!",
	[LIBAXL_REQUEST_GET_POINTER_CONTROL]        = "1_",
	[LIBAXL_REQUEST_SET_SCREEN_SAVER]           = "1_ss11,",
	[LIBAXL_REQUEST_GET_SCREEN_SAVER]           = "1_",
	[LIBAXL_REQUEST_CHANGE_HOSTS]               = "11,1.2$*1",
	[LIBAXL_REQUEST_LIST_HOSTS]                 = "1_",
	[LIBAXL_REQUEST_SET_ACCESS_CONTROL]         = "11,",
	[LIBAXL_REQUEST_SET_CLOSE_DOWN_MODE]        = "11,",
	[LIBAXL_REQUEST_KILL_CLIENT]                = "1_4",
	[LIBAXL_REQUEST_ROTATE_PROPERTIES]          = "1_42$s*4",
	[LIBAXL_REQUEST_FORCE_SCREEN_SAVER]         = "11,",
	[LIBAXL_REQUEST_SET_POINTER_MAPPING]        = "11$,*1",
	[LIBAXL_REQUEST_GET_POINTER_MAPPING]        = "1_",
	[LIBAXL_REQUEST_SET_MODIFIER_MAPPING]       = "11$,*11",
	[LIBAXL_REQUEST_GET_MODIFIER_MAPPING]       = "1_",
	[LIBAXL_REQUEST_NO_OPERATION]               = "1_?"
};

static int
send_all(int fd, const void *restrict data, size_t n, int flags, size_t *restrict progressp)
{
	const char *bs = data;
	ssize_t r;

	while (n) {
		r = liberror_send(fd, bs, n, flags, "<display server>");
		if (r < 0)
			return -1;
		bs += r, n -= (size_t)r;
		*progressp += (size_t)r;
	}

	return 0;
}

/* XXX make it possible to send multiple request in the same syscall */
int
libaxl_send_request(LIBAXL_CONTEXT *restrict ctx, union libaxl_request_const_ptr request, int flags, uint16_t *restrict seqnump)
{
	LIBAXL_CONNECTION *conn = ctx->conn;
	LIBAXL_CONTEXT *pending, **pendingp;
	const char *req = request.as_voidptr;
	uint8_t code = *(const uint8_t *)req;
	const char *fmt, *repeat = NULL, *data;
	size_t i = 0, o = 0, j, n;
	size_t count = 0, format = 1, repcnt = 0;
	uint32_t mask;
	char *buf = ctx->out_buf, *subbuf, *new;
	size_t size = ctx->out_buf_size;
	uint16_t u16;
	uint32_t u32;

	RLOCK_CONNECTION_SEND(conn);
	pending = conn->pending_out;
	RUNLOCK_CONNECTION_SEND(conn);
	if (pending) {
		liberror_save_backtrace(NULL);
		liberror_set_error(pending == ctx
		                   ? "Request from the previous call is pending to be flush"
		                   : "A request from another thread is pending to be flushed",
		                   "libaxl_send_request", "errno", EALREADY);
		errno = EALREADY;
		return LIBAXL_ERROR_SYSTEM;
	}

	if (!code || (code > LIBAXL_REQUEST_GET_MODIFIER_MAPPING && code != LIBAXL_REQUEST_NO_OPERATION)) {
		liberror_save_backtrace(NULL);
		liberror_set_error("Invalid request opcode", "libaxl_send_request", "libaxl",
		                   LIBAXL_ERROR_INVALID_REQUEST_OPCODE);
		errno = EINVAL;
		return LIBAXL_ERROR_INVALID_REQUEST_OPCODE;
	}

	fmt = req_formats[code];

again:
	for (;; fmt++) {
		if (o + 4 > size) {
			size += 32;
			new = liberror_realloc(buf, size);
			if (!new)
				return LIBAXL_ERROR_SYSTEM;
			ctx->out_buf = buf = new;
			ctx->out_buf_size = size;
		}

		switch (*fmt) {
		case '\0':
			if (repeat && --count) {
				fmt = repeat;
				goto again;
			}
			goto done;

		case '_':
			buf[o++] = 0; /* need not be 0 */
			i += 1;
			/* fall through */
		case ',':
			buf[o++] = 0; /* need not be 0 */
			i += 1;
			/* fall through */
		case '.':
			buf[o++] = 0; /* need not be 0 */
			i += 1;
			break;

		case '!':
			buf[o++] = !!req[i++];
			break;

		case '1':
			buf[o++] = req[i++];
			break;

		case '2':
			*(uint16_t *)&buf[o] = htons(*(const uint16_t *)&req[i]);
			i += 2;
			o += 2;
			break;

		case '4':
			*(uint32_t *)&buf[o] = htonl(*(const uint32_t *)&req[i]);
			i += 4;
			o += 4;
			break;

		case 'z':
			buf[o] = req[i];
			TWOS_COMPLEMENT8(&buf[o]);
			i += 1;
			o += 1;
			break;

		case 's':
			u16 = *(const uint16_t *)&req[i];
			TWOS_COMPLEMENT16(&u16);
			*(uint16_t *)&buf[o] = htons(u16);
			i += 2;
			o += 2;
			break;

		case 'S':
			u32 = *(const uint32_t *)&req[i];
			TWOS_COMPLEMENT32(&u32);
			*(uint32_t *)&buf[o] = htonl(u32);
			i += 4;
			o += 4;
			break;

		case '$':
			if (fmt[-1] == '2')
				count = (size_t)*(const uint16_t *)&req[i - 2];
			else if (fmt[-1] == '1')
				count = (size_t)*(const uint8_t *)&req[i - 1];
			else
				count = (size_t)*(const uint32_t *)&req[i - 4];
			break;

		case '%':
			if (fmt[-1] == '1')
				format = (size_t)*(const uint8_t *)&req[i - 1];
			else if (fmt[-1] == '2')
				format = (size_t)*(const uint16_t *)&req[i - 2];
			else
				format = (size_t)*(const uint32_t *)&req[i - 4];
			break;

		case '@':
			ALIGN(&i, size_t);
			count = *(const size_t *)&req[i - 2];
			break;

		case 'u':
			format = 8;
			goto case_d;

		case 'U':
			format = 16;
			goto case_d;

		case 'd':
		case_d:
			ALIGN(&i, void *);
			data = *(const char *const *)&req[i];
			count *= format / 8;
			if (o + count > size) {
				size += (count & 3) ? (count | 3) + 1 : count;
				new = liberror_realloc(buf, size);
				if (!new)
					return LIBAXL_ERROR_SYSTEM;
				ctx->out_buf = buf = new;
				ctx->out_buf_size = size;
			}
			subbuf = &buf[o];
			if (format == 8) {
				for (j = 0; j < count; j += 1)
					*(uint8_t *)&subbuf[j] = *(const uint8_t *)&data[j];
			} else if (format == 16) {
				for (j = 0; j < count; j += 2)
					*(uint16_t *)&subbuf[j] = htons(*(const uint16_t *)&data[j]);
			} else if (format == 32) {
				for (j = 0; j < count; j += 4)
					*(uint32_t *)&subbuf[j] = htonl(*(const uint32_t *)&data[j]);
			} else if (format != 0) {
				liberror_save_backtrace(NULL);
				liberror_set_error("Invalid format number", "libaxl_send_request", "libaxl",
						   LIBAXL_ERROR_INVALID_FORMAT_NUMBER);
				errno = EINVAL;
				return LIBAXL_ERROR_INVALID_FORMAT_NUMBER;
			}
			o += count;
			i += count;
			break;

		case 'e': /* TODO event */
			ALIGN(&i, union libaxl_event);
			fprintf(stderr, "libaxl_send_request: function not fully implemented: 'e' case\n");
			abort();
			break;

		case '?':
			fmt++;
			goto special_encoding;

		case '*':
			repcnt = count;
			if (!repcnt)
				goto done;
			ALIGN(&i, void *);
			req = *(const char *const *)&req[i];
			repeat = &fmt[1];
			i = 0;
			break;

		case '|':
			if (fmt[-1] == '4')
				mask = *(const uint32_t *)&req[i - 4];
			else if (fmt[-1] == '2')
				mask = (uint32_t)*(const uint16_t *)&req[i - 2];
			else
				mask = (uint32_t)*(const uint8_t *)&req[i - 1];
			if (o + 4 > size) {
				size += 32;
				new = liberror_realloc(buf, size);
				if (!new)
					return LIBAXL_ERROR_SYSTEM;
				ctx->out_buf = buf = new;
				ctx->out_buf_size = size;
			}
			memset(&buf[o], 0, 4 - (fmt[-1] - '0'));
			o += 4 - (fmt[-1] - '0');
			for (fmt++;; fmt++) {
				if (o + 4 > size) {
					size += 32;
					new = liberror_realloc(buf, size);
					if (!new)
						return LIBAXL_ERROR_SYSTEM;
					ctx->out_buf = buf = new;
					ctx->out_buf_size = size;
				}

				switch (*fmt) {
				case '\0':
					goto done;

				case '.':
					i += 1;
					break;

				case ',':
					i += 2;
					break;

				case '_':
					i += 3;
					break;

				case '!':
					if (mask & 1) {
						*(uint32_t *)&buf[o] = htonl((uint32_t)(req[i] ? 1 : 0));
						o += 4;
					}
					mask >>= 1;
					i += 1;
					break;

				case '1':
				case 'z':
					if (mask & 1) {
						*(uint32_t *)&buf[o] = htonl((uint32_t)*(const uint8_t *)&req[i]);
						if (*fmt == 'z')
							TWOS_COMPLEMENT8(&buf[o]);
						o += 4;
					}
					mask >>= 1;
					i += 1;
					break;

				case '2':
				case 's':
					if (mask & 1) {
						u16 = *(const uint16_t *)&req[i];
						if (*fmt == 's')
							TWOS_COMPLEMENT16(&u16);
						*(uint32_t *)&buf[o] = htonl((uint32_t)u16);
						o += 4;
					}
					mask >>= 1;
					i += 2;
					break;

				case '4':
				case 'S':
					if (mask & 1) {
						u32 = *(const uint32_t *)&req[i];
						if (*fmt == 'S')
							TWOS_COMPLEMENT32(&u32);
						*(uint32_t *)&buf[o] = htonl(u32);
						o += 4;
					}
					mask >>= 1;
					i += 4;
					break;

				default:
					abort();
				};
			}

		default:
			abort();
		}
	}

special_encoding:
	switch (code) {
	case LIBAXL_REQUEST_QUERY_TEXT_EXTENTS:
		buf[2] = (count & 1); /* _odd_length */
		break;

	case LIBAXL_REQUEST_CHANGE_KEYBOARD_MAPPING:
		count  = (size_t)((const union libaxl_request *)req)->change_keyboard_mapping.keycode_count;
		count *= (size_t)((const union libaxl_request *)req)->change_keyboard_mapping.keysyms_per_keycode;
		goto again;

	case LIBAXL_REQUEST_POLY_TEXT8:
	case LIBAXL_REQUEST_POLY_TEXT16:
		if (((const uint8_t *)req)[i] == LIBAXL_FONT_SHIFT_INDICATOR)
			while (*fmt++);
		goto again;

	case LIBAXL_REQUEST_NO_OPERATION:
		if (!((const union libaxl_request *)req)->_request_length) {
			liberror_save_backtrace(NULL);
			liberror_set_error("Malformatted request: request length most be at least 1 byte-quad",
			                   "libaxl_send_request", "libaxl", LIBAXL_ERROR_MALFORMATTED_REQUEST);
			errno = EINVAL;
			return LIBAXL_ERROR_MALFORMATTED_REQUEST;
		}
		o = 4 * (size_t)((const union libaxl_request *)req)->_request_length;
		if (o > size) {
			new = liberror_realloc(buf, o);
			if (!new)
				return LIBAXL_ERROR_SYSTEM;
			ctx->out_buf = buf = new;
			ctx->out_buf_size = size = o;
			memset(&buf[4], 0, o - 4);
		}
		break;

	default:
		abort();
	}

done:
	if (o & 3) {
		n = o;
		o = (o | 3) + 1;
		if (o > size) {
			new = liberror_realloc(buf, o);
			if (!new)
				return LIBAXL_ERROR_SYSTEM;
			ctx->out_buf = buf = new;
			ctx->out_buf_size = o;
		}
		memset(&buf[n], 0, o - n);
	}
	n = o / 4;
	if (n > UINT16_MAX) {
		liberror_save_backtrace(NULL);
		liberror_set_error("Message is too long, cannot be longer than 65535 byte-quads",
		                   "libaxl_send_request", "errno", EMSGSIZE);
		errno = EMSGSIZE;
		return LIBAXL_ERROR_SYSTEM;
	}
	*(uint16_t *)&buf[2] = htons((uint16_t)n); /* _request_length */
	ctx->out_length = o;
	ctx->out_progress = 0;

	WLOCK_CONNECTION_SEND(conn);

	conn->request_map[++conn->last_seqnum] = code;
	if (seqnump)
		*seqnump = conn->last_seqnum;
	if (send_all(conn->fd, ctx->out_buf, ctx->out_length, flags, &ctx->out_progress)) {
		ctx->next_pending_out = NULL;
		pendingp = &conn->pending_out;
		while (*pendingp)
			pendingp = &(*pendingp)->next_pending_out;
		*pendingp = ctx;
		ctx->refcount += 1;

		WUNLOCK_CONNECTION_SEND(conn);

		liberror_save_backtrace(NULL);
		liberror_set_error("Request has been buffered and is ready to be sent",
		                   "libaxl_send_request", "errno", EINPROGRESS);
		errno = EINPROGRESS;
		return LIBAXL_ERROR_SYSTEM;
	}

	WUNLOCK_CONNECTION_SEND(conn);

	return 0;
}