29#include <netinet/in.h>
32#include <sys/socket.h>
39#define SOCKET_PORT 32000
51 if ((
socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
52 perror(
"Socket: socket");
55 if (fcntl(
socket_fd, F_SETFD, FD_CLOEXEC) != 0) {
60 int status_flags = fcntl(
socket_fd, F_GETFL);
61 if (status_flags < 0 || fcntl(
socket_fd, F_SETFL, status_flags | O_NONBLOCK) != 0) {
74 int bind_errno = errno;
75 if (bind_errno == EADDRINUSE) {
77 "USHER:\tanother USHER instance or payload listener already owns "
79 "USHER:\tstop the existing process before starting another instance\n"
80 "USHER:\tfor the managed MORA service, use: "
81 "sudo -n systemctl stop usher.service\n",
84 fprintf(stderr,
"USHER:\tfailed to bind local payload port 127.0.0.1:%d: %s\n",
109 if (!buffer || len <= 0) { errno = EINVAL;
return -1; }
110 if (
socket_fd < 0) { errno = ENOTCONN;
return -1; }
111 struct iovec vector = {.iov_base = buffer, .iov_len = (size_t)len};
112 struct msghdr message = {
118 ssize_t received = recvmsg(
socket_fd, &message, 0);
119 if (received >= 0 && (message.msg_flags & MSG_TRUNC) != 0) {
123 return (
int)received;
128 if (len < 0 || (!buffer && len)) { errno = EINVAL;
return; }
129 if (
socket_fd < 0) { errno = ENOTCONN;
return; }
void(* close)(struct usbfs_handle *)
int socket_get_fd(void)
Return the active UDP descriptor for polling.
int socket_recv(char *buffer, int len)
Receive one datagram without blocking.
int socket_init(int is_server)
Create the loopback UDP endpoint and, for a server, claim USHER ownership.
void socket_deinit(void)
Close the owned UDP endpoint; repeated calls are harmless.
static struct sockaddr_in socket_server
void socket_send(char *buffer, int len)
Send one loopback datagram without blocking.
Local non-blocking UDP channel used by USHER's development pipeline.