Can two different services on the same machine use the same port on IPv4 and IPv6?
I presume you are talking about TCP and UDP ports.
Say I have two TCP services A and B. A is an IPv4-only service listening on
127.0.0.1:10000
. B is an IPv6-only service listening on::1:10000
. Can I start these services on the same machine?
Yes.
That is, how are IP ports assigned? Per machine? Per interface? Per protocol? Etc…
First off there is no such thing as an "IP port". Ports are a feature of certain transport layer protocols. In particular TCP and UDP use port numbers. I belive some other protocols use them too but I've only worked with TCP and UDP myself.
TCP ports and UDP ports are entirely separate. A TCP server and a UDP server can use the same IP/port combination.
Servers can either listen on a specific IP address/port combination or can listen on a port for all addresses. The latter is specified by specifying a listen address of 0.0.0.0 for IPv4 or :: for ipv6. If a server is listening on a port for all addresses then other servers can't listen on that port for a specific address.
Finally on most operating systems it is possible for a server listening on :: to accept connections over both IPv4 and IPv6. There is a socket option called "IPV6_V6ONLY" to enable/disable this behaviour. The default setting for this option varies. On linux the option is disabled by default but this can be changed through the sysctl "net.ipv6.bindv6only". On windows vista and later IPV6_V6ONLY is enabled by default. On older versions of windows it is not possible for a single socket to accept both v4 and v6 connections, an applicaiton that wants to listen for both needs to open two seperate sockets.
Related videos on Youtube
levant pied
Updated on September 18, 2022Comments
-
levant pied over 1 year
Say I have two TCP services A and B. A is an IPv4-only service listening on
127.0.0.1:10000
. B is an IPv6-only service listening on::1:10000
. Can I start these services on the same machine? That is, how are IP ports assigned? Per machine? Per interface? Per protocol? Etc… -
levant pied over 9 yearsThanks xEODGuy - yes, meant TCP in this case, edited to clarify. OK, when you say each IP address, does that differentiate between ipv4 and ipv6? That is, are 127.0.0.1 and ::1 different addresses or not? Also, do you have some references, so I can read more?
-
xEODGuy over 9 yearsYou're welcome. As far as reading up on this, Wikipedia has a page that explains the ports pretty well here: TCP/UDP Port Numbers You can also check out their page on IPv6, which does a fair job of explaining that, as well: IPv6
-
user2313067 over 9 years@sous lesquels it might be worth noting that as stated on en.m.wikipedia.org/wiki/IPv6_address, ::ffff:127.0.0.1 is the same as 127.0.0.1.
-
levant pied over 8 yearsThanks plugwash! Not required, but do you have some references for the above? I'm just interested in reading a bit more in-depth about this, such as the BINDV6ONLY flag that you mentioned and how it applies to different OSes and such.
-
plugwash over 8 yearsUnfortunately this information is spread all over the place. Regarding bindv6only I realise I made an error in my post. You may have more luck googling after I correct it.
-
Ron Maupin over 8 yearsIP addresses (network layer) do not have ports (transport layer), so each IP address has nothing to do with the ports. There are transport layer protocols which don't use ports, but do run on top of IP. A port is a transport layer address for a particular transport protocol, the same way an IP address is the network address for the IP protocol, and a MAC address is the address for the ethernet protocol. Each layer of the stack is independent of the other layers.
-
TOOGAM over 8 yearsAdditional info: The <A HREF="iana.org/assignments/port-numbers">IANA list of port numbers (and services)</A> now also mentions some SCTP ports (as well as TCP ports and UDP ports). ICMP and ICMPv6 don't use a port number, but they do use a "message type" number, which some firewall configuration syntax might treat quite similar to a port number.
-
JohnyTex about 6 years@RonMaupin 's comment above explains nicely what is actually going on.