configure.ac: Add switch to disable libusb support
[libjaylink.git] / libjaylink / socket.c
blobf2a6588d308ac1330192758f12cdb442e635d422
1 /*
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/>.
20 #ifdef _WIN32
21 #include <winsock2.h>
22 #else
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <unistd.h>
26 #endif
28 #include "libjaylink.h"
29 #include "libjaylink-internal.h"
31 /**
32 * @file
34 * Socket abstraction layer.
37 /**
38 * Close a socket.
40 * @param[in] sock Socket descriptor.
42 * @return Whether the socket was successfully closed.
44 JAYLINK_PRIV bool socket_close(int sock)
46 int ret;
48 #ifdef _WIN32
49 ret = closesocket(sock);
50 #else
51 ret = close(sock);
52 #endif
54 if (!ret)
55 return true;
57 return false;
60 /**
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,
70 size_t length)
72 int ret;
74 ret = bind(sock, address, length);
76 #ifdef _WIN32
77 if (ret == SOCKET_ERROR)
78 return false;
79 #else
80 if (ret < 0)
81 return false;
82 #endif
84 return true;
87 /**
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,
101 int flags)
103 ssize_t ret;
105 ret = send(sock, buffer, *length, flags);
106 #ifdef _WIN32
107 if (ret == SOCKET_ERROR)
108 return false;
109 #else
110 if (ret < 0)
111 return false;
112 #endif
113 *length = ret;
115 return true;
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,
133 int flags)
135 ssize_t ret;
137 ret = recv(sock, buffer, *length, flags);
139 #ifdef _WIN32
140 if (ret == SOCKET_ERROR)
141 return false;
142 #else
143 if (ret < 0)
144 return false;
145 #endif
147 *length = ret;
149 return true;
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
164 * in bytes.
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)
172 ssize_t ret;
174 ret = sendto(sock, buffer, *length, flags, address, address_length);
176 #ifdef _WIN32
177 if (ret == SOCKET_ERROR)
178 return false;
179 #else
180 if (ret < 0)
181 return false;
182 #endif
184 *length = ret;
186 return true;
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.
202 * Can be NULL.
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)
214 ssize_t ret;
215 #ifdef _WIN32
216 int tmp;
218 tmp = *address_length;
219 ret = recvfrom(sock, buffer, *length, flags, address, &tmp);
221 if (ret == SOCKET_ERROR)
222 return false;
223 #else
224 socklen_t tmp;
226 tmp = *address_length;
227 ret = recvfrom(sock, buffer, *length, flags, address, &tmp);
229 if (ret < 0)
230 return false;
231 #endif
233 *address_length = tmp;
234 *length = ret;
236 return true;
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))
254 return true;
256 return false;