2 * This file is part of the libjaylink project.
4 * Copyright (C) 2016-2017 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
24 #include <sys/socket.h>
28 #include "libjaylink.h"
29 #include "libjaylink-internal.h"
34 * Socket abstraction layer.
40 * @param[in] sock Socket descriptor.
42 * @return Whether the socket was successfully closed.
44 JAYLINK_PRIV
bool socket_close(int sock
)
49 ret
= closesocket(sock
);
61 * Bind an address to a socket.
63 * @param[in] sock Socket descriptor.
64 * @param[in] address Address to be bound to the socket.
65 * @param[in] length Length of the structure pointed to by @p address in bytes.
67 * @return Whether the address was successfully assigned to the socket.
69 JAYLINK_PRIV
bool socket_bind(int sock
, const struct sockaddr
*address
,
74 ret
= bind(sock
, address
, length
);
77 if (ret
== SOCKET_ERROR
)
88 * Send a message on a socket.
90 * @param[in] sock Socket descriptor.
91 * @param[in] buffer Buffer of the message to be sent.
92 * @param[in,out] length Length of the message in bytes. On success, the value
93 * gets updated with the actual number of bytes sent. The
94 * value is undefined on failure.
95 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
96 * specify multiple flags.
98 * @return Whether the message was sent successfully.
100 JAYLINK_PRIV
bool socket_send(int sock
, const void *buffer
, size_t *length
,
105 ret
= send(sock
, buffer
, *length
, flags
);
107 if (ret
== SOCKET_ERROR
)
119 * Receive a message from a socket.
121 * @param[in] sock Socket descriptor.
122 * @param[out] buffer Buffer to store the received message on success. Its
123 * content is undefined on failure.
124 * @param[in,out] length Maximum length of the message in bytes. On success,
125 * the value gets updated with the actual number of
126 * received bytes. The value is undefined on failure.
127 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
128 * specify multiple flags.
130 * @return Whether a message was successfully received.
132 JAYLINK_PRIV
bool socket_recv(int sock
, void *buffer
, size_t *length
,
137 ret
= recv(sock
, buffer
, *length
, flags
);
140 if (ret
== SOCKET_ERROR
)
153 * Send a message on a socket.
155 * @param[in] sock Socket descriptor.
156 * @param[in] buffer Buffer to send message from.
157 * @param[in,out] length Number of bytes to send. On success, the value gets
158 * updated with the actual number of bytes sent. The
159 * value is undefined on failure.
160 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
161 * specify multiple flags.
162 * @param[in] address Destination address of the message.
163 * @param[in] address_length Length of the structure pointed to by @p address
166 * @return Whether the message was successfully sent.
168 JAYLINK_PRIV
bool socket_sendto(int sock
, const void *buffer
, size_t *length
,
169 int flags
, const struct sockaddr
*address
,
170 size_t address_length
)
174 ret
= sendto(sock
, buffer
, *length
, flags
, address
, address_length
);
177 if (ret
== SOCKET_ERROR
)
190 * Receive a message from a socket.
192 * @param[in] sock Socket descriptor.
193 * @param[out] buffer Buffer to store the received message on success. Its
194 * content is undefined on failure.
195 * @param[in,out] length Maximum length of the message in bytes. On success,
196 * the value gets updated with the actual number of
197 * received bytes. The value is undefined on failure.
198 * @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
199 * specify multiple flags.
200 * @param[out] address Structure to store the source address of the message on
201 * success. Its content is undefined on failure.
203 * @param[in,out] address_length Length of the structure pointed to by
204 * @p address in bytes. On success, the value
205 * gets updated with the actual length of the
206 * structure. The value is undefined on failure.
207 * Should be NULL if @p address is NULL.
209 * @return Whether a message was successfully received.
211 JAYLINK_PRIV
bool socket_recvfrom(int sock
, void *buffer
, size_t *length
,
212 int flags
, struct sockaddr
*address
, size_t *address_length
)
218 tmp
= *address_length
;
219 ret
= recvfrom(sock
, buffer
, *length
, flags
, address
, &tmp
);
221 if (ret
== SOCKET_ERROR
)
226 tmp
= *address_length
;
227 ret
= recvfrom(sock
, buffer
, *length
, flags
, address
, &tmp
);
233 *address_length
= tmp
;
240 * Set an option on a socket.
242 * @param[in] sock Socket descriptor.
243 * @param[in] level Level at which the option is defined.
244 * @param[in] option Option to set the value for.
245 * @param[in] value Buffer of the value to be set.
246 * @param[in] length Length of the value buffer in bytes.
248 * @return Whether the option was set successfully.
250 JAYLINK_PRIV
bool socket_set_option(int sock
, int level
, int option
,
251 const void *value
, size_t length
)
253 if (!setsockopt(sock
, level
, option
, value
, length
))