15

Under IPv4, the de facto standard notation 10.1.2.3:1234 is completely clear: it is an IP address with port number. This is understood in URL's for instance: http://10.1.2.3:1234/doc.xhtml.

If I have a sockaddr_ipv4 class in some code, its tostring method can return this colon notation and everything is cool.

In a fit of myopia, the designers for the IPv6 addresses numeric notation decided that the colon character was available for their use as a separator. As a result, a colon-delimited port number looks ambiguous on an IPv6 address.

How can we incorporate a port number into a printed representation of an IPv6 address-with-port? Is there some de facto standard way?

Kaz
  • 3,572
  • 1
  • 19
  • 30
  • 5
    [Boom](http://stackoverflow.com/questions/186829/how-do-ports-work-with-ipv6) and [bam](http://serverfault.com/questions/205793/how-can-one-distinguish-the-host-and-the-port-in-an-ipv6-url) – Ordous Feb 29 '16 at 16:25
  • 1
    I think what @Ordous is trying to say is that this question is a duplicate of some existing questions with answers :) – Sander Steffann Feb 29 '16 at 16:27
  • @Ordous ...and, of course, [bang](http://stackoverflow.com/a/5436548/839601) – gnat Feb 29 '16 at 16:28
  • 4
    It's not a duplicate of any question *here*; those are off topic on those sites. The question isn't about a broken piece of code, so it's not appropriate for StackOverflow, and it's not a sysadmin issue for ServerFault. It's a "conceptual questions about software development": what do I put into the `tostring` method of an IPv6 socket address class. – Kaz Feb 29 '16 at 16:34

1 Answers1

22

The common way of doing this is to enclose the IPv6 address in square brackets like this:

[2001:db8::1]:8080

This is what is being used in URLs for example:

http://[2001:db8::1]:8080/
Sander Steffann
  • 344
  • 2
  • 4
  • 3
    You forgot to copy over the RFC reference from the SO answer. :) – Kaz Feb 29 '16 at 16:34
  • In fact RFC 5952 has a more well-rounded answer in section 6. The `[ADDR]:PORT` isn't the only notation; just the one that should be used in URLs according to RFC 3986. – Kaz Feb 29 '16 at 20:11
  • For instance, it turns out that this is acceptable, for an IPv6 mapped IPv6: `::ffff:10.1.2.3:1234`, without the square brackets. (Though that is not valid in URL use, it is not ambiguous.) – Kaz Feb 29 '16 at 22:05