1 #ifndef _BABELTRACE_COMPAT_SOCKET_H
2 #define _BABELTRACE_COMPAT_SOCKET_H
5 * babeltrace/compat/socket.h
7 * Copyright (C) 2015-2017 Michael Jeanson <mjeanson@efficios.com>
8 * 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #define BT_INVALID_SOCKET INVALID_SOCKET
34 #define BT_SOCKET_ERROR SOCKET_ERROR
35 #define BT_SOCKET SOCKET
38 int bt_socket_init(void)
44 /* Request winsock 2.2 support */
45 verreq
= MAKEWORD(2, 2);
47 ret
= WSAStartup(verreq
, &wsa
);
49 BT_LOGE("Winsock init failed with error: %d", ret
);
53 if (LOBYTE(wsa
.wVersion
) != 2 || HIBYTE(wsa
.wVersion
) != 2) {
54 BT_LOGE_STR("Could not init winsock 2.2 support");
64 int bt_socket_fini(void)
70 int bt_socket_send(int sockfd
, const void *buf
, size_t len
, int flags
)
72 return send(sockfd
, buf
, len
, flags
);
76 int bt_socket_recv(int sockfd
, void *buf
, size_t len
, int flags
)
78 return recv(sockfd
, buf
, len
, flags
);
82 int bt_socket_close(int fd
)
84 return closesocket(fd
);
88 bool bt_socket_interrupted(void)
90 /* There is no equivalent to EINTR in winsock 2.2 */
95 const char *bt_socket_errormsg(void)
98 int error
= WSAGetLastError();
102 errstr
= "Call interrupted";
108 errstr
= "Bad access";
111 errstr
= "Bad argument";
114 errstr
= "Invalid arguments";
117 errstr
= "Out of file descriptors";
120 errstr
= "Call would block";
124 errstr
= "Blocking call in progress";
127 errstr
= "Descriptor is not a socket";
129 case WSAEDESTADDRREQ
:
130 errstr
= "Need destination address";
133 errstr
= "Bad message size";
136 errstr
= "Bad protocol";
139 errstr
= "Protocol option is unsupported";
141 case WSAEPROTONOSUPPORT
:
142 errstr
= "Protocol is unsupported";
144 case WSAESOCKTNOSUPPORT
:
145 errstr
= "Socket is unsupported";
148 errstr
= "Operation not supported";
150 case WSAEAFNOSUPPORT
:
151 errstr
= "Address family not supported";
153 case WSAEPFNOSUPPORT
:
154 errstr
= "Protocol family not supported";
157 errstr
= "Address already in use";
159 case WSAEADDRNOTAVAIL
:
160 errstr
= "Address not available";
163 errstr
= "Network down";
166 errstr
= "Network unreachable";
169 errstr
= "Network has been reset";
171 case WSAECONNABORTED
:
172 errstr
= "Connection was aborted";
175 errstr
= "Connection was reset";
178 errstr
= "No buffer space";
181 errstr
= "Socket is already connected";
184 errstr
= "Socket is not connected";
187 errstr
= "Socket has been shut down";
189 case WSAETOOMANYREFS
:
190 errstr
= "Too many references";
193 errstr
= "Timed out";
195 case WSAECONNREFUSED
:
196 errstr
= "Connection refused";
201 case WSAENAMETOOLONG
:
202 errstr
= "Name too long";
205 errstr
= "Host down";
207 case WSAEHOSTUNREACH
:
208 errstr
= "Host unreachable";
211 errstr
= "Not empty";
214 errstr
= "Process limit reached";
217 errstr
= "Too many users";
220 errstr
= "Bad quota";
223 errstr
= "Something is stale";
226 errstr
= "Remote error";
229 errstr
= "Disconnected";
232 /* Extended Winsock errors */
234 errstr
= "Winsock library is not ready";
236 case WSANOTINITIALISED
:
237 errstr
= "Winsock library not initialised";
239 case WSAVERNOTSUPPORTED
:
240 errstr
= "Winsock version not supported";
243 /* getXbyY() errors (already handled in herrmsg):
244 * Authoritative Answer: Host not found */
245 case WSAHOST_NOT_FOUND
:
246 errstr
= "Host not found";
249 /* Non-Authoritative: Host not found, or SERVERFAIL */
251 errstr
= "Host not found, try again";
254 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
256 errstr
= "Unrecoverable error in call to nameserver";
259 /* Valid name, no data record of requested type */
261 errstr
= "No data record of requested type";
265 errstr
= "Unknown error";
271 #else /* __MINGW32__ */
274 #include <sys/socket.h>
275 #include <netinet/in.h>
278 #define BT_INVALID_SOCKET -1
279 #define BT_SOCKET_ERROR -1
280 #define BT_SOCKET int
283 int bt_socket_init(void)
289 int bt_socket_fini(void)
295 int bt_socket_send(int sockfd
, const void *buf
, size_t len
, int flags
)
297 return send(sockfd
, buf
, len
, flags
);
301 int bt_socket_recv(int sockfd
, void *buf
, size_t len
, int flags
)
303 return recv(sockfd
, buf
, len
, flags
);
307 int bt_socket_close(int fd
)
313 bool bt_socket_interrupted(void)
315 return (errno
== EINTR
);
319 const char *bt_socket_errormsg(void)
321 return g_strerror(errno
);
327 * This wrapper is used on platforms that have no way of ignoring SIGPIPE
333 # define MSG_NOSIGNAL SO_NOSIGPIPE
334 # elif defined(__MINGW32__)
335 # define MSG_NOSIGNAL 0
339 #if defined(MSG_NOSIGNAL)
341 ssize_t
bt_socket_send_nosigpipe(int fd
, const void *buffer
, size_t size
)
343 return bt_socket_send(fd
, buffer
, size
, MSG_NOSIGNAL
);
350 ssize_t
bt_socket_send_nosigpipe(int fd
, const void *buffer
, size_t size
)
354 sigset_t sigpipe_set
, pending_set
, old_set
;
355 int sigpipe_was_pending
;
358 * Discard the SIGPIPE from send(), not disturbing any SIGPIPE
359 * that might be already pending. If a bogus SIGPIPE is sent to
360 * the entire process concurrently by a malicious user, it may
361 * be simply discarded.
363 if (sigemptyset(&pending_set
)) {
367 * sigpending returns the mask of signals that are _both_
368 * blocked for the thread _and_ pending for either the thread or
369 * the entire process.
371 if (sigpending(&pending_set
)) {
374 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
376 * If sigpipe was pending, it means it was already blocked, so
377 * no need to block it.
379 if (!sigpipe_was_pending
) {
380 if (sigemptyset(&sigpipe_set
)) {
383 if (sigaddset(&sigpipe_set
, SIGPIPE
)) {
386 if (pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
)) {
391 /* Send and save errno. */
392 sent
= bt_socket_send(fd
, buffer
, size
, 0);
395 if (sent
== -1 && errno
== EPIPE
&& !sigpipe_was_pending
) {
396 struct timespec timeout
= { 0, 0 };
400 ret
= sigtimedwait(&sigpipe_set
, NULL
,
402 } while (ret
== -1 && errno
== EINTR
);
404 if (!sigpipe_was_pending
) {
405 if (pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
)) {
409 /* Restore send() errno */
417 #endif /* _BABELTRACE_COMPAT_SOCKET_H */
This page took 0.059507 seconds and 4 git commands to generate.