inet_net_pton() alternative for IPv6

tomas@tuxteam.de tomas@tuxteam.de
Mon Jul 4 18:23:52 GMT 2022


On Mon, Jul 04, 2022 at 01:07:59PM -0500, Ian Pilcher via Libc-help wrote:
> I am searching for the best way to parse both IPv4 and IPv6 CIDR-
> formatted addresses.  I have found inet_net_pton(), but it only supports
> IPv4 (AF_INET) addresses.

I never tried it, but this is what my version [1] of the libc docs say:

 -- Function: int inet_pton (int AF, const char *CP, void *BUF)

     Preliminary: | MT-Safe locale | AS-Safe | AC-Safe | *Note POSIX
     Safety Concepts::.

     This function converts an Internet address (either IPv4 or IPv6)
     from presentation (textual) to network (binary) format.  AF should
     be either ‘AF_INET’ or ‘AF_INET6’, as appropriate for the type of
     address being converted.  CP is a pointer to the input string, and
     BUF is a pointer to a buffer for the result.  It is the caller’s
     responsibility to make sure the buffer is large enough.

 -- Function: const char * inet_ntop (int AF, const void *CP, char *BUF,
          socklen_t LEN)

     Preliminary: | MT-Safe locale | AS-Safe | AC-Safe | *Note POSIX
     Safety Concepts::.

     This function converts an Internet address (either IPv4 or IPv6)
     from network (binary) to presentation (textual) form.  AF should be
     either ‘AF_INET’ or ‘AF_INET6’, as appropriate.  CP is a pointer to
     the address to be converted.  BUF should be a pointer to a buffer
     to hold the result, and LEN is the length of this buffer.  The
     return value from the function will be this buffer address.

so they seem to cover your needs?

> Needless to say, this is pretty disappointing in 2022.  Is there an
> alternative API that I should be using, or am I stuck writing my own?

C'm on. Be nice :)

Cheers

[1] This is ‘The GNU C Library Reference Manual’, for Version (Debian
   glibc-doc-reference 2.31-1) of the GNU C Library.

-- 
t
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-help/attachments/20220704/1ae67564/attachment.sig>


More information about the Libc-help mailing list