public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
94c34d3cd01f13db66f653df50450d82a71c6bc7 blob 2934 bytes (raw)

  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
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* PASST - Plug A Simple Socket Transport
 *  for qemu/UNIX domain socket mode
 *
 * PASTA - Pack A Subtle Tap Abstraction
 *  for network namespace/tap device mode
 *
 * PESTO - Programmable Extensible Socket Translation Orchestrator
 *  front-end for passt(1) and pasta(1) forwarding configuration
 *
 * serialise.c - Serialisation of data structures over bytestreams
 *
 * Copyright Red Hat
 * Author: David Gibson <david@gibson.dropbear.id.au>
 */

#include <assert.h>
#include <endian.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include "serialise.h"

/**
 * read_all_buf() - Fill a whole buffer from a file descriptor
 * @fd:		File descriptor
 * @buf:	Pointer to base of buffer
 * @len:	Length of buffer
 *
 * Return: 0 on success, -1 on error (with errno set)
 *
 * #syscalls read
 */
int read_all_buf(int fd, void *buf, size_t len)
{
	size_t left = len;
	char *p = buf;

	while (left) {
		ssize_t rc;

		assert(left <= len);

		do
			rc = read(fd, p, left);
		while ((rc < 0) && errno == EINTR);

		if (rc < 0)
			return -1;

		if (rc == 0) {
			errno = ENODATA;
			return -1;
		}

		p += rc;
		left -= rc;
	}
	return 0;
}

/**
 * write_all_buf() - write all of a buffer to an fd
 * @fd:		File descriptor
 * @buf:	Pointer to base of buffer
 * @len:	Length of buffer
 *
 * Return: 0 on success, -1 on error (with errno set)
 *
 * #syscalls write
 */
int write_all_buf(int fd, const void *buf, size_t len)
{
	const char *p = buf;
	size_t left = len;

	while (left) {
		ssize_t rc;

		do
			rc = write(fd, p, left);
		while ((rc < 0) && errno == EINTR);

		if (rc < 0)
			return -1;

		p += rc;
		left -= rc;
	}
	return 0;
}

/**
 * read_uXXX() - Receive a uXXX value from an fd
 * @fd:		File descriptor to read from
 * @valp:	Pointer to variable to update with read value
 *
 * Return: 0 on success, -1 on error
 */
/**
 * write_uXXX() - Send a uXXX value to an fd
 * @fd:		File descriptor to write to
 * @val:	Value to send
 *
 * Return: 0 on success, -1 on error
 */
#define SERIALISE_UINT(bits)						\
	int read_u##bits(int fd, uint##bits##_t *val)			\
	{								\
		uint##bits##_t beval;					\
		if (read_all_buf(fd, &beval, sizeof(beval)) < 0)	\
			return -1;					\
		*val = be##bits##toh(beval);				\
		return 0;						\
	}								\
	int write_u##bits(int fd, uint##bits##_t val)			\
	{								\
		uint##bits##_t beval = htobe##bits(val);		\
		return write_all_buf(fd, &beval, sizeof(beval));	\
	}

#define be8toh(x)	(x)
#define htobe8(x)	(x)

SERIALISE_UINT(8)
SERIALISE_UINT(32)

#undef SERIALISE_UNIT

/**
 * write_str() - Write a string to an fd in length/value format
 * @fd:		Socket to the client
 * @s:		String to send
 *
 * Return: 0 on success, -1 on error
 */
int write_str(int fd, const char *s)
{
	uint32_t len = strlen(s) + 1; /* Include \0 */

	if (write_u32(fd, len) < 0)
		return -1;
	return write_all_buf(fd, s, len);
}
debug log:

solving 94c34d3c ...
found 94c34d3c in https://archives.passt.top/passt-dev/20260323073732.3158468-16-david@gibson.dropbear.id.au/
found e3ea86e3 in https://archives.passt.top/passt-dev/20260323073732.3158468-14-david@gibson.dropbear.id.au/
found d6c3396a in https://archives.passt.top/passt-dev/20260323073732.3158468-5-david@gibson.dropbear.id.au/
found f162eeb6 in https://passt.top/passt
preparing index
index prepared:
100644 f162eeb6f7e75e2e9615ed21bca2b24199fc118d	serialise.c

applying [1/3] https://archives.passt.top/passt-dev/20260323073732.3158468-5-david@gibson.dropbear.id.au/
diff --git a/serialise.c b/serialise.c
index f162eeb6..d6c3396a 100644


applying [2/3] https://archives.passt.top/passt-dev/20260323073732.3158468-14-david@gibson.dropbear.id.au/
diff --git a/serialise.c b/serialise.c
index d6c3396a..e3ea86e3 100644


applying [3/3] https://archives.passt.top/passt-dev/20260323073732.3158468-16-david@gibson.dropbear.id.au/
diff --git a/serialise.c b/serialise.c
index e3ea86e3..94c34d3c 100644

Checking patch serialise.c...
Applied patch serialise.c cleanly.
Checking patch serialise.c...
Applied patch serialise.c cleanly.
Checking patch serialise.c...
Applied patch serialise.c cleanly.

index at:
100644 94c34d3cd01f13db66f653df50450d82a71c6bc7	serialise.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).