Skip to content

Commit ca88ce5

Browse files
Add socketpair (#122)
1 parent 6388ef2 commit ca88ce5

File tree

7 files changed

+238
-4
lines changed

7 files changed

+238
-4
lines changed

libc/calls/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ hidden extern int __vforked;
6767
hidden extern unsigned __sighandrvas[NSIG];
6868
hidden extern struct Fds g_fds;
6969
hidden extern const struct NtSecurityAttributes kNtIsInheritable;
70+
hidden extern const char kPipeNamePrefix[];
7071

7172
int __reservefd(void) hidden;
7273
void __releasefd(int) hidden;
@@ -290,6 +291,8 @@ int __mkntpathat(int, const char *, int, char16_t[PATH_MAX]) hidden;
290291
unsigned __wincrash_nt(struct NtExceptionPointers *);
291292
ssize_t sys_readv_nt(struct Fd *, const struct iovec *, int) hidden;
292293
ssize_t sys_writev_nt(struct Fd *, const struct iovec *, int) hidden;
294+
char16_t *CreatePipeName(char16_t *) hidden;
295+
size_t UintToChar16Array(char16_t *, uint64_t) hidden;
293296

294297
/*───────────────────────────────────────────────────────────────────────────│─╗
295298
│ cosmopolitan § syscalls » metal ─╬─│┼

libc/calls/pipe-nt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#include "libc/nt/runtime.h"
2828
#include "libc/sysv/consts/o.h"
2929

30-
static const char kPipeNamePrefix[] = "\\\\?\\pipe\\cosmo\\";
30+
const char kPipeNamePrefix[] = "\\\\?\\pipe\\cosmo\\";
3131

32-
static size_t UintToChar16Array(char16_t *a, uint64_t i) {
32+
size_t UintToChar16Array(char16_t *a, uint64_t i) {
3333
size_t j = 0;
3434
do {
3535
a[j++] = i % 10 + '0';
@@ -40,7 +40,7 @@ static size_t UintToChar16Array(char16_t *a, uint64_t i) {
4040
return j;
4141
}
4242

43-
static char16_t *CreatePipeName(char16_t *a) {
43+
char16_t *CreatePipeName(char16_t *a) {
4444
static long x;
4545
unsigned i;
4646
for (i = 0; kPipeNamePrefix[i]; ++i) a[i] = kPipeNamePrefix[i];

libc/sock/internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ int32_t sys_getpeername(int32_t, void *, uint32_t *) hidden;
8282
int32_t sys_poll(struct pollfd *, uint64_t, signed) hidden;
8383
int32_t sys_shutdown(int32_t, int32_t) hidden;
8484
int32_t sys_socket(int32_t, int32_t, int32_t) hidden;
85+
int32_t sys_socketpair(int32_t, int32_t, int32_t, int32_t[2]) hidden;
8586
int64_t sys_readv(int32_t, const struct iovec *, int32_t) hidden;
8687
int64_t sys_writev(int32_t, const struct iovec *, int32_t) hidden;
8788
ssize_t sys_recvfrom(int, void *, size_t, int, void *, uint32_t *) hidden;
@@ -104,6 +105,11 @@ int sys_bind_nt(struct Fd *, const void *, uint32_t);
104105
int sys_accept_nt(struct Fd *, void *, uint32_t *, int) hidden;
105106
int sys_closesocket_nt(struct Fd *) hidden;
106107
int sys_socket_nt(int, int, int) hidden;
108+
/*
109+
int sys_socketpair_nt_stream(int, int, int, int[2]) hidden;
110+
int sys_socketpair_nt_dgram(int, int, int, int[2]) hidden;
111+
*/
112+
int sys_socketpair_nt(int, int, int, int[2]) hidden;
107113
int sys_select_nt(int, fd_set *, fd_set *, fd_set *, struct timeval *) hidden;
108114
int sys_shutdown_nt(struct Fd *, int) hidden;
109115

libc/sock/sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ ssize_t writev(int, const struct iovec *, int);
9191
ssize_t sendfile(int, int, int64_t *, size_t);
9292
int getsockopt(int, int, int, void *, uint32_t *) paramsnonnull((5));
9393
int setsockopt(int, int, int, const void *, uint32_t);
94-
int socketpair(int, int, int, int64_t[2]) paramsnonnull();
94+
int socketpair(int, int, int, int[2]) paramsnonnull();
9595
int poll(struct pollfd *, uint64_t, int32_t) paramsnonnull();
9696
int ppoll(struct pollfd *, uint64_t, const struct timespec *,
9797
const struct sigset *) paramsnonnull((1, 4));

libc/sock/socketpair-nt.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2020 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/internal.h"
20+
#include "libc/errno.h"
21+
#include "libc/mem/mem.h"
22+
#include "libc/sock/internal.h"
23+
#include "libc/sock/yoink.inc"
24+
#include "libc/sysv/consts/fio.h"
25+
#include "libc/sysv/consts/o.h"
26+
#include "libc/sysv/consts/sock.h"
27+
#include "libc/sysv/consts/af.h"
28+
29+
#include "libc/alg/reverse.internal.h"
30+
#include "libc/nt/createfile.h"
31+
#include "libc/nt/enum/accessmask.h"
32+
#include "libc/nt/enum/creationdisposition.h"
33+
#include "libc/nt/enum/filesharemode.h"
34+
#include "libc/nt/ipc.h"
35+
#include "libc/nt/process.h"
36+
#include "libc/nt/runtime.h"
37+
38+
39+
// {{{ sys_socketpair_nt
40+
int sys_socketpair_nt(int family, int type, int proto, int sv[2]) {
41+
int64_t hpipe, h1, h2;
42+
int reader, writer;
43+
char16_t pipename[64];
44+
uint32_t mode;
45+
46+
// Supports only AF_UNIX
47+
if (family != AF_UNIX) {
48+
errno = EAFNOSUPPORT;
49+
return -1;
50+
}
51+
52+
mode = kNtPipeWait;
53+
if (type == SOCK_STREAM) {
54+
mode |= kNtPipeReadmodeByte | kNtPipeTypeByte;
55+
} else if ((type == SOCK_DGRAM) || (type == SOCK_SEQPACKET)) {
56+
mode |= kNtPipeReadmodeMessage | kNtPipeTypeMessage;
57+
} else {
58+
errno = EOPNOTSUPP;
59+
return -1;
60+
}
61+
62+
CreatePipeName(pipename);
63+
if ((reader = __reservefd()) == -1) return -1;
64+
if ((writer = __reservefd()) == -1) {
65+
__releasefd(reader);
66+
return -1;
67+
}
68+
if ((hpipe = CreateNamedPipe(pipename,
69+
kNtPipeAccessDuplex,
70+
mode,
71+
1,
72+
65536,
73+
65536,
74+
0,
75+
&kNtIsInheritable)) == -1) {
76+
__winerr();
77+
__releasefd(writer);
78+
__releasefd(reader);
79+
return -1;
80+
}
81+
82+
h1 = CreateFile(pipename,
83+
kNtGenericWrite | kNtGenericRead,
84+
0, // Not shared
85+
&kNtIsInheritable,
86+
kNtOpenExisting, 0, 0);
87+
if (h1 == -1) {
88+
CloseHandle(hpipe);
89+
__winerr();
90+
__releasefd(writer);
91+
__releasefd(reader);
92+
return -1;
93+
}
94+
95+
g_fds.p[reader].kind = kFdFile;
96+
g_fds.p[reader].flags = 0; // TODO
97+
g_fds.p[reader].handle = hpipe;
98+
99+
g_fds.p[writer].kind = kFdFile;
100+
g_fds.p[writer].flags = 0; // TODO
101+
g_fds.p[writer].handle = h1;
102+
103+
sv[0] = reader;
104+
sv[1] = writer;
105+
return 0;
106+
}
107+
// }}}
108+
109+
110+
111+

libc/sock/socketpair.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2020 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/dce.h"
20+
#include "libc/sock/internal.h"
21+
#include "libc/sock/sock.h"
22+
#include "libc/sysv/consts/af.h"
23+
#include "libc/sysv/consts/sock.h"
24+
#include "libc/sysv/errfuns.h"
25+
26+
/**
27+
* Creates a pair of connected sockets
28+
*
29+
* @param family can be AF_UNIX, AF_INET, etc.
30+
* @param type can be SOCK_STREAM (for TCP), SOCK_DGRAM (e.g. UDP), or
31+
* SOCK_RAW (IP) so long as IP_HDRINCL was passed to setsockopt();
32+
* and additionally, may be or'd with SOCK_NONBLOCK, SOCK_CLOEXEC
33+
* @param protocol can be IPPROTO_TCP, IPPROTO_UDP, or IPPROTO_ICMP
34+
* @param sv a vector of 2 integers to store the created sockets.
35+
* @return 0 if success, -1 in case of error
36+
* @error EFAULT, EPFNOSUPPORT, etc.
37+
* @see libc/sysv/consts.sh
38+
* @asyncsignalsafe
39+
*/
40+
int socketpair(int family, int type, int protocol, int sv[2]) {
41+
if (family == AF_UNSPEC) {
42+
family = AF_UNIX;
43+
} else if (family == AF_INET6) {
44+
/* Recommend IPv6 on frontend serving infrastructure only. That's
45+
what Google Cloud does. It's more secure. It also means poll()
46+
will work on Windows, which doesn't allow mixing third layers. */
47+
errno = EAFNOSUPPORT;
48+
return epfnosupport();
49+
}
50+
if (!IsWindows()) {
51+
return sys_socketpair(family, type, protocol, sv);
52+
}
53+
return sys_socketpair_nt(family, type, protocol, sv);
54+
}

test/libc/sock/socketpair_test.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2021 Alison Winters │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/assert.h"
20+
#include "libc/dce.h"
21+
#include "libc/sock/sock.h"
22+
#include "libc/sysv/consts/af.h"
23+
#include "libc/sysv/consts/sock.h"
24+
#include "libc/testlib/testlib.h"
25+
26+
TEST(socketpair, testAfUnixStream) {
27+
int fd[2];
28+
const char ping[] = "ping";
29+
const char pong[] = "pong";
30+
char buf[32];
31+
32+
ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_STREAM, 0, fd));
33+
ASSERT_EQ(sizeof(ping), write(fd[0], ping, sizeof(ping)));
34+
ASSERT_EQ(sizeof(ping), read(fd[1], buf, sizeof(ping)));
35+
EXPECT_STREQ(ping, buf);
36+
ASSERT_EQ(sizeof(pong), write(fd[1], pong, sizeof(pong)));
37+
ASSERT_EQ(sizeof(pong), read(fd[0], buf, sizeof(pong)));
38+
EXPECT_STREQ(pong, buf);
39+
ASSERT_NE(-1, close(fd[0]));
40+
ASSERT_NE(-1, close(fd[1]));
41+
}
42+
43+
TEST(socketpair, testAfUnixDgram) {
44+
int fd[2];
45+
const char ping[] = "ping";
46+
const char pong[] = "pong";
47+
char buf[32];
48+
49+
ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_DGRAM, 0, fd));
50+
ASSERT_EQ(sizeof(ping), write(fd[0], ping, sizeof(ping)));
51+
ASSERT_EQ(sizeof(ping), read(fd[1], buf, sizeof(buf)));
52+
EXPECT_STREQ(ping, buf);
53+
ASSERT_EQ(sizeof(pong), write(fd[1], pong, sizeof(pong)));
54+
ASSERT_EQ(sizeof(pong), read(fd[0], buf, sizeof(buf)));
55+
EXPECT_STREQ(pong, buf);
56+
ASSERT_NE(-1, close(fd[0]));
57+
ASSERT_NE(-1, close(fd[1]));
58+
}
59+
60+

0 commit comments

Comments
 (0)