blob: e5d4c8e1f90c3cb863d4ca374428be4ce88702b3 [file] [log] [blame]
Sharvil Nanavatic2031c42014-07-25 15:50:17 -07001/******************************************************************************
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#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
Jakub Pawlowski713993d2016-04-21 13:16:45 -070026#ifdef __cplusplus
27extern "C" {
28#endif
29
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070030typedef struct reactor_t reactor_t;
31typedef struct socket_t socket_t;
32typedef uint16_t port_t;
33
Myles Watsonb55040c2016-10-19 13:15:34 -070034typedef void (*socket_cb)(socket_t* socket, void* context);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070035
Myles Watsonb55040c2016-10-19 13:15:34 -070036// Returns a new socket object. The socket is in an idle, disconnected state
37// when
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070038// it is returned by this function. The returned object must be freed by calling
39// |socket_free|. Returns NULL on failure.
Myles Watsonb55040c2016-10-19 13:15:34 -070040socket_t* socket_new(void);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070041
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070042// Returns a new socket object backed by |fd|. The socket object is in whichever
Myles Watsonb55040c2016-10-19 13:15:34 -070043// state |fd| is in when it was passed to this function. The returned object
44// must
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070045// be freed by calling |socket_free|. Returns NULL on failure. If this function
Myles Watsonb55040c2016-10-19 13:15:34 -070046// is successful, ownership of |fd| is transferred and the caller must not close
47// it.
48socket_t* socket_new_from_fd(int fd);
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070049
Myles Watsonb55040c2016-10-19 13:15:34 -070050// Frees a socket object created by |socket_new| or |socket_accept|. |socket|
51// may
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070052// be NULL. If the socket was connected, it will be disconnected.
Myles Watsonb55040c2016-10-19 13:15:34 -070053void socket_free(socket_t* socket);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070054
55// Puts |socket| in listening mode for incoming TCP connections on the specified
Pavlin Radoslavovd2199cb2015-08-17 18:54:22 -070056// |port| and the loopback IPv4 address. Returns true on success, false on
57// failure (e.g. |port| is bound by another socket). |socket| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070058bool socket_listen(const socket_t* socket, port_t port);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070059
Myles Watsonb55040c2016-10-19 13:15:34 -070060// Blocks on a listening socket, |socket|, until a client connects to it.
61// Returns
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070062// a connected socket on success, NULL on failure. The returned object must be
63// freed by calling |socket_free|. |socket| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070064socket_t* socket_accept(const socket_t* socket);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070065
66// Reads up to |count| bytes from |socket| into |buf|. This function will not
67// block. This function returns a positive integer representing the number
68// of bytes copied into |buf| on success, 0 if the socket has disconnected,
69// and -1 on error. This function may return a value less than |count| if not
Pavlin Radoslavovd6121a32016-05-12 11:36:44 -070070// enough data is currently available. If this function returns -1, errno will
71// also be set (see recv(2) for possible errno values). However, if the reading
72// system call was interrupted with errno of EINTR, the read operation is
73// restarted internally without propagating EINTR back to the caller. If there
74// were no bytes available to be read, this function returns -1 and sets errno
75// to EWOULDBLOCK. Neither |socket| nor |buf| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070076ssize_t socket_read(const socket_t* socket, void* buf, size_t count);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070077
78// Writes up to |count| bytes from |buf| into |socket|. This function will not
79// block. Returns a positive integer representing the number of bytes written
Pavlin Radoslavovd6121a32016-05-12 11:36:44 -070080// to |socket| on success, 0 if the socket has disconnected, and -1 on error.
81// This function may return a value less than |count| if writing more bytes
82// would result in blocking. If this function returns -1, errno will also be
83// set (see send(2) for possible errno values). However, if the writing system
84// call was interrupted with errno of EINTR, the write operation is restarted
85// internally without propagating EINTR back to the caller. If no bytes could
86// be written without blocking, this function will return -1 and set errno to
87// EWOULDBLOCK. Neither |socket| nor |buf| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070088ssize_t socket_write(const socket_t* socket, const void* buf, size_t count);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070089
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070090// This function performs the same write operation as |socket_write| and also
91// sends the file descriptor |fd| over the socket to a remote process. Ownership
Myles Watsonb55040c2016-10-19 13:15:34 -070092// of |fd| transfers to this function and the descriptor must not be used any
93// longer.
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070094// If |fd| is INVALID_FD, this function behaves the same as |socket_write|.
Myles Watsonb55040c2016-10-19 13:15:34 -070095ssize_t socket_write_and_transfer_fd(const socket_t* socket, const void* buf,
96 size_t count, int fd);
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070097
Myles Watsonb55040c2016-10-19 13:15:34 -070098// Returns the number of bytes that can be read from |socket| without blocking.
99// On error,
Sharvil Nanavati98bf85f2014-08-27 19:05:40 -0700100// this function returns -1. |socket| may not be NULL.
101//
Myles Watsonb55040c2016-10-19 13:15:34 -0700102// Note: this function should not be part of the socket interface. It is only
103// provided as
104// a stop-gap until we can refactor away code that depends on a priori
105// knowledge of
106// the byte count. Do not use this function unless you need it while
107// refactoring
Sharvil Nanavati98bf85f2014-08-27 19:05:40 -0700108// legacy bluedroid code.
Myles Watsonb55040c2016-10-19 13:15:34 -0700109ssize_t socket_bytes_available(const socket_t* socket);
Sharvil Nanavati98bf85f2014-08-27 19:05:40 -0700110
Myles Watsonb55040c2016-10-19 13:15:34 -0700111// Registers |socket| with the |reactor|. When the socket becomes readable,
112// |read_cb|
113// will be called. When the socket becomes writeable, |write_cb| will be called.
114// The
115// |context| parameter is passed, untouched, to each of the callback routines.
116// Neither
117// |socket| nor |reactor| may be NULL. |read_cb|, |write_cb|, and |context| may
118// be NULL.
119void socket_register(socket_t* socket, reactor_t* reactor, void* context,
120 socket_cb read_cb, socket_cb write_cb);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -0700121
Myles Watsonb55040c2016-10-19 13:15:34 -0700122// Unregisters |socket| from whichever reactor it is registered with, if any.
123// This
Sharvil Nanavatic2031c42014-07-25 15:50:17 -0700124// function is idempotent.
Myles Watsonb55040c2016-10-19 13:15:34 -0700125void socket_unregister(socket_t* socket);
Jakub Pawlowski713993d2016-04-21 13:16:45 -0700126
127#ifdef __cplusplus
128}
Marie Janssend19e0782016-07-15 12:48:27 -0700129#endif