summaryrefslogtreecommitdiffstats
path: root/src/contrib/net.h
blob: 1b6b527eeeecadcb6c558f16b4eb3481957dd2fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdbool.h>
#include <stdint.h>
#include <sys/socket.h>

/*!
 * \brief Network interface flags.
 */
enum net_flags {
	NET_BIND_NONLOCAL = (1 << 0), //!< Allow to bind unavailable address.
	NET_BIND_MULTIPLE = (1 << 1), //!< Allow to bind address multiple times.
};

/*!
 * \brief Create unbound socket of given family and type.
 *
 * \note The socket is set to non-blocking mode.
 *
 * \param type  Socket transport type (SOCK_STREAM, SOCK_DGRAM).
 * \param sa    Socket address.
 *
 * \return socket or error code
 */
int net_unbound_socket(int type, const struct sockaddr *sa);

/*!
 * \brief Create socket bound to given address.
 *
 * The socket is set to non-blocking mode.
 *
 * \param type   Socket transport type (SOCK_STREAM, SOCK_DGRAM).
 * \param sa     Socket address.
 * \param flags  Socket binding options.
 *
 * \return socket or error code
 */
int net_bound_socket(int type, const struct sockaddr *sa, enum net_flags flags);

/*!
 * \brief Create socket connected (asynchronously) to destination address.
 *
 * \note The socket is set to non-blocking mode.
 *
 * \param type      Socket transport type (SOCK_STREAM, SOCK_DGRAM).
 * \param dst_addr  Destination address.
 * \param src_addr  Source address (can be NULL).
 *
 * \return socket or error code
 */
int net_connected_socket(int type, const struct sockaddr *dst_addr,
                         const struct sockaddr *src_addr);

/*!
 * \brief Return true if the socket is fully connected.
 *
 * \param sock  Socket.
 *
 * \return true if connected
 */
bool net_is_connected(int sock);

/*!
 * \brief Get socket type (e.g. \a SOCK_STREAM).
 *
 * \param sock  Socket.
 */
int net_socktype(int sock);

/*!
 * \brief Check if socket is a SOCK_STREAM socket.
 */
bool net_is_stream(int sock);

/*!
 * \brief Accept a connection on a listening socket.
 *
 * \brief The socket is set to non-blocking mode.
 *
 * \param sock  Socket
 * \param addr  Remote address (can be NULL).
 *
 * \return socket or error code
 */
int net_accept(int sock, struct sockaddr_storage *addr);

/*!
 * \brief Send a message on a socket.
 *
 * The socket can be SOCK_STREAM or SOCK_DGRAM.
 *
 * The implementation handles partial-writes automatically.
 *
 * \param[in] sock        Socket.
 * \param[in] buffer      Message buffer.
 * \param[in] size        Size of the message.
 * \param[in] addr        Remote address (ignored for SOCK_STREAM).
 * \param[in] timeout_ms  Write timeout in miliseconds (-1 for infinity,
 *                        not valid for SOCK_DGRAM).
 *
 * \return Number of bytes sent or negative error code.
 */
ssize_t net_send(int sock, const uint8_t *buffer, size_t size,
                 const struct sockaddr *addr, int timeout_ms);

/*!
 * \brief Receive a message from a socket.
 *
 * \param[in]  sock        Socket.
 * \param[out] buffer      Receiving buffer.
 * \param[in]  size        Capacity of the receiving buffer.
 * \param[out] addr        Remote address (can be NULL).
 * \param[in]  timeout_ms  Read timeout in miliseconds (-1 for infinity).
 *
 * \return Number of bytes read or negative error code.
 */
ssize_t net_recv(int sock, uint8_t *buffer, size_t size,
                 struct sockaddr_storage *addr, int timeout_ms);

/*!
 * \brief Send a message on a SOCK_DGRAM socket.
 *
 * \see net_send
 */
ssize_t net_dgram_send(int sock, const uint8_t *buffer, size_t size,
                       const struct sockaddr *addr);

/*!
 * \brief Receive a message from a SOCK_DGRAM socket.
 *
 * \see net_recv
 */
ssize_t net_dgram_recv(int sock, uint8_t *buffer, size_t size, int timeout_ms);

/*!
 * \brief Send a message on a SOCK_STREAM socket.
 *
 * \see net_send
 */
ssize_t net_stream_send(int sock, const uint8_t *buffer, size_t size, int timeout_ms);

/*!
 * \brief Receive a message from a SOCK_STREAM socket.
 *
 * \see net_recv
 */
ssize_t net_stream_recv(int sock, uint8_t *buffer, size_t size, int timeout_ms);

/*!
 * \brief Send a DNS message on a TCP socket.
 *
 * The outgoing message is prefixed with a two-byte value carrying the DNS
 * message size according to the specification. These two bytes are not
 * reflected in the return value.
 *
 * \see net_send
 */
ssize_t net_dns_tcp_send(int sock, const uint8_t *buffer, size_t size, int timeout_ms);

/*!
 * \brief Receive a DNS message from a TCP socket.
 *
 * The first two bytes of the incoming message are interpreted as a DNS message
 * size according to the specification. These two bytes are not included in
 * the returned size. Only a complete DNS message is retrieved.
 *
 * \see net_recv
 */
ssize_t net_dns_tcp_recv(int sock, uint8_t *buffer, size_t size, int timeout_ms);