blob: 83a4f2d54882f0cf68f526122a3635fc2a2ce695 [file] [log] [blame]
Sharvil Nanavatic2031c42014-07-25 15:50:17 -07001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2014 Google, Inc.
Sharvil Nanavatic2031c42014-07-25 15:50:17 -07004 *
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#pragma once
20
Sharvil Nanavati98bf85f2014-08-27 19:05:40 -070021#include <stdbool.h>
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070022#include <stddef.h>
23#include <stdint.h>
Sharvil Nanavati98bf85f2014-08-27 19:05:40 -070024#include <sys/types.h>
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070025
26typedef struct reactor_t reactor_t;
27typedef struct socket_t socket_t;
28typedef uint16_t port_t;
29
Myles Watsonb55040c2016-10-19 13:15:34 -070030typedef void (*socket_cb)(socket_t* socket, void* context);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070031
Myles Watsonb55040c2016-10-19 13:15:34 -070032// Returns a new socket object. The socket is in an idle, disconnected state
Myles Watson9ca07092016-11-28 16:41:53 -080033// when it is returned by this function. The returned object must be freed by
34// calling |socket_free|. Returns NULL on failure.
Myles Watsonb55040c2016-10-19 13:15:34 -070035socket_t* socket_new(void);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070036
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070037// Returns a new socket object backed by |fd|. The socket object is in whichever
Myles Watsonb55040c2016-10-19 13:15:34 -070038// state |fd| is in when it was passed to this function. The returned object
Myles Watson9ca07092016-11-28 16:41:53 -080039// must be freed by calling |socket_free|. Returns NULL on failure. If this
40// function is successful, ownership of |fd| is transferred and the caller must
41// not close it.
Myles Watsonb55040c2016-10-19 13:15:34 -070042socket_t* socket_new_from_fd(int fd);
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070043
Myles Watsonb55040c2016-10-19 13:15:34 -070044// Frees a socket object created by |socket_new| or |socket_accept|. |socket|
Myles Watson9ca07092016-11-28 16:41:53 -080045// may be NULL. If the socket was connected, it will be disconnected.
Myles Watsonb55040c2016-10-19 13:15:34 -070046void socket_free(socket_t* socket);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070047
48// Puts |socket| in listening mode for incoming TCP connections on the specified
Pavlin Radoslavovd2199cb2015-08-17 18:54:22 -070049// |port| and the loopback IPv4 address. Returns true on success, false on
50// failure (e.g. |port| is bound by another socket). |socket| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070051bool socket_listen(const socket_t* socket, port_t port);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070052
Myles Watsonb55040c2016-10-19 13:15:34 -070053// Blocks on a listening socket, |socket|, until a client connects to it.
Myles Watson9ca07092016-11-28 16:41:53 -080054// Returns a connected socket on success, NULL on failure. The returned object
55// must be freed by calling |socket_free|. |socket| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070056socket_t* socket_accept(const socket_t* socket);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070057
58// Reads up to |count| bytes from |socket| into |buf|. This function will not
59// block. This function returns a positive integer representing the number
60// of bytes copied into |buf| on success, 0 if the socket has disconnected,
61// and -1 on error. This function may return a value less than |count| if not
Pavlin Radoslavovd6121a32016-05-12 11:36:44 -070062// enough data is currently available. If this function returns -1, errno will
63// also be set (see recv(2) for possible errno values). However, if the reading
64// system call was interrupted with errno of EINTR, the read operation is
65// restarted internally without propagating EINTR back to the caller. If there
66// were no bytes available to be read, this function returns -1 and sets errno
67// to EWOULDBLOCK. Neither |socket| nor |buf| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070068ssize_t socket_read(const socket_t* socket, void* buf, size_t count);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070069
70// Writes up to |count| bytes from |buf| into |socket|. This function will not
71// block. Returns a positive integer representing the number of bytes written
Pavlin Radoslavovd6121a32016-05-12 11:36:44 -070072// to |socket| on success, 0 if the socket has disconnected, and -1 on error.
73// This function may return a value less than |count| if writing more bytes
74// would result in blocking. If this function returns -1, errno will also be
75// set (see send(2) for possible errno values). However, if the writing system
76// call was interrupted with errno of EINTR, the write operation is restarted
77// internally without propagating EINTR back to the caller. If no bytes could
78// be written without blocking, this function will return -1 and set errno to
79// EWOULDBLOCK. Neither |socket| nor |buf| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070080ssize_t socket_write(const socket_t* socket, const void* buf, size_t count);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070081
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070082// This function performs the same write operation as |socket_write| and also
83// sends the file descriptor |fd| over the socket to a remote process. Ownership
Myles Watsonb55040c2016-10-19 13:15:34 -070084// of |fd| transfers to this function and the descriptor must not be used any
Myles Watson9ca07092016-11-28 16:41:53 -080085// longer. If |fd| is INVALID_FD, this function behaves the same as
86// |socket_write|.
Myles Watsonb55040c2016-10-19 13:15:34 -070087ssize_t socket_write_and_transfer_fd(const socket_t* socket, const void* buf,
88 size_t count, int fd);
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070089
Myles Watsonb55040c2016-10-19 13:15:34 -070090// Returns the number of bytes that can be read from |socket| without blocking.
Myles Watson9ca07092016-11-28 16:41:53 -080091// On error, this function returns -1. |socket| may not be NULL.
Sharvil Nanavati98bf85f2014-08-27 19:05:40 -070092//
Myles Watsonb55040c2016-10-19 13:15:34 -070093// Note: this function should not be part of the socket interface. It is only
Myles Watson9ca07092016-11-28 16:41:53 -080094// provided as a stop-gap until we can refactor away code that depends on
95// a priori knowledge of the byte count. Do not use this function unless
96// you need it while refactoring legacy bluedroid code.
Myles Watsonb55040c2016-10-19 13:15:34 -070097ssize_t socket_bytes_available(const socket_t* socket);
Sharvil Nanavati98bf85f2014-08-27 19:05:40 -070098
Myles Watsonb55040c2016-10-19 13:15:34 -070099// Registers |socket| with the |reactor|. When the socket becomes readable,
Myles Watson9ca07092016-11-28 16:41:53 -0800100// |read_cb| will be called. When the socket becomes writeable, |write_cb| will
101// be called. The |context| parameter is passed, untouched, to each of the
102// callback routines. Neither |socket| nor |reactor| may be NULL. |read_cb|,
103// |write_cb|, and |context| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -0700104void socket_register(socket_t* socket, reactor_t* reactor, void* context,
105 socket_cb read_cb, socket_cb write_cb);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -0700106
Myles Watsonb55040c2016-10-19 13:15:34 -0700107// Unregisters |socket| from whichever reactor it is registered with, if any.
Myles Watson9ca07092016-11-28 16:41:53 -0800108// This function is idempotent.
Myles Watsonb55040c2016-10-19 13:15:34 -0700109void socket_unregister(socket_t* socket);