1c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati/****************************************************************************** 2c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * 3c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * Copyright (C) 2014 Google, Inc. 4c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * 5c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * Licensed under the Apache License, Version 2.0 (the "License"); 6c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * you may not use this file except in compliance with the License. 7c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * You may obtain a copy of the License at: 8c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * 9c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * http://www.apache.org/licenses/LICENSE-2.0 10c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * 11c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * Unless required by applicable law or agreed to in writing, software 12c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * distributed under the License is distributed on an "AS IS" BASIS, 13c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * See the License for the specific language governing permissions and 15c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * limitations under the License. 16c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati * 17c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati ******************************************************************************/ 18c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 19c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#define LOG_TAG "bt_osi_socket" 20c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 2198bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati#include <asm/ioctls.h> 22c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#include <assert.h> 23c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#include <errno.h> 24c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#include <netinet/in.h> 25c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#include <string.h> 2698bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati#include <sys/ioctl.h> 27c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#include <sys/socket.h> 28c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#include <sys/types.h> 29c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati#include <unistd.h> 30c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 310f9b91e150e153229235c163861198e23600e636Sharvil Nanavati#include "osi/include/allocator.h" 320f9b91e150e153229235c163861198e23600e636Sharvil Nanavati#include "osi/include/osi.h" 3344802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati#include "osi/include/log.h" 340f9b91e150e153229235c163861198e23600e636Sharvil Nanavati#include "osi/include/reactor.h" 350f9b91e150e153229235c163861198e23600e636Sharvil Nanavati#include "osi/include/socket.h" 36c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 37d2199cbb8f361772819402b56e6fa46587a31c56Pavlin Radoslavov// The IPv4 loopback address: 127.0.0.1 38d2199cbb8f361772819402b56e6fa46587a31c56Pavlin Radoslavovstatic const in_addr_t LOCALHOST_ = 0x7f000001; 39d2199cbb8f361772819402b56e6fa46587a31c56Pavlin Radoslavov 40c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatistruct socket_t { 41fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati int fd; 42fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati reactor_object_t *reactor_object; 43c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket_cb read_ready; 44c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket_cb write_ready; 45fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati void *context; // Not owned, do not free. 46c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati}; 47c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 48c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatistatic void internal_read_ready(void *context); 49c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatistatic void internal_write_ready(void *context); 50c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 51c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatisocket_t *socket_new(void) { 52384f8a948e9837475c4f504a626205334522df2aZach Johnson socket_t *ret = (socket_t *)osi_calloc(sizeof(socket_t)); 53c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati if (!ret) { 5444802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to allocate memory for socket.", __func__); 55c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati goto error; 56c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati } 57c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 58fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati ret->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 59fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati if (ret->fd == INVALID_FD) { 6044802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to create socket: %s", __func__, strerror(errno)); 61c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati goto error; 62c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati } 63c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 64c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati int enable = 1; 65fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati if (setsockopt(ret->fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) { 6644802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to set SO_REUSEADDR: %s", __func__, strerror(errno)); 67c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati goto error; 68c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati } 69c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 70c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return ret; 71c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 72c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatierror:; 73c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati if (ret) 74fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati close(ret->fd); 75384f8a948e9837475c4f504a626205334522df2aZach Johnson osi_free(ret); 76c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return NULL; 77c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 78c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 79ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavatisocket_t *socket_new_from_fd(int fd) { 80ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati assert(fd != INVALID_FD); 81ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 82384f8a948e9837475c4f504a626205334522df2aZach Johnson socket_t *ret = (socket_t *)osi_calloc(sizeof(socket_t)); 83ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati if (!ret) { 8444802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to allocate memory for socket.", __func__); 85ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati return NULL; 86ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati } 87ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 88ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati ret->fd = fd; 89ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati return ret; 90ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati} 91ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 92c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavativoid socket_free(socket_t *socket) { 93c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati if (!socket) 94c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return; 95c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 96c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket_unregister(socket); 97fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati close(socket->fd); 98384f8a948e9837475c4f504a626205334522df2aZach Johnson osi_free(socket); 99c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 100c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 101c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatibool socket_listen(const socket_t *socket, port_t port) { 102c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(socket != NULL); 103c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 104c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati struct sockaddr_in addr; 105c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati addr.sin_family = AF_INET; 106d2199cbb8f361772819402b56e6fa46587a31c56Pavlin Radoslavov addr.sin_addr.s_addr = htonl(LOCALHOST_); 107c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati addr.sin_port = htons(port); 108fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati if (bind(socket->fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 10944802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to bind socket to port %u: %s", __func__, port, strerror(errno)); 110c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return false; 111c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati } 112c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 113fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati if (listen(socket->fd, 10) == -1) { 11444802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to listen on port %u: %s", __func__, port, strerror(errno)); 115c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return false; 116c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati } 117c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 118c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return true; 119c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 120c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 121c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatisocket_t *socket_accept(const socket_t *socket) { 122c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(socket != NULL); 123c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 124fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati int fd = accept(socket->fd, NULL, NULL); 125fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati if (fd == INVALID_FD) { 12644802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to accept socket: %s", __func__, strerror(errno)); 127c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return NULL; 128c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati } 129c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 130384f8a948e9837475c4f504a626205334522df2aZach Johnson socket_t *ret = (socket_t *)osi_calloc(sizeof(socket_t)); 131c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati if (!ret) { 132c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati close(fd); 13344802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati LOG_ERROR("%s unable to allocate memory for socket.", __func__); 134c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return NULL; 135c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati } 136c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 137fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati ret->fd = fd; 138c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati return ret; 139c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 140c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 141c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatissize_t socket_read(const socket_t *socket, void *buf, size_t count) { 142c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(socket != NULL); 143c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(buf != NULL); 144c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 145fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati return recv(socket->fd, buf, count, MSG_DONTWAIT); 146c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 147c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 148c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatissize_t socket_write(const socket_t *socket, const void *buf, size_t count) { 149c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(socket != NULL); 150c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(buf != NULL); 151c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 152fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati return send(socket->fd, buf, count, MSG_DONTWAIT); 153c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 154c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 155ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavatissize_t socket_write_and_transfer_fd(const socket_t *socket, const void *buf, size_t count, int fd) { 156ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati assert(socket != NULL); 157ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati assert(buf != NULL); 158ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 159ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati if (fd == INVALID_FD) 160ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati return socket_write(socket, buf, count); 161ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 162ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati struct msghdr msg; 163ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati struct iovec iov; 164ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati char control_buf[CMSG_SPACE(sizeof(int))]; 165ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 166ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati iov.iov_base = (void *)buf; 167ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati iov.iov_len = count; 168ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 169ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati msg.msg_iov = &iov; 170ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati msg.msg_iovlen = 1; 171ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati msg.msg_control = control_buf; 172ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati msg.msg_controllen = sizeof(control_buf); 173ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati msg.msg_name = NULL; 174ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati msg.msg_namelen = 0; 175ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 176ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati struct cmsghdr *header = CMSG_FIRSTHDR(&msg); 177ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati header->cmsg_level = SOL_SOCKET; 178ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati header->cmsg_type = SCM_RIGHTS; 179ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati header->cmsg_len = CMSG_LEN(sizeof(int)); 180ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati *(int *)CMSG_DATA(header) = fd; 181ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 182ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati ssize_t ret = sendmsg(socket->fd, &msg, MSG_DONTWAIT); 183ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati close(fd); 184ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati return ret; 185ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati} 186ad3067b83e4d309b4027a4e9b44b0a614f267da8Sharvil Nanavati 18798bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavatissize_t socket_bytes_available(const socket_t *socket) { 18898bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati assert(socket != NULL); 18998bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati 19098bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati int size = 0; 19198bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati if (ioctl(socket->fd, FIONREAD, &size) == -1) 19298bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati return -1; 19398bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati return size; 19498bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati} 19598bf85fea392258ba22a252eeeed1153dc3eb16fSharvil Nanavati 196fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavativoid socket_register(socket_t *socket, reactor_t *reactor, void *context, socket_cb read_cb, socket_cb write_cb) { 197c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(socket != NULL); 198c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 199c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati // Make sure the socket isn't currently registered. 200c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket_unregister(socket); 201c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 202c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket->read_ready = read_cb; 203c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket->write_ready = write_cb; 204c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket->context = context; 205c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 206fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati void (*read_fn)(void *) = (read_cb != NULL) ? internal_read_ready : NULL; 207fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati void (*write_fn)(void *) = (write_cb != NULL) ? internal_write_ready : NULL; 208fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati 209fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati socket->reactor_object = reactor_register(reactor, socket->fd, socket, read_fn, write_fn); 210c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 211c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 212c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavativoid socket_unregister(socket_t *socket) { 213c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(socket != NULL); 214c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 215fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati if (socket->reactor_object) 216fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati reactor_unregister(socket->reactor_object); 217fbf89085bf308a98b00da77d1538539f6dd58604Sharvil Nanavati socket->reactor_object = NULL; 218c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 219c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 220c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatistatic void internal_read_ready(void *context) { 221c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(context != NULL); 222c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 223c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket_t *socket = (void *)context; 224c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket->read_ready(socket, socket->context); 225c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 226c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 227c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavatistatic void internal_write_ready(void *context) { 228c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati assert(context != NULL); 229c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati 230c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket_t *socket = (void *)context; 231c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati socket->write_ready(socket, socket->context); 232c2031c4fbab9ed2d2e51a0b287a3fab805932ca4Sharvil Nanavati} 233