send length + data at the same time

This commit is contained in:
Alba Mendez
2023-03-14 17:03:35 +01:00
parent 38709f3ad8
commit 76e267968d
2 changed files with 10 additions and 10 deletions

View File

@@ -219,6 +219,7 @@ static ssize_t sendToVICC(struct vicc_ctx *ctx, size_t length, const unsigned ch
{
ssize_t r;
uint16_t size;
char *sendBuffer;
if (!ctx || length > 0xFFFF) {
errno = EINVAL;
@@ -226,11 +227,11 @@ static ssize_t sendToVICC(struct vicc_ctx *ctx, size_t length, const unsigned ch
}
/* send size of message on 2 bytes */
sendBuffer = (char *) alloca(length + 2);
size = htons((uint16_t) length);
r = sendall(ctx->client_sock, (void *) &size, sizeof size);
if (r == sizeof size)
/* send message */
r = sendall(ctx->client_sock, buffer, length);
memcpy(sendBuffer, &size, 2);
memcpy(sendBuffer + 2, buffer, length);
r = sendall(ctx->client_sock, sendBuffer, length + 2);
if (r < 0)
vicc_eject(ctx);