Subject: IPv6 Address validation

From: Ravi Kumar M <[email-masked]>
Date: Wed, 23 Apr 2003 17:56:14 +0500 (IST)

Hi,

Can any one tell me how to validate IPv6 address? any one has an example code? kindly let me know

thanks & regards
ravi
-------------------------------------------------
Sify Mail - now with Anti-virus protection powered by Trend Micro, USA.
Know more at http://mail.sify.com

Sify Power mail- a Premium Service from Sify Mail!
know more at http://mail.sify.com
--------------------------------------------------------------------
IETF IPng Working Group Mailing List
IPng Home Page:                      http://playground.sun.com/ipng
FTP archive:                      ftp://playground.sun.com/pub/ipng
Direct all administrative requests to [email-masked]
--------------------------------------------------------------------

---

From: S Ramesh <[email-masked]>
Date: Wed, 23 Apr 2003 20:48:15 +0530

If you are using linux (bsd networking code) as platform, take a look
at /usr/include/netinet/in.h file. It has macros defined for IPv6
address validation (e.g. IN6_IS_ADDR_LINKLOCAL etc.)

Thanks,
Ramesh.

-- 
http://voip.hcltech.com

---

From: Shannon -jj Behrens <[email-masked]>
Date: Wed, 23 Apr 2003 08:30:44 -0700

/*
 * Author:			Shannon -jj Behrens
 * Date: 			Fri Nov  1 12:09:17 PST 2002
 *
 * Given a string on the command line, return 0 if the string is a valid IPv6 
 * address, 1 otherwise.
 */

#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>

char *argv0;			/* This is argv[0] sans path. */

/* Output usage information to the user and exit. */
void
usage ()
{
  fprintf (stderr, "usage: %s [-v] address\n", argv0);
  exit (1);
}

int
main (int argc, char *argv[])
{
  int optchar, verbose = 0, is_ipv6addr;
  struct sockaddr addr;

  argv0 = argv[0] = basename (argv[0]);
  while ((optchar = getopt (argc, argv, "v")) != -1)
    switch (optchar)
      {
      case 'v':
	verbose = 1;
	break;
      default:
	usage ();
      }
  if (optind + 1 != argc)
    usage ();
  is_ipv6addr = inet_pton (AF_INET6, argv[optind], &addr);
  if (is_ipv6addr < 0)
    {
      fprintf (stderr, "%s: inet_pton: %s\n", argv0, strerror (errno));
      return 1;
    }
  if (verbose)
    {
      if (is_ipv6addr)
	printf ("%s is a valid IPv6 address.\n", argv[optind]);
      else
	printf ("%s is not a valid IPv6 address.\n", argv[optind]);
    }

  return !is_ipv6addr;
}

---

From: [email-masked]
Date: Thu, 24 Apr 2003 08:41:53 +1000

Is the following a legal IPv6 literal?  The RFC's are unclear
	about whether leading zeros are allowed or not if they would
	make a segment more than 4 character wide.

		1234:1234:1234:1234:01234::

	Note I would never expect inet_ntop() to produce the above.

	We were discussing this when reviewing our inet_pton()
	implementation.

	Mark

--
Mark Andrews, Internet Software Consortium
1 Seymour St., Dundas Valley, NSW 2117, Australia
PHONE: +61 2 9871 4742                 INTERNET: [email-masked]
--------------------------------------------------------------------
IETF IPng Working Group Mailing List
IPng Home Page:                      http://playground.sun.com/ipng
FTP archive:                      ftp://playground.sun.com/pub/ipng
Direct all administrative requests to [email-masked]
--------------------------------------------------------------------

---

From: [email-masked]
Date: Thu, 24 Apr 2003 11:39:56 +0900

>      fprintf (stderr, "%s: inet_pton: %s\n", argv0, strerror (errno));

	the clause is buggy.  inet_pton return value is defined as follows:
	1: valid
	0: wasn't parsable as specified address famiily
	-1: some other error (see errno)

	you need to check if is_ipv6addr is 1 or not.

