Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method to set random local port #116

Merged
merged 3 commits into from
Sep 3, 2020
Merged

Added method to set random local port #116

merged 3 commits into from
Sep 3, 2020

Conversation

luigigubello
Copy link
Contributor

@luigigubello luigigubello commented Aug 31, 2020

Added method to set a (pseudo)random local port, so that the board needs not to use always the same embedded local port for receiving NTP packets.

At the moment, the variable port is a constant embedded value used by the board for receiving NTP packets via unencrypted UDP connection. Changing randomly port to receive NTP packets doesn't improve cryptographic security and it is not the final solution, but it adds a layer to make harder the attacker's job.

How to use

void setup() {
  ...
}
void loop() {
  timeClient.setRandomPort();
  timeClient.update();
}

How it works

setRandomPort() sets a pseudorandom port and NTPClient::update() launches NTPClient.begin(port) if port is different by default value NTP_DEFAULT_LOCAL_PORT

Luigi Gubello added 2 commits August 31, 2020 02:25
Added method to set a random local port, so that the board needs not
to use always the same embedded local port for receiving NTP packets.
Copy link
Contributor

@aentinger aentinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great change, small corrections necessary ;)

NTPClient.h Outdated
@@ -15,7 +15,7 @@ class NTPClient {

const char* _poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP;
int _port = NTP_DEFAULT_LOCAL_PORT;
long _port = NTP_DEFAULT_LOCAL_PORT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @luigigubello 👋 ☕ Is there any special reason why you change this data type from int to long? Certainly int is capable of holding values up 2^16-1 even on AVR 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly int is capable of holding values up 2^16-1 even on AVR

Reading the documentation I have understood it's not, but your suggestion to use unsigned int is a good idea. So, yes, I have chosen long instead int to be sure to maintain the AVR compatibility. It seems unsigned int is the best choice 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked again - so yes, int 2 byte on AVR which covers from -2^15 to 2^15-1. unsigned int covers 0 to 2^16-1 which is enough.

NTPClient.h Outdated
/**
* Set random local port
*/
void setRandomPort(long minValue, long maxValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ... I think int or even better unsigned int (as it excludes negative values) would do the job just nicely here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right

@aentinger aentinger merged commit aff9c6b into arduino-libraries:master Sep 3, 2020
chrisy pushed a commit to chrisy/NTPClient that referenced this pull request Apr 16, 2022
…al_port

Added method to set random local port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants