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