Depending on the version of Perl you're using, I believe Socket6 isn't required for IPv6 lookups, Socket itself handles both IPv4 and IPv6. You're correct in the comment that this is (most likely) an IPv6/IPv4 thing as IPv6 addresses are 16 bytes and I've seen this before myself.

In most operating systems, IPv6 is preferred over IPv4, so if you don't want that behavior, turn off IPv6 on the interface(s) that $addr returns values for.

Alternatively, use calls that can deal with IPv4/IPv6 address families:

gethostbyname -> getaddrinfo gethostbyaddr -> getnameinfo inet_aton -> inet_pton inet_ntoa -> inet_ntop

Your grabbing a substring is certainly *not* a way to get around this as you'll just be pulling the last 4 bytes of an IPv6 address - this is *not* equivalent to the IPv4 address (in most all cases unless specifically configured or using IPv4 compatible addressing).


In reply to Re: IO::Socket::INET's sockaddr() returns 16 byte instead of 4, Socket::inet_ntoa() complains by VinsWorldcom
in thread IO::Socket::INET's sockaddr() returns 16 byte instead of 4, Socket::inet_ntoa() complains by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.