ARES_INET_PTON(3)

πŸ’… ARES_INET_PTON(3) πŸ’…

okay so bestie, welcome to ares_inet_pton β€” a little function from the Library Functions Manual section of the c-ares world, and honestly she's iconic for reasons we're about to get into πŸŽ€

✨ NAME ✨

ares_inet_pton β€” converts an IPv4 or IPv6 address from text form into binary form. literally just glow-up-ifying your string into bytes, no cap πŸ’–

🌸 SYNOPSIS 🌸

#include <ares.h>

int ares_inet_pton(int af, const char *src, void *dst);

πŸ¦‹ DESCRIPTION πŸ¦‹

so this is the portable version of the inet_pton you already know and love. for AF_INET6 she behaves exactly the same β€” no notes, same energy. but for AF_INET she's got a little extra, and she's gonna accept the legacy forms described below πŸ‘€ (yes those ones, we'll get there).

The ares_inet_pton() function takes the address sitting in its standard text presentation form and converts it into its numeric binary form. The af argument shall specify the family of the address β€” and AF_INET and AF_INET6 shall be the supported ones. The src argument points to the string being passed in. The dst argument points to a buffer where the function stores the numeric address, and bestie listen: this buffer shall be big enough to hold the numeric address β€” that's 32 bits for AF_INET and 128 bits for AF_INET6. do not skimp on the buffer, I'm begging πŸ™

The src argument must be an address, not a network. A "/bits" prefix length suffix (CIDR notation, you know the vibe) is not accepted, and the function just returns 0 for that kind of input. 🫠

On success, the whole address β€” 4 bytes for AF_INET, 16 bytes for AF_INET6 β€” is always written to dst. And here's a little lore drop for you: versions of c-ares before 1.34.9 accepted a prefix length here. we don't do that anymore, she's moved on πŸ’…

Unlike POSIX inet_pton, AF_INET also accepts shortened forms such as 10.1 and hexadecimal forms such as 0x0a000001. Octets that are not given are set to zero, so 10.1 is 10.1.0.0. so generous of her honestly 🎁

⭐ RETURN VALUES (the vibe check) ⭐

she returns:

  • 1 β€” the address was valid for the specified address family. slay, it worked πŸŽ‰
  • 0 β€” the address was not parseable in the specified address family. not her fault, the string was giving chaos 😭
  • -1 β€” the address family is not supported (and in that case errno will have been set to EAFNOSUPPORT). devious of you to even ask πŸ’€

πŸ”₯ SEE ALSO πŸ”₯

(the round trip is real, iykyk 🌈)

πŸ‘‘ AVAILABILITY πŸ‘‘

made properly publicly available in c-ares for real in version 1.10.0. no cap, she's been out here since then 🫢


okay bestie that's the whole page ✨ ngl this one's a lil legend: she's from 17 Feb 2013, she lives in the Library Functions Manual as ARES_INET_PTON(3), and she's serving portable-binary-conversion realness. go forth and parse responsibly πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from