1 /******************************************************************************
2 *
3 * Copyright 2014 Google, Inc.
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_osi_socket"
20
21 #include "osi/include/socket.h"
22
23 #include <asm/ioctls.h>
24 #include <bluetooth/log.h>
25 #include <netinet/in.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30
31 #include "osi/include/allocator.h"
32 #include "osi/include/osi.h"
33 #include "osi/include/reactor.h"
34
35 using namespace bluetooth;
36
37 // The IPv4 loopback address: 127.0.0.1
38 static const in_addr_t LOCALHOST_ = 0x7f000001;
39
40 struct socket_t {
41 int fd;
42 reactor_object_t* reactor_object;
43 socket_cb read_ready;
44 socket_cb write_ready;
45 void* context; // Not owned, do not free.
46 };
47
48 static void internal_read_ready(void* context);
49 static void internal_write_ready(void* context);
50
socket_new(void)51 socket_t* socket_new(void) {
52 socket_t* ret = (socket_t*)osi_calloc(sizeof(socket_t));
53 int enable = 1;
54
55 ret->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
56 if (ret->fd == INVALID_FD) {
57 log::error("unable to create socket: {}", strerror(errno));
58 goto error;
59 }
60
61 if (setsockopt(ret->fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) {
62 log::error("unable to set SO_REUSEADDR: {}", strerror(errno));
63 goto error;
64 }
65
66 return ret;
67
68 error:;
69 if (ret) {
70 close(ret->fd);
71 }
72 osi_free(ret);
73 return NULL;
74 }
75
socket_new_from_fd(int fd)76 socket_t* socket_new_from_fd(int fd) {
77 log::assert_that(fd != INVALID_FD, "assert failed: fd != INVALID_FD");
78
79 socket_t* ret = (socket_t*)osi_calloc(sizeof(socket_t));
80
81 ret->fd = fd;
82 return ret;
83 }
84
socket_free(socket_t * socket)85 void socket_free(socket_t* socket) {
86 if (!socket) {
87 return;
88 }
89
90 socket_unregister(socket);
91 close(socket->fd);
92 osi_free(socket);
93 }
94
socket_listen(const socket_t * socket,port_t port)95 bool socket_listen(const socket_t* socket, port_t port) {
96 log::assert_that(socket != NULL, "assert failed: socket != NULL");
97
98 struct sockaddr_in addr;
99 addr.sin_family = AF_INET;
100 addr.sin_addr.s_addr = htonl(LOCALHOST_);
101 addr.sin_port = htons(port);
102 if (bind(socket->fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
103 log::error("unable to bind socket to port {}: {}", port, strerror(errno));
104 return false;
105 }
106
107 if (listen(socket->fd, 10) == -1) {
108 log::error("unable to listen on port {}: {}", port, strerror(errno));
109 return false;
110 }
111
112 return true;
113 }
114
socket_accept(const socket_t * socket)115 socket_t* socket_accept(const socket_t* socket) {
116 log::assert_that(socket != NULL, "assert failed: socket != NULL");
117
118 int fd;
119 OSI_NO_INTR(fd = accept(socket->fd, NULL, NULL));
120 if (fd == INVALID_FD) {
121 log::error("unable to accept socket: {}", strerror(errno));
122 return NULL;
123 }
124
125 socket_t* ret = (socket_t*)osi_calloc(sizeof(socket_t));
126
127 ret->fd = fd;
128 return ret;
129 }
130
socket_read(const socket_t * socket,void * buf,size_t count)131 ssize_t socket_read(const socket_t* socket, void* buf, size_t count) {
132 log::assert_that(socket != NULL, "assert failed: socket != NULL");
133 log::assert_that(buf != NULL, "assert failed: buf != NULL");
134
135 ssize_t ret;
136 OSI_NO_INTR(ret = recv(socket->fd, buf, count, MSG_DONTWAIT));
137
138 return ret;
139 }
140
socket_write(const socket_t * socket,const void * buf,size_t count)141 ssize_t socket_write(const socket_t* socket, const void* buf, size_t count) {
142 log::assert_that(socket != NULL, "assert failed: socket != NULL");
143 log::assert_that(buf != NULL, "assert failed: buf != NULL");
144
145 ssize_t ret;
146 OSI_NO_INTR(ret = send(socket->fd, buf, count, MSG_DONTWAIT));
147
148 return ret;
149 }
150
socket_write_and_transfer_fd(const socket_t * socket,const void * buf,size_t count,int fd)151 ssize_t socket_write_and_transfer_fd(const socket_t* socket, const void* buf, size_t count,
152 int fd) {
153 log::assert_that(socket != NULL, "assert failed: socket != NULL");
154 log::assert_that(buf != NULL, "assert failed: buf != NULL");
155
156 if (fd == INVALID_FD) {
157 return socket_write(socket, buf, count);
158 }
159
160 struct msghdr msg;
161 struct iovec iov;
162 char control_buf[CMSG_SPACE(sizeof(int))];
163
164 iov.iov_base = (void*)buf;
165 iov.iov_len = count;
166
167 msg.msg_iov = &iov;
168 msg.msg_iovlen = 1;
169 msg.msg_control = control_buf;
170 msg.msg_controllen = sizeof(control_buf);
171 msg.msg_name = NULL;
172 msg.msg_namelen = 0;
173
174 struct cmsghdr* header = CMSG_FIRSTHDR(&msg);
175 header->cmsg_level = SOL_SOCKET;
176 header->cmsg_type = SCM_RIGHTS;
177 header->cmsg_len = CMSG_LEN(sizeof(int));
178 *(int*)CMSG_DATA(header) = fd;
179
180 ssize_t ret;
181 OSI_NO_INTR(ret = sendmsg(socket->fd, &msg, MSG_DONTWAIT));
182
183 close(fd);
184 return ret;
185 }
186
socket_bytes_available(const socket_t * socket)187 ssize_t socket_bytes_available(const socket_t* socket) {
188 log::assert_that(socket != NULL, "assert failed: socket != NULL");
189
190 int size = 0;
191 if (ioctl(socket->fd, FIONREAD, &size) == -1) {
192 return -1;
193 }
194 return size;
195 }
196
socket_register(socket_t * socket,reactor_t * reactor,void * context,socket_cb read_cb,socket_cb write_cb)197 void socket_register(socket_t* socket, reactor_t* reactor, void* context, socket_cb read_cb,
198 socket_cb write_cb) {
199 log::assert_that(socket != NULL, "assert failed: socket != NULL");
200
201 // Make sure the socket isn't currently registered.
202 socket_unregister(socket);
203
204 socket->read_ready = read_cb;
205 socket->write_ready = write_cb;
206 socket->context = context;
207
208 void (*read_fn)(void*) = (read_cb != NULL) ? internal_read_ready : NULL;
209 void (*write_fn)(void*) = (write_cb != NULL) ? internal_write_ready : NULL;
210
211 socket->reactor_object = reactor_register(reactor, socket->fd, socket, read_fn, write_fn);
212 }
213
socket_unregister(socket_t * socket)214 void socket_unregister(socket_t* socket) {
215 log::assert_that(socket != NULL, "assert failed: socket != NULL");
216
217 if (socket->reactor_object) {
218 reactor_unregister(socket->reactor_object);
219 }
220 socket->reactor_object = NULL;
221 }
222
internal_read_ready(void * context)223 static void internal_read_ready(void* context) {
224 log::assert_that(context != NULL, "assert failed: context != NULL");
225
226 socket_t* socket = static_cast<socket_t*>(context);
227 socket->read_ready(socket, socket->context);
228 }
229
internal_write_ready(void * context)230 static void internal_write_ready(void* context) {
231 log::assert_that(context != NULL, "assert failed: context != NULL");
232
233 socket_t* socket = static_cast<socket_t*>(context);
234 socket->write_ready(socket, socket->context);
235 }
236