1 /** @file socket_utils.cc
2 * @brief Socket handling utilities.
4 /* Copyright (C) 2006,2007,2008,2015 Olly Betts
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "socket_utils.h"
27 #include "safesyssocket.h"
32 # include "safeerrno.h"
35 // __STDC_SECURE_LIB__ doesn't appear to be publicly documented, but appears
36 // to be a good idea. We cribbed this test from the python sources - see, for
37 // example, http://svn.python.org/view?rev=47223&view=rev
38 # if defined _MSC_VER && _MSC_VER >= 1400 && defined __STDC_SECURE_LIB__
39 # include <cstdlib> // For _set_invalid_parameter_handler(), etc.
40 # include <crtdbg.h> // For _CrtSetReportMode, etc.
42 /** A dummy invalid parameter handler which ignores the error. */
43 static void dummy_handler(const wchar_t*,
51 // Recent versions of MSVC call an "_invalid_parameter_handler" if a
52 // CRT function receives an invalid parameter. However, there are cases
53 // where this is totally reasonable. To avoid the application dying,
54 // you just need to instantiate the MSVCIgnoreInvalidParameter class in
55 // the scope where you want MSVC to ignore invalid parameters.
56 class MSVCIgnoreInvalidParameter
{
57 _invalid_parameter_handler old_handler
;
61 MSVCIgnoreInvalidParameter() {
62 // Install a dummy handler to avoid the program dying.
63 old_handler
= _set_invalid_parameter_handler(dummy_handler
);
64 // Make sure that no dialog boxes appear.
65 old_report_mode
= _CrtSetReportMode(_CRT_ASSERT
, 0);
68 ~MSVCIgnoreInvalidParameter() {
69 // Restore the previous settings.
70 _set_invalid_parameter_handler(old_handler
);
71 _CrtSetReportMode(_CRT_ASSERT
, old_report_mode
);
75 // Mingw seems to be free of this insanity, so for this and older MSVC versions
76 // define a dummy class to allow MSVCIgnoreInvalidParameter to be used
78 struct MSVCIgnoreInvalidParameter
{
79 // Provide an explicit constructor so this isn't a POD struct - this seems
80 // to prevent GCC warning about an unused variable whenever we instantiate
82 MSVCIgnoreInvalidParameter() { }
86 /// Convert an fd (which might be a socket) to a WIN32 HANDLE.
87 extern HANDLE
fd_to_handle(int fd
) {
88 MSVCIgnoreInvalidParameter invalid_handle_value_is_ok
;
89 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
90 if (handle
!= INVALID_HANDLE_VALUE
) return handle
;
91 // On WIN32, a socket fd isn't the same as a non-socket fd - in fact it's
93 return reinterpret_cast<HANDLE
>(fd
);
96 /// Close an fd, which might be a socket.
97 extern void close_fd_or_socket(int fd
) {
98 MSVCIgnoreInvalidParameter invalid_fd_value_is_ok
;
99 if (close(fd
) == -1 && errno
== EBADF
) {
100 // Bad file descriptor - probably because the fd is actually
109 set_socket_timeouts(int fd
, double timeout
)
113 #if defined SO_SNDTIMEO || defined SO_RCVTIMEO
117 RealTime::to_timeval(timeout
, &t
);
119 // Just to be different, it's a DWORD counting in milliseconds.
121 if (usual(timeout
< numeric_limits
<DWORD
>::max() / 1000))
124 t
= numeric_limits
<DWORD
>::max();
127 (void)setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
,
128 reinterpret_cast<char*>(&t
), sizeof(t
));
131 (void)setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
,
132 reinterpret_cast<char*>(&t
), sizeof(t
));
137 // SO_SNDTIMEO and SO_RCVTIMEO may be ignored even if they exist, so set
138 // SO_KEEPALIVE anyway if it exists, as it will cause stuck connections to
139 // time out eventually (though it may take ~2 hours).
146 (void)setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
,
147 reinterpret_cast<char*>(&flag
), sizeof(flag
));