2017-11-27 07:37:28 +00:00
|
|
|
#include <fcntl.h>
|
2018-09-16 08:16:18 +00:00
|
|
|
#include <endian.h>
|
2017-11-27 07:37:28 +00:00
|
|
|
|
2020-04-12 12:34:56 +00:00
|
|
|
#include <socket.hpp>
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <utils.hpp>
|
2017-11-27 07:37:28 +00:00
|
|
|
|
2021-01-12 08:07:48 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2020-06-19 10:52:25 +00:00
|
|
|
static size_t socket_len(sockaddr_un *sun) {
|
2020-12-31 06:11:24 +00:00
|
|
|
if (sun->sun_path[0])
|
|
|
|
return sizeof(sa_family_t) + strlen(sun->sun_path) + 1;
|
|
|
|
else
|
|
|
|
return sizeof(sa_family_t) + strlen(sun->sun_path + 1) + 1;
|
2018-09-16 08:16:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 10:52:25 +00:00
|
|
|
socklen_t setup_sockaddr(sockaddr_un *sun, const char *name) {
|
2020-12-31 06:11:24 +00:00
|
|
|
memset(sun, 0, sizeof(*sun));
|
|
|
|
sun->sun_family = AF_UNIX;
|
|
|
|
strcpy(sun->sun_path + 1, name);
|
|
|
|
return socket_len(sun);
|
2017-11-27 07:37:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
void get_client_cred(int fd, ucred *cred) {
|
|
|
|
socklen_t len = sizeof(*cred);
|
|
|
|
getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &len);
|
2018-09-16 08:16:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
static int send_fds(int sockfd, void *cmsgbuf, size_t bufsz, const int *fds, int cnt) {
|
|
|
|
iovec iov = {
|
|
|
|
.iov_base = &cnt,
|
|
|
|
.iov_len = sizeof(cnt),
|
|
|
|
};
|
|
|
|
msghdr msg = {
|
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
};
|
2019-02-09 20:02:46 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
if (cnt) {
|
|
|
|
msg.msg_control = cmsgbuf;
|
|
|
|
msg.msg_controllen = bufsz;
|
|
|
|
cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * cnt);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
2020-12-31 06:11:24 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return xsendmsg(sockfd, &msg, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int send_fds(int sockfd, const int *fds, int cnt) {
|
|
|
|
if (cnt == 0) {
|
|
|
|
return send_fds(sockfd, nullptr, 0, nullptr, 0);
|
|
|
|
}
|
|
|
|
vector<char> cmsgbuf;
|
|
|
|
cmsgbuf.resize(CMSG_SPACE(sizeof(int) * cnt));
|
|
|
|
return send_fds(sockfd, cmsgbuf.data(), cmsgbuf.size(), fds, cnt);
|
|
|
|
}
|
2020-12-31 06:11:24 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
int send_fd(int sockfd, int fd) {
|
|
|
|
if (fd < 0) {
|
|
|
|
return send_fds(sockfd, nullptr, 0, nullptr, 0);
|
|
|
|
}
|
2020-12-31 06:11:24 +00:00
|
|
|
char cmsgbuf[CMSG_SPACE(sizeof(int))];
|
2021-08-19 11:36:47 +00:00
|
|
|
return send_fds(sockfd, cmsgbuf, sizeof(cmsgbuf), &fd, 1);
|
|
|
|
}
|
2020-12-31 06:11:24 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
static void *recv_fds(int sockfd, char *cmsgbuf, size_t bufsz, int cnt) {
|
|
|
|
iovec iov = {
|
|
|
|
.iov_base = &cnt,
|
|
|
|
.iov_len = sizeof(cnt),
|
|
|
|
};
|
|
|
|
msghdr msg = {
|
2020-12-31 06:11:24 +00:00
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = cmsgbuf,
|
2021-08-19 11:36:47 +00:00
|
|
|
.msg_controllen = bufsz
|
2020-12-31 06:11:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
xrecvmsg(sockfd, &msg, MSG_WAITALL);
|
2021-08-19 11:36:47 +00:00
|
|
|
cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
|
|
|
|
if (msg.msg_controllen != bufsz ||
|
|
|
|
cmsg == nullptr ||
|
|
|
|
cmsg->cmsg_len != CMSG_LEN(sizeof(int) * cnt) ||
|
|
|
|
cmsg->cmsg_level != SOL_SOCKET ||
|
|
|
|
cmsg->cmsg_type != SCM_RIGHTS) {
|
|
|
|
return nullptr;
|
2020-12-31 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
return CMSG_DATA(cmsg);
|
|
|
|
}
|
2020-12-31 06:11:24 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
vector<int> recv_fds(int sockfd) {
|
2021-10-13 11:52:02 +00:00
|
|
|
vector<int> results;
|
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
// Peek fd count to allocate proper buffer
|
|
|
|
int cnt;
|
|
|
|
recv(sockfd, &cnt, sizeof(cnt), MSG_PEEK);
|
2021-10-13 11:52:02 +00:00
|
|
|
if (cnt == 0)
|
|
|
|
return results;
|
2017-11-27 07:37:28 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
vector<char> cmsgbuf;
|
|
|
|
cmsgbuf.resize(CMSG_SPACE(sizeof(int) * cnt));
|
2018-02-10 19:40:09 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
void *data = recv_fds(sockfd, cmsgbuf.data(), cmsgbuf.size(), cnt);
|
|
|
|
if (data == nullptr)
|
|
|
|
return results;
|
|
|
|
|
|
|
|
results.resize(cnt);
|
|
|
|
memcpy(results.data(), data, sizeof(int) * cnt);
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
2018-02-10 19:40:09 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
int recv_fd(int sockfd) {
|
2020-12-31 06:11:24 +00:00
|
|
|
char cmsgbuf[CMSG_SPACE(sizeof(int))];
|
2018-02-10 19:40:09 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
void *data = recv_fds(sockfd, cmsgbuf, sizeof(cmsgbuf), 1);
|
|
|
|
if (data == nullptr)
|
|
|
|
return -1;
|
2018-02-10 19:40:09 +00:00
|
|
|
|
2021-08-19 11:36:47 +00:00
|
|
|
int result;
|
|
|
|
memcpy(&result, data, sizeof(int));
|
|
|
|
return result;
|
2017-11-27 07:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int read_int(int fd) {
|
2020-12-31 06:11:24 +00:00
|
|
|
int val;
|
|
|
|
if (xxread(fd, &val, sizeof(val)) != sizeof(val))
|
|
|
|
return -1;
|
|
|
|
return val;
|
2017-11-27 07:37:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 08:16:18 +00:00
|
|
|
int read_int_be(int fd) {
|
2020-12-31 06:11:24 +00:00
|
|
|
uint32_t val;
|
|
|
|
if (xxread(fd, &val, sizeof(val)) != sizeof(val))
|
|
|
|
return -1;
|
|
|
|
return ntohl(val);
|
2018-09-16 08:16:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 07:37:28 +00:00
|
|
|
void write_int(int fd, int val) {
|
2020-12-31 06:11:24 +00:00
|
|
|
if (fd < 0) return;
|
|
|
|
xwrite(fd, &val, sizeof(val));
|
2017-11-27 07:37:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 08:16:18 +00:00
|
|
|
void write_int_be(int fd, int val) {
|
2020-12-31 06:11:24 +00:00
|
|
|
uint32_t nl = htonl(val);
|
|
|
|
xwrite(fd, &nl, sizeof(nl));
|
2018-09-16 08:16:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 08:07:48 +00:00
|
|
|
void read_string(int fd, std::string &str) {
|
2020-12-31 06:11:24 +00:00
|
|
|
int len = read_int(fd);
|
2021-01-12 08:07:48 +00:00
|
|
|
str.clear();
|
|
|
|
str.resize(len);
|
|
|
|
xxread(fd, str.data(), len);
|
2018-09-16 08:16:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 08:07:48 +00:00
|
|
|
string read_string(int fd) {
|
|
|
|
string str;
|
|
|
|
read_string(fd, str);
|
|
|
|
return str;
|
2018-09-16 08:16:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 08:07:48 +00:00
|
|
|
void write_string(int fd, string_view str) {
|
2020-12-31 06:11:24 +00:00
|
|
|
if (fd < 0) return;
|
2021-01-12 08:07:48 +00:00
|
|
|
write_int(fd, str.size());
|
|
|
|
xwrite(fd, str.data(), str.size());
|
2018-09-16 08:16:18 +00:00
|
|
|
}
|