itojun

---
     The inet_pton() function converts a presentation format address (that is,
     printable form as held in a character string) to network format (usually
     a struct in_addr or some other internal binary representation, in network
     byte order).  It returns 1 if the address was valid for the specified ad-
     dress family, or 0 if the address wasn't parsable in the specified ad-
     dress family, or -1 if some system error occurred (in which case errno
     will have been set).  This function is presently valid for AF_INET and
     AF_INET6.

---

From: "Vijayabhaskar A K" <[email-masked]>
Date: Thu, 24 Apr 2003 11:42:39 +0530

Hi Mark

RFC 3513 says that,
The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the
hexadecimal values of the eight 16-bit pieces of the address.

Examples:

   FEDC:BA98:7654:3210:FEDC:BA98:7654:3210

   1080:0:0:0:8:800:200C:417A

The only constraint here is, "x" should refer the 16 bit pieces
of the address, "01234" is a valid hex number and it doesn't exeed
16 bits.

I beleive a good implementation of inet_pton should ignore the leading
0s.

~Vijay

-----Original Message-----
From: [email-masked]
[mailto:[email-masked]]On Behalf Of [email-masked]
Sent: Thursday, April 24, 2003 4:12 AM
To: Shannon -jj Behrens
Cc: S Ramesh; Ravi Kumar M; [email-masked]
Subject: Re: IPv6 Address validation

	Mark

---

From: Bill Manning <[email-masked]>
Date: Thu, 24 Apr 2003 04:31:15 -0700 (PDT)

one valid interpretation of "01234"  would be
	
	0000.0001.0010.0011.0100  

	which is 20 bits.

% The only constraint here is, "x" should refer the 16 bit pieces
% of the address, "01234" is a valid hex number and it doesn't exeed
% 16 bits.
% 
% I beleive a good implementation of inet_pton should ignore the leading
% 0s.
% 
% ~Vijay
% 
% 
% -----Original Message-----
% From: [email-masked]
% [mailto:[email-masked]]On Behalf Of [email-masked]
% Sent: Thursday, April 24, 2003 4:12 AM
% To: Shannon -jj Behrens
% Cc: S Ramesh; Ravi Kumar M; [email-masked]
% Subject: Re: IPv6 Address validation
% 
% 
% 
% 	Is the following a legal IPv6 literal?  The RFC's are unclear
% 	about whether leading zeros are allowed or not if they would
% 	make a segment more than 4 character wide.
% 
% 		1234:1234:1234:1234:01234::
% 
% 	Note I would never expect inet_ntop() to produce the above.
% 
% 	We were discussing this when reviewing our inet_pton()
% 	implementation.
% 
% 	Mark
% 
% > /*
% >  * Author:			Shannon -jj Behrens
% >  * Date: 			Fri Nov  1 12:09:17 PST 2002
% >  *
% >  * Given a string on the command line, return 0 if the string is a valid
% IPv6
% >
% >  * address, 1 otherwise.
% >  */
% >
% > #include <errno.h>
% > #include <getopt.h>
% > #include <libgen.h>
% > #include <stdio.h>
% > #include <stdlib.h>
% > #include <string.h>
% > #include <arpa/inet.h>
% > #include <sys/socket.h>
% > #include <sys/types.h>
% >
% > char *argv0;			/* This is argv[0] sans path. */
% >
% > /* Output usage information to the user and exit. */
% > void
% > usage ()
% > {
% >   fprintf (stderr, "usage: %s [-v] address\n", argv0);
% >   exit (1);
% > }
% >
% > int
% > main (int argc, char *argv[])
% > {
% >   int optchar, verbose = 0, is_ipv6addr;
% >   struct sockaddr addr;
% >
% >   argv0 = argv[0] = basename (argv[0]);
% >   while ((optchar = getopt (argc, argv, "v")) != -1)
% >     switch (optchar)
% >       {
% >       case 'v':
% > 	verbose = 1;
% > 	break;
% >       default:
% > 	usage ();
% >       }
% >   if (optind + 1 != argc)
% >     usage ();
% >   is_ipv6addr = inet_pton (AF_INET6, argv[optind], &addr);
% >   if (is_ipv6addr < 0)
% >     {
% >       fprintf (stderr, "%s: inet_pton: %s\n", argv0, strerror (errno));
% >       return 1;
% >     }
% >   if (verbose)
% >     {
% >       if (is_ipv6addr)
% > 	printf ("%s is a valid IPv6 address.\n", argv[optind]);
% >       else
% > 	printf ("%s is not a valid IPv6 address.\n", argv[optind]);
% >     }
% >
% >   return !is_ipv6addr;
% > }
% >
% > --------------------------------------------------------------------
% > IETF IPng Working Group Mailing List
% > IPng Home Page:                      http://playground.sun.com/ipng
% > FTP archive:                      ftp://playground.sun.com/pub/ipng
% > Direct all administrative requests to [email-masked]
% > --------------------------------------------------------------------
% --
% Mark Andrews, Internet Software Consortium
% 1 Seymour St., Dundas Valley, NSW 2117, Australia
% PHONE: +61 2 9871 4742                 INTERNET: [email-masked]
% --------------------------------------------------------------------
% IETF IPng Working Group Mailing List
% IPng Home Page:                      http://playground.sun.com/ipng
% FTP archive:                      ftp://playground.sun.com/pub/ipng
% Direct all administrative requests to [email-masked]
% --------------------------------------------------------------------
% 
% --------------------------------------------------------------------
% IETF IPng Working Group Mailing List
% IPng Home Page:                      http://playground.sun.com/ipng
% FTP archive:                      ftp://playground.sun.com/pub/ipng
% Direct all administrative requests to [email-masked]
% --------------------------------------------------------------------
% 

