From 2cbcde19f62970a8da308d5b128b2b2d7c725077 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 18 Nov 2023 13:55:27 +0100 Subject: Properly send socketpair fd to kms server --- kms/client/kms_client.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'kms/client') diff --git a/kms/client/kms_client.c b/kms/client/kms_client.c index 3e60c63..8e1dafb 100644 --- a/kms/client/kms_client.c +++ b/kms/client/kms_client.c @@ -53,11 +53,29 @@ static int send_msg_to_server(int server_fd, gsr_kms_request *request) { iov.iov_base = request; iov.iov_len = sizeof(*request); - struct msghdr request_message = {0}; - request_message.msg_iov = &iov; - request_message.msg_iovlen = 1; + struct msghdr response_message = {0}; + response_message.msg_iov = &iov; + response_message.msg_iovlen = 1; + + char cmsgbuf[CMSG_SPACE(sizeof(int) * 1)]; + memset(cmsgbuf, 0, sizeof(cmsgbuf)); + + if(request->new_connection_fd > 0) { + response_message.msg_control = cmsgbuf; + response_message.msg_controllen = sizeof(cmsgbuf); + + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof(int) * 1); + + int *fds = (int*)CMSG_DATA(cmsg); + fds[0] = request->new_connection_fd; + + response_message.msg_controllen = cmsg->cmsg_len; + } - return sendmsg(server_fd, &request_message, 0); + return sendmsg(server_fd, &response_message, 0); } static int recv_msg_from_server(int server_fd, gsr_kms_response *response) { -- cgit v1.2.3