From 6ce6a70973eb2d9bbcdb35593f9095023f6ad9a5 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 2 Nov 2017 17:27:24 +0100 Subject: [PATCH] PCP : reset epoch after address change see #254 --- miniupnpd/miniupnpd.c | 6 ++++++ miniupnpd/pcpserver.c | 17 +++++++++++++++-- miniupnpd/pcpserver.h | 6 ++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/miniupnpd/miniupnpd.c b/miniupnpd/miniupnpd.c index 3629dcb..1c5275d 100644 --- a/miniupnpd/miniupnpd.c +++ b/miniupnpd/miniupnpd.c @@ -2060,6 +2060,12 @@ main(int argc, char * * argv) { upnp_event_var_change_notify(EWanIPC); } +#endif +#ifdef ENABLE_PCP + if(GETFLAG(ENABLENATPMPMASK)) + { + PCPPublicAddressChanged(); + } #endif should_send_public_address_change_notif = 0; } diff --git a/miniupnpd/pcpserver.c b/miniupnpd/pcpserver.c index d1742e0..e76d57f 100644 --- a/miniupnpd/pcpserver.c +++ b/miniupnpd/pcpserver.c @@ -101,8 +101,10 @@ struct pcp_server_info { }; /* default server settings, highest version supported is the default */ -static struct pcp_server_info this_server_info = {2}; +static const struct pcp_server_info this_server_info = {2}; +/* origin for "epoch time" sent into responses */ +time_t epoch_origin = 0; /* structure holding information from PCP msg*/ /* all variables are in host byte order except IP addresses */ @@ -1448,7 +1450,10 @@ static void createPCPResponse(unsigned char *response, pcp_info_t *pcp_msg_info) response[1] |= 0x80; /* r_opcode */ response[3] = pcp_msg_info->result_code; - WRITENU32(response + 8, time(NULL) - startup_time); /* epochtime */ + if(epoch_origin == 0) { + epoch_origin = startup_time; + } + WRITENU32(response + 8, time(NULL) - epoch_origin); /* epochtime */ switch (pcp_msg_info->result_code) { /*long lifetime errors*/ case PCP_ERR_UNSUPP_VERSION: @@ -1633,4 +1638,12 @@ int OpenAndConfPCPv6Socket(void) return s; } #endif /*ENABLE_IPV6*/ + +void PCPPublicAddressChanged(void) +{ + /* according to RFC 6887 8.5 : + * if the external IP address(es) of the NAT (controlled by + * the PCP server) changes, the Epoch time MUST be reset. */ + epoch_origin = time(NULL); +} #endif /*ENABLE_PCP*/ diff --git a/miniupnpd/pcpserver.h b/miniupnpd/pcpserver.h index 71a4fc4..7974c22 100644 --- a/miniupnpd/pcpserver.h +++ b/miniupnpd/pcpserver.h @@ -52,4 +52,10 @@ int ProcessIncomingPCPPacket(int s, unsigned char *msg_buff, int len, */ int OpenAndConfPCPv6Socket(void); + +/* + * To be called when Public IP address changed (IPv4) + */ +void PCPPublicAddressChanged(void); + #endif /* PCPSERVER_H_INCLUDED */