neosmart / udpproxy

Cross-platform UDP proxy
MIT License
39 stars 10 forks source link

correct way to select an unused udp port #1

Open darksilkmaster opened 7 years ago

darksilkmaster commented 7 years ago

https://github.com/neosmart/udpproxy/blob/940bed7bb308676acaaf17dcc5df9067c1256e95/src/main.rs#L132

if you generate a random u16 and add 1024 to it, the result might be larger than 65535

actually, there's a very simple way to do it right, just the port 0 and the os will take care of the rest

mqudsi commented 6 years ago

Hey @darksilkmaster, thanks for filing. I thought I replied to this via email, but either I'm misremembering or GitHub didn't like something about my message and never replied.

Thanks for catching the bug, that's kind of embarrassing. I imagine rust may have caught that overflow if the port were cast to u16 after the addition and prior to the conversion?

In all cases, I've been meaning to revisit the codebase and move it over to an event loop via tokiors to boost performance and lower resource requirements. The complementary tcpproxy project has already been ported: https://github.com/neosmart/tcpproxy/commit/62651197d52d99281f0d0169629b91cae62f3261