7a57678b377e1995b147ce72d9f7aaaf144e0283
2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2015-2017 Michael Jeanson <mjeanson@efficios.com>
5 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 #ifndef _BABELTRACE_COMPAT_SOCKET_H
9 #define _BABELTRACE_COMPAT_SOCKET_H
17 #define BT_INVALID_SOCKET INVALID_SOCKET
18 #define BT_SOCKET_ERROR SOCKET_ERROR
19 #define BT_SOCKET SOCKET
22 int bt_socket_init(int log_level
)
28 /* Request winsock 2.2 support */
29 verreq
= MAKEWORD(2, 2);
31 ret
= WSAStartup(verreq
, &wsa
);
33 #ifdef BT_LOG_WRITE_CUR_LVL
34 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
35 "Winsock init failed with error: %d", ret
);
40 if (LOBYTE(wsa
.wVersion
) != 2 || HIBYTE(wsa
.wVersion
) != 2) {
41 #ifdef BT_LOG_WRITE_CUR_LVL
42 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
43 "Could not init winsock 2.2 support");
54 int bt_socket_fini(void)
60 int bt_socket_send(int sockfd
, const void *buf
, size_t len
, int flags
)
62 return send(sockfd
, (const char *) buf
, len
, flags
);
66 int bt_socket_recv(int sockfd
, void *buf
, size_t len
, int flags
)
68 return recv(sockfd
, (char *) buf
, len
, flags
);
72 int bt_socket_close(int fd
)
74 return closesocket(fd
);
78 bool bt_socket_interrupted(void)
80 /* There is no equivalent to EINTR in winsock 2.2 */
85 const char *bt_socket_errormsg(void)
88 int error
= WSAGetLastError();
92 errstr
= "Call interrupted";
98 errstr
= "Bad access";
101 errstr
= "Bad argument";
104 errstr
= "Invalid arguments";
107 errstr
= "Out of file descriptors";
110 errstr
= "Call would block";
114 errstr
= "Blocking call in progress";
117 errstr
= "Descriptor is not a socket";
119 case WSAEDESTADDRREQ
:
120 errstr
= "Need destination address";
123 errstr
= "Bad message size";
126 errstr
= "Bad protocol";
129 errstr
= "Protocol option is unsupported";
131 case WSAEPROTONOSUPPORT
:
132 errstr
= "Protocol is unsupported";
134 case WSAESOCKTNOSUPPORT
:
135 errstr
= "Socket is unsupported";
138 errstr
= "Operation not supported";
140 case WSAEAFNOSUPPORT
:
141 errstr
= "Address family not supported";
143 case WSAEPFNOSUPPORT
:
144 errstr
= "Protocol family not supported";
147 errstr
= "Address already in use";
149 case WSAEADDRNOTAVAIL
:
150 errstr
= "Address not available";
153 errstr
= "Network down";
156 errstr
= "Network unreachable";
159 errstr
= "Network has been reset";
161 case WSAECONNABORTED
:
162 errstr
= "Connection was aborted";
165 errstr
= "Connection was reset";
168 errstr
= "No buffer space";
171 errstr
= "Socket is already connected";
174 errstr
= "Socket is not connected";
177 errstr
= "Socket has been shut down";
179 case WSAETOOMANYREFS
:
180 errstr
= "Too many references";
183 errstr
= "Timed out";
185 case WSAECONNREFUSED
:
186 errstr
= "Connection refused";
191 case WSAENAMETOOLONG
:
192 errstr
= "Name too long";
195 errstr
= "Host down";
197 case WSAEHOSTUNREACH
:
198 errstr
= "Host unreachable";
201 errstr
= "Not empty";
204 errstr
= "Process limit reached";
207 errstr
= "Too many users";
210 errstr
= "Bad quota";
213 errstr
= "Something is stale";
216 errstr
= "Remote error";
219 errstr
= "Disconnected";
222 /* Extended Winsock errors */
224 errstr
= "Winsock library is not ready";
226 case WSANOTINITIALISED
:
227 errstr
= "Winsock library not initialised";
229 case WSAVERNOTSUPPORTED
:
230 errstr
= "Winsock version not supported";
233 /* getXbyY() errors (already handled in herrmsg):
234 * Authoritative Answer: Host not found */
235 case WSAHOST_NOT_FOUND
:
236 errstr
= "Host not found";
239 /* Non-Authoritative: Host not found, or SERVERFAIL */
241 errstr
= "Host not found, try again";
244 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
246 errstr
= "Unrecoverable error in call to nameserver";
249 /* Valid name, no data record of requested type */
251 errstr
= "No data record of requested type";
255 errstr
= "Unknown error";
261 #else /* __MINGW32__ */
265 #include <netinet/in.h>
266 #include <sys/socket.h>
271 #define BT_INVALID_SOCKET -1
272 #define BT_SOCKET_ERROR -1
273 #define BT_SOCKET int
276 int bt_socket_init(int log_level
)
282 int bt_socket_fini(void)
288 int bt_socket_send(int sockfd
, const void *buf
, size_t len
, int flags
)
290 return send(sockfd
, buf
, len
, flags
);
294 int bt_socket_recv(int sockfd
, void *buf
, size_t len
, int flags
)
296 return recv(sockfd
, buf
, len
, flags
);
300 int bt_socket_close(int fd
)
306 bool bt_socket_interrupted(void)
308 return (errno
== EINTR
);
312 const char *bt_socket_errormsg(void)
314 return g_strerror(errno
);
320 * This wrapper is used on platforms that have no way of ignoring SIGPIPE
326 # define MSG_NOSIGNAL SO_NOSIGPIPE
327 # elif defined(__MINGW32__)
328 # define MSG_NOSIGNAL 0
332 #if defined(MSG_NOSIGNAL)
334 ssize_t
bt_socket_send_nosigpipe(int fd
, const void *buffer
, size_t size
)
336 return bt_socket_send(fd
, buffer
, size
, MSG_NOSIGNAL
);
343 ssize_t
bt_socket_send_nosigpipe(int fd
, const void *buffer
, size_t size
)
347 sigset_t sigpipe_set
, pending_set
, old_set
;
348 int sigpipe_was_pending
;
351 * Discard the SIGPIPE from send(), not disturbing any SIGPIPE
352 * that might be already pending. If a bogus SIGPIPE is sent to
353 * the entire process concurrently by a malicious user, it may
354 * be simply discarded.
356 if (sigemptyset(&pending_set
)) {
360 * sigpending returns the mask of signals that are _both_
361 * blocked for the thread _and_ pending for either the thread or
362 * the entire process.
364 if (sigpending(&pending_set
)) {
367 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
369 * If sigpipe was pending, it means it was already blocked, so
370 * no need to block it.
372 if (!sigpipe_was_pending
) {
373 if (sigemptyset(&sigpipe_set
)) {
376 if (sigaddset(&sigpipe_set
, SIGPIPE
)) {
379 if (pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
)) {
384 /* Send and save errno. */
385 sent
= bt_socket_send(fd
, buffer
, size
, 0);
388 if (sent
== -1 && errno
== EPIPE
&& !sigpipe_was_pending
) {
389 struct timespec timeout
= { 0, 0 };
393 ret
= sigtimedwait(&sigpipe_set
, NULL
,
395 } while (ret
== -1 && errno
== EINTR
);
397 if (!sigpipe_was_pending
) {
398 if (pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
)) {
402 /* Restore send() errno */
410 #endif /* _BABELTRACE_COMPAT_SOCKET_H */
This page took 0.036979 seconds and 3 git commands to generate.