From 79f48e48813322d12edbfb7ccb90649e616f54c6 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 5 Aug 2011 23:46:12 +0200 Subject: [PATCH] gettimeofday: fix name space pollution For systems without its own gettimeofday() implementation, we still must not provide one outside our namespace. Reported by: Bill Segall --- src/misc.c | 13 +++++-------- src/misc.h | 15 ++++++++++++++- src/session.c | 13 +++++++------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/misc.c b/src/misc.c index f0bb390..cc07fc6 100644 --- a/src/misc.c +++ b/src/misc.c @@ -424,7 +424,7 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...) } } - gettimeofday(&now, NULL); + _libssh2_gettimeofday(&now, NULL); if(!firstsec) { firstsec = now.tv_sec; } @@ -563,16 +563,15 @@ void _libssh2_list_insert(struct list_node *after, /* insert before this */ #endif - - -#if defined(LIBSSH2_WIN32) && !defined(__MINGW32__) +/* this define is defined in misc.h for the correct platforms */ +#ifdef LIBSSH2_GETTIMEOFDAY_WIN32 /* * gettimeofday * Implementation according to: * The Open Group Base Specifications Issue 6 * IEEE Std 1003.1, 2004 Edition */ - + /* * THIS SOFTWARE IS NOT COPYRIGHTED * @@ -591,9 +590,7 @@ void _libssh2_list_insert(struct list_node *after, /* insert before this */ /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ #define _W32_FT_OFFSET (116444736000000000) - -int __cdecl gettimeofday(struct timeval *tp, - void *tzp) +int __cdecl _libssh2_gettimeofday(struct timeval *tp, void *tzp) { union { unsigned __int64 ns100; /*time since 1 Jan 1601 in 100ns units */ diff --git a/src/misc.h b/src/misc.h index 453f79b..bcb7d0e 100644 --- a/src/misc.h +++ b/src/misc.h @@ -1,6 +1,6 @@ #ifndef __LIBSSH2_MISC_H #define __LIBSSH2_MISC_H -/* Copyright (c) 2009-2010 by Daniel Stenberg +/* Copyright (c) 2009-2011 by Daniel Stenberg * * All rights reserved. * @@ -78,4 +78,17 @@ void _libssh2_htonu32(unsigned char *buf, uint32_t val); void _libssh2_store_u32(unsigned char **buf, uint32_t value); void _libssh2_store_str(unsigned char **buf, const char *str, size_t len); +#if defined(LIBSSH2_WIN32) && !defined(__MINGW32__) +/* 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 + #endif /* _LIBSSH2_MISC_H */ diff --git a/src/session.c b/src/session.c index 3cf92ec..46ef016 100644 --- a/src/session.c +++ b/src/session.c @@ -56,6 +56,7 @@ #include "session.h" #include "channel.h" #include "mac.h" +#include "misc.h" /* libssh2_default_alloc */ @@ -1573,13 +1574,13 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout) } #ifdef HAVE_POLL -#ifdef HAVE_GETTIMEOFDAY +#ifdef HAVE_LIBSSH2_GETTIMEOFDAY { struct timeval tv_begin, tv_end; - gettimeofday((struct timeval *) &tv_begin, NULL); + _libssh2_gettimeofday((struct timeval *) &tv_begin, NULL); sysret = poll(sockets, nfds, timeout_remaining); - gettimeofday((struct timeval *) &tv_end, NULL); + _libssh2_gettimeofday((struct timeval *) &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; } @@ -1633,13 +1634,13 @@ 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_GETTIMEOFDAY +#ifdef HAVE_LIBSSH2_GETTIMEOFDAY { struct timeval tv_begin, tv_end; - gettimeofday((struct timeval *) &tv_begin, NULL); + _libssh2_gettimeofday((struct timeval *) &tv_begin, NULL); sysret = select(maxfd+1, &rfds, &wfds, NULL, &tv); - gettimeofday((struct timeval *) &tv_end, NULL); + _libssh2_gettimeofday((struct timeval *) &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;