[PATCH ucspi-vsock 1/7] vsock: get cid and port instead of just cid

Alyssa Ross
2021-03-19T02:58:51Z

We can get both in the same system call, so we might as well, rather
than having two different functions that make the system call twice.
---

When we introduce vsockserverd, it will have to be able to figure out
both of these values for the socket it is given to be able to set
VSOCKLOCALCID and VSOCKLOCALPORT.

vsock.c | 9 ++++++---
vsock.h | 5 +++--
vsockserver.c | 2 +-
3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/vsock.c b/vsock.c
index e6a173c..99945c3 100644
--- a/vsock.c
+++ b/vsock.c
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-or-later
-// SPDX-FileCopyrightText: 2020 Alyssa Ross <hi@alyssa.is>
+// SPDX-FileCopyrightText: 2020-2021 Alyssa Ross <hi@alyssa.is>

#define _GNU_SOURCE

@@ -62,7 +62,7 @@ int vsock_open(uint32_t cid, uint32_t port)
return fd;
}

-int vsock_get_port(int fd, uint32_t *port)
+int vsock_get_cid_and_port(int fd, uint32_t *cid, uint32_t *port)
{
struct sockaddr_vm addr;
socklen_t addrlen = sizeof addr;
@@ -70,7 +70,10 @@ int vsock_get_port(int fd, uint32_t *port)
if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) == -1)
return -1;

- *port = addr.svm_port;
+ if (cid)
+ *cid = addr.svm_cid;
+ if (port)
+ *port = addr.svm_port;

return 0;
}
diff --git a/vsock.h b/vsock.h
index e7d66c9..e7ffd62 100644
--- a/vsock.h
+++ b/vsock.h
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-or-later
-// SPDX-FileCopyrightText: 2020 Alyssa Ross <hi@alyssa.is>
+// SPDX-FileCopyrightText: 2020-2021 Alyssa Ross <hi@alyssa.is>

#include <stdint.h>

@@ -9,4 +9,5 @@ int vsock_accept(int sockfd, uint32_t *cid, uint32_t *port);
int vsock_connect(int fd, uint32_t cid, uint32_t port);
int vsock_open(uint32_t cid, uint32_t port);

-int vsock_get_port(int fd, uint32_t *port);
+// `cid' and `port' can be null.
+int vsock_get_cid_and_port(int fd, uint32_t *cid, uint32_t *port);
diff --git a/vsockserver.c b/vsockserver.c
index df9d334..58cd063 100644
--- a/vsockserver.c
+++ b/vsockserver.c
@@ -80,7 +80,7 @@ int main(int argc, char *argv[])
diee(EX_OSERR, "listen");

if (lport == VMADDR_PORT_ANY)
- if (vsock_get_port(fd, &lport) == -1)
+ if (vsock_get_cid_and_port(fd, NULL, &lport) == -1)
diee(EX_OSERR, "getsockname");

if (notify) {
--
2.30.0