diff --git a/example/direct_tcpip.c b/example/direct_tcpip.c index 34fecd3c..1d9b041a 100644 --- a/example/direct_tcpip.c +++ b/example/direct_tcpip.c @@ -22,9 +22,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/scp_write_nonblock.c b/example/scp_write_nonblock.c index 7a2754ee..2cc176c2 100644 --- a/example/scp_write_nonblock.c +++ b/example/scp_write_nonblock.c @@ -20,9 +20,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/sftp_RW_nonblock.c b/example/sftp_RW_nonblock.c index 22fceb73..023aefbd 100644 --- a/example/sftp_RW_nonblock.c +++ b/example/sftp_RW_nonblock.c @@ -27,9 +27,6 @@ #ifdef HAVE_NETINET_IN_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/sftp_write_nonblock.c b/example/sftp_write_nonblock.c index 5c662f83..958084b7 100644 --- a/example/sftp_write_nonblock.c +++ b/example/sftp_write_nonblock.c @@ -26,9 +26,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/sftp_write_sliding.c b/example/sftp_write_sliding.c index 15cca39c..b8169002 100644 --- a/example/sftp_write_sliding.c +++ b/example/sftp_write_sliding.c @@ -26,9 +26,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/ssh2_agent_forwarding.c b/example/ssh2_agent_forwarding.c index 3583daaa..8718e885 100644 --- a/example/ssh2_agent_forwarding.c +++ b/example/ssh2_agent_forwarding.c @@ -29,9 +29,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/ssh2_echo.c b/example/ssh2_echo.c index f918a1cc..1fa87b7a 100644 --- a/example/ssh2_echo.c +++ b/example/ssh2_echo.c @@ -24,9 +24,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/ssh2_exec.c b/example/ssh2_exec.c index 4c368bf3..d180f62d 100644 --- a/example/ssh2_exec.c +++ b/example/ssh2_exec.c @@ -26,9 +26,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/tcpip-forward.c b/example/tcpip-forward.c index 874ff51f..a0b1cd69 100644 --- a/example/tcpip-forward.c +++ b/example/tcpip-forward.c @@ -22,9 +22,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #include #include diff --git a/example/x11.c b/example/x11.c index 87d1aa3b..6254519f 100644 --- a/example/x11.c +++ b/example/x11.c @@ -29,9 +29,6 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif #ifdef HAVE_SYS_UN_H #include #endif diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index b3b65179..70c2ee47 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -64,7 +64,6 @@ # ifdef HAVE_SYS_SELECT_H # include # else -# include # include # endif # endif @@ -112,12 +111,22 @@ /* Use local implementation when not available */ #if !defined(HAVE_SNPRINTF) -#define LIBSSH2_SNPRINTF #undef snprintf #define snprintf _libssh2_snprintf +#define LIBSSH2_SNPRINTF int _libssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...); #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 +#endif + /* "inline" keyword is valid only with C++ engine! */ #ifdef __GNUC__ #undef inline diff --git a/src/misc.c b/src/misc.c index c7ec0ffe..8151eab7 100644 --- a/src/misc.c +++ b/src/misc.c @@ -46,10 +46,6 @@ #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif - #ifdef WIN32 /* Force parameter type. */ #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) { firstsec = now.tv_sec; } @@ -672,8 +668,8 @@ void _libssh2_list_insert(struct list_node *after, /* insert before this */ #endif -/* this define is defined in misc.h for the correct platforms */ -#ifdef LIBSSH2_GETTIMEOFDAY_WIN32 +/* Defined in libssh2_priv.h for the correct platforms */ +#ifdef LIBSSH2_GETTIMEOFDAY /* * _libssh2_gettimeofday * Implementation according to: @@ -696,27 +692,31 @@ void _libssh2_list_insert(struct list_node *after, /* insert before this */ * Danny Smith */ -/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ -#define _W32_FT_OFFSET (116444736000000000) - -int __cdecl _libssh2_gettimeofday(struct timeval *tp, void *tzp) +int _libssh2_gettimeofday(struct timeval *tp, void *tzp) { - union { - unsigned __int64 ns100; /* time since 1 Jan 1601 in 100ns units */ - FILETIME ft; - } _now; (void)tzp; 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); 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. Do not set errno on error. */ return 0; } - - #endif void *_libssh2_calloc(LIBSSH2_SESSION* session, size_t size) diff --git a/src/misc.h b/src/misc.h index 579b7ce8..d8a26c87 100644 --- a/src/misc.h +++ b/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_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, const unsigned char *input1, const unsigned char *input2, diff --git a/src/packet.c b/src/packet.c index 33efad8f..2bf37a20 100644 --- a/src/packet.c +++ b/src/packet.c @@ -46,10 +46,6 @@ #include #endif -#ifdef HAVE_SYS_TIME_H -#include -#endif - #ifdef HAVE_INTTYPES_H #include #endif diff --git a/src/session.c b/src/session.c index a96c9d88..7b1f6828 100644 --- a/src/session.c +++ b/src/session.c @@ -50,9 +50,6 @@ #include #include -#ifdef HAVE_GETTIMEOFDAY -#include -#endif #ifdef HAVE_ALLOCA_H #include #endif @@ -1721,23 +1718,15 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout) } #ifdef HAVE_POLL -#ifdef HAVE_LIBSSH2_GETTIMEOFDAY { struct timeval tv_begin, tv_end; - _libssh2_gettimeofday((struct timeval *) &tv_begin, NULL); + gettimeofday(&tv_begin, NULL); 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_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) { for(i = 0; i < nfds; i++) { @@ -1784,24 +1773,17 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout) #elif defined(HAVE_SELECT) tv.tv_sec = timeout_remaining / 1000; tv.tv_usec = (timeout_remaining % 1000) * 1000; -#ifdef HAVE_LIBSSH2_GETTIMEOFDAY + { 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); - _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_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) { for(i = 0; i < nfds; i++) {