12 #include <sys/types.h>
13 #include <sys/socket.h>
31 cmsg_len(
const socklen_t len) {
33 return (CMSG_LEN(len));
37 const uintptr_t hdrlen = (uintptr_t)CMSG_DATA(((
struct cmsghdr*)NULL));
38 return (hdrlen + len);
43 cmsg_space(
const socklen_t len) {
45 return (CMSG_SPACE(len));
48 struct cmsghdr* cmsgp;
51 char dummybuf[
sizeof(
struct cmsghdr) + 1024];
53 memset(&msg, 0,
sizeof(msg));
54 msg.msg_control = dummybuf;
55 msg.msg_controllen =
sizeof(dummybuf);
57 cmsgp = (
struct cmsghdr*)dummybuf;
58 cmsgp->cmsg_len = cmsg_len(len);
60 cmsgp = CMSG_NXTHDR(&msg, cmsgp);
62 return ((
char*)cmsgp - (
char*)msg.msg_control);
73 struct iovec iov_dummy;
74 unsigned char dummy_data;
76 iov_dummy.iov_base = &dummy_data;
77 iov_dummy.iov_len =
sizeof(dummy_data);
78 msghdr.msg_name = NULL;
79 msghdr.msg_namelen = 0;
80 msghdr.msg_iov = &iov_dummy;
81 msghdr.msg_iovlen = 1;
83 msghdr.msg_controllen = cmsg_space(
sizeof(
int));
84 msghdr.msg_control = malloc(msghdr.msg_controllen);
85 if (msghdr.msg_control == NULL) {
89 const int cc = recvmsg(sock, &msghdr, 0);
91 free(msghdr.msg_control);
97 const struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msghdr);
99 if (cmsg != NULL && cmsg->cmsg_len == cmsg_len(
sizeof(
int)) &&
100 cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
106 std::memcpy(&fd, CCMSG_DATA(cmsg),
sizeof(
int));
108 std::memcpy(&fd, CMSG_DATA(cmsg),
sizeof(
int));
111 free(msghdr.msg_control);
113 int close_error = -1;
119 close_error = close(fd);
121 if (close_error == -1 || new_fd == -1) {
135 struct msghdr msghdr;
136 struct iovec iov_dummy;
137 unsigned char dummy_data = 0;
139 iov_dummy.iov_base = &dummy_data;
140 iov_dummy.iov_len =
sizeof(dummy_data);
141 msghdr.msg_name = NULL;
142 msghdr.msg_namelen = 0;
143 msghdr.msg_iov = &iov_dummy;
144 msghdr.msg_iovlen = 1;
145 msghdr.msg_flags = 0;
146 msghdr.msg_controllen = cmsg_space(
sizeof(
int));
147 msghdr.msg_control = malloc(msghdr.msg_controllen);
148 if (msghdr.msg_control == NULL) {
151 std::memset(msghdr.msg_control, 0, msghdr.msg_controllen);
153 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msghdr);
154 cmsg->cmsg_len = cmsg_len(
sizeof(
int));
155 cmsg->cmsg_level = SOL_SOCKET;
156 cmsg->cmsg_type = SCM_RIGHTS;
157 std::memcpy(CMSG_DATA(cmsg), &fd,
sizeof(
int));
159 const int ret = sendmsg(sock, &msghdr, 0);
160 free(msghdr.msg_control);
int recv_fd(const int sock)
Receives a file descriptor.
Support to transfer file descriptors between processes.
int send_fd(const int sock, const int fd)
Sends a file descriptor.
Defines the logger used by the top-level component of kea-dhcp-ddns.
const int FD_SYSTEM_ERROR