mirror of
https://github.com/ntop/n2n.git
synced 2024-09-20 00:51:10 +02:00
changed timer source (#522)
* changed timer source * changed timer source * changed timer source * changed timer source * changed timer source * changed timer source
This commit is contained in:
parent
c45da8714b
commit
0298efa36e
|
@ -48,8 +48,8 @@ N2N_LIB=libn2n.a
|
||||||
N2N_OBJS=$(patsubst src/%.c, src/%.o, $(wildcard src/*.c))
|
N2N_OBJS=$(patsubst src/%.c, src/%.o, $(wildcard src/*.c))
|
||||||
N2N_DEPS=$(wildcard include/*.h) $(wildcard src/*.c) Makefile $(N2N_LIB)
|
N2N_DEPS=$(wildcard include/*.h) $(wildcard src/*.c) Makefile $(N2N_LIB)
|
||||||
|
|
||||||
LIBS_EDGE+=$(LIBS_EDGE_OPT)
|
LIBS_EDGE+=-lrt $(LIBS_EDGE_OPT)
|
||||||
LIBS_SN=
|
LIBS_SN=-lrt
|
||||||
|
|
||||||
#For OpenSolaris (Solaris too?)
|
#For OpenSolaris (Solaris too?)
|
||||||
ifeq ($(shell uname), SunOS)
|
ifeq ($(shell uname), SunOS)
|
||||||
|
|
|
@ -54,7 +54,6 @@
|
||||||
|
|
||||||
#define PACKAGE_BUILDDATE (__DATE__ " " __TIME__)
|
#define PACKAGE_BUILDDATE (__DATE__ " " __TIME__)
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
@ -121,6 +120,7 @@
|
||||||
#define closesocket(a) close(a)
|
#define closesocket(a) close(a)
|
||||||
#endif /* #ifndef WIN32 */
|
#endif /* #ifndef WIN32 */
|
||||||
|
|
||||||
|
#include <time.h> /* should be included after eventually including unistd.h for _POSIX_TIMERS macro gets correctly defined */
|
||||||
#include "minilzo.h"
|
#include "minilzo.h"
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
30
src/n2n.c
30
src/n2n.c
|
@ -546,19 +546,31 @@ int gettimeofday(struct timeval *tp, void *tzp) {
|
||||||
// returns a time stamp for use with replay protection
|
// returns a time stamp for use with replay protection
|
||||||
uint64_t time_stamp (void) {
|
uint64_t time_stamp (void) {
|
||||||
|
|
||||||
struct timeval tod;
|
|
||||||
uint64_t micro_seconds;
|
uint64_t micro_seconds;
|
||||||
|
#if defined (_POSIX_TIMERS)
|
||||||
|
struct timespec t;
|
||||||
|
clock_gettime(CLOCK_REALTIME, &t);
|
||||||
|
#else
|
||||||
|
struct timeval t;
|
||||||
|
gettimeofday (&t, NULL);
|
||||||
|
#endif
|
||||||
|
|
||||||
gettimeofday (&tod, NULL);
|
|
||||||
/* We will (roughly) calculate the microseconds since 1970 leftbound into the return value.
|
/* We will (roughly) calculate the microseconds since 1970 leftbound into the return value.
|
||||||
The leading 32 bits are used for tv_sec. The following 20 bits (sufficent as microseconds
|
The leading 32 bits are used for tv_sec. The following 20 bits (sufficent as microseconds
|
||||||
fraction never exceeds 1,000,000,) encode the value tv_usec. The remaining lowest 12 bits
|
fraction never exceeds 1,000,000,) encode the value tv_nsec/1024 ( ~ usec) or tv_usec
|
||||||
are kept random for use in IV */
|
respectively. The remaining lowest 12 bits are kept random for use in IV */
|
||||||
micro_seconds = n2n_rand();
|
micro_seconds = (uint64_t)(t.tv_sec) << 32;
|
||||||
micro_seconds = ( (((uint64_t)(tod.tv_sec) << 32) + (tod.tv_usec << 12))
|
#if defined (_POSIX_TIMERS)
|
||||||
| (micro_seconds >> 52) );
|
micro_seconds += (t.tv_nsec >> 10) << 12;
|
||||||
// more exact but more costly due to the multiplication:
|
#else
|
||||||
// micro_seconds = (tod.tv_sec * 1000000 + tod.tv_usec) << 12) | ...
|
micro_seconds += t.tv_usec << 12;
|
||||||
|
#endif
|
||||||
|
micro_seconds |= (uint64_t)n2n_rand() >> 52;
|
||||||
|
|
||||||
|
// to do the following would be more exact but also
|
||||||
|
// more costly due to the multiplication and divison:
|
||||||
|
// micro_seconds = (t.tv_sec * 1000000 + t.tv_nsec / 1000) << 12) | ... or
|
||||||
|
// micro_seconds = (t.tv_sec * 1000000 + t.tv_usec) << 12) | ...
|
||||||
|
|
||||||
return (micro_seconds);
|
return (micro_seconds);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user