2000-07-05 08:27:50 +00:00
|
|
|
.\" $FreeBSD$
|
2000-08-09 23:16:23 +00:00
|
|
|
.\" $KAME: getaddrinfo.3,v 1.22 2000/08/09 21:16:17 itojun Exp $
|
2000-07-05 08:27:50 +00:00
|
|
|
.\"
|
2000-08-09 23:16:23 +00:00
|
|
|
.\" Copyright (c) 1983, 1987, 1991, 1993
|
1999-12-28 02:37:14 +00:00
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the University of
|
|
|
|
.\" California, Berkeley and its contributors.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
|
|
|
|
.\"
|
|
|
|
.Dd May 25, 1995
|
|
|
|
.Dt GETADDRINFO 3
|
2000-08-09 23:16:23 +00:00
|
|
|
.Os
|
1999-12-28 02:37:14 +00:00
|
|
|
.\"
|
|
|
|
.Sh NAME
|
2000-07-05 08:27:50 +00:00
|
|
|
.Nm getaddrinfo ,
|
1999-12-28 02:37:14 +00:00
|
|
|
.Nm freeaddrinfo ,
|
|
|
|
.Nm gai_strerror
|
|
|
|
.Nd nodename-to-address translation in protocol-independent manner
|
|
|
|
.\"
|
2000-04-21 09:42:15 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1999-12-28 02:37:14 +00:00
|
|
|
.Sh SYNOPSIS
|
2000-01-05 19:02:14 +00:00
|
|
|
.Fd #include <sys/types.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
.Fd #include <sys/socket.h>
|
|
|
|
.Fd #include <netdb.h>
|
|
|
|
.Ft int
|
2000-07-05 08:27:50 +00:00
|
|
|
.Fn getaddrinfo "const char *nodename" "const char *servname" \
|
|
|
|
"const struct addrinfo *hints" "struct addrinfo **res"
|
1999-12-28 02:37:14 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn freeaddrinfo "struct addrinfo *ai"
|
|
|
|
.Ft "char *"
|
|
|
|
.Fn gai_strerror "int ecode"
|
|
|
|
.\"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Fn getaddrinfo
|
|
|
|
function is defined for protocol-independent nodename-to-address translation.
|
2000-07-05 08:27:50 +00:00
|
|
|
It performs the functionality of
|
1999-12-28 02:37:14 +00:00
|
|
|
.Xr gethostbyname 3
|
|
|
|
and
|
|
|
|
.Xr getservbyname 3 ,
|
2000-07-05 08:27:50 +00:00
|
|
|
but in a more sophisticated manner.
|
1999-12-28 02:37:14 +00:00
|
|
|
.Pp
|
2000-07-05 08:27:50 +00:00
|
|
|
The
|
|
|
|
.Li addrinfo
|
|
|
|
structure is defined as a result of including the
|
|
|
|
.Aq Pa netdb.h
|
1999-12-28 02:37:14 +00:00
|
|
|
header:
|
|
|
|
.Bd -literal -offset
|
2001-02-10 07:48:01 +00:00
|
|
|
struct addrinfo {
|
1999-12-28 02:37:14 +00:00
|
|
|
int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
|
|
|
|
int ai_family; /* PF_xxx */
|
|
|
|
int ai_socktype; /* SOCK_xxx */
|
|
|
|
int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
|
|
|
|
size_t ai_addrlen; /* length of ai_addr */
|
|
|
|
char *ai_canonname; /* canonical name for nodename */
|
|
|
|
struct sockaddr *ai_addr; /* binary address */
|
|
|
|
struct addrinfo *ai_next; /* next structure in linked list */
|
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fa nodename
|
|
|
|
and
|
|
|
|
.Fa servname
|
|
|
|
arguments are pointers to null-terminated strings or
|
|
|
|
.Dv NULL .
|
|
|
|
One or both of these two arguments must be a
|
|
|
|
.Pf non Dv -NULL
|
|
|
|
pointer.
|
|
|
|
In the normal client scenario, both the
|
|
|
|
.Fa nodename
|
|
|
|
and
|
|
|
|
.Fa servname
|
|
|
|
are specified.
|
|
|
|
In the normal server scenario, only the
|
|
|
|
.Fa servname
|
|
|
|
is specified.
|
|
|
|
A
|
|
|
|
.Pf non Dv -NULL
|
|
|
|
.Fa nodename
|
|
|
|
string can be either a node name or a numeric host address string
|
|
|
|
.Po
|
|
|
|
i.e., a dotted-decimal IPv4 address or an IPv6 hex address
|
|
|
|
.Pc .
|
|
|
|
A
|
|
|
|
.Pf non Dv -NULL
|
|
|
|
.Fa servname
|
|
|
|
string can be either a service name or a decimal port number.
|
|
|
|
.Pp
|
|
|
|
The caller can optionally pass an
|
|
|
|
.Li addrinfo
|
|
|
|
structure, pointed to by the third argument,
|
|
|
|
to provide hints concerning the type of socket that the caller supports.
|
|
|
|
In this
|
|
|
|
.Fa hints
|
|
|
|
structure all members other than
|
|
|
|
.Fa ai_flags ,
|
|
|
|
.Fa ai_family ,
|
|
|
|
.Fa ai_socktype ,
|
|
|
|
and
|
|
|
|
.Fa ai_protocol
|
|
|
|
must be zero or a
|
|
|
|
.Dv NULL
|
|
|
|
pointer.
|
|
|
|
A value of
|
|
|
|
.Dv PF_UNSPEC
|
|
|
|
for
|
|
|
|
.Fa ai_family
|
|
|
|
means the caller will accept any protocol family.
|
|
|
|
A value of 0 for
|
|
|
|
.Fa ai_socktype
|
|
|
|
means the caller will accept any socket type.
|
|
|
|
A value of 0 for
|
|
|
|
.Fa ai_protocol
|
|
|
|
means the caller will accept any protocol.
|
|
|
|
For example, if the caller handles only TCP and not UDP, then the
|
|
|
|
.Fa ai_socktype
|
|
|
|
member of the hints structure should be set to
|
|
|
|
.Dv SOCK_STREAM
|
|
|
|
when
|
|
|
|
.Fn getaddrinfo
|
|
|
|
is called.
|
|
|
|
If the caller handles only IPv4 and not IPv6, then the
|
|
|
|
.Fa ai_family
|
|
|
|
member of the
|
|
|
|
.Fa hints
|
|
|
|
structure should be set to
|
|
|
|
.Dv PF_INET
|
|
|
|
when
|
|
|
|
.Fn getaddrinfo
|
|
|
|
is called.
|
|
|
|
If the third argument to
|
|
|
|
.Fn getaddrinfo
|
|
|
|
is a
|
|
|
|
.Dv NULL
|
|
|
|
pointer, this is the same as if the caller had filled in an
|
|
|
|
.Li addrinfo
|
|
|
|
structure initialized to zero with
|
|
|
|
.Fa ai_family
|
2000-07-05 08:27:50 +00:00
|
|
|
set to
|
|
|
|
.Dv PF_UNSPEC .
|
1999-12-28 02:37:14 +00:00
|
|
|
.Pp
|
|
|
|
Upon successful return a pointer to a linked list of one or more
|
|
|
|
.Li addrinfo
|
|
|
|
structures is returned through the final argument.
|
|
|
|
The caller can process each
|
|
|
|
.Li addrinfo
|
|
|
|
structure in this list by following the
|
|
|
|
.Fa ai_next
|
|
|
|
pointer, until a
|
|
|
|
.Dv NULL
|
|
|
|
pointer is encountered.
|
|
|
|
In each returned
|
|
|
|
.Li addrinfo
|
|
|
|
structure the three members
|
|
|
|
.Fa ai_family ,
|
|
|
|
.Fa ai_socktype ,
|
|
|
|
and
|
|
|
|
.Fa ai_protocol
|
|
|
|
are the corresponding arguments for a call to the
|
|
|
|
.Fn socket
|
|
|
|
function.
|
|
|
|
In each
|
|
|
|
.Li addrinfo
|
|
|
|
structure the
|
|
|
|
.Fa ai_addr
|
|
|
|
member points to a filled-in socket address structure whose length is
|
|
|
|
specified by the
|
|
|
|
.Fa ai_addrlen
|
|
|
|
member.
|
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Dv AI_PASSIVE
|
|
|
|
bit is set in the
|
|
|
|
.Fa ai_flags
|
|
|
|
member of the
|
|
|
|
.Fa hints
|
|
|
|
structure, then the caller plans to use the returned socket address
|
|
|
|
structure in a call to
|
|
|
|
.Fn bind .
|
|
|
|
In this case, if the
|
|
|
|
.Fa nodename
|
|
|
|
argument is a
|
|
|
|
.Dv NULL
|
|
|
|
pointer, then the IP address portion of the socket
|
|
|
|
address structure will be set to
|
|
|
|
.Dv INADDR_ANY
|
|
|
|
for an IPv4 address or
|
|
|
|
.Dv IN6ADDR_ANY_INIT
|
|
|
|
for an IPv6 address.
|
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Dv AI_PASSIVE
|
|
|
|
bit is not set in the
|
|
|
|
.Fa ai_flags
|
|
|
|
member of the
|
|
|
|
.Fa hints
|
|
|
|
structure, then the returned socket address structure will be ready for a
|
|
|
|
call to
|
|
|
|
.Fn connect
|
|
|
|
.Pq for a connection-oriented protocol
|
|
|
|
or either
|
|
|
|
.Fn connect ,
|
2000-07-05 08:27:50 +00:00
|
|
|
.Fn sendto ,
|
|
|
|
or
|
1999-12-28 02:37:14 +00:00
|
|
|
.Fn sendmsg
|
|
|
|
.Pq for a connectionless protocol .
|
|
|
|
In this case, if the
|
|
|
|
.Fa nodename
|
|
|
|
argument is a
|
|
|
|
.Dv NULL
|
|
|
|
pointer, then the IP address portion of the
|
|
|
|
socket address structure will be set to the loopback address.
|
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Dv AI_CANONNAME
|
|
|
|
bit is set in the
|
|
|
|
.Fa ai_flags
|
|
|
|
member of the
|
|
|
|
.Fa hints
|
|
|
|
structure, then upon successful return the
|
|
|
|
.Fa ai_canonname
|
|
|
|
member of the first
|
|
|
|
.Li addrinfo
|
|
|
|
structure in the linked list will point to a null-terminated string
|
|
|
|
containing the canonical name of the specified
|
|
|
|
.Fa nodename .
|
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Dv AI_NUMERICHOST
|
|
|
|
bit is set in the
|
|
|
|
.Fa ai_flags
|
|
|
|
member of the
|
|
|
|
.Fa hints
|
|
|
|
structure, then a
|
|
|
|
.Pf non Dv -NULL
|
|
|
|
.Fa nodename
|
|
|
|
string must be a numeric host address string.
|
|
|
|
Otherwise an error of
|
|
|
|
.Dv EAI_NONAME
|
|
|
|
is returned.
|
|
|
|
This flag prevents any type of name resolution service (e.g., the DNS)
|
|
|
|
from being called.
|
|
|
|
.Pp
|
2000-08-09 23:16:23 +00:00
|
|
|
The arguments to
|
|
|
|
.Fn getaddrinfo
|
2001-02-10 07:48:01 +00:00
|
|
|
must be sufficiently consistent and unambiguous.
|
|
|
|
Here are some problem cases you may encounter:
|
2000-08-09 23:16:23 +00:00
|
|
|
.Bl -bullet
|
|
|
|
.It
|
|
|
|
.Fn getaddrinfo
|
2001-02-10 07:48:01 +00:00
|
|
|
will fail if the members in the
|
2000-08-09 23:16:23 +00:00
|
|
|
.Fa hints
|
2001-02-10 07:48:01 +00:00
|
|
|
structure are not consistent.
|
2000-08-09 23:16:23 +00:00
|
|
|
For example, for internet address families,
|
|
|
|
.Fn getaddrinfo
|
2001-02-10 07:48:01 +00:00
|
|
|
will fail if you specify
|
2000-08-09 23:16:23 +00:00
|
|
|
.Dv SOCK_STREAM
|
|
|
|
to
|
|
|
|
.Fa ai_socktype
|
|
|
|
while you specify
|
|
|
|
.Dv IPPROTO_UDP
|
|
|
|
to
|
|
|
|
.Fa ai_protocol .
|
|
|
|
.It
|
|
|
|
If you specify a
|
|
|
|
.Fa servname
|
|
|
|
which is defined only for certain
|
|
|
|
.Fa ai_socktype ,
|
|
|
|
.Fn getaddrinfo
|
2001-02-10 07:48:01 +00:00
|
|
|
will fail because the arguments are not consistent.
|
2000-08-09 23:16:23 +00:00
|
|
|
For example,
|
|
|
|
.Fn getaddrinfo
|
2001-02-10 07:48:01 +00:00
|
|
|
will return an error if you ask for
|
2000-08-09 23:16:23 +00:00
|
|
|
.Dq Li tftp
|
|
|
|
service on
|
|
|
|
.Dv SOCK_STREAM .
|
|
|
|
.It
|
|
|
|
For internet address families, if you specify
|
|
|
|
.Fa servname
|
|
|
|
while you set
|
|
|
|
.Fa ai_socktype
|
|
|
|
to
|
|
|
|
.Dv SOCK_RAW ,
|
|
|
|
.Fn getaddrinfo
|
2001-02-10 07:48:01 +00:00
|
|
|
will fail, because service names are not defined for the internet
|
2000-08-09 23:16:23 +00:00
|
|
|
.Dv SOCK_RAW
|
|
|
|
space.
|
|
|
|
.It
|
|
|
|
If you specify numeric
|
|
|
|
.Fa servname ,
|
|
|
|
while leaving
|
|
|
|
.Fa ai_socktype
|
|
|
|
and
|
|
|
|
.Fa ai_protocol
|
|
|
|
unspecified,
|
|
|
|
.Fn getaddrinfo
|
2001-02-10 07:48:01 +00:00
|
|
|
will fail.
|
2000-08-09 23:16:23 +00:00
|
|
|
This is because the numeric
|
|
|
|
.Fa servname
|
|
|
|
does not identify any socket type, and
|
|
|
|
.Fn getaddrinfo
|
|
|
|
is not allowed to glob the argument in such case.
|
|
|
|
.El
|
|
|
|
.Pp
|
1999-12-28 02:37:14 +00:00
|
|
|
All of the information returned by
|
|
|
|
.Fn getaddrinfo
|
|
|
|
is dynamically allocated:
|
|
|
|
the
|
|
|
|
.Li addrinfo
|
2000-07-05 08:27:50 +00:00
|
|
|
structures, the socket address structures, and canonical node name
|
1999-12-28 02:37:14 +00:00
|
|
|
strings pointed to by the addrinfo structures.
|
|
|
|
To return this information to the system the function
|
|
|
|
.Fn freeaddrinfo
|
|
|
|
is called.
|
|
|
|
The
|
|
|
|
.Fa addrinfo
|
|
|
|
structure pointed to by the
|
|
|
|
.Fa ai argument
|
|
|
|
is freed, along with any dynamic storage pointed to by the structure.
|
|
|
|
This operation is repeated until a
|
|
|
|
.Dv NULL
|
|
|
|
.Fa ai_next
|
|
|
|
pointer is encountered.
|
|
|
|
.Pp
|
|
|
|
To aid applications in printing error messages based on the
|
|
|
|
.Dv EAI_xxx
|
|
|
|
codes returned by
|
|
|
|
.Fn getaddrinfo ,
|
|
|
|
.Fn gai_strerror
|
|
|
|
is defined.
|
|
|
|
The argument is one of the
|
|
|
|
.Dv EAI_xxx
|
|
|
|
values defined earlier and the return value points to a string describing
|
|
|
|
the error.
|
|
|
|
If the argument is not one of the
|
|
|
|
.Dv EAI_xxx
|
|
|
|
values, the function still returns a pointer to a string whose contents
|
|
|
|
indicate an unknown error.
|
|
|
|
.\"
|
2001-02-10 07:48:01 +00:00
|
|
|
.Sh EXTENSIONS
|
|
|
|
This implementation supports numeric IPv6 address notation with the
|
|
|
|
experimental scope identifier.
|
|
|
|
By appending a percent sign and scope identifier to the address, you
|
|
|
|
can specify the value of the
|
1999-12-28 02:37:14 +00:00
|
|
|
.Li sin6_scope_id
|
2001-02-10 07:48:01 +00:00
|
|
|
field of the socket address.
|
|
|
|
This makes management of scoped address easier,
|
|
|
|
and allows cut-and-paste input of scoped addresses.
|
1999-12-28 02:37:14 +00:00
|
|
|
.Pp
|
2001-02-10 07:48:01 +00:00
|
|
|
At the moment the code supports only link-local addresses in this format.
|
|
|
|
The scope identifier is hardcoded to name of hardware interface associated
|
|
|
|
with the link,
|
1999-12-28 02:37:14 +00:00
|
|
|
.Po
|
|
|
|
such as
|
|
|
|
.Li ne0
|
|
|
|
.Pc .
|
2001-02-10 07:48:01 +00:00
|
|
|
For example,
|
2000-07-05 08:27:50 +00:00
|
|
|
.Dq Li fe80::1%ne0 ,
|
1999-12-28 02:37:14 +00:00
|
|
|
which means
|
|
|
|
.Do
|
|
|
|
.Li fe80::1
|
2001-02-10 07:48:01 +00:00
|
|
|
on the link associated with the
|
1999-12-28 02:37:14 +00:00
|
|
|
.Li ne0
|
|
|
|
interface
|
|
|
|
.Dc .
|
|
|
|
.Pp
|
2001-02-10 07:48:01 +00:00
|
|
|
This implementation is still very experimental and non-standard.
|
|
|
|
The current implementation assumes a one-to-one relationship between
|
|
|
|
interfaces and links, which is not necessarily true according to the
|
|
|
|
specification.
|
1999-12-28 02:37:14 +00:00
|
|
|
.\"
|
2000-07-05 08:27:50 +00:00
|
|
|
.Sh EXAMPLES
|
|
|
|
The following code tries to connect to
|
|
|
|
.Dq Li www.kame.net
|
|
|
|
service
|
|
|
|
.Dq Li http .
|
|
|
|
via stream socket.
|
2001-02-10 07:48:01 +00:00
|
|
|
It loops through all the addresses available, regardless of the address family.
|
|
|
|
If the destination resolves to an IPv4 address, it will use an
|
2000-07-05 08:27:50 +00:00
|
|
|
.Dv AF_INET
|
|
|
|
socket.
|
2001-02-10 07:48:01 +00:00
|
|
|
Similarly, if it resolves to IPv6, an
|
2000-07-05 08:27:50 +00:00
|
|
|
.Dv AF_INET6
|
|
|
|
socket is used.
|
|
|
|
Observe that there is no hardcoded reference to particular address family.
|
|
|
|
The code works even if
|
2000-11-20 14:11:46 +00:00
|
|
|
.Fn getaddrinfo
|
2000-07-05 08:27:50 +00:00
|
|
|
returns addresses that are not IPv4/v6.
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
struct addrinfo hints, *res, *res0;
|
|
|
|
int error;
|
|
|
|
int s;
|
|
|
|
const char *cause = NULL;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
error = getaddrinfo("www.kame.net", "http", &hints, &res0);
|
|
|
|
if (error) {
|
2001-01-26 13:46:40 +00:00
|
|
|
errx(1, "%s", gai_strerror(error));
|
2000-07-05 08:27:50 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
s = -1;
|
2001-02-10 07:48:01 +00:00
|
|
|
cause = "no addresses";
|
|
|
|
errno = EADDRNOTAVAIL;
|
2000-07-05 08:27:50 +00:00
|
|
|
for (res = res0; res; res = res->ai_next) {
|
|
|
|
s = socket(res->ai_family, res->ai_socktype,
|
|
|
|
res->ai_protocol);
|
|
|
|
if (s < 0) {
|
|
|
|
cause = "socket";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
|
|
|
|
cause = "connect";
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break; /* okay we got one */
|
|
|
|
}
|
|
|
|
if (s < 0) {
|
|
|
|
err(1, cause);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2001-02-10 07:48:01 +00:00
|
|
|
The following example tries to open a wildcard listening socket onto service
|
2000-07-05 08:27:50 +00:00
|
|
|
.Dq Li http ,
|
|
|
|
for all the address families available.
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
struct addrinfo hints, *res, *res0;
|
|
|
|
int error;
|
|
|
|
int s[MAXSOCK];
|
|
|
|
int nsock;
|
|
|
|
const char *cause = NULL;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
error = getaddrinfo(NULL, "http", &hints, &res0);
|
|
|
|
if (error) {
|
2001-01-26 13:46:40 +00:00
|
|
|
errx(1, "%s", gai_strerror(error));
|
2000-07-05 08:27:50 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
nsock = 0;
|
|
|
|
for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
|
|
|
|
s[nsock] = socket(res->ai_family, res->ai_socktype,
|
|
|
|
res->ai_protocol);
|
|
|
|
if (s[nsock] < 0) {
|
|
|
|
cause = "socket";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-02-10 07:48:01 +00:00
|
|
|
if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
|
2001-05-21 10:05:36 +00:00
|
|
|
cause = "bind";
|
2000-07-05 08:27:50 +00:00
|
|
|
close(s[nsock]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-02-10 07:48:01 +00:00
|
|
|
if (listen(s[nsock], SOMAXCONN) < 0) {
|
|
|
|
cause = "listen";
|
|
|
|
close(s[nsock]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-07-05 08:27:50 +00:00
|
|
|
nsock++;
|
|
|
|
}
|
|
|
|
if (nsock == 0) {
|
|
|
|
err(1, cause);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
.Ed
|
|
|
|
.\"
|
1999-12-28 02:37:14 +00:00
|
|
|
.Sh FILES
|
Add nsswitch support. By creating an /etc/nsswitch.conf file, you can
configure FreeBSD so that various databases such as passwd and group can be
looked up using flat files, NIS, or Hesiod.
= Hesiod has been added to libc (see hesiod(3)).
= A library routine for parsing nsswitch.conf and invoking callback
functions as specified has been added to libc (see nsdispatch(3)).
= The following C library functions have been modified to use nsdispatch:
. getgrent, getgrnam, getgrgid
. getpwent, getpwnam, getpwuid
. getusershell
. getaddrinfo
. gethostbyname, gethostbyname2, gethostbyaddr
. getnetbyname, getnetbyaddr
. getipnodebyname, getipnodebyaddr, getnodebyname, getnodebyaddr
= host.conf has been removed from src/etc. rc.network has been modified
to warn that host.conf is no longer used at boot time. In addition, if
there is a host.conf but no nsswitch.conf, the latter is created at boot
time from the former.
Obtained from: NetBSD
2000-09-06 18:16:48 +00:00
|
|
|
.Bl -tag -width /etc/nsswitch.conf -compact
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Pa /etc/hosts
|
Add nsswitch support. By creating an /etc/nsswitch.conf file, you can
configure FreeBSD so that various databases such as passwd and group can be
looked up using flat files, NIS, or Hesiod.
= Hesiod has been added to libc (see hesiod(3)).
= A library routine for parsing nsswitch.conf and invoking callback
functions as specified has been added to libc (see nsdispatch(3)).
= The following C library functions have been modified to use nsdispatch:
. getgrent, getgrnam, getgrgid
. getpwent, getpwnam, getpwuid
. getusershell
. getaddrinfo
. gethostbyname, gethostbyname2, gethostbyaddr
. getnetbyname, getnetbyaddr
. getipnodebyname, getipnodebyaddr, getnodebyname, getnodebyaddr
= host.conf has been removed from src/etc. rc.network has been modified
to warn that host.conf is no longer used at boot time. In addition, if
there is a host.conf but no nsswitch.conf, the latter is created at boot
time from the former.
Obtained from: NetBSD
2000-09-06 18:16:48 +00:00
|
|
|
.It Pa /etc/nsswitch.conf
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Pa /etc/resolv.conf
|
|
|
|
.El
|
|
|
|
.\"
|
|
|
|
.Sh DIAGNOSTICS
|
|
|
|
Error return status from
|
|
|
|
.Fn getaddrinfo
|
|
|
|
is zero on success and non-zero on errors.
|
|
|
|
Non-zero error codes are defined in
|
2000-07-05 08:27:50 +00:00
|
|
|
.Aq Pa netdb.h ,
|
1999-12-28 02:37:14 +00:00
|
|
|
and as follows:
|
|
|
|
.Pp
|
|
|
|
.Bl -tag -width EAI_ADDRFAMILY -compact
|
|
|
|
.It Dv EAI_ADDRFAMILY
|
2000-07-05 08:27:50 +00:00
|
|
|
Address family for
|
|
|
|
.Fa nodename
|
|
|
|
not supported.
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_AGAIN
|
2000-07-05 08:27:50 +00:00
|
|
|
Temporary failure in name resolution.
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_BADFLAGS
|
2000-07-05 08:27:50 +00:00
|
|
|
Invalid value for
|
|
|
|
.Fa ai_flags .
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_FAIL
|
2000-07-05 08:27:50 +00:00
|
|
|
Non-recoverable failure in name resolution.
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_FAMILY
|
2000-07-05 08:27:50 +00:00
|
|
|
.Fa ai_family
|
|
|
|
not supported.
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_MEMORY
|
2000-07-05 08:27:50 +00:00
|
|
|
Memory allocation failure.
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_NODATA
|
2000-07-05 08:27:50 +00:00
|
|
|
No address associated with
|
|
|
|
.Fa nodename .
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_NONAME
|
2000-07-05 08:27:50 +00:00
|
|
|
.Fa nodename
|
|
|
|
nor
|
|
|
|
.Fa servname
|
|
|
|
provided, or not known.
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_SERVICE
|
2000-07-05 08:27:50 +00:00
|
|
|
.Fa servname
|
|
|
|
not supported for
|
|
|
|
.Fa ai_socktype .
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_SOCKTYPE
|
2000-07-05 08:27:50 +00:00
|
|
|
.Fa ai_socktype
|
|
|
|
not supported.
|
1999-12-28 02:37:14 +00:00
|
|
|
.It Dv EAI_SYSTEM
|
2000-07-05 08:27:50 +00:00
|
|
|
System error returned in
|
|
|
|
.Va errno .
|
2001-03-13 01:08:36 +00:00
|
|
|
.It Dv EAI_BADHINTS
|
|
|
|
Invalid value for
|
|
|
|
.Fa hints .
|
|
|
|
.It Dv EAI_PROTOCOL
|
|
|
|
Resolved protocol is unknown.
|
|
|
|
.It Dv EAI_MAX
|
|
|
|
Unknown error.
|
1999-12-28 02:37:14 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2001-02-10 07:48:01 +00:00
|
|
|
If called with an appropriate argument,
|
1999-12-28 02:37:14 +00:00
|
|
|
.Fn gai_strerror
|
|
|
|
returns a pointer to a string describing the given error code.
|
|
|
|
If the argument is not one of the
|
|
|
|
.Dv EAI_xxx
|
|
|
|
values, the function still returns a pointer to a string whose contents
|
|
|
|
indicate an unknown error.
|
|
|
|
.\"
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr gethostbyname 3 ,
|
2001-07-06 16:46:48 +00:00
|
|
|
.Xr getnameinfo 3 ,
|
1999-12-28 02:37:14 +00:00
|
|
|
.Xr getservbyname 3 ,
|
|
|
|
.Xr hosts 5 ,
|
|
|
|
.Xr services 5 ,
|
|
|
|
.Xr hostname 7 ,
|
2000-07-05 08:27:50 +00:00
|
|
|
.Xr named 8
|
1999-12-28 02:37:14 +00:00
|
|
|
.Pp
|
|
|
|
.Rs
|
|
|
|
.%A R. Gilligan
|
|
|
|
.%A S. Thomson
|
|
|
|
.%A J. Bound
|
|
|
|
.%A W. Stevens
|
|
|
|
.%T Basic Socket Interface Extensions for IPv6
|
|
|
|
.%R RFC2553
|
|
|
|
.%D March 1999
|
|
|
|
.Re
|
2000-07-05 08:27:50 +00:00
|
|
|
.Rs
|
|
|
|
.%A Tatsuya Jinmei
|
|
|
|
.%A Atsushi Onoe
|
|
|
|
.%T "An Extension of Format for IPv6 Scoped Addresses"
|
|
|
|
.%R internet draft
|
|
|
|
.%N draft-ietf-ipngwg-scopedaddr-format-02.txt
|
|
|
|
.%O work in progress material
|
|
|
|
.Re
|
|
|
|
.Rs
|
|
|
|
.%A Craig Metz
|
|
|
|
.%T Protocol Independence Using the Sockets API
|
|
|
|
.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
|
|
|
|
.%D June 2000
|
|
|
|
.Re
|
1999-12-28 02:37:14 +00:00
|
|
|
.\"
|
|
|
|
.Sh HISTORY
|
|
|
|
The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
|
|
|
|
.\"
|
|
|
|
.Sh STANDARDS
|
|
|
|
The
|
|
|
|
.Fn getaddrinfo
|
2001-02-26 16:08:15 +00:00
|
|
|
function is defined in
|
2001-03-12 17:28:01 +00:00
|
|
|
.St -p1003.1g-2000 ,
|
2000-07-05 08:27:50 +00:00
|
|
|
and documented in
|
|
|
|
.Dq Basic Socket Interface Extensions for IPv6
|
|
|
|
.Pq RFC2553 .
|
1999-12-28 02:37:14 +00:00
|
|
|
.\"
|
|
|
|
.Sh BUGS
|
2000-08-09 23:16:23 +00:00
|
|
|
The current implementation is not thread-safe.
|
|
|
|
.Pp
|
1999-12-28 02:37:14 +00:00
|
|
|
The text was shamelessly copied from RFC2553.
|