if LEASEFILE_USE_REMAINING_TIME is defined, only the remaining time is stored

see #295
This commit is contained in:
Thomas Bernard 2018-04-30 17:25:26 +02:00
parent 00abd9e6c8
commit 17773f0a72
1 changed files with 14 additions and 2 deletions

View File

@ -109,9 +109,12 @@ lease_file_add(unsigned short eport,
return -1; return -1;
} }
/* convert our time to unix time */ /* convert our time to unix time
* if LEASEFILE_USE_REMAINING_TIME is defined, only the remaining time is stored */
timestamp -= upnp_time(); timestamp -= upnp_time();
#ifndef LEASEFILE_USE_REMAINING_TIME
timestamp += time(NULL); timestamp += time(NULL);
#endif
fprintf(fd, "%s:%hu:%s:%hu:%u:%s\n", fprintf(fd, "%s:%hu:%s:%hu:%u:%s\n",
proto_itoa(proto), eport, iaddr, iport, proto_itoa(proto), eport, iaddr, iport,
@ -194,7 +197,9 @@ int reload_from_lease_file()
unsigned int leaseduration; unsigned int leaseduration;
unsigned int timestamp; unsigned int timestamp;
time_t current_time; time_t current_time;
#ifndef LEASEFILE_USE_REMAINING_TIME
time_t current_unix_time; time_t current_unix_time;
#endif
char line[128]; char line[128];
int r; int r;
@ -209,7 +214,9 @@ int reload_from_lease_file()
} }
current_time = upnp_time(); current_time = upnp_time();
#ifndef LEASEFILE_USE_REMAINING_TIME
current_unix_time = time(NULL); current_unix_time = time(NULL);
#endif
while(fgets(line, sizeof(line), fd)) { while(fgets(line, sizeof(line), fd)) {
syslog(LOG_DEBUG, "parsing lease file line '%s'", line); syslog(LOG_DEBUG, "parsing lease file line '%s'", line);
proto = line; proto = line;
@ -257,13 +264,18 @@ int reload_from_lease_file()
*(p--) = '\0'; *(p--) = '\0';
if(timestamp > 0) { if(timestamp > 0) {
#ifdef LEASEFILE_USE_REMAINING_TIME
leaseduration = timestamp;
timestamp += current_time; /* convert to our time */
#else
if(timestamp <= (unsigned int)current_unix_time) { if(timestamp <= (unsigned int)current_unix_time) {
syslog(LOG_NOTICE, "already expired lease in lease file"); syslog(LOG_NOTICE, "already expired lease in lease file");
continue; continue;
} else { } else {
leaseduration = timestamp - current_unix_time; leaseduration = timestamp - current_unix_time;
timestamp = lease_duration + current_time; timestamp = lease_duration + current_time; /* convert to our time */
} }
#endif
} else { } else {
leaseduration = 0; /* default value */ leaseduration = 0; /* default value */
} }