blob: aadfd5bc3bd06ad707562e611b217c031fdcd099 [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
21#include <stddef.h>
22#include <stdint.h>
23
24typedef struct reactor_t reactor_t;
25typedef struct socket_t socket_t;
26typedef uint16_t port_t;
27
28typedef void (*socket_cb)(socket_t *socket, void *context);
29
30// Returns a new socket object. The socket is in an idle, disconnected state when
31// it is returned by this function. The returned object must be freed by calling
32// |socket_free|. Returns NULL on failure.
33socket_t *socket_new(void);
34
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070035// Returns a new socket object backed by |fd|. The socket object is in whichever
36// state |fd| is in when it was passed to this function. The returned object must
37// be freed by calling |socket_free|. Returns NULL on failure. If this function
38// is successful, ownership of |fd| is transferred and the caller must not close it.
39socket_t *socket_new_from_fd(int fd);
40
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070041// Frees a socket object created by |socket_new| or |socket_accept|. |socket| may
42// be NULL. If the socket was connected, it will be disconnected.
43void socket_free(socket_t *socket);
44
45// Puts |socket| in listening mode for incoming TCP connections on the specified
46// |port|. Returns true on success, false on failure (e.g. |port| is bound by
47// another socket). |socket| may not be NULL.
48bool socket_listen(const socket_t *socket, port_t port);
49
50// Blocks on a listening socket, |socket|, until a client connects to it. Returns
51// a connected socket on success, NULL on failure. The returned object must be
52// freed by calling |socket_free|. |socket| may not be NULL.
53socket_t *socket_accept(const socket_t *socket);
54
55// Reads up to |count| bytes from |socket| into |buf|. This function will not
56// block. This function returns a positive integer representing the number
57// of bytes copied into |buf| on success, 0 if the socket has disconnected,
58// and -1 on error. This function may return a value less than |count| if not
59// enough data is currently available. If this function returns -1, errno will also
60// be set (see recv(2) for possible errno values). If there were no bytes available
61// to be read, this function returns -1 and sets errno to EWOULDBLOCK. Neither
62// |socket| nor |buf| may be NULL.
63ssize_t socket_read(const socket_t *socket, void *buf, size_t count);
64
65// Writes up to |count| bytes from |buf| into |socket|. This function will not
66// block. Returns a positive integer representing the number of bytes written
67// to |socket| on success, 0 if the socket has disconnected, and -1 on error. This
68// function may return a value less than |count| if writing more bytes would result
69// in blocking. If this function returns -1, errno will also be set (see send(2) for
70// possible errno values). If no bytes could be written without blocking, this
71// function will return -1 and set errno to EWOULDBLOCK. Neither |socket| nor |buf|
72// may be NULL.
73ssize_t socket_write(const socket_t *socket, const void *buf, size_t count);
74
Sharvil Nanavatiad3067b2014-08-21 18:13:46 -070075// This function performs the same write operation as |socket_write| and also
76// sends the file descriptor |fd| over the socket to a remote process. Ownership
77// of |fd| transfers to this function and the descriptor must not be used any longer.
78// If |fd| is INVALID_FD, this function behaves the same as |socket_write|.
79ssize_t socket_write_and_transfer_fd(const socket_t *socket, const void *buf, size_t count, int fd);
80
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070081// Registers |socket| with the |reactor|. When the socket becomes readable, |read_cb|
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070082// will be called. When the socket becomes writeable, |write_cb| will be called. The
83// |context| parameter is passed, untouched, to each of the callback routines. Neither
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070084// |socket| nor |reactor| may be NULL. |read_cb| or |write_cb|, but not both, may be NULL.
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070085// |context| may be NULL.
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070086void socket_register(socket_t *socket, reactor_t *reactor, void *context, socket_cb read_cb, socket_cb write_cb);
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070087
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070088// Unregisters |socket| from whichever reactor it is registered with, if any. This
Sharvil Nanavatic2031c42014-07-25 15:50:17 -070089// function is idempotent.
90void socket_unregister(socket_t *socket);