toaruos/lib/pex.c

74 lines
1.8 KiB
C
Raw Normal View History

2018-08-15 04:23:41 +03:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
2018-02-25 11:14:43 +03:00
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2014-2018 K. Lange
2018-08-15 04:23:41 +03:00
*
* pex - Packet EXchange client library
*
* Provides a friendly interface to the "Packet Exchange"
* functionality provided by the packetfs kernel module.
2018-02-25 11:14:43 +03:00
*/
#include <alloca.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
2018-03-19 05:38:11 +03:00
#include <toaru/pex.h>
2018-02-25 11:14:43 +03:00
2021-05-31 04:47:02 +03:00
size_t pex_send(FILE * sock, uintptr_t rcpt, size_t size, char * blob) {
2018-02-25 11:14:43 +03:00
assert(size <= MAX_PACKET_SIZE);
pex_header_t * broadcast = malloc(sizeof(pex_header_t) + size);
broadcast->target = rcpt;
memcpy(broadcast->data, blob, size);
size_t out = write(fileno(sock), broadcast, sizeof(pex_header_t) + size);
free(broadcast);
return out;
}
size_t pex_broadcast(FILE * sock, size_t size, char * blob) {
return pex_send(sock, 0, size, blob);
}
size_t pex_listen(FILE * sock, pex_packet_t * packet) {
return read(fileno(sock), packet, PACKET_SIZE);
}
size_t pex_reply(FILE * sock, size_t size, char * blob) {
return write(fileno(sock), blob, size);
}
size_t pex_recv(FILE * sock, char * blob) {
memset(blob, 0, MAX_PACKET_SIZE);
return read(fileno(sock), blob, MAX_PACKET_SIZE);
}
FILE * pex_connect(char * target) {
char tmp[100];
2019-06-25 14:18:43 +03:00
if (strlen(target) > 80) return NULL;
2018-02-25 11:14:43 +03:00
sprintf(tmp, "/dev/pex/%s", target);
FILE * out = fopen(tmp, "r+");
if (out) {
setbuf(out, NULL);
}
2018-02-25 11:14:43 +03:00
return out;
}
FILE * pex_bind(char * target) {
char tmp[100];
2019-06-25 14:18:43 +03:00
if (strlen(target) > 80) return NULL;
2018-02-25 11:14:43 +03:00
sprintf(tmp, "/dev/pex/%s", target);
FILE * out = fopen(tmp, "a+");
if (out) {
setbuf(out, NULL);
}
2018-02-25 11:14:43 +03:00
return out;
}
size_t pex_query(FILE * sock) {
return ioctl(fileno(sock), IOCTL_PACKETFS_QUEUED, NULL);
}