Page 1 of 1

Is player can use negative port?

Posted: Sun Aug 23, 2009 2:01 pm
by Unknown Jedi
I'm always thinking that a port number is a 16-bit unsigned integer, thus ranging from 0 to 65535. But when I'm analyse player's full userinfo, I've founded some players with negative port, like \ip\77.252.227.44:-9144\. How it's possible?

Re: Is player can use negative port?

Posted: Sun Aug 23, 2009 4:26 pm
by Gamall
Port numbers are 16 bits integers, yes. Unsigned, yes in theory.

But from the computer's perspective it does not really matter whether an N-bits integer is signed or unsigned, until the value is read; what matters is the binary representation of it, and the same representation may encode two different numbers depending on the value of the Most Significant Bit. That's known as the Two's complement.

Here, the same 16 bits binary blob, let's call it B is read twice: once by the networking apparatus of the client and the server, which read it as unsigned: unsigned(B) = 32768 + signed(B) = 32768 - 9144 = 23624; and a second time by the application which wrote the player's userinfo; and this one read it as signed, probably because the programmer felt too lazy to type the "unsigned" keyword.

It does not really matter because if you read this signed number and stored it in a 16bits int, signed or unsigned, you'd get the same binary representation anyway. And this applies even if you are out of signed bounds. Signed range for 16bits is −32768 to 32767 (total 65536 distinct values, of course); but if you read a bigger unsigned value, 32768 for instance, it will just wrap around and become −32768, which has the same binary representation.

Hope that served to clear things for you, and not muddle them further :)

Re: Is player can use negative port?

Posted: Sun Aug 23, 2009 5:17 pm
by Unknown Jedi
Thanks. I'm think about signed value, but I've think that unsigned(B) = signed(B)-32768, and this mistake confused me.
My server auto-banned two players, because they used negative port - I've thinking that negative port can be used only by fakes Image