• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
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 "socket_util.h"
17 #include <cerrno>
18 #include <cstring>
19 #include <sys/socket.h>
20 #include "log.h"
21 #include "securec.h"
22 
23 namespace bluetooth {
SocketSendData(int sockFd,const uint8_t * buf,int len)24 int SocketUtil::SocketSendData(int sockFd, const uint8_t *buf, int len)
25 {
26     int sendSize = len;
27 
28     while (sendSize) {
29         ssize_t size = send(sockFd, buf, sendSize, 0);
30         if (size <= 0) {
31             return -1;
32         }
33         if (size > sendSize) {
34             return -1;
35         }
36         buf += size;
37         sendSize -= size;
38     }
39     return len;
40 }
41 
SocketSendFd(int sockFd,const uint8_t * buf,int len,int acceptFd)42 int SocketUtil::SocketSendFd(int sockFd, const uint8_t *buf, int len, int acceptFd)
43 {
44     LOG_INFO("[sock]%{public}s", __func__);
45 
46     if ((sockFd == -1 || acceptFd == -1)) {
47         return -1;
48     }
49 
50     int sendSize = len;
51     struct msghdr msghdr = {};
52     struct iovec iovec = {};
53     struct cmsghdr *cmsghdr;
54     char msgbuffer[CMSG_SPACE(1)] = {0};
55 
56     (void)memset_s(&msghdr, sizeof(msghdr), 0, sizeof(msghdr));
57     (void)memset_s(&iovec, sizeof(iovec), 0, sizeof(iovec));
58 
59     msghdr.msg_control = msgbuffer;
60     msghdr.msg_controllen = sizeof(msgbuffer);
61     cmsghdr = CMSG_FIRSTHDR(&msghdr);
62     cmsghdr->cmsg_len = CMSG_LEN(sizeof(acceptFd));
63     cmsghdr->cmsg_type = SCM_RIGHTS;
64     cmsghdr->cmsg_level = SOL_SOCKET;
65     (void)memcpy_s(CMSG_DATA(cmsghdr), sizeof(acceptFd), &acceptFd, sizeof(acceptFd));
66 
67     while (len > 0) {
68         iovec.iov_base = const_cast<void *>(static_cast<const void *>(buf));
69         iovec.iov_len = len;
70 
71         msghdr.msg_iov = &iovec;
72         msghdr.msg_iovlen = 1;
73 
74 #ifdef DARWIN_PLATFORM
75         ssize_t size = sendmsg(sockFd, &msghdr, 0);
76 #else
77         ssize_t size = sendmsg(sockFd, &msghdr, MSG_NOSIGNAL);
78 #endif
79         LOG_INFO("[sock]%{public}s sendmsg size:%{public}zu", __func__, size);
80         if (size < 0) {
81             sendSize = -1;
82             break;
83         }
84 
85         buf += size;
86         len -= size;
87         (void)memset_s(&msghdr, sizeof(msghdr), 0, sizeof(msghdr));
88     }
89     LOG_INFO("[sock]%{public}s close accept_fd:%{public}d after sent", __func__, acceptFd);
90     return sendSize;
91 }
92 }  // namespace bluetooth
93