Skip to content

Commit 722e25d

Browse files
author
Luigi Gubello
committed
set random NTP port by default
1 parent 5bdc209 commit 722e25d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/utility/time/NTPUtils.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@
2525
#include "NTPUtils.h"
2626

2727
#include <Arduino.h>
28+
#ifdef BOARD_HAS_ECCX08
29+
#include <ArduinoECCX08.h>
30+
bool has_crypto = 1;
31+
#else
32+
bool has_crypto = 0;
33+
#endif
2834

2935
/**************************************************************************************
3036
* PUBLIC MEMBER FUNCTIONS
3137
**************************************************************************************/
3238

3339
unsigned long NTPUtils::getTime(UDP & udp)
3440
{
35-
udp.begin(NTP_LOCAL_PORT);
36-
41+
NTPUtils randomPort;
42+
int _randomPort = randomPort.setRandomPort(MIN_NTP_PORT, MAX_NTP_PORT);
43+
udp.begin(_randomPort);
44+
3745
sendNTPpacket(udp);
3846

3947
bool is_timeout = false;
@@ -83,4 +91,13 @@ void NTPUtils::sendNTPpacket(UDP & udp)
8391
udp.endPacket();
8492
}
8593

94+
int NTPUtils::setRandomPort(int minValue, int maxValue) {
95+
if (has_crypto) {
96+
return ECCX08.random(minValue, maxValue);
97+
} else {
98+
randomSeed(analogRead(0));
99+
return random(minValue, maxValue);
100+
}
101+
}
102+
86103
#endif /* #ifndef HAS_LORA */

src/utility/time/NTPUtils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,26 @@
3737
* CLASS DECLARATION
3838
**************************************************************************************/
3939

40+
#define NTP_DEFAULT_LOCAL_PORT 8888
41+
extern const int MIN_NTP_PORT;
42+
extern const int MAX_NTP_PORT;
43+
4044
class NTPUtils
4145
{
4246
public:
4347

4448
static unsigned long getTime(UDP & udp);
49+
int setRandomPort(int minValue, int maxValue);
4550

4651
private:
4752

4853
static size_t const NTP_PACKET_SIZE = 48;
4954
static int const NTP_TIME_SERVER_PORT = 123;
50-
static int const NTP_LOCAL_PORT = 8888;
55+
static int const NTP_LOCAL_PORT = NTP_DEFAULT_LOCAL_PORT;
5156
static unsigned long const NTP_TIMEOUT_MS = 1000;
5257
static char constexpr * NTP_TIME_SERVER = "time.arduino.cc";
5358

5459
static void sendNTPpacket(UDP & udp);
55-
5660
};
5761

5862
#endif /* #ifndef HAS_LORA */

0 commit comments

Comments
 (0)