From 564aff28aa6dc6f0d567844dc737110e0c619dbd Mon Sep 17 00:00:00 2001 From: "Joseph C. Lehner" Date: Fri, 18 Nov 2016 17:39:04 +0100 Subject: [PATCH] Use time_monotonic for timeouts --- Makefile | 19 ++++++------------- ethsock.c | 4 ++-- nmrp.c | 4 ++-- nmrpd.h | 2 ++ nmrpflash.dev | 12 +++++++++++- util.c | 13 +++---------- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index d05011e..175ff06 100644 --- a/Makefile +++ b/Makefile @@ -5,22 +5,15 @@ LIBS = -lpcap CFLAGS += -Wall -g -DNMRPFLASH_VERSION=\"$(VERSION)\" LDFLAGS += $(LIBS) -.PHONY: clean install release release/osx release/linux release/win32 - -nmrpflash: nmrp.o tftp.o ethsock.o main.o - $(CC) $(CFLAGS) -o nmrpflash nmrp.o tftp.o ethsock.o main.o $(LDFLAGS) +nmrpflash_OBJ = nmrp.o tftp.o ethsock.o main.o util.o -nmrp.o: nmrp.c nmrpd.h - $(CC) $(CFLAGS) -c -o nmrp.o nmrp.c - -tftp.o: tftp.c nmrpd.h - $(CC) $(CFLAGS) -c -o tftp.o tftp.c +.PHONY: clean install release release/osx release/linux release/win32 -ethsock.o: ethsock.c nmrpd.h - $(CC) $(CFLAGS) -c -o ethsock.o ethsock.c +nmrpflash: $(nmrpflash_OBJ) + $(CC) $(CFLAGS) -o nmrpflash $(nmrpflash_OBJ) $(LDFLAGS) -main.o: main.c nmrpd.h - $(CC) $(CFLAGS) -c -o main.o main.c +%.o: %.c nmrpd.h + $(CC) -c $(CFLAGS) $< -o $@ fuzz: clean CC=afl-gcc CFLAGS=-DNMRPFLASH_FUZZ make nmrpflash diff --git a/ethsock.c b/ethsock.c index 5312e3b..1c6a039 100644 --- a/ethsock.c +++ b/ethsock.c @@ -755,12 +755,12 @@ int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struc } set_addr(&sin, ipaddr); - clock_t now = clock(); + time_t beg = time_monotonic(); /* Wait until the new IP has actually been added */ while (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) != 0) { - if (((clock() - now) / CLOCKS_PER_SEC) >= 5) { + if ((time_monotonic() - beg) >= 5) { fprintf(stderr, "Failed to bind after 5 seconds: "); sock_perror("bind"); DeleteIPAddress((*undo)->context); diff --git a/nmrp.c b/nmrp.c index b36983a..7f65e56 100644 --- a/nmrp.c +++ b/nmrp.c @@ -538,7 +538,7 @@ int nmrp_do(struct nmrpd_args *args) i = 0; upload_ok = 0; - beg = time(NULL); + beg = time_monotonic(); while (1) { printf("\rAdvertising NMRP server on %s ... %c", @@ -557,7 +557,7 @@ int nmrp_do(struct nmrpd_args *args) } else if (status == 1) { goto out; } else { - if ((time(NULL) - beg) >= 60) { + if ((time_monotonic() - beg) >= 60) { printf("\nNo response after 60 seconds. Bailing out.\n"); goto out; } diff --git a/nmrpd.h b/nmrpd.h index d54e4eb..ac54949 100644 --- a/nmrpd.h +++ b/nmrpd.h @@ -129,4 +129,6 @@ int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback, int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo); int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo); + +time_t time_monotonic(); #endif diff --git a/nmrpflash.dev b/nmrpflash.dev index 7015c12..b7e1253 100644 --- a/nmrpflash.dev +++ b/nmrpflash.dev @@ -29,7 +29,7 @@ IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=3 CompilerSettings=0000000100000000000001000 -UnitCount=5 +UnitCount=6 [VersionInfo] Major=1 @@ -100,3 +100,13 @@ Priority=1000 OverrideBuildCmd=0 BuildCmd= +[Unit6] +FileName=util.c +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/util.c b/util.c index ff2b191..4743af3 100644 --- a/util.c +++ b/util.c @@ -1,4 +1,6 @@ #include +#include +#include "nmrpd.h" #ifdef NMRPFLASH_OSX #include @@ -10,7 +12,7 @@ time_t time_monotonic() #ifndef NMRPFLASH_OSX struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.ts_sec; + return ts.tv_sec; #else static double factor = 0.0; mach_timebase_info_data_t timebase; @@ -25,12 +27,3 @@ time_t time_monotonic() return round(GetTickCount() / 1000.0); #endif } - -int main() -{ - time_t beg = time_monotonic(); - printf("now: %ld\n", beg); - sleep(2); - printf("+2s: %ld\n", time_monotonic()); - return 0; -} -- 2.25.1