aboutsummaryrefslogtreecommitdiff
path: root/libjaylink/socket.c
blob: 6a9fdd7692853280218ad466e14663c3e8807cae (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * This file is part of the libjaylink project.
 *
 * Copyright (C) 2016-2017 Marc Schink <jaylink-dev@marcschink.de>
 *
 * 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 2 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/>.
 */

#include <errno.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#endif

#include "libjaylink.h"
#include "libjaylink-internal.h"

/**
 * @file
 *
 * Socket abstraction layer.
 */

/**
 * Initiate a connection on a socket.
 *
 * @param[in] sock Socket descriptor.
 * @param[in] address Address to establish the connection.
 * @param[in] length Length of the structure pointed to by @p address in bytes.
 * @param[in] timeout Connection timeout in milliseconds, 0 for no timeout.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
 * @retval JAYLINK_ERR Other error conditions.
 */
JAYLINK_PRIV int socket_connect(int sock, const struct sockaddr *address,
		size_t address_length, size_t timeout)
{
	int ret;
	fd_set fds;
	struct timeval tv;
	int socket_error;
	size_t option_length;

	if (!address)
		return JAYLINK_ERR_ARG;

	if (!socket_set_blocking(sock, false))
		return JAYLINK_ERR;
#ifdef _WIN32
	ret = connect(sock, address, address_length);

	if (ret != 0 && WSAGetLastError() != WSAEWOULDBLOCK)
		return JAYLINK_ERR;
#else
	errno = 0;
	ret = connect(sock, address, address_length);

	if (ret != 0 && errno != EINPROGRESS)
		return JAYLINK_ERR;
#endif
	if (!ret)
		return JAYLINK_OK;

	FD_ZERO(&fds);
	FD_SET(sock, &fds);

	tv.tv_sec = timeout / 1000;
	tv.tv_usec = (timeout % 1000) * 1000;

	ret = select(sock + 1, NULL, &fds, NULL, &tv);

	socket_set_blocking(sock, true);

	if (!ret)
		return JAYLINK_ERR_TIMEOUT;

	option_length = sizeof(socket_error);

	if (!socket_get_option(sock, SOL_SOCKET, SO_ERROR, &socket_error,
			&option_length))
		return JAYLINK_ERR;

	if (!socket_error)
		return JAYLINK_OK;

	return JAYLINK_ERR;
}

/**
 * Close a socket.
 *
 * @param[in] sock Socket descriptor.
 *
 * @return Whether the socket was successfully closed.
 */
JAYLINK_PRIV bool socket_close(int sock)
{
	int ret;

#ifdef _WIN32
	ret = closesocket(sock);
#else
	ret = close(sock);
#endif

	if (!ret)
		return true;

	return false;
}

/**
 * Bind an address to a socket.
 *
 * @param[in] sock Socket descriptor.
 * @param[in] address Address to be bound to the socket.
 * @param[in] length Length of the structure pointed to by @p address in bytes.
 *
 * @return Whether the address was successfully assigned to the socket.
 */
JAYLINK_PRIV bool socket_bind(int sock, const struct sockaddr *address,
		size_t length)
{
	int ret;

	ret = bind(sock, address, length);

#ifdef _WIN32
	if (ret == SOCKET_ERROR)
		return false;
#else
	if (ret < 0)
		return false;
#endif

	return true;
}

/**
 * Send a message on a socket.
 *
 * @param[in] sock Socket descriptor.
 * @param[in] buffer Buffer of the message to be sent.
 * @param[in,out] length Length of the message in bytes. On success, the value
 *                       gets updated with the actual number of bytes sent. The
 *                       value is undefined on failure.
 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
 *                  specify multiple flags.
 *
 * @return Whether the message was sent successfully.
 */
JAYLINK_PRIV bool socket_send(int sock, const void *buffer, size_t *length,
		int flags)
{
	ssize_t ret;

	ret = send(sock, buffer, *length, flags);
#ifdef _WIN32
	if (ret == SOCKET_ERROR)
		return false;
#else
	if (ret < 0)
		return false;
#endif
	*length = ret;

	return true;
}

/**
 * Receive a message from a socket.
 *
 * @param[in] sock Socket descriptor.
 * @param[out] buffer Buffer to store the received message on success. Its
 *                    content is undefined on failure.
 * @param[in,out] length Maximum length of the message in bytes. On success,
 *                       the value gets updated with the actual number of
 *                       received bytes. The value is undefined on failure.
 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
 *                  specify multiple flags.
 *
 * @return Whether a message was successfully received.
 */
JAYLINK_PRIV bool socket_recv(int sock, void *buffer, size_t *length,
		int flags)
{
	ssize_t ret;

	ret = recv(sock, buffer, *length, flags);

#ifdef _WIN32
	if (ret == SOCKET_ERROR)
		return false;
#else
	if (ret < 0)
		return false;
#endif

	*length = ret;

	return true;
}

/**
 * Send a message on a socket.
 *
 * @param[in] sock Socket descriptor.
 * @param[in] buffer Buffer to send message from.
 * @param[in,out] length Number of bytes to send. On success, the value gets
 *                       updated with the actual number of bytes sent. The
 *                       value is undefined on failure.
 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
 *                  specify multiple flags.
 * @param[in] address Destination address of the message.
 * @param[in] address_length Length of the structure pointed to by @p address
 *                           in bytes.
 *
 * @return Whether the message was successfully sent.
 */
JAYLINK_PRIV bool socket_sendto(int sock, const void *buffer, size_t *length,
		int flags, const struct sockaddr *address,
		size_t address_length)
{
	ssize_t ret;

	ret = sendto(sock, buffer, *length, flags, address, address_length);

#ifdef _WIN32
	if (ret == SOCKET_ERROR)
		return false;
#else
	if (ret < 0)
		return false;
#endif

	*length = ret;

	return true;
}

/**
 * Receive a message from a socket.
 *
 * @param[in] sock Socket descriptor.
 * @param[out] buffer Buffer to store the received message on success. Its
 *                    content is undefined on failure.
 * @param[in,out] length Maximum length of the message in bytes. On success,
 *                       the value gets updated with the actual number of
 *                       received bytes. The value is undefined on failure.
 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
 *                  specify multiple flags.
 * @param[out] address Structure to store the source address of the message on
 *                     success. Its content is undefined on failure.
 *                     Can be NULL.
 * @param[in,out] address_length Length of the structure pointed to by
 *                               @p address in bytes. On success, the value
 *                               gets updated with the actual length of the
 *                               structure. The value is undefined on failure.
 *                               Should be NULL if @p address is NULL.
 *
 * @return Whether a message was successfully received.
 */
JAYLINK_PRIV bool socket_recvfrom(int sock, void *buffer, size_t *length,
		int flags, struct sockaddr *address, size_t *address_length)
{
	ssize_t ret;
#ifdef _WIN32
	int tmp;

	tmp = *address_length;
	ret = recvfrom(sock, buffer, *length, flags, address, &tmp);

	if (ret == SOCKET_ERROR)
		return false;
#else
	socklen_t tmp;

	tmp = *address_length;
	ret = recvfrom(sock, buffer, *length, flags, address, &tmp);

	if (ret < 0)
		return false;
#endif

	*address_length = tmp;
	*length = ret;

	return true;
}

/**
 * Get the value of a socket option.
 *
 * @param[in] sock Socket descriptor.
 * @param[in] level Level at which the option is defined.
 * @param[in] option Option to get the value for.
 * @param[in] value Buffer to store the value.
 * @param[in] length Length of the value buffer in bytes.
 *
 * @return Whether the option value was retrieved successfully.
 */
JAYLINK_PRIV bool socket_get_option(int sock, int level, int option,
		void *value, size_t *length)
{
	if (!getsockopt(sock, level, option, value, (socklen_t *)length))
		return true;

	return false;
}

/**
 * Set the value of a socket option.
 *
 * @param[in] sock Socket descriptor.
 * @param[in] level Level at which the option is defined.
 * @param[in] option Option to set the value for.
 * @param[in] value Buffer of the value to be set.
 * @param[in] length Length of the value buffer in bytes.
 *
 * @return Whether the option value was set successfully.
 */
JAYLINK_PRIV bool socket_set_option(int sock, int level, int option,
		const void *value, size_t length)
{
	if (!setsockopt(sock, level, option, value, length))
		return true;

	return false;
}

/**
 * Set the blocking mode of a socket.
 *
 * @param[in] sock Socket descriptor.
 * @param[in] blocking Blocking mode.
 *
 * @return Whether the blocking mode was set successfully.
 */
JAYLINK_PRIV bool socket_set_blocking(int sock, bool blocking)
{
	int ret;
#ifdef _WIN32
	u_long mode;

	mode = !blocking;
	ret = ioctlsocket(sock, FIONBIO, &mode);

	if (ret != NO_ERROR)
		return false;
#else
	int flags;

	flags = fcntl(sock, F_GETFL, 0);

	if (flags < 0)
		return false;

	if (blocking)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	ret = fcntl(sock, F_SETFL, flags);

	if (ret != 0)
		return false;
#endif
	return true;
}