network/stack: socketpair for non-stream sockets

`socketpair` should skip the `listen` and `accept` step for
non-stream sockets. This allows the implementation to work for
UNIX domain datagram sockets.

Change-Id: I81ba826d605b77be5a4ec9046ea4c6bd556a35a0
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6618
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: Jérôme Duval <[email protected]>
This commit is contained in:
Trung Nguyen
2023-07-04 15:09:20 +00:00
committed by waddlesplash
parent 419466254d
commit 4313795d2e
+19 -11
View File
@@ -1645,7 +1645,7 @@ socket_socketpair(int family, int type, int protocol, net_socket* sockets[2])
error = socket_bind(sockets[0], NULL, 0);
// start listening
if (error == B_OK)
if (error == B_OK && type == SOCK_STREAM)
error = socket_listen(sockets[0], 1);
// connect them
@@ -1654,17 +1654,25 @@ socket_socketpair(int family, int type, int protocol, net_socket* sockets[2])
sockets[0]->address.ss_len);
}
// accept a socket
net_socket* acceptedSocket = NULL;
if (error == B_OK)
error = socket_accept(sockets[0], NULL, NULL, &acceptedSocket);
if (error == B_OK) {
// everything worked: close the listener socket
socket_close(sockets[0]);
socket_free(sockets[0]);
sockets[0] = acceptedSocket;
} else {
// accept a socket
if (type == SOCK_STREAM) {
net_socket* acceptedSocket = NULL;
error = socket_accept(sockets[0], NULL, NULL, &acceptedSocket);
if (error == B_OK) {
// everything worked: close the listener socket
socket_close(sockets[0]);
socket_free(sockets[0]);
sockets[0] = acceptedSocket;
}
// connect the other side
} else {
error = socket_connect(sockets[0], (sockaddr*)&sockets[1]->address,
sockets[1]->address.ss_len);
}
}
if (error != B_OK) {
// close sockets on error
for (int i = 0; i < 2; i++) {
if (sockets[i] != NULL) {