4fc87b2ad8a5ef9186c5db4508ca9f9e288ef865
[src/drm-lease-manager.git] / common / dlm-protocol.c
1 /* Copyright 2020-2021 IGEL Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "dlm-protocol.h"
17
18 #include <errno.h>
19 #include <string.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 bool receive_dlm_client_request(int socket, struct dlm_client_request *request)
25 {
26
27         ssize_t len;
28         struct iovec iov = {
29             .iov_base = request,
30             .iov_len = sizeof(*request),
31         };
32         struct msghdr msg = {
33             .msg_iov = &iov,
34             .msg_iovlen = 1,
35         };
36
37         while ((len = recvmsg(socket, &msg, 0)) < 0) {
38                 if (errno != EINTR)
39                         return false;
40         }
41
42         if (len != sizeof(*request)) {
43                 errno = EPROTO;
44                 return false;
45         }
46         return true;
47 }
48
49 bool send_dlm_client_request(int socket, struct dlm_client_request *request)
50 {
51         struct iovec iov = {
52             .iov_base = request,
53             .iov_len = sizeof(*request),
54         };
55
56         struct msghdr msg = {
57             .msg_iov = &iov,
58             .msg_iovlen = 1,
59         };
60
61         while (sendmsg(socket, &msg, 0) < 1) {
62                 if (errno != EINTR)
63                         return false;
64         }
65         return true;
66 }