IPv6 address parsing

Michael Powell <[email protected]>
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <CAMEoF_GcEzSS=JtEENJ4Qs99xG06_gJ4_cMNWK+_krjbkJRePw@mail.gmail.com>
Hello,

I'm moving on to IPv6 address parsing, which grammar is a super set,
sort of, of IPv4.

Basically modeled after the IPv6 grammar, more or less. I am using the
digit-wise IPv4 parsing approach here instead of uint_parser, because
I need the entire string to parse, from what I understand, and
according to RFC 5954 section 4.1.

I was also having difficulty getting "repeat" to work quite right.

_dot = ".";
_col = ":";
_col_x2 = "::";

/* TODO: TBD: This "may" work, except I really do not want to parse actual
INTEGERS, but rather them as STRINGS. */

// _dec_octet = uint_parser<std::uint8_t, 10, 1, 3>();

qi::rule<Iterator> _dec_octet = (
    char_('0')
    | char_('1') >> -(digit >> -digit)
    | char_('2') >> (
        char_('0', '4') >> -digit
        | char_('5') >> -char_('0', '5')
        | char_('6', '9')
        )
    | char_('3', '9') >> -digit
    );

qi::rule<Iterator> _ipv4_addr = _dec_octet >> repeat(3)[_dot >> _dec_octet];
// or, qi::rule<Iterator> _ipv4_addr = _dec_octet >> _dot >>
_dec_octet >> _dot >> _dec_octet >> _dot >> _dec_octet;

qi::rule<Iterator> _h16 = repeat(1, 4)[hex];
//_h16 = hex >> -(hex >> -(hex >> -hex));
qi::rule<Iterator> _h16_col = _h16 >> _col;
qi::rule<Iterator> _ls32 = _h16_col >> _h16 | _ipv4_addr;

qi::rule<Iterator> _ipv6_addr
    = repeat(6)[_h16_col] >> _ls32
    | _col_x2 >> repeat(5)[_h16_col] >> _ls32
    | -(_h16) >> _col_x2 >> repeat(4)[_h16_col] >> _ls32
    | -(_h16_col >> _h16) >> _col_x2 >> repeat(3)[_h16_col] >> _ls32
    | -(repeat(1, 2)[_h16_col] >> _h16) >> _col_x2 >>
repeat(2)[_h16_col] >> _ls32
    | -(repeat(1, 3)[_h16_col] >> _h16) >> _col_x2 >> _h16_col >> _ls32
    | -(repeat(1, 4)[_h16_col] >> _h16) >> _col_x2 >> _ls32
    | -(repeat(1, 5)[_h16_col] >> _h16) >> _col_x2 >> _h16
    | -(repeat(1, 6)[_h16_col] >> _h16) >> _col_x2
    ;

qi::rule<Iterator> _start = _ipv6_addr >> eoi;

ref: http://www.ietf.org/rfc/rfc5954.txt, section 4.1 Resolution for
Extra Colon in IPv4-Mapped IPv6 Address

I'll cook up a web program example if necessary, just wanted to get
some initial feedback.

Thoughts?

Thanks!

Regards,

Michael Powell

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.