commit ea30c6d
uint
·
2026-10-30 14:38:44 +0000 UTC
parent 6ec5baa
Pipe: Add opening, recieving, closing sockets, include address in ulepipe struct
2 files changed,
+106,
-21
+17,
-2
1@@ -1,11 +1,26 @@
2 #ifndef ULEUPIPE_H
3 #define ULEUPIPE_H
4
5+#include <stddef.h>
6+#include <sys/un.h>
7+#include <unistd.h>
8+
9 typedef struct {
10- int fd;
11+ int fd;
12+ struct sockaddr_un addr;
13 } ulepipe;
14
15-int ulepipe_connect(ulepipe* ctx, const char* path);
16+/* Open a ulepipe socket */
17+int ulepipe_open(ulepipe* pipe, const char* path);
18+
19+/* Close a ulepipe */
20+void ulepipe_close(ulepipe* pipe);
21+
22+/* Send data to a ulepipe socket */
23+ssize_t ulepipe_send(const char* path, const void* data, size_t size);
24+
25+/* Receive data from a ulepipe socket */
26+ssize_t ulepipe_recieve(ulepipe* pipe, void* data, size_t size);
27
28 #endif /* ULEUPIPE_H */
29
+89,
-19
1@@ -1,55 +1,125 @@
2+#include <errno.h>
3 #include <sys/socket.h>
4 #include <sys/un.h>
5-#include <unistd.h>
6 #include <string.h>
7-#include <errno.h>
8+#include <unistd.h>
9
10 #include "ulepipe.h"
11
12 int
13-ulepipe_connect(ulepipe* ctx, const char* path)
14+ulepipe_open(ulepipe* pipe, const char* path)
15 {
16- struct sockaddr_un addr = { 0 };
17+ int sverrno = 0;
18 int fd = -1;
19
20- if (!ctx || !path) {
21+ if (!pipe || !path) {
22 errno = EINVAL;
23 return -1;
24 }
25
26- if (strlen(path) >= sizeof(addr.sun_path)) {
27+ pipe->fd = -1;
28+
29+ if (strlen(path) >= sizeof(pipe->addr.sun_path)) {
30 errno = ENAMETOOLONG;
31 return -1;
32 }
33
34- fd = socket(AF_UNIX, SOCK_STREAM, 0);
35+ fd = socket(AF_UNIX, SOCK_DGRAM, 0);
36 if (fd == -1)
37 return -1;
38
39- memset(&addr, 0, sizeof(addr));
40- addr.sun_family = AF_UNIX;
41- strcpy(addr.sun_path, path);
42+ memset(&pipe->addr, 0, sizeof(pipe->addr));
43+ pipe->addr.sun_family = AF_UNIX;
44+ memcpy(pipe->addr.sun_path, path, sizeof(pipe->addr.sun_path));
45
46- if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
47- int saved_errno = errno;
48+ if (bind(fd, (struct sockaddr*)&pipe->addr, sizeof(pipe->addr)) == -1) {
49+ sverrno = errno;
50 close(fd);
51- errno = saved_errno;
52+ errno = sverrno;
53 return -1;
54 }
55
56- ctx->fd = fd;
57+ pipe->fd = fd;
58 return 0;
59 }
60
61 void
62-tpipe_close(ulepipe* ctx)
63+ulepipe_close(ulepipe* pipe)
64 {
65- if (!ctx)
66+ if (!pipe || pipe->fd < 0)
67 return;
68
69- if (ctx->fd >= 0) {
70- close(ctx->fd);
71- ctx->fd = -1;
72+ close(pipe->fd);
73+ unlink(pipe->addr.sun_path);
74+ pipe->fd = -1;
75+}
76+
77+ssize_t
78+ulepipe_send(const char* path, const void* data, size_t size)
79+{
80+ struct sockaddr_un addr = { 0 };
81+ int sverrno = 0;
82+ ssize_t n = -1;
83+ int fd = -1;
84+
85+ if (!path || (!data && size)) {
86+ errno = EINVAL;
87+ return -1;
88 }
89+
90+ if (strlen(path) >= sizeof(addr.sun_path)) {
91+ errno = ENAMETOOLONG;
92+ return -1;
93+ }
94+
95+ fd = socket(AF_UNIX, SOCK_DGRAM, 0);
96+ if (fd == -1)
97+ return -1;
98+
99+ addr.sun_family = AF_UNIX;
100+ memcpy(addr.sun_path, path, sizeof(addr.sun_path));
101+
102+ do
103+ n = sendto(fd, data, size, 0, (struct sockaddr*)&addr, sizeof(addr));
104+ while (n == -1 && errno == EINTR);
105+
106+ if (n == -1) {
107+ sverrno = errno;
108+ close(fd);
109+ errno = sverrno;
110+ return -1;
111+ }
112+
113+ close(fd);
114+ return n;
115+}
116+
117+ssize_t
118+ulepipe_recieve(ulepipe* pipe, void* data, size_t size)
119+{
120+ struct msghdr msg = { 0 };
121+ struct iovec iov = { 0 };
122+ ssize_t n = -1;
123+
124+ if (!pipe || pipe->fd < 0 || (!data && size)) {
125+ errno = EINVAL;
126+ return -1;
127+ }
128+
129+ iov.iov_base = data;
130+ iov.iov_len = size;
131+ msg.msg_iov = &iov;
132+ msg.msg_iovlen = 1;
133+
134+ do
135+ n = recvmsg(pipe->fd, &msg, 0);
136+ while (n == -1 && errno == EINTR);
137+
138+ if (n != -1 && (msg.msg_flags & MSG_TRUNC)) {
139+ errno = EMSGSIZE;
140+ return -1;
141+ }
142+
143+ return n;
144 }
145