]> xenbits.xensource.com Git - unikraft/libs/lwip.git/commitdiff
lwip: Add dumb socketpair implementation RELEASE-0.7.0
authorVlad Badoiu <vlad_andrei.badoiu@upb.ro>
Tue, 18 Jan 2022 20:16:12 +0000 (22:16 +0200)
committerUnikraft <monkey@unikraft.io>
Thu, 3 Feb 2022 08:44:26 +0000 (08:44 +0000)
We add a dumb implementation of socketpair until
posix-socket is upstreamed. Since we have no SMP
available on Unikraft, this implementation works
well.

Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@upb.ro>
Reviewed-by: Sergiu Moga <sergiu.moga@protonmail.com>
Approved-by: Razvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Pull-Request: #15

sockets.c

index 7db82c7fdccac0b56d8a5c8de028e5985876baa9..8f25e3f8b2c76fec22c5ad178054f94bf67d9a38 100644 (file)
--- a/sockets.c
+++ b/sockets.c
@@ -890,8 +890,54 @@ EXIT:
        return ret;
 }
 
-int socketpair(int domain, int type, int protocol, int sv[2])
+int socketpair(int domain, int type, int protocol, int socks[2])
 {
+       int listener;
+       int reuse = 1;
+       union {
+               struct sockaddr_in inaddr;
+               struct sockaddr addr;
+       } a;
+
+       socklen_t addrlen = sizeof(a.inaddr);
+
+       listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+
+       memset(&a, 0, sizeof(a));
+       a.inaddr.sin_family = AF_INET;
+       a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+       a.inaddr.sin_port = 0;
+
+       if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
+              (char*) &reuse, (socklen_t) sizeof(reuse)) == -1)
+               goto error;
+
+       if  (bind(listener, &a.addr, sizeof(a.inaddr)) < 0)
+               goto error;
+
+       if  (getsockname(listener, &a.addr, &addrlen) < 0)
+               goto error;
+
+       if (listen(listener, 1) < 0)
+               goto error;
+
+       socks[0] = socket(AF_INET, SOCK_STREAM, 0);
+
+       if (socks[0] < 0)
+               goto error;
+
+       if (connect(socks[0], &a.addr, sizeof(a.inaddr)) < 0)
+               goto error;
+
+       socks[1] = accept(listener, NULL, NULL);
+
+       if (socks[1] < 0)
+               goto error;
+
+       close(listener);
+       return 0;
+
+error:
        errno = ENOTSUP;
        return -1;
 }