add option LEASEFILE_USE_REMAINING_TIME

new function lease_file_rewrite()
that is called just before exiting, and when SIGUSR2 is received
see #295
This commit is contained in:
Thomas Bernard 2018-05-02 09:40:12 +02:00
parent d0e7958617
commit a92138345b
6 changed files with 65 additions and 0 deletions

View File

@ -1,7 +1,11 @@
$Id: Changelog.txt,v 1.439 2018/04/12 09:32:22 nanard Exp $
2018/05/02:
option to store remaining time in leasefile
2018/04/12:
pf: set dst address in rule if use_ext_ip_addr is set
2018/04/06:
Add options for netfilter scripts

View File

@ -185,6 +185,7 @@ or if your linux system use /etc/init.d/
miniupnpd handles the following signals :
SIGUSR1: Send public IP address change notification
SIGUSR2: Handle special actions in Tomato Firmware version
Or rewrite the lease_file
SIGINT: Close gracefully
SIGTERM: Close gracefully
SIGPIPE: Ignore

View File

@ -472,6 +472,8 @@ if [ -n "$LEASEFILE" ] ; then
else
echo "/*#define ENABLE_LEASEFILE*/" >> ${CONFIGFILE}
fi
echo "/* Uncomment the following line to store remaining time in lease file */" >> ${CONFIGFILE}
echo "/*#define LEASEFILE_USE_REMAINING_TIME*/" >> ${CONFIGFILE}
echo "" >> ${CONFIGFILE}
echo "/* Uncomment the following line to enable port in use check */" >> ${CONFIGFILE}

View File

@ -112,6 +112,9 @@ int get_udp_dst_port (char *payload);
/* variables used by signals */
static volatile sig_atomic_t quitting = 0;
volatile sig_atomic_t should_send_public_address_change_notif = 0;
#if !defined(TOMATO) && defined(ENABLE_LEASEFILE) && defined(LEASEFILE_USE_REMAINING_TIME)
volatile sig_atomic_t should_rewrite_leasefile = 0;
#endif /* !TOMATO && ENABLE_LEASEFILE && LEASEFILE_USE_REMAINING_TIME */
#ifdef TOMATO
#if 1
@ -792,6 +795,16 @@ sigusr1(int sig)
should_send_public_address_change_notif = 1;
}
#if !defined(TOMATO) && defined(ENABLE_LEASEFILE) && defined(LEASEFILE_USE_REMAINING_TIME)
/* Handler for the SIGUSR2 signal to request rewrite of lease_file */
static void
sigusr2(int sig)
{
UNUSED(sig);
should_rewrite_leasefile = 1;
}
#endif /* !TOMATO && ENABLE_LEASEFILE && LEASEFILE_USE_REMAINING_TIME */
/* record the startup time, for returning uptime */
static void
set_startup_time(void)
@ -1678,6 +1691,13 @@ init(int argc, char * * argv, struct runtime_vars * v)
{
syslog(LOG_NOTICE, "Failed to set %s handler", "SIGUSR1");
}
#if !defined(TOMATO) && defined(ENABLE_LEASEFILE) && defined(LEASEFILE_USE_REMAINING_TIME)
sa.sa_handler = sigusr2;
if(sigaction(SIGUSR2, &sa, NULL) < 0)
{
syslog(LOG_NOTICE, "Failed to set %s handler", "SIGUSR2");
}
#endif /* !TOMATO && ENABLE_LEASEFILE && LEASEFILE_USE_REMAINING_TIME */
/* initialize random number generator */
srandom((unsigned int)time(NULL));
@ -2071,6 +2091,13 @@ main(int argc, char * * argv)
upnp_bootid = time(NULL);
}
#endif
#if !defined(TOMATO) && defined(ENABLE_LEASEFILE) && defined(LEASEFILE_USE_REMAINING_TIME)
if(should_rewrite_leasefile)
{
lease_file_rewrite();
should_rewrite_leasefile = 0;
}
#endif /* !TOMATO && ENABLE_LEASEFILE && LEASEFILE_USE_REMAINING_TIME */
/* send public address change notifications if needed */
if(should_send_public_address_change_notif)
{
@ -2623,6 +2650,9 @@ shutdown:
#ifdef TOMATO
tomato_save("/etc/upnp/data");
#endif /* TOMATO */
#if defined(ENABLE_LEASEFILE) && defined(LEASEFILE_USE_REMAINING_TIME)
lease_file_rewrite();
#endif /* ENABLE_LEASEFILE && LEASEFILE_USE_REMAINING_TIME */
/* close out open sockets */
while(upnphttphead.lh_first != NULL)
{

View File

@ -295,6 +295,31 @@ int reload_from_lease_file()
return 0;
}
#ifdef LEASEFILE_USE_REMAINING_TIME
void lease_file_rewrite(void)
{
int index;
unsigned short eport, iport;
int proto;
char iaddr[32];
char desc[64];
char rhost[40];
unsigned int timestamp;
if (lease_file == NULL) return;
remove(lease_file);
for(index = 0; ; index++) {
if(get_redirect_rule_by_index(index, 0/*ifname*/, &eport, iaddr, sizeof(iaddr),
&iport, &proto, desc, sizeof(desc),
rhost, sizeof(rhost), &timestamp,
0, 0) < 0)
break;
if(lease_file_add(eport, iaddr, iport, proto, desc, timestamp) < 0)
break;
}
}
#endif
#endif
/* upnp_redirect()

View File

@ -16,6 +16,9 @@
#ifdef ENABLE_LEASEFILE
int reload_from_lease_file(void);
#ifdef LEASEFILE_USE_REMAINING_TIME
void lease_file_rewrite(void);
#endif
#endif
/* upnp_redirect()