1 #ifndef _BABELTRACE_COMPAT_SOCKET_H
2 #define _BABELTRACE_COMPAT_SOCKET_H
5 * Copyright (C) 2015-2017 Michael Jeanson <mjeanson@efficios.com>
6 * 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define BT_INVALID_SOCKET INVALID_SOCKET
32 #define BT_SOCKET_ERROR SOCKET_ERROR
33 #define BT_SOCKET SOCKET
36 int bt_socket_init(void)
42 /* Request winsock 2.2 support */
43 verreq
= MAKEWORD(2, 2);
45 ret
= WSAStartup(verreq
, &wsa
);
48 BT_LOGE("Winsock init failed with error: %d", ret
);
53 if (LOBYTE(wsa
.wVersion
) != 2 || HIBYTE(wsa
.wVersion
) != 2) {
55 BT_LOGE_STR("Could not init winsock 2.2 support");
66 int bt_socket_fini(void)
72 int bt_socket_send(int sockfd
, const void *buf
, size_t len
, int flags
)
74 return send(sockfd
, buf
, len
, flags
);
78 int bt_socket_recv(int sockfd
, void *buf
, size_t len
, int flags
)
80 return recv(sockfd
, buf
, len
, flags
);
84 int bt_socket_close(int fd
)
86 return closesocket(fd
);
90 bool bt_socket_interrupted(void)
92 /* There is no equivalent to EINTR in winsock 2.2 */
97 const char *bt_socket_errormsg(void)
100 int error
= WSAGetLastError();
104 errstr
= "Call interrupted";
110 errstr
= "Bad access";
113 errstr
= "Bad argument";
116 errstr
= "Invalid arguments";
119 errstr
= "Out of file descriptors";
122 errstr
= "Call would block";
126 errstr
= "Blocking call in progress";
129 errstr
= "Descriptor is not a socket";
131 case WSAEDESTADDRREQ
:
132 errstr
= "Need destination address";
135 errstr
= "Bad message size";
138 errstr
= "Bad protocol";
141 errstr
= "Protocol option is unsupported";
143 case WSAEPROTONOSUPPORT
:
144 errstr
= "Protocol is unsupported";
146 case WSAESOCKTNOSUPPORT
:
147 errstr
= "Socket is unsupported";
150 errstr
= "Operation not supported";
152 case WSAEAFNOSUPPORT
:
153 errstr
= "Address family not supported";
155 case WSAEPFNOSUPPORT
:
156 errstr
= "Protocol family not supported";
159 errstr
= "Address already in use";
161 case WSAEADDRNOTAVAIL
:
162 errstr
= "Address not available";
165 errstr
= "Network down";
168 errstr
= "Network unreachable";
171 errstr
= "Network has been reset";
173 case WSAECONNABORTED
:
174 errstr
= "Connection was aborted";
177 errstr
= "Connection was reset";
180 errstr
= "No buffer space";
183 errstr
= "Socket is already connected";
186 errstr
= "Socket is not connected";
189 errstr
= "Socket has been shut down";
191 case WSAETOOMANYREFS
:
192 errstr
= "Too many references";
195 errstr
= "Timed out";
197 case WSAECONNREFUSED
:
198 errstr
= "Connection refused";
203 case WSAENAMETOOLONG
:
204 errstr
= "Name too long";
207 errstr
= "Host down";
209 case WSAEHOSTUNREACH
:
210 errstr
= "Host unreachable";
213 errstr
= "Not empty";
216 errstr
= "Process limit reached";
219 errstr
= "Too many users";
222 errstr
= "Bad quota";
225 errstr
= "Something is stale";
228 errstr
= "Remote error";
231 errstr
= "Disconnected";
234 /* Extended Winsock errors */
236 errstr
= "Winsock library is not ready";
238 case WSANOTINITIALISED
:
239 errstr
= "Winsock library not initialised";
241 case WSAVERNOTSUPPORTED
:
242 errstr
= "Winsock version not supported";
245 /* getXbyY() errors (already handled in herrmsg):
246 * Authoritative Answer: Host not found */
247 case WSAHOST_NOT_FOUND
:
248 errstr
= "Host not found";
251 /* Non-Authoritative: Host not found, or SERVERFAIL */
253 errstr
= "Host not found, try again";
256 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
258 errstr
= "Unrecoverable error in call to nameserver";
261 /* Valid name, no data record of requested type */
263 errstr
= "No data record of requested type";
267 errstr
= "Unknown error";
273 #else /* __MINGW32__ */
276 #include <sys/socket.h>
277 #include <netinet/in.h>
280 #define BT_INVALID_SOCKET -1
281 #define BT_SOCKET_ERROR -1
282 #define BT_SOCKET int
285 int bt_socket_init(void)
291 int bt_socket_fini(void)
297 int bt_socket_send(int sockfd
, const void *buf
, size_t len
, int flags
)
299 return send(sockfd
, buf
, len
, flags
);
303 int bt_socket_recv(int sockfd
, void *buf
, size_t len
, int flags
)
305 return recv(sockfd
, buf
, len
, flags
);
309 int bt_socket_close(int fd
)
315 bool bt_socket_interrupted(void)
317 return (errno
== EINTR
);
321 const char *bt_socket_errormsg(void)
323 return g_strerror(errno
);
329 * This wrapper is used on platforms that have no way of ignoring SIGPIPE
335 # define MSG_NOSIGNAL SO_NOSIGPIPE
336 # elif defined(__MINGW32__)
337 # define MSG_NOSIGNAL 0
341 #if defined(MSG_NOSIGNAL)
343 ssize_t
bt_socket_send_nosigpipe(int fd
, const void *buffer
, size_t size
)
345 return bt_socket_send(fd
, buffer
, size
, MSG_NOSIGNAL
);
352 ssize_t
bt_socket_send_nosigpipe(int fd
, const void *buffer
, size_t size
)
356 sigset_t sigpipe_set
, pending_set
, old_set
;
357 int sigpipe_was_pending
;
360 * Discard the SIGPIPE from send(), not disturbing any SIGPIPE
361 * that might be already pending. If a bogus SIGPIPE is sent to
362 * the entire process concurrently by a malicious user, it may
363 * be simply discarded.
365 if (sigemptyset(&pending_set
)) {
369 * sigpending returns the mask of signals that are _both_
370 * blocked for the thread _and_ pending for either the thread or
371 * the entire process.
373 if (sigpending(&pending_set
)) {
376 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
378 * If sigpipe was pending, it means it was already blocked, so
379 * no need to block it.
381 if (!sigpipe_was_pending
) {
382 if (sigemptyset(&sigpipe_set
)) {
385 if (sigaddset(&sigpipe_set
, SIGPIPE
)) {
388 if (pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
)) {
393 /* Send and save errno. */
394 sent
= bt_socket_send(fd
, buffer
, size
, 0);
397 if (sent
== -1 && errno
== EPIPE
&& !sigpipe_was_pending
) {
398 struct timespec timeout
= { 0, 0 };
402 ret
= sigtimedwait(&sigpipe_set
, NULL
,
404 } while (ret
== -1 && errno
== EINTR
);
406 if (!sigpipe_was_pending
) {
407 if (pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
)) {
411 /* Restore send() errno */
419 #endif /* _BABELTRACE_COMPAT_SOCKET_H */
This page took 0.038765 seconds and 4 git commands to generate.