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