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 <errno.h>
25 #include <netinet/in.h>
26 #include <netinet/tcp.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/un.h>
34 #include <unistd.h>
35 
36 #include <hardware/bluetooth.h>
37 #include <hardware/bt_sock.h>
38 
39 #include "bt_target.h"
40 #include "btif_util.h"
41 #include "osi/include/osi.h"
42 
43 #define asrt(s)                                                              \
44   do {                                                                       \
45     if (!(s))                                                                \
46       BTIF_TRACE_ERROR("## %s assert %s failed at line:%d ##", __func__, #s, \
47                        __LINE__)                                             \
48   } while (0)
49 
sock_send_all(int sock_fd,const uint8_t * buf,int len)50 int sock_send_all(int sock_fd, const uint8_t* buf, int len) {
51   int s = len;
52 
53   while (s) {
54     ssize_t ret;
55     OSI_NO_INTR(ret = send(sock_fd, buf, s, 0));
56     if (ret <= 0) {
57       BTIF_TRACE_ERROR("sock fd:%d send errno:%d, ret:%d", sock_fd, errno, ret);
58       return -1;
59     }
60     buf += ret;
61     s -= ret;
62   }
63   return len;
64 }
sock_recv_all(int sock_fd,uint8_t * buf,int len)65 int sock_recv_all(int sock_fd, uint8_t* buf, int len) {
66   int r = len;
67 
68   while (r) {
69     ssize_t ret;
70     OSI_NO_INTR(ret = recv(sock_fd, buf, r, MSG_WAITALL));
71     if (ret <= 0) {
72       BTIF_TRACE_ERROR("sock fd:%d recv errno:%d, ret:%d", sock_fd, errno, ret);
73       return -1;
74     }
75     buf += ret;
76     r -= ret;
77   }
78   return len;
79 }
80 
sock_send_fd(int sock_fd,const uint8_t * buf,int len,int send_fd)81 int sock_send_fd(int sock_fd, const uint8_t* buf, int len, int send_fd) {
82   struct msghdr msg;
83   unsigned char* buffer = (unsigned char*)buf;
84   memset(&msg, 0, sizeof(msg));
85 
86   struct cmsghdr* cmsg;
87   char msgbuf[CMSG_SPACE(1)];
88   asrt(send_fd != -1);
89   if (sock_fd == -1 || send_fd == -1) return -1;
90   // Add any pending outbound file descriptors to the message
91   // See "man cmsg" really
92   msg.msg_control = msgbuf;
93   msg.msg_controllen = sizeof msgbuf;
94   cmsg = CMSG_FIRSTHDR(&msg);
95   cmsg->cmsg_level = SOL_SOCKET;
96   cmsg->cmsg_type = SCM_RIGHTS;
97   cmsg->cmsg_len = CMSG_LEN(sizeof send_fd);
98   memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd);
99 
100   // We only write our msg_control during the first write
101   int ret_len = len;
102   while (len > 0) {
103     struct iovec iv;
104     memset(&iv, 0, sizeof(iv));
105 
106     iv.iov_base = buffer;
107     iv.iov_len = len;
108 
109     msg.msg_iov = &iv;
110     msg.msg_iovlen = 1;
111 
112     ssize_t ret;
113     OSI_NO_INTR(ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL));
114     if (ret < 0) {
115       BTIF_TRACE_ERROR("fd:%d, send_fd:%d, sendmsg ret:%d, errno:%d, %s",
116                        sock_fd, send_fd, (int)ret, errno, strerror(errno));
117       ret_len = -1;
118       break;
119     }
120 
121     buffer += ret;
122     len -= ret;
123 
124     // Wipes out any msg_control too
125     memset(&msg, 0, sizeof(msg));
126   }
127   BTIF_TRACE_DEBUG("close fd:%d after sent", send_fd);
128   // TODO: This seems wrong - if the FD is not opened in JAVA before this is
129   // called
130   //       we get a "socket closed" exception in java, when reading from the
131   //       socket...
132   close(send_fd);
133   return ret_len;
134 }
135