mirror of
https://github.com/libssh2/libssh2.git
synced 2025-07-31 00:03:08 +03:00
tidy-up: gettimeofday()
fallback and use
Simplify the way we handle `gettimeofday()` fallback for platforms without native support or without any support. Make it similar to how we handle `snprintf()`. In case of no native `gettimeofday()` support and a non-Windows platform, our local fallback returns zero in `tv_usec` and `tv_sec`, ending up with a zero `timeout_remaining` in `session.c`, same as before this patch. Also: - drop unused `sys/time.h` headers. - fix our fallback code to compile with any Windows compilers (not just MSVC) - delete unnecessary casts. Closes #1001
This commit is contained in:
@ -22,9 +22,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -20,9 +20,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -27,9 +27,6 @@
|
|||||||
#ifdef HAVE_NETINET_IN_H
|
#ifdef HAVE_NETINET_IN_H
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -26,9 +26,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -26,9 +26,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -29,9 +29,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -24,9 +24,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -26,9 +26,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -22,9 +22,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -29,9 +29,6 @@
|
|||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
#ifdef HAVE_SYS_UN_H
|
#ifdef HAVE_SYS_UN_H
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -64,7 +64,6 @@
|
|||||||
# ifdef HAVE_SYS_SELECT_H
|
# ifdef HAVE_SYS_SELECT_H
|
||||||
# include <sys/select.h>
|
# include <sys/select.h>
|
||||||
# else
|
# else
|
||||||
# include <sys/time.h>
|
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# endif
|
# endif
|
||||||
# endif
|
# endif
|
||||||
@ -112,12 +111,22 @@
|
|||||||
|
|
||||||
/* Use local implementation when not available */
|
/* Use local implementation when not available */
|
||||||
#if !defined(HAVE_SNPRINTF)
|
#if !defined(HAVE_SNPRINTF)
|
||||||
#define LIBSSH2_SNPRINTF
|
|
||||||
#undef snprintf
|
#undef snprintf
|
||||||
#define snprintf _libssh2_snprintf
|
#define snprintf _libssh2_snprintf
|
||||||
|
#define LIBSSH2_SNPRINTF
|
||||||
int _libssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...);
|
int _libssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(HAVE_GETTIMEOFDAY)
|
||||||
|
#define HAVE_GETTIMEOFDAY
|
||||||
|
#undef gettimeofday
|
||||||
|
#define gettimeofday _libssh2_gettimeofday
|
||||||
|
#define LIBSSH2_GETTIMEOFDAY
|
||||||
|
int _libssh2_gettimeofday(struct timeval *tp, void *tzp);
|
||||||
|
#elif defined(HAVE_SYS_TIME_H)
|
||||||
|
#include <sys/time.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/* "inline" keyword is valid only with C++ engine! */
|
/* "inline" keyword is valid only with C++ engine! */
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#undef inline
|
#undef inline
|
||||||
|
36
src/misc.c
36
src/misc.c
@ -46,10 +46,6 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
/* Force parameter type. */
|
/* Force parameter type. */
|
||||||
#define recv(s, b, l, f) recv((s), (b), (int)(l), (f))
|
#define recv(s, b, l, f) recv((s), (b), (int)(l), (f))
|
||||||
@ -533,7 +529,7 @@ _libssh2_debug_low(LIBSSH2_SESSION * session, int context, const char *format,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_libssh2_gettimeofday(&now, NULL);
|
gettimeofday(&now, NULL);
|
||||||
if(!firstsec) {
|
if(!firstsec) {
|
||||||
firstsec = now.tv_sec;
|
firstsec = now.tv_sec;
|
||||||
}
|
}
|
||||||
@ -672,8 +668,8 @@ void _libssh2_list_insert(struct list_node *after, /* insert before this */
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* this define is defined in misc.h for the correct platforms */
|
/* Defined in libssh2_priv.h for the correct platforms */
|
||||||
#ifdef LIBSSH2_GETTIMEOFDAY_WIN32
|
#ifdef LIBSSH2_GETTIMEOFDAY
|
||||||
/*
|
/*
|
||||||
* _libssh2_gettimeofday
|
* _libssh2_gettimeofday
|
||||||
* Implementation according to:
|
* Implementation according to:
|
||||||
@ -696,27 +692,31 @@ void _libssh2_list_insert(struct list_node *after, /* insert before this */
|
|||||||
* Danny Smith <dannysmith@users.sourceforge.net>
|
* Danny Smith <dannysmith@users.sourceforge.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
|
int _libssh2_gettimeofday(struct timeval *tp, void *tzp)
|
||||||
#define _W32_FT_OFFSET (116444736000000000)
|
|
||||||
|
|
||||||
int __cdecl _libssh2_gettimeofday(struct timeval *tp, void *tzp)
|
|
||||||
{
|
{
|
||||||
union {
|
|
||||||
unsigned __int64 ns100; /* time since 1 Jan 1601 in 100ns units */
|
|
||||||
FILETIME ft;
|
|
||||||
} _now;
|
|
||||||
(void)tzp;
|
(void)tzp;
|
||||||
if(tp) {
|
if(tp) {
|
||||||
|
#ifdef WIN32
|
||||||
|
/* Offset between 1601-01-01 and 1970-01-01 in 100 nanosec units */
|
||||||
|
#define _WIN32_FT_OFFSET (116444736000000000)
|
||||||
|
|
||||||
|
union {
|
||||||
|
libssh2_uint64_t ns100; /* time since 1 Jan 1601 in 100ns units */
|
||||||
|
FILETIME ft;
|
||||||
|
} _now;
|
||||||
GetSystemTimeAsFileTime(&_now.ft);
|
GetSystemTimeAsFileTime(&_now.ft);
|
||||||
tp->tv_usec = (long)((_now.ns100 / 10) % 1000000);
|
tp->tv_usec = (long)((_now.ns100 / 10) % 1000000);
|
||||||
tp->tv_sec = (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
|
tp->tv_sec = (long)((_now.ns100 - _WIN32_FT_OFFSET) / 10000000);
|
||||||
|
#else
|
||||||
|
/* Platforms without a native implementation or local replacement */
|
||||||
|
tp->tv_usec = 0;
|
||||||
|
tp->tv_sec = 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
/* Always return 0 as per Open Group Base Specifications Issue 6.
|
/* Always return 0 as per Open Group Base Specifications Issue 6.
|
||||||
Do not set errno on error. */
|
Do not set errno on error. */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void *_libssh2_calloc(LIBSSH2_SESSION* session, size_t size)
|
void *_libssh2_calloc(LIBSSH2_SESSION* session, size_t size)
|
||||||
|
13
src/misc.h
13
src/misc.h
@ -131,19 +131,6 @@ int _libssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf,
|
|||||||
int _libssh2_check_length(struct string_buf *buf, size_t requested_len);
|
int _libssh2_check_length(struct string_buf *buf, size_t requested_len);
|
||||||
int _libssh2_eob(struct string_buf *buf);
|
int _libssh2_eob(struct string_buf *buf);
|
||||||
|
|
||||||
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
|
|
||||||
/* provide a private one */
|
|
||||||
#undef HAVE_GETTIMEOFDAY
|
|
||||||
int __cdecl _libssh2_gettimeofday(struct timeval *tp, void *tzp);
|
|
||||||
#define HAVE_LIBSSH2_GETTIMEOFDAY
|
|
||||||
#define LIBSSH2_GETTIMEOFDAY_WIN32 /* enable the win32 implementation */
|
|
||||||
#else
|
|
||||||
#ifdef HAVE_GETTIMEOFDAY
|
|
||||||
#define _libssh2_gettimeofday(x,y) gettimeofday(x,y)
|
|
||||||
#define HAVE_LIBSSH2_GETTIMEOFDAY
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void _libssh2_xor_data(unsigned char *output,
|
void _libssh2_xor_data(unsigned char *output,
|
||||||
const unsigned char *input1,
|
const unsigned char *input1,
|
||||||
const unsigned char *input2,
|
const unsigned char *input2,
|
||||||
|
@ -46,10 +46,6 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_SYS_TIME_H
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HAVE_INTTYPES_H
|
#ifdef HAVE_INTTYPES_H
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -50,9 +50,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#ifdef HAVE_GETTIMEOFDAY
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
#ifdef HAVE_ALLOCA_H
|
#ifdef HAVE_ALLOCA_H
|
||||||
#include <alloca.h>
|
#include <alloca.h>
|
||||||
#endif
|
#endif
|
||||||
@ -1721,23 +1718,15 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
|
|||||||
}
|
}
|
||||||
#ifdef HAVE_POLL
|
#ifdef HAVE_POLL
|
||||||
|
|
||||||
#ifdef HAVE_LIBSSH2_GETTIMEOFDAY
|
|
||||||
{
|
{
|
||||||
struct timeval tv_begin, tv_end;
|
struct timeval tv_begin, tv_end;
|
||||||
|
|
||||||
_libssh2_gettimeofday((struct timeval *) &tv_begin, NULL);
|
gettimeofday(&tv_begin, NULL);
|
||||||
sysret = poll(sockets, nfds, (int)timeout_remaining);
|
sysret = poll(sockets, nfds, (int)timeout_remaining);
|
||||||
_libssh2_gettimeofday((struct timeval *) &tv_end, NULL);
|
gettimeofday(&tv_end, NULL);
|
||||||
timeout_remaining -= (tv_end.tv_sec - tv_begin.tv_sec) * 1000;
|
timeout_remaining -= (tv_end.tv_sec - tv_begin.tv_sec) * 1000;
|
||||||
timeout_remaining -= (tv_end.tv_usec - tv_begin.tv_usec) / 1000;
|
timeout_remaining -= (tv_end.tv_usec - tv_begin.tv_usec) / 1000;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
/* If the platform doesn't support gettimeofday,
|
|
||||||
* then just make the call non-blocking and walk away
|
|
||||||
*/
|
|
||||||
sysret = poll(sockets, nfds, (int)timeout_remaining);
|
|
||||||
timeout_remaining = 0;
|
|
||||||
#endif /* HAVE_LIBSSH2_GETTIMEOFDAY */
|
|
||||||
|
|
||||||
if(sysret > 0) {
|
if(sysret > 0) {
|
||||||
for(i = 0; i < nfds; i++) {
|
for(i = 0; i < nfds; i++) {
|
||||||
@ -1784,24 +1773,17 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
|
|||||||
#elif defined(HAVE_SELECT)
|
#elif defined(HAVE_SELECT)
|
||||||
tv.tv_sec = timeout_remaining / 1000;
|
tv.tv_sec = timeout_remaining / 1000;
|
||||||
tv.tv_usec = (timeout_remaining % 1000) * 1000;
|
tv.tv_usec = (timeout_remaining % 1000) * 1000;
|
||||||
#ifdef HAVE_LIBSSH2_GETTIMEOFDAY
|
|
||||||
{
|
{
|
||||||
struct timeval tv_begin, tv_end;
|
struct timeval tv_begin, tv_end;
|
||||||
|
|
||||||
_libssh2_gettimeofday((struct timeval *) &tv_begin, NULL);
|
gettimeofday(&tv_begin, NULL);
|
||||||
sysret = select((int)(maxfd + 1), &rfds, &wfds, NULL, &tv);
|
sysret = select((int)(maxfd + 1), &rfds, &wfds, NULL, &tv);
|
||||||
_libssh2_gettimeofday((struct timeval *) &tv_end, NULL);
|
gettimeofday(&tv_end, NULL);
|
||||||
|
|
||||||
timeout_remaining -= (tv_end.tv_sec - tv_begin.tv_sec) * 1000;
|
timeout_remaining -= (tv_end.tv_sec - tv_begin.tv_sec) * 1000;
|
||||||
timeout_remaining -= (tv_end.tv_usec - tv_begin.tv_usec) / 1000;
|
timeout_remaining -= (tv_end.tv_usec - tv_begin.tv_usec) / 1000;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
/* If the platform doesn't support gettimeofday,
|
|
||||||
* then just make the call non-blocking and walk away
|
|
||||||
*/
|
|
||||||
sysret = select((int)(maxfd + 1), &rfds, &wfds, NULL, &tv);
|
|
||||||
timeout_remaining = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(sysret > 0) {
|
if(sysret > 0) {
|
||||||
for(i = 0; i < nfds; i++) {
|
for(i = 0; i < nfds; i++) {
|
||||||
|
Reference in New Issue
Block a user