2003-04-16 19:04:58 +02:00
|
|
|
/* Copyright 2003 Roger Dingledine */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
2003-08-12 05:08:41 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
#include <stdlib.h>
|
2003-04-17 01:22:05 +02:00
|
|
|
#include <limits.h>
|
2003-08-12 05:08:41 +02:00
|
|
|
#if _MSC_VER > 1300
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FCNTL_H
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2003-05-20 08:37:34 +02:00
|
|
|
void *tor_malloc(size_t size) {
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
result = malloc(size);
|
|
|
|
|
|
|
|
if(!result) {
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_ERR, "Out of memory. Dying.");
|
2003-05-20 08:37:34 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
void
|
|
|
|
my_gettimeofday(struct timeval *timeval)
|
|
|
|
{
|
2003-08-12 05:08:41 +02:00
|
|
|
#ifdef HAVE_GETTIMEOFDAY
|
2003-04-16 19:04:58 +02:00
|
|
|
if (gettimeofday(timeval, NULL)) {
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_ERR, "gettimeofday failed.");
|
2003-04-16 19:04:58 +02:00
|
|
|
/* If gettimeofday dies, we have either given a bad timezone (we didn't),
|
|
|
|
or segfaulted.*/
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-08-12 05:08:41 +02:00
|
|
|
#elif defined(HAVE_FTIME)
|
|
|
|
ftime(timeval);
|
|
|
|
#else
|
|
|
|
#error "No way to get time."
|
|
|
|
#endif
|
2003-04-16 19:04:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
tv_udiff(struct timeval *start, struct timeval *end)
|
|
|
|
{
|
2003-04-17 01:22:05 +02:00
|
|
|
long udiff;
|
2003-04-17 03:59:41 +02:00
|
|
|
long end_usec = end->tv_usec;
|
2003-04-16 19:04:58 +02:00
|
|
|
long secdiff = end->tv_sec - start->tv_sec;
|
2003-04-17 03:59:41 +02:00
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
if (secdiff+1 > LONG_MAX/1000000) {
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_NOTICE, "comparing times too far apart.");
|
2003-04-16 19:04:58 +02:00
|
|
|
return LONG_MAX;
|
|
|
|
}
|
2003-04-17 04:03:55 +02:00
|
|
|
|
2003-04-17 03:59:41 +02:00
|
|
|
udiff = secdiff*1000000L + (end_usec - start->tv_usec);
|
2003-04-17 01:22:05 +02:00
|
|
|
if(udiff < 0) {
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_NOTICE, "start is after end. Returning 0.");
|
2003-04-17 01:22:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return udiff;
|
2003-04-16 19:04:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int tv_cmp(struct timeval *a, struct timeval *b) {
|
|
|
|
if (a->tv_sec > b->tv_sec)
|
|
|
|
return 1;
|
|
|
|
if (a->tv_sec < b->tv_sec)
|
|
|
|
return -1;
|
|
|
|
if (a->tv_usec > b->tv_usec)
|
|
|
|
return 1;
|
|
|
|
if (a->tv_usec < b->tv_usec)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tv_add(struct timeval *a, struct timeval *b) {
|
|
|
|
a->tv_usec += b->tv_usec;
|
|
|
|
a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
|
|
|
|
a->tv_usec %= 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tv_addms(struct timeval *a, long ms) {
|
|
|
|
a->tv_usec += (ms * 1000) % 1000000;
|
|
|
|
a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
|
|
|
|
a->tv_usec %= 1000000;
|
|
|
|
}
|
2003-08-12 05:08:41 +02:00
|
|
|
|
|
|
|
void set_socket_nonblocking(int socket)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* Yes means no and no means yes. Do you not want to be nonblocking? */
|
|
|
|
int nonblocking = 0;
|
|
|
|
ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
|
|
|
|
#else
|
|
|
|
fcntl(socket, F_SETFL, O_NONBLOCK);
|
|
|
|
#endif
|
|
|
|
}
|