Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions toolbelt/sockets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,25 @@ static struct sockaddr_un BuildUnixSocketName(const std::string &pathname) {
// On Linux we can create it in the abstract namespace which doesn't
// consume a pathname.
addr.sun_path[0] = '\0';
memcpy(addr.sun_path + 1, pathname.c_str(), std::min(pathname.size(), sizeof(addr.sun_path) - 2));
memcpy(addr.sun_path + 1, pathname.c_str(),
std::min(pathname.size(), sizeof(addr.sun_path) - 2));
#else
// Portable uses the file system so it must be a valid path name.
memcpy(addr.sun_path, pathname.c_str(), std::min(pathname.size(), sizeof(addr.sun_path) - 1));
memcpy(addr.sun_path, pathname.c_str(),
std::min(pathname.size(), sizeof(addr.sun_path) - 1));
#endif
return addr;
}

static std::string ExtractUnixSocketNameString(const struct sockaddr_un &addr, socklen_t addrlen) {
static std::string ExtractUnixSocketNameString(const struct sockaddr_un &addr,
socklen_t addrlen) {
#if defined(__linux__)
auto addr_str_len = strnlen(addr.sun_path + 1, addrlen - offsetof(sockaddr_un, sun_path) - 1);
auto addr_str_len =
strnlen(addr.sun_path + 1, addrlen - offsetof(sockaddr_un, sun_path) - 1);
return std::string(addr.sun_path + 1, addr.sun_path + addr_str_len + 1);
#else
auto addr_str_len = strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path));
auto addr_str_len =
strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path));
return std::string(addr.sun_path, addr.sun_path + addr_str_len);
#endif
}
Expand Down Expand Up @@ -900,11 +905,16 @@ absl::Status VirtualStreamSocket::Connect(const VirtualAddress &addr) {

absl::StatusOr<VirtualAddress>
VirtualStreamSocket::LocalAddress(uint32_t port) const {
#if defined(IOCTL_VM_SOCKETS_GET_LOCAL_CID)
int32_t cid;
int e = ioctl(fd_.Fd(), IOCTL_VM_SOCKETS_GET_LOCAL_CID, &cid);
if (e == -1) {
return absl::InternalError("Failed to get local CID");
}
#else
// If we cannot get the local CID, return ANY.
int32_t cid = VMADDR_CID_ANY;
#endif
return VirtualAddress(cid, port);
}

Expand Down
31 changes: 31 additions & 0 deletions toolbelt/sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,40 @@
#include <sys/un.h>

#if defined(__linux__)

#if __has_include(<linux/vm_sockets.h>)
#include <linux/vm_sockets.h>
#define HAS_VM_SOCKETS 1
#else
#define HAS_VM_SOCKETS 0
#endif

#else
#if __has_include(<sys/vsock.h>)
#include <sys/vsock.h>
#define HAS_VM_SOCKETS 1
#else
#define HAS_VM_SOCKETS 0
#endif
#endif

// Older systems may not have the header file.
#if !HAS_VM_SOCKETS
struct sockaddr_vm {
#if defined(_APPLE__)
uint8_t svm_len; /* total length of sockaddr */
#endif
sa_family_t svm_family; /* AF_VSOCK */
uint32_t svm_reserved1;
uint32_t svm_port;
uint32_t svm_cid;
uint32_t svm_reserved2;
};
#define VMADDR_CID_ANY (~0U)
#define VMADDR_CID_HOST 1
#define VMADDR_CID_HYPERVISOR 2
#define VMADDR_CID_LOCAL 3
#define AF_VSOCK 40
#endif

#include <unistd.h>
Expand Down