pidgin/pidgin

zephyr: Replace gettimeofday by GLib functions

2020-11-22, Elliott Sales de Andrade
6260f39c7c65
Parents 16bba33a970f
Children 6f7bbd42d36c
zephyr: Replace gettimeofday by GLib functions

* Use monotonic time for some timeouts.
* Use `g_get_real_time` instead of `gettimeofday` for remaining uses.

Testing Done:
Compile only

Reviewed at https://reviews.imfreedom.org/r/248/
--- a/libpurple/protocols/zephyr/ZWait4Not.c Sat Nov 21 22:54:04 2020 -0600
+++ b/libpurple/protocols/zephyr/ZWait4Not.c Sun Nov 22 01:44:50 2020 -0600
@@ -11,21 +11,6 @@
#ifdef WIN32
#include <winsock2.h>
-
-#ifndef ZEPHYR_USES_KERBEROS
-static int gettimeofday(struct timeval* tv, struct timezone* tz){
- union {
- long long ns100; /*time since 1 Jan 1601 in 100ns units */
- FILETIME ft;
- } _now;
-
- GetSystemTimeAsFileTime( &(_now.ft) );
- tv->tv_usec=(long)((_now.ns100 / 10LL) % 1000000LL );
- tv->tv_sec= (long)((_now.ns100-(116444736000000000LL))/10000000LL);
- return 0;
- }
-#endif
-
#else
#include <sys/socket.h>
#endif
@@ -35,7 +20,8 @@
int timeout)
{
Code_t retval;
- struct timeval tv, t0;
+ gint64 t0, tdiff;
+ struct timeval tv;
fd_set fdmask;
int i, fd;
@@ -50,8 +36,7 @@
FD_ZERO (&fdmask);
tv.tv_sec = timeout;
tv.tv_usec = 0;
- gettimeofday (&t0, (struct timezone *)NULL);
- t0.tv_sec += timeout;
+ t0 = g_get_monotonic_time() + timeout * G_USEC_PER_SEC;
while (1) {
FD_SET (fd, &fdmask);
i = select (fd + 1, &fdmask, (fd_set *) 0, (fd_set *) 0, &tv);
@@ -65,14 +50,9 @@
if (retval != ZERR_NONOTICE) /* includes ZERR_NONE */
return retval;
}
- gettimeofday (&tv, (struct timezone *) NULL);
- tv.tv_usec = t0.tv_usec - tv.tv_usec;
- if (tv.tv_usec < 0) {
- tv.tv_usec += 1000000;
- tv.tv_sec = t0.tv_sec - tv.tv_sec - 1;
- }
- else
- tv.tv_sec = t0.tv_sec - tv.tv_sec;
+ tdiff = t0 - g_get_monotonic_time();
+ tv.tv_sec = tdiff / G_USEC_PER_SEC;
+ tv.tv_usec = tdiff - tv.tv_sec * G_USEC_PER_SEC;
}
/*NOTREACHED*/
}
--- a/libpurple/protocols/zephyr/Zinternal.c Sat Nov 21 22:54:04 2020 -0600
+++ b/libpurple/protocols/zephyr/Zinternal.c Sun Nov 22 01:44:50 2020 -0600
@@ -12,21 +12,6 @@
#include "internal.h"
#ifdef WIN32
#include <winsock2.h>
-
-#ifndef ZEPHYR_USES_KERBEROS
- int gettimeofday(struct timeval* p, struct timezone* tz ){
- union {
- long long ns100; /*time since 1 Jan 1601 in 100ns units */
- FILETIME ft;
- } _now;
-
- GetSystemTimeAsFileTime( &(_now.ft) );
- p->tv_usec=(long)((_now.ns100 / 10LL) % 1000000LL );
- p->tv_sec= (long)((_now.ns100-(116444736000000000LL))/10000000LL);
- return 0;
- }
-#endif
-
#else
#include <arpa/inet.h>
#include <sys/socket.h>
@@ -192,23 +177,25 @@
static struct _Z_InputQ *Z_SearchQueue(ZUnique_Id_t *uid, ZNotice_Kind_t kind)
{
- register struct _Z_InputQ *qptr;
- struct _Z_InputQ *next;
- struct timeval tv;
+ register struct _Z_InputQ *qptr;
+ struct _Z_InputQ *next;
+ gint64 now;
- (void) gettimeofday(&tv, (struct timezone *)0);
+ now = g_get_monotonic_time();
- qptr = __Q_Head;
+ qptr = __Q_Head;
- while (qptr) {
- if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind)
- return (qptr);
- next = qptr->next;
- if (qptr->timep && ((time_t)qptr->timep+Z_NOTICETIMELIMIT < tv.tv_sec))
- Z_RemQueue(qptr);
- qptr = next;
- }
- return (NULL);
+ while (qptr) {
+ if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind) {
+ return qptr;
+ }
+ next = qptr->next;
+ if (qptr->time && qptr->time + Z_NOTICETIMELIMIT < now) {
+ Z_RemQueue(qptr);
+ }
+ qptr = next;
+ }
+ return NULL;
}
/*
@@ -474,16 +461,15 @@
{
int last, oldfirst, oldlast;
struct _Z_Hole *hole, *lasthole;
- struct timeval tv;
- /* Incorporate this notice's checked authentication. */
- if (notice->z_checked_auth == ZAUTH_FAILED)
- qptr->auth = ZAUTH_FAILED;
- else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED)
- qptr->auth = ZAUTH_NO;
+ /* Incorporate this notice's checked authentication. */
+ if (notice->z_checked_auth == ZAUTH_FAILED) {
+ qptr->auth = ZAUTH_FAILED;
+ } else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED) {
+ qptr->auth = ZAUTH_NO;
+ }
- (void) gettimeofday(&tv, (struct timezone *)0);
- qptr->timep = tv.tv_sec;
+ qptr->time = g_get_monotonic_time();
last = part+notice->z_message_len-1;
@@ -572,7 +558,7 @@
if (!qptr->complete)
__Q_CompleteLength++;
qptr->complete = 1;
- qptr->timep = 0; /* don't time out anymore */
+ qptr->time = 0; /* don't time out anymore */
qptr->packet_len = qptr->header_len+qptr->msg_len;
if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len)))
return (ENOMEM);
@@ -590,6 +576,7 @@
{
Code_t retval;
static char version[BUFSIZ]; /* default init should be all \0 */
+ gint64 realtime;
struct sockaddr_in name;
socklen_t namelen = sizeof(name);
@@ -610,9 +597,12 @@
notice->z_multinotice = "";
- (void) gettimeofday(&notice->z_uid.tv, (struct timezone *)0);
- notice->z_uid.tv.tv_sec = htonl((unsigned long) notice->z_uid.tv.tv_sec);
- notice->z_uid.tv.tv_usec = htonl((unsigned long) notice->z_uid.tv.tv_usec);
+ realtime = g_get_real_time();
+ notice->z_uid.tv.tv_sec = realtime / G_USEC_PER_SEC;
+ notice->z_uid.tv.tv_usec =
+ realtime - notice->z_uid.tv.tv_sec * G_USEC_PER_SEC;
+ notice->z_uid.tv.tv_sec = htonl((unsigned long)notice->z_uid.tv.tv_sec);
+ notice->z_uid.tv.tv_usec = htonl((unsigned long)notice->z_uid.tv.tv_usec);
(void) memcpy(&notice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr));
@@ -867,12 +857,14 @@
(void) sprintf(multi, "%d/%d", offset, notice->z_message_len);
partnotice.z_multinotice = multi;
if (offset > 0) {
- (void) gettimeofday(&partnotice.z_uid.tv,
- (struct timezone *)0);
- partnotice.z_uid.tv.tv_sec =
- htonl((unsigned long) partnotice.z_uid.tv.tv_sec);
- partnotice.z_uid.tv.tv_usec =
- htonl((unsigned long) partnotice.z_uid.tv.tv_usec);
+ gint64 realtime = g_get_real_time();
+ partnotice.z_uid.tv.tv_sec = realtime / G_USEC_PER_SEC;
+ partnotice.z_uid.tv.tv_usec =
+ realtime - partnotice.z_uid.tv.tv_sec * G_USEC_PER_SEC;
+ partnotice.z_uid.tv.tv_sec =
+ htonl((unsigned long)partnotice.z_uid.tv.tv_sec);
+ partnotice.z_uid.tv.tv_usec =
+ htonl((unsigned long)partnotice.z_uid.tv.tv_usec);
(void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
sizeof(__My_addr));
}
--- a/libpurple/protocols/zephyr/internal.h Sat Nov 21 22:54:04 2020 -0600
+++ b/libpurple/protocols/zephyr/internal.h Sun Nov 22 01:44:50 2020 -0600
@@ -65,7 +65,7 @@
struct _Z_InputQ *next;
struct _Z_InputQ *prev;
ZNotice_Kind_t kind;
- unsigned ZEPHYR_INT32 timep;
+ gint64 time;
int packet_len;
char *packet;
int complete;