-- 
--bill

Opinions expressed may not even be mine by the time you read them, and
certainly don't reflect those of any other entity (legal or otherwise).

---

From: Ramanan <[email-masked]>
Date: Thu, 24 Apr 2003 17:25:08 +0530

hi,

>         make a segment more than 4 character wide.

defensive programming may consider this as a
valid address.

However, when this address is stored in buffers
of limited size, it would overflow as most
buffers assume 46 as the max size of an ipv6
address string. open group suggests this bufsz.

>         Note I would never expect inet_ntop() to produce the above.

neither would I.

>         We were discussing this when reviewing our inet_pton()

I would prefer to code inet_pton to accept this
address. because there is a possibility that
some 'ill written' scripts may produce such
ip addresses.  scripts get preference because
handwriting ipv6 addresses is a nightmare at
present.

Also moving on to IPv6 applications do not
want to call inet_pton and but expect
getaddrinfo to validate the v6 address.
the expectation is that, APIs provide more
options to simplify application coding.

regs
ramanan

---

From: "Vijayabhaskar A K" <[email-masked]>
Date: Thu, 24 Apr 2003 19:53:58 +0530

since the rfc says that "x" should hold only 16 bits,
the leading zeros has to be ignored

-----Original Message-----
From: [email-masked]
[mailto:[email-masked]]On Behalf Of Bill Manning
Sent: Thursday, April 24, 2003 5:01 PM
To: [email-masked]
Cc: [email-masked]; [email-masked]; [email-masked];
[email-masked]; [email-masked]
Subject: Re: IPv6 Address validation

	0000.0001.0010.0011.0100

	which is 20 bits.

--
--bill

---

From: Bill Manning <[email-masked]>
Date: Thu, 24 Apr 2003 07:29:05 -0700 (PDT)

yes, I understand.  why drop the leading zeros and not the last four bits?
 I would assert that "01234" is illegal and one should -NOT- presume to know
 which are the desired bits.  

 and yes, a good/compliant implementation of inet_pton should ignore
 leading zeros, if it has a way of determining that there are insignificant
 leading zeros.

