aboutsummaryrefslogtreecommitdiff
path: root/libjaylink/socket.c
blob: f2a6588d308ac1330192758f12cdb442e635d422 (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
/*
 * 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/>.
 */

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

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

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

/**
 * 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;
}

/**
 * Set an option on a socket.
 *
 * @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 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;
}