10

I was asked a question my one of my teachers. What is TCP port 0 reserved for? I did a bit of searching and research found 2 reasons:

  1. For linux based system, to get the next available port from the OS that the program can use. Like asking for dynamically allocated ports.

  2. Packets that don't need port numbers (like ICMP) use port number 0 in their packet.

But these seemed like something that has evolved to be used for, just because port 0 was not being used for anything else. I did not find any use of port 0 that was assigned by 'IANA' in the beginning. Is there anything that was officially assigned to port 0?

2
  • 5
    ICMP is not TCP and does not use TCP, and as such does not use TCP/0.
    – YLearn
    Oct 1, 2014 at 14:24
  • @YLearn To send ICMP packets programmatically, you need to create a sockaddr_in, or sockaddr_in6 structure, which require you to fill a port number even if in the end the socket type is SOCK_RAW. So maybe this is where the confusion occurs.
    – DannyNiu
    Nov 30, 2022 at 0:59

4 Answers 4

5

Keep in mind that a "reserved" resource is not an "assigned" resource. Just because it is reserved doesn't mean that it is intended to be used for anything, at least at the current time. Rather it has been set aside and should not be used, unless otherwise specified.

From RFC6335:

 Reserved port numbers are not available for regular
 assignment; they are "assigned to IANA" for special purposes.
 Reserved port numbers include values at the edges of each range,
 e.g., 0, 1023, 1024, etc., which may be used to extend these
 ranges or the overall port number space in the future.
2
  • 5
    You might imagine inventing TCP+ which is like TCP but with 64-bit port numbers. Then you could (once IANA approves) put 0 in the port number to indicate the extended port number field should be used instead.
    – richardb
    Oct 1, 2014 at 16:38
  • 1
    @richardb, exactly, although the RFC only provides this as a possible use for those port numbers. It could also be for some purpose that hasn't even been conceived of today...that is the beauty of technology, it is always changing and having the flexibility built into the design can make it much easier for technology to do so.
    – YLearn
    Oct 1, 2014 at 17:47
5

Disclaimer: My answer can be considered as having conflict of interest to some people as the material I reference in my answer is actually initially created by me.

Neither IETF nor IANA currently (as of 2019) have any documented real-world use for the TCP port 0, however the book "UNIX Network Programming" by Richard Stevens et.al. teaches people to obtain an dynamically assigned port by binding to port 0. This practice is documented in several other places including Linux and FreeBSD man page.

Currently, there is an open bug at Austin Group (the group who develops POSIX and Single Unix Specification) concerning this waiting resolution.

Currently there is a bug at Austin Group that had been accepted, which amends the standard to support the binding to port 0 behavior for TCP and UDP. The next revision - Issue 8 / Single Unix Specification ver 5 is expected to have these changes incorporated.

0
1

TCP port 0 has a special meaning in many socket APIs (especially in the common Berkeley socket API that the POSIX API grew out of). It requests an ephemeral/dynamic port from the stack. Probably for that reason IANA has reserved that port, so that it is not used in general.

Within the actual stack, port 0 has no special meaning (nor does any other number have). Due to its API use it might be (very) hard to put it into actual use though. And simply, there's no point to try.

Note that ports are only used with some transport-layer protocols. ICMP (which actually belongs to the network layer although it is encapsulated like an L4 protocol) doesn't use ports.

0

Port 0

•Port 0 is reserved for binding purposes, whenever you will try to bind with port 0, a random available port will be binded instead of 0.

•That random port is decided by the OS.

•It is very useful for programmers by reducing their effort by automatically finding available port instead of programmer doing it also increasing the speed of the program.

•However, trying to connect a socket with providing its port as 0 will definitely cause failure to connect as it is considered invalid everywhere except in binding

•In case all ports from 1 to 65535 are used and you try to bind with port 0. A binding error or failure will be caused.

Also, ICMP doesn't use any port, not even 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.