% since the rfc says that "x" should hold only 16 bits,
% the leading zeros has to be ignored
% 
% 
% -----Original Message-----
% From: [email-masked]
% [mailto:[email-masked]]On Behalf Of Bill Manning
% Sent: Thursday, April 24, 2003 5:01 PM
% To: [email-masked]
% Cc: [email-masked]; [email-masked]; [email-masked];
% [email-masked]; [email-masked]
% Subject: Re: IPv6 Address validation
% 
% 
% 
% 	one valid interpretation of "01234"  would be
% 
% 	0000.0001.0010.0011.0100
% 
% 	which is 20 bits.
% 
% 
% % The only constraint here is, "x" should refer the 16 bit pieces
% % of the address, "01234" is a valid hex number and it doesn't exeed
% % 16 bits.
% %
% % I beleive a good implementation of inet_pton should ignore the leading
% % 0s.
% %
% % ~Vijay
% %
% %
% % -----Original Message-----
% % From: [email-masked]
% % [mailto:[email-masked]]On Behalf Of [email-masked]
% % Sent: Thursday, April 24, 2003 4:12 AM
% % To: Shannon -jj Behrens
% % Cc: S Ramesh; Ravi Kumar M; [email-masked]
% % Subject: Re: IPv6 Address validation
% %
% %
% %
% % 	Is the following a legal IPv6 literal?  The RFC's are unclear
% % 	about whether leading zeros are allowed or not if they would
% % 	make a segment more than 4 character wide.
% %
% % 		1234:1234:1234:1234:01234::
% %
% % 	Note I would never expect inet_ntop() to produce the above.
% %
% % 	We were discussing this when reviewing our inet_pton()
% % 	implementation.
% %
% % 	Mark
% %
% % > /*
% % >  * Author:			Shannon -jj Behrens
% % >  * Date: 			Fri Nov  1 12:09:17 PST 2002
% % >  *
% % >  * Given a string on the command line, return 0 if the string is a valid
% % IPv6
% % >
% % >  * address, 1 otherwise.
% % >  */
% % >
% % > #include <errno.h>
% % > #include <getopt.h>
% % > #include <libgen.h>
% % > #include <stdio.h>
% % > #include <stdlib.h>
% % > #include <string.h>
% % > #include <arpa/inet.h>
% % > #include <sys/socket.h>
% % > #include <sys/types.h>
% % >
% % > char *argv0;			/* This is argv[0] sans path. */
% % >
% % > /* Output usage information to the user and exit. */
% % > void
% % > usage ()
% % > {
% % >   fprintf (stderr, "usage: %s [-v] address\n", argv0);
% % >   exit (1);
% % > }
% % >
% % > int
% % > main (int argc, char *argv[])
% % > {
% % >   int optchar, verbose = 0, is_ipv6addr;
% % >   struct sockaddr addr;
% % >
% % >   argv0 = argv[0] = basename (argv[0]);
% % >   while ((optchar = getopt (argc, argv, "v")) != -1)
% % >     switch (optchar)
% % >       {
% % >       case 'v':
% % > 	verbose = 1;
% % > 	break;
% % >       default:
% % > 	usage ();
% % >       }
% % >   if (optind + 1 != argc)
% % >     usage ();
% % >   is_ipv6addr = inet_pton (AF_INET6, argv[optind], &addr);
% % >   if (is_ipv6addr < 0)
% % >     {
% % >       fprintf (stderr, "%s: inet_pton: %s\n", argv0, strerror (errno));
% % >       return 1;
% % >     }
% % >   if (verbose)
% % >     {
% % >       if (is_ipv6addr)
% % > 	printf ("%s is a valid IPv6 address.\n", argv[optind]);
% % >       else
% % > 	printf ("%s is not a valid IPv6 address.\n", argv[optind]);
% % >     }
% % >
% % >   return !is_ipv6addr;
% % > }
% % >
% % > --------------------------------------------------------------------
% % > IETF IPng Working Group Mailing List
% % > IPng Home Page:                      http://playground.sun.com/ipng
% % > FTP archive:                      ftp://playground.sun.com/pub/ipng
% % > Direct all administrative requests to [email-masked]
% % > --------------------------------------------------------------------
% % --
% % Mark Andrews, Internet Software Consortium
% % 1 Seymour St., Dundas Valley, NSW 2117, Australia
% % PHONE: +61 2 9871 4742                 INTERNET: [email-masked]
% % --------------------------------------------------------------------
% % IETF IPng Working Group Mailing List
% % IPng Home Page:                      http://playground.sun.com/ipng
% % FTP archive:                      ftp://playground.sun.com/pub/ipng
% % Direct all administrative requests to [email-masked]
% % --------------------------------------------------------------------
% %
% % --------------------------------------------------------------------
% % IETF IPng Working Group Mailing List
% % IPng Home Page:                      http://playground.sun.com/ipng
% % FTP archive:                      ftp://playground.sun.com/pub/ipng
% % Direct all administrative requests to [email-masked]
% % --------------------------------------------------------------------
% %
% 
% 
% --
% --bill
% 
% Opinions expressed may not even be mine by the time you read them, and
% certainly don't reflect those of any other entity (legal or otherwise).
% 
% --------------------------------------------------------------------
% IETF IPng Working Group Mailing List
% IPng Home Page:                      http://playground.sun.com/ipng
% FTP archive:                      ftp://playground.sun.com/pub/ipng
% Direct all administrative requests to [email-masked]
% --------------------------------------------------------------------
% 

