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 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(LOG_TAG, "%s unable to create socket: %s", __func__, 58 strerror(errno)); 59 goto error; 60 } 61 62 if (setsockopt(ret->fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == 63 -1) { 64 LOG_ERROR(LOG_TAG, "%s unable to set SO_REUSEADDR: %s", __func__, 65 strerror(errno)); 66 goto error; 67 } 68 69 return ret; 70 71 error:; 72 if (ret) close(ret->fd); 73 osi_free(ret); 74 return NULL; 75 } 76 77 socket_t* socket_new_from_fd(int fd) { 78 CHECK(fd != INVALID_FD); 79 80 socket_t* ret = (socket_t*)osi_calloc(sizeof(socket_t)); 81 82 ret->fd = fd; 83 return ret; 84 } 85 86 void socket_free(socket_t* socket) { 87 if (!socket) return; 88 89 socket_unregister(socket); 90 close(socket->fd); 91 osi_free(socket); 92 } 93 94 bool socket_listen(const socket_t* socket, port_t port) { 95 CHECK(socket != NULL); 96 97 struct sockaddr_in addr; 98 addr.sin_family = AF_INET; 99 addr.sin_addr.s_addr = htonl(LOCALHOST_); 100 addr.sin_port = htons(port); 101 if (bind(socket->fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { 102 LOG_ERROR(LOG_TAG, "%s unable to bind socket to port %u: %s", __func__, 103 port, strerror(errno)); 104 return false; 105 } 106 107 if (listen(socket->fd, 10) == -1) { 108 LOG_ERROR(LOG_TAG, "%s unable to listen on port %u: %s", __func__, port, 109 strerror(errno)); 110 return false; 111 } 112 113 return true; 114 } 115 116 socket_t* socket_accept(const socket_t* socket) { 117 CHECK(socket != NULL); 118 119 int fd; 120 OSI_NO_INTR(fd = accept(socket->fd, NULL, NULL)); 121 if (fd == INVALID_FD) { 122 LOG_ERROR(LOG_TAG, "%s unable to accept socket: %s", __func__, 123 strerror(errno)); 124 return NULL; 125 } 126 127 socket_t* ret = (socket_t*)osi_calloc(sizeof(socket_t)); 128 129 ret->fd = fd; 130 return ret; 131 } 132 133 ssize_t socket_read(const socket_t* socket, void* buf, size_t count) { 134 CHECK(socket != NULL); 135 CHECK(buf != NULL); 136 137 ssize_t ret; 138 OSI_NO_INTR(ret = recv(socket->fd, buf, count, MSG_DONTWAIT)); 139 140 return ret; 141 } 142 143 ssize_t socket_write(const socket_t* socket, const void* buf, size_t count) { 144 CHECK(socket != NULL); 145 CHECK(buf != NULL); 146 147 ssize_t ret; 148 OSI_NO_INTR(ret = send(socket->fd, buf, count, MSG_DONTWAIT)); 149 150 return ret; 151 } 152 153 ssize_t socket_write_and_transfer_fd(const socket_t* socket, const void* buf, 154 size_t count, int fd) { 155 CHECK(socket != NULL); 156 CHECK(buf != NULL); 157 158 if (fd == INVALID_FD) return socket_write(socket, buf, count); 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 187 ssize_t socket_bytes_available(const socket_t* socket) { 188 CHECK(socket != NULL); 189 190 int size = 0; 191 if (ioctl(socket->fd, FIONREAD, &size) == -1) return -1; 192 return size; 193 } 194 195 void socket_register(socket_t* socket, reactor_t* reactor, void* context, 196 socket_cb read_cb, socket_cb write_cb) { 197 CHECK(socket != NULL); 198 199 // Make sure the socket isn't currently registered. 200 socket_unregister(socket); 201 202 socket->read_ready = read_cb; 203 socket->write_ready = write_cb; 204 socket->context = context; 205 206 void (*read_fn)(void*) = (read_cb != NULL) ? internal_read_ready : NULL; 207 void (*write_fn)(void*) = (write_cb != NULL) ? internal_write_ready : NULL; 208 209 socket->reactor_object = 210 reactor_register(reactor, socket->fd, socket, read_fn, write_fn); 211 } 212 213 void socket_unregister(socket_t* socket) { 214 CHECK(socket != NULL); 215 216 if (socket->reactor_object) reactor_unregister(socket->reactor_object); 217 socket->reactor_object = NULL; 218 } 219 220 static void internal_read_ready(void* context) { 221 CHECK(context != NULL); 222 223 socket_t* socket = static_cast<socket_t*>(context); 224 socket->read_ready(socket, socket->context); 225 } 226 227 static void internal_write_ready(void* context) { 228 CHECK(context != NULL); 229 230 socket_t* socket = static_cast<socket_t*>(context); 231 socket->write_ready(socket, socket->context); 232 } 233