• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "check.h"
33 #include "osi/include/allocator.h"
34 #include "osi/include/log.h"
35 #include "osi/include/osi.h"
36 #include "osi/include/reactor.h"
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("%s unable to create socket: %s", __func__, strerror(errno));
59     goto error;
60   }
61 
62   if (setsockopt(ret->fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) ==
63       -1) {
64     LOG_ERROR("%s unable to set SO_REUSEADDR: %s", __func__, 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   CHECK(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   CHECK(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("%s unable to bind socket to port %u: %s", __func__, port,
102               strerror(errno));
103     return false;
104   }
105 
106   if (listen(socket->fd, 10) == -1) {
107     LOG_ERROR("%s unable to listen on port %u: %s", __func__, port,
108               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   CHECK(socket != NULL);
117 
118   int fd;
119   OSI_NO_INTR(fd = accept(socket->fd, NULL, NULL));
120   if (fd == INVALID_FD) {
121     LOG_ERROR("%s unable to accept socket: %s", __func__, 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   CHECK(socket != NULL);
133   CHECK(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   CHECK(socket != NULL);
143   CHECK(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,
152                                      size_t count, int fd) {
153   CHECK(socket != NULL);
154   CHECK(buf != NULL);
155 
156   if (fd == INVALID_FD) return socket_write(socket, buf, count);
157 
158   struct msghdr msg;
159   struct iovec iov;
160   char control_buf[CMSG_SPACE(sizeof(int))];
161 
162   iov.iov_base = (void*)buf;
163   iov.iov_len = count;
164 
165   msg.msg_iov = &iov;
166   msg.msg_iovlen = 1;
167   msg.msg_control = control_buf;
168   msg.msg_controllen = sizeof(control_buf);
169   msg.msg_name = NULL;
170   msg.msg_namelen = 0;
171 
172   struct cmsghdr* header = CMSG_FIRSTHDR(&msg);
173   header->cmsg_level = SOL_SOCKET;
174   header->cmsg_type = SCM_RIGHTS;
175   header->cmsg_len = CMSG_LEN(sizeof(int));
176   *(int*)CMSG_DATA(header) = fd;
177 
178   ssize_t ret;
179   OSI_NO_INTR(ret = sendmsg(socket->fd, &msg, MSG_DONTWAIT));
180 
181   close(fd);
182   return ret;
183 }
184 
socket_bytes_available(const socket_t * socket)185 ssize_t socket_bytes_available(const socket_t* socket) {
186   CHECK(socket != NULL);
187 
188   int size = 0;
189   if (ioctl(socket->fd, FIONREAD, &size) == -1) return -1;
190   return size;
191 }
192 
socket_register(socket_t * socket,reactor_t * reactor,void * context,socket_cb read_cb,socket_cb write_cb)193 void socket_register(socket_t* socket, reactor_t* reactor, void* context,
194                      socket_cb read_cb, socket_cb write_cb) {
195   CHECK(socket != NULL);
196 
197   // Make sure the socket isn't currently registered.
198   socket_unregister(socket);
199 
200   socket->read_ready = read_cb;
201   socket->write_ready = write_cb;
202   socket->context = context;
203 
204   void (*read_fn)(void*) = (read_cb != NULL) ? internal_read_ready : NULL;
205   void (*write_fn)(void*) = (write_cb != NULL) ? internal_write_ready : NULL;
206 
207   socket->reactor_object =
208       reactor_register(reactor, socket->fd, socket, read_fn, write_fn);
209 }
210 
socket_unregister(socket_t * socket)211 void socket_unregister(socket_t* socket) {
212   CHECK(socket != NULL);
213 
214   if (socket->reactor_object) reactor_unregister(socket->reactor_object);
215   socket->reactor_object = NULL;
216 }
217 
internal_read_ready(void * context)218 static void internal_read_ready(void* context) {
219   CHECK(context != NULL);
220 
221   socket_t* socket = static_cast<socket_t*>(context);
222   socket->read_ready(socket, socket->context);
223 }
224 
internal_write_ready(void * context)225 static void internal_write_ready(void* context) {
226   CHECK(context != NULL);
227 
228   socket_t* socket = static_cast<socket_t*>(context);
229   socket->write_ready(socket, socket->context);
230 }
231