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
|
2018-05-01 11:12:56 +03:00
|
|
|
* 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
|
|
|
|
|
|
|
size_t pex_send(FILE * sock, unsigned int rcpt, size_t size, char * blob) {
|
|
|
|
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];
|
|
|
|
sprintf(tmp, "/dev/pex/%s", target);
|
|
|
|
FILE * out = fopen(tmp, "r+");
|
2018-08-01 03:20:57 +03:00
|
|
|
if (out) {
|
|
|
|
setbuf(out, NULL);
|
|
|
|
}
|
2018-02-25 11:14:43 +03:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE * pex_bind(char * target) {
|
|
|
|
char tmp[100];
|
|
|
|
sprintf(tmp, "/dev/pex/%s", target);
|
|
|
|
FILE * out = fopen(tmp, "a+");
|
2018-08-01 03:20:57 +03:00
|
|
|
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);
|
|
|
|
}
|