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 "os/log.h"
38 #include "osi/include/osi.h"
39
40 #define asrt(s) \
41 do { \
42 if (!(s)) 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) return -1;
87 // Add any pending outbound file descriptors to the message
88 // See "man cmsg" really
89 msg.msg_control = msgbuf;
90 msg.msg_controllen = sizeof msgbuf;
91 cmsg = CMSG_FIRSTHDR(&msg);
92 cmsg->cmsg_level = SOL_SOCKET;
93 cmsg->cmsg_type = SCM_RIGHTS;
94 cmsg->cmsg_len = CMSG_LEN(sizeof send_fd);
95 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd);
96
97 // We only write our msg_control during the first write
98 int ret_len = len;
99 while (len > 0) {
100 struct iovec iv;
101 memset(&iv, 0, sizeof(iv));
102
103 iv.iov_base = buffer;
104 iv.iov_len = len;
105
106 msg.msg_iov = &iv;
107 msg.msg_iovlen = 1;
108
109 ssize_t ret;
110 OSI_NO_INTR(ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL));
111 if (ret < 0) {
112 log::error("fd:{}, send_fd:{}, sendmsg ret:{}, errno:{}, {}", sock_fd,
113 send_fd, (int)ret, errno, strerror(errno));
114 ret_len = -1;
115 break;
116 }
117
118 buffer += ret;
119 len -= ret;
120
121 // Wipes out any msg_control too
122 memset(&msg, 0, sizeof(msg));
123 }
124 log::verbose("close fd:{} after sent", send_fd);
125 // TODO: This seems wrong - if the FD is not opened in JAVA before this is
126 // called
127 // we get a "socket closed" exception in java, when reading from the
128 // socket...
129 close(send_fd);
130 return ret_len;
131 }
132