1 /******************************************************************************
2  *
3  *  Copyright 2009-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "bt_btif_sock"
20 
21 #include "btif_sock_util.h"
22 
23 #include <arpa/inet.h>
24 #include <bluetooth/log.h>
25 #include <hardware/bluetooth.h>
26 #include <hardware/bt_sock.h>
27 #include <netinet/in.h>
28 #include <netinet/tcp.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <sys/un.h>
35 #include <unistd.h>
36 
37 #include "osi/include/osi.h"
38 
39 #define asrt(s)                                 \
40   do {                                          \
41     if (!(s))                                   \
42       log::error("## assert {} failed ##", #s); \
43   } while (0)
44 
45 using namespace bluetooth;
46 
sock_send_all(int sock_fd,const uint8_t * buf,int len)47 int sock_send_all(int sock_fd, const uint8_t* buf, int len) {
48   int s = len;
49 
50   while (s) {
51     ssize_t ret;
52     OSI_NO_INTR(ret = send(sock_fd, buf, s, 0));
53     if (ret <= 0) {
54       log::error("sock fd:{} send errno:{}, ret:{}", sock_fd, errno, ret);
55       return -1;
56     }
57     buf += ret;
58     s -= ret;
59   }
60   return len;
61 }
sock_recv_all(int sock_fd,uint8_t * buf,int len)62 int sock_recv_all(int sock_fd, uint8_t* buf, int len) {
63   int r = len;
64 
65   while (r) {
66     ssize_t ret;
67     OSI_NO_INTR(ret = recv(sock_fd, buf, r, MSG_WAITALL));
68     if (ret <= 0) {
69       log::error("sock fd:{} recv errno:{}, ret:{}", sock_fd, errno, ret);
70       return -1;
71     }
72     buf += ret;
73     r -= ret;
74   }
75   return len;
76 }
77 
sock_send_fd(int sock_fd,const uint8_t * buf,int len,int send_fd)78 int sock_send_fd(int sock_fd, const uint8_t* buf, int len, int send_fd) {
79   struct msghdr msg;
80   unsigned char* buffer = (unsigned char*)buf;
81   memset(&msg, 0, sizeof(msg));
82 
83   struct cmsghdr* cmsg;
84   char msgbuf[CMSG_SPACE(1)];
85   asrt(send_fd != -1);
86   if (sock_fd == -1 || send_fd == -1) {
87     return -1;
88   }
89   // Add any pending outbound file descriptors to the message
90   // See "man cmsg" really
91   msg.msg_control = msgbuf;
92   msg.msg_controllen = sizeof msgbuf;
93   cmsg = CMSG_FIRSTHDR(&msg);
94   cmsg->cmsg_level = SOL_SOCKET;
95   cmsg->cmsg_type = SCM_RIGHTS;
96   cmsg->cmsg_len = CMSG_LEN(sizeof send_fd);
97   memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd);
98 
99   // We only write our msg_control during the first write
100   int ret_len = len;
101   while (len > 0) {
102     struct iovec iv;
103     memset(&iv, 0, sizeof(iv));
104 
105     iv.iov_base = buffer;
106     iv.iov_len = len;
107 
108     msg.msg_iov = &iv;
109     msg.msg_iovlen = 1;
110 
111     ssize_t ret;
112     OSI_NO_INTR(ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL));
113     if (ret < 0) {
114       log::error("fd:{}, send_fd:{}, sendmsg ret:{}, errno:{}, {}", sock_fd, send_fd, (int)ret,
115                  errno, strerror(errno));
116       ret_len = -1;
117       break;
118     }
119 
120     buffer += ret;
121     len -= ret;
122 
123     // Wipes out any msg_control too
124     memset(&msg, 0, sizeof(msg));
125   }
126   log::verbose("close fd:{} after sent", send_fd);
127   // TODO: This seems wrong - if the FD is not opened in JAVA before this is
128   // called
129   //       we get a "socket closed" exception in java, when reading from the
130   //       socket...
131   close(send_fd);
132   return ret_len;
133 }
134