-- 
--bill

---

From: Shannon -jj Behrens <[email-masked]>
Date: Thu, 24 Apr 2003 10:05:32 -0700

> 	make a segment more than 4 character wide.

I don't have any information about this that you don't already have.  However,
I know that there's a general rule that you should be strict in what you 
produce, and forgiving in what you accept.  Nonetheless, my gut reaction is to 
reject it since it's convenient to demand that an IPv6 address never be longer
than 4*8 + 7 characters long.

Best Regards,
-jj

-- 
Hacker is to software engineer as 
Climbing Mt. Everest is to building a Denny's there.
--------------------------------------------------------------------
IETF IPng Working Group Mailing List
IPng Home Page:                      http://playground.sun.com/ipng
FTP archive:                      ftp://playground.sun.com/pub/ipng
Direct all administrative requests to [email-masked]
--------------------------------------------------------------------

---

From: Shannon -jj Behrens <[email-masked]>
Date: Thu, 24 Apr 2003 10:19:24 -0700

Itojun,

Thank you for your comment.  In fact, most of my knowledge of the C API's comes
from you.  Nonetheless, the code below is correct.  The if statement is there
*only* to check for the -1 case.  The code below it goes on to deal with the 
1 or 0 case.  

  if (is_ipv6addr < 0)			/* -1 case */
    {
      fprintf (stderr, "%s: inet_pton: %s\n", argv0, strerror (errno));
      return 1;
    }
  if (verbose)
    {
      if (is_ipv6addr)			/* 1 case */
	printf ("%s is a valid IPv6 address.\n", argv[optind]);
      else				/* 0 case */
	printf ("%s is not a valid IPv6 address.\n", argv[optind]);
    }

I imagine you thought that I was writing the if statement in 
order to catch both the -1 and 0 case, but this is not so.  If I have 
misunderstood your statements, I welcome your further correction.

Best Regards,
-jj

---

From: "EricLKlein" <[email-masked]>
Date: Thu, 24 Apr 2003 13:50:42 +0300

----- Original Message -----
From: <[email-masked]>
> make a segment more than 4 character wide.

It is my understanding that the address is not valid, only because you are
truncating the ending zeros, not the preceding ones with the final ::.

Otherwise it is valid.

Eric

---

From: [email-masked]
Date: Fri, 25 Apr 2003 10:39:28 +0900

>the leading zeros has to be ignored

	i would interpret it as 
	"since the rfc says that "x" should hold only 16 bits,"
	inet_pton() and stuff should reject more than 4 digits between colons.

itojun