OpenOCD
socket.c
Go to the documentation of this file.
1 /*
2  * This file is part of the libjaylink project.
3  *
4  * Copyright (C) 2016-2017 Marc Schink <jaylink-dev@marcschink.de>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
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
27 
28 #include "libjaylink.h"
29 #include "libjaylink-internal.h"
30 
44 JAYLINK_PRIV bool socket_close(int sock)
45 {
46  int ret;
47 
48 #ifdef _WIN32
49  ret = closesocket(sock);
50 #else
51  ret = close(sock);
52 #endif
53 
54  if (!ret)
55  return true;
56 
57  return false;
58 }
59 
69 JAYLINK_PRIV bool socket_bind(int sock, const struct sockaddr *address,
70  size_t length)
71 {
72  int ret;
73 
74  ret = bind(sock, address, length);
75 
76 #ifdef _WIN32
77  if (ret == SOCKET_ERROR)
78  return false;
79 #else
80  if (ret < 0)
81  return false;
82 #endif
83 
84  return true;
85 }
86 
100 JAYLINK_PRIV bool socket_send(int sock, const void *buffer, size_t *length,
101  int flags)
102 {
103  ssize_t ret;
104 
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;
114 
115  return true;
116 }
117 
132 JAYLINK_PRIV bool socket_recv(int sock, void *buffer, size_t *length,
133  int flags)
134 {
135  ssize_t ret;
136 
137  ret = recv(sock, buffer, *length, flags);
138 
139 #ifdef _WIN32
140  if (ret == SOCKET_ERROR)
141  return false;
142 #else
143  if (ret < 0)
144  return false;
145 #endif
146 
147  *length = ret;
148 
149  return true;
150 }
151 
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)
171 {
172  ssize_t ret;
173 
174  ret = sendto(sock, buffer, *length, flags, address, address_length);
175 
176 #ifdef _WIN32
177  if (ret == SOCKET_ERROR)
178  return false;
179 #else
180  if (ret < 0)
181  return false;
182 #endif
183 
184  *length = ret;
185 
186  return true;
187 }
188 
211 JAYLINK_PRIV bool socket_recvfrom(int sock, void *buffer, size_t *length,
212  int flags, struct sockaddr *address, size_t *address_length)
213 {
214  ssize_t ret;
215 #ifdef _WIN32
216  int tmp;
217 
218  tmp = *address_length;
219  ret = recvfrom(sock, buffer, *length, flags, address, &tmp);
220 
221  if (ret == SOCKET_ERROR)
222  return false;
223 #else
224  socklen_t tmp;
225 
226  tmp = *address_length;
227  ret = recvfrom(sock, buffer, *length, flags, address, &tmp);
228 
229  if (ret < 0)
230  return false;
231 #endif
232 
233  *address_length = tmp;
234  *length = ret;
235 
236  return true;
237 }
238 
250 JAYLINK_PRIV bool socket_set_option(int sock, int level, int option,
251  const void *value, size_t length)
252 {
253  if (!setsockopt(sock, level, option, value, length))
254  return true;
255 
256  return false;
257 }
JAYLINK_PRIV bool socket_send(int sock, const void *buffer, size_t *length, int flags)
Send a message on a socket.
Definition: socket.c:100
JAYLINK_PRIV bool socket_recv(int sock, void *buffer, size_t *length, int flags)
Receive a message from a socket.
Definition: socket.c:132
JAYLINK_PRIV bool socket_recvfrom(int sock, void *buffer, size_t *length, int flags, struct sockaddr *address, size_t *address_length)
Receive a message from a socket.
Definition: socket.c:211
JAYLINK_PRIV bool socket_sendto(int sock, const void *buffer, size_t *length, int flags, const struct sockaddr *address, size_t address_length)
Send a message on a socket.
Definition: socket.c:168
JAYLINK_PRIV bool socket_bind(int sock, const struct sockaddr *address, size_t length)
Bind an address to a socket.
Definition: socket.c:69
JAYLINK_PRIV bool socket_close(int sock)
Close a socket.
Definition: socket.c:44
JAYLINK_PRIV bool socket_set_option(int sock, int level, int option, const void *value, size_t length)
Set an option on a socket.
Definition: socket.c:250