Skip to content

Commit 18aa1fd

Browse files
authored
Merge pull request #16 from dallison/vm_sockets
Allow for older systems with no VM sockets header files.
2 parents 30edc0f + 2fea8f6 commit 18aa1fd

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

MODULE.bazel.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

toolbelt/sockets.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,20 +331,25 @@ static struct sockaddr_un BuildUnixSocketName(const std::string &pathname) {
331331
// On Linux we can create it in the abstract namespace which doesn't
332332
// consume a pathname.
333333
addr.sun_path[0] = '\0';
334-
memcpy(addr.sun_path + 1, pathname.c_str(), std::min(pathname.size(), sizeof(addr.sun_path) - 2));
334+
memcpy(addr.sun_path + 1, pathname.c_str(),
335+
std::min(pathname.size(), sizeof(addr.sun_path) - 2));
335336
#else
336337
// Portable uses the file system so it must be a valid path name.
337-
memcpy(addr.sun_path, pathname.c_str(), std::min(pathname.size(), sizeof(addr.sun_path) - 1));
338+
memcpy(addr.sun_path, pathname.c_str(),
339+
std::min(pathname.size(), sizeof(addr.sun_path) - 1));
338340
#endif
339341
return addr;
340342
}
341343

342-
static std::string ExtractUnixSocketNameString(const struct sockaddr_un &addr, socklen_t addrlen) {
344+
static std::string ExtractUnixSocketNameString(const struct sockaddr_un &addr,
345+
socklen_t addrlen) {
343346
#if defined(__linux__)
344-
auto addr_str_len = strnlen(addr.sun_path + 1, addrlen - offsetof(sockaddr_un, sun_path) - 1);
347+
auto addr_str_len =
348+
strnlen(addr.sun_path + 1, addrlen - offsetof(sockaddr_un, sun_path) - 1);
345349
return std::string(addr.sun_path + 1, addr.sun_path + addr_str_len + 1);
346350
#else
347-
auto addr_str_len = strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path));
351+
auto addr_str_len =
352+
strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path));
348353
return std::string(addr.sun_path, addr.sun_path + addr_str_len);
349354
#endif
350355
}
@@ -900,11 +905,16 @@ absl::Status VirtualStreamSocket::Connect(const VirtualAddress &addr) {
900905

901906
absl::StatusOr<VirtualAddress>
902907
VirtualStreamSocket::LocalAddress(uint32_t port) const {
908+
#if defined(IOCTL_VM_SOCKETS_GET_LOCAL_CID)
903909
int32_t cid;
904910
int e = ioctl(fd_.Fd(), IOCTL_VM_SOCKETS_GET_LOCAL_CID, &cid);
905911
if (e == -1) {
906912
return absl::InternalError("Failed to get local CID");
907913
}
914+
#else
915+
// If we cannot get the local CID, return ANY.
916+
int32_t cid = VMADDR_CID_ANY;
917+
#endif
908918
return VirtualAddress(cid, port);
909919
}
910920

toolbelt/sockets.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,40 @@
1717
#include <sys/un.h>
1818

1919
#if defined(__linux__)
20+
21+
#if __has_include(<linux/vm_sockets.h>)
2022
#include <linux/vm_sockets.h>
23+
#define HAS_VM_SOCKETS 1
2124
#else
25+
#define HAS_VM_SOCKETS 0
26+
#endif
27+
28+
#else
29+
#if __has_include(<sys/vsock.h>)
2230
#include <sys/vsock.h>
31+
#define HAS_VM_SOCKETS 1
32+
#else
33+
#define HAS_VM_SOCKETS 0
34+
#endif
35+
#endif
36+
37+
// Older systems may not have the header file.
38+
#if !HAS_VM_SOCKETS
39+
struct sockaddr_vm {
40+
#if defined(_APPLE__)
41+
uint8_t svm_len; /* total length of sockaddr */
42+
#endif
43+
sa_family_t svm_family; /* AF_VSOCK */
44+
uint32_t svm_reserved1;
45+
uint32_t svm_port;
46+
uint32_t svm_cid;
47+
uint32_t svm_reserved2;
48+
};
49+
#define VMADDR_CID_ANY (~0U)
50+
#define VMADDR_CID_HOST 1
51+
#define VMADDR_CID_HYPERVISOR 2
52+
#define VMADDR_CID_LOCAL 3
53+
#define AF_VSOCK 40
2354
#endif
2455

2556
#include <unistd.h>

0 commit comments

Comments
 (0)