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