<s>
In	O
C	B-Language
programming	I-Language
,	O
the	O
functions	O
getaddrinfo( )	O
and	O
getnameinfo( )	O
convert	O
domain	O
names	O
,	O
hostnames	B-Protocol
,	O
and	O
IP	B-Protocol
addresses	I-Protocol
between	O
human-readable	O
text	O
representations	O
and	O
structured	O
binary	O
formats	O
for	O
the	O
operating	B-General_Concept
system	I-General_Concept
's	O
networking	O
API	B-Application
.	O
</s>
<s>
Both	O
functions	O
are	O
contained	O
in	O
the	O
POSIX	O
standard	O
application	B-Application
programming	I-Application
interface	I-Application
(	O
API	B-Application
)	O
.	O
</s>
<s>
getaddrinfo	B-Language
and	O
getnameinfo	B-Language
are	O
inverse	O
functions	O
of	O
each	O
other	O
.	O
</s>
<s>
They	O
are	O
network	O
protocol	O
agnostic	O
,	O
and	O
support	O
both	O
IPv4	B-Protocol
and	O
IPv6	B-Protocol
.	O
</s>
<s>
It	O
is	O
the	O
recommended	O
interface	O
for	O
name	O
resolution	O
in	O
building	O
protocol	O
independent	O
applications	O
and	O
for	O
transitioning	O
legacy	O
IPv4	B-Protocol
code	O
to	O
the	O
IPv6	B-Protocol
Internet	I-Protocol
.	O
</s>
<s>
Internally	O
,	O
the	O
functions	O
may	O
use	O
a	O
variety	O
of	O
resolution	O
methods	O
not	O
limited	O
to	O
the	O
Domain	B-Protocol
Name	I-Protocol
System	I-Protocol
(	O
DNS	B-Protocol
)	O
.	O
</s>
<s>
The	O
Name	B-Protocol
Service	I-Protocol
Switch	I-Protocol
is	O
commonly	O
used	O
on	O
Unix-like	O
systems	O
and	O
affects	O
most	O
implementation	O
of	O
this	O
pair	O
as	O
it	O
did	O
with	O
their	O
BSD-socket	O
era	O
predecessors	O
.	O
</s>
<s>
The	O
C	B-Language
data	O
structure	O
used	O
to	O
represent	O
addresses	O
and	O
hostnames	B-Protocol
within	O
the	O
networking	O
API	B-Application
is	O
the	O
following	O
:	O
</s>
<s>
If	O
the	O
types	O
are	O
incompatible	O
,	O
e.g.	O
,	O
on	O
a	O
64-bit	O
Solaris	B-Application
9	I-Application
system	O
where	O
size_t	O
is	O
8	O
bytes	O
and	O
socklen_t	O
is	O
4	O
bytes	O
,	O
then	O
run-time	O
errors	O
may	O
result	O
.	O
</s>
<s>
These	O
are	O
set	O
to	O
the	O
same	O
value	O
when	O
the	O
structure	O
is	O
created	O
with	O
function	O
getaddrinfo	B-Language
in	O
some	O
implementations	O
.	O
</s>
<s>
getaddrinfo( )	O
converts	O
human-readable	O
text	O
strings	O
representing	O
hostnames	B-Protocol
or	O
IP	B-Protocol
addresses	I-Protocol
into	O
a	O
dynamically	B-General_Concept
allocated	I-General_Concept
linked	B-Data_Structure
list	I-Data_Structure
of	O
struct	O
addrinfo	O
structures	O
.	O
</s>
<s>
Then	O
,	O
if	O
hostname	B-Protocol
is	O
given	O
(	O
not	O
NULL	O
)	O
,	O
a	O
call	O
to	O
gethostbyname( )	O
resolves	O
it	O
,	O
or	O
otherwise	O
the	O
address	O
0.0.0.0	O
is	O
used	O
,	O
if	O
hints->ai_flags	O
is	O
set	O
to	O
AI_PASSIVE	O
,	O
and	O
127.0.0.1	O
otherwise	O
.	O
</s>
<s>
This	O
function	O
frees	O
the	O
memory	O
allocated	O
by	O
function	O
getaddrinfo( )	O
.	O
</s>
<s>
As	O
the	O
result	O
of	O
the	O
latter	O
is	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
of	O
addrinfo	O
structures	O
starting	O
at	O
the	O
address	O
ai	O
,	O
freeaddrinfo( )	O
loops	O
through	O
the	O
list	O
and	O
frees	O
each	O
one	O
in	O
turn	O
.	O
</s>
<s>
The	O
function	O
getnameinfo( )	O
converts	O
the	O
internal	O
binary	O
representation	O
of	O
an	O
IP	B-Protocol
address	I-Protocol
in	O
the	O
form	O
of	O
a	O
pointer	O
to	O
a	O
struct	O
sockaddr	O
into	O
text	O
strings	O
consisting	O
of	O
the	O
hostname	B-Protocol
or	O
,	O
if	O
the	O
address	O
cannot	O
be	O
resolved	O
into	O
a	O
name	O
,	O
a	O
textual	O
IP	B-Protocol
address	I-Protocol
representation	O
,	O
as	O
well	O
as	O
the	O
service	O
port	O
name	O
or	O
number	O
.	O
</s>
<s>
The	O
following	O
example	O
uses	O
getaddrinfo( )	O
to	O
resolve	O
the	O
domain	O
name	O
www.example.com	O
into	O
its	O
list	O
of	O
addresses	O
and	O
then	O
calls	O
getnameinfo( )	O
on	O
each	O
result	O
to	O
return	O
the	O
canonical	O
name	O
for	O
the	O
address	O
.	O
</s>
<s>
In	O
general	O
,	O
this	O
produces	O
the	O
original	O
hostname	B-Protocol
,	O
unless	O
the	O
particular	O
address	O
has	O
multiple	O
names	O
,	O
in	O
which	O
case	O
the	O
canonical	O
name	O
is	O
returned	O
.	O
</s>
<s>
On	O
February	O
16	O
,	O
2016	O
,	O
a	O
security	O
bug	O
was	O
announced	O
in	O
the	O
glibc	B-Language
implementation	O
of	O
getaddrinfo( )	O
,	O
using	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
technique	O
,	O
that	O
may	O
allow	O
execution	O
of	O
arbitrary	O
code	O
by	O
the	O
attacker	O
.	O
</s>
