From: Vlad Badoiu Date: Tue, 18 Jan 2022 20:16:12 +0000 (+0200) Subject: lwip: Add dumb socketpair implementation X-Git-Tag: RELEASE-0.7.0^0 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=40f2c7731e2f00042d6b13b134e6b07ebe769c49;p=unikraft%2Flibs%2Flwip.git lwip: Add dumb socketpair implementation 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 Reviewed-by: Sergiu Moga Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Pull-Request: #15 --- diff --git a/sockets.c b/sockets.c index 7db82c7..8f25e3f 100644 --- 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; }