/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R. Campbell.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
for (ai = addrinfo; ai != NULL; ai = ai->ai_next) {
/* Print the socket type. */
if ((ai->ai_socktype >= 0) &&
((size_t)ai->ai_socktype < __arraycount(socket_types)) &&
(socket_types[ai->ai_socktype] != NULL))
n = printf("%s", socket_types[ai->ai_socktype]);
else
n = printf("%d", ai->ai_socktype);
if (n < 0)
err(1, "printf");
/* Print the address family. */
if ((ai->ai_family >= 0) &&
((size_t)ai->ai_family < __arraycount(address_families)) &&
(address_families[ai->ai_family] != NULL))
n = printf(" %s", address_families[ai->ai_family]);
else
n = printf(" %d", ai->ai_family);
if (n < 0)
err(1, "printf");
/* Print the protocol number. */
protoent = getprotobynumber(ai->ai_protocol);
if (protoent == NULL)
n = printf(" %d", ai->ai_protocol);
else
n = printf(" %s", protoent->p_name);
if (n < 0)
err(1, "printf");
/* Format the sockaddr. */
switch (ai->ai_family) {
case AF_INET:
case AF_INET6:
n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
ai->ai_addr);
break;
default:
n = sockaddr_snprintf(buf, sizeof(buf),
"%a %p %I %F %R %S", ai->ai_addr);
}
/*
* Check for sockaddr_snprintf failure.
*
* XXX sockaddr_snprintf's error reporting is botched
* -- man page says it sets errno, but if getnameinfo
* fails, errno is not where it reports the error...
*/
if (n < 0) {
warnx("sockaddr_snprintf failed");
continue;
}
if (sizeof(buf) <= (size_t)n)
warnx("truncated sockaddr_snprintf output");
/* Print the formatted sockaddr. */
if (printf("%s\n", buf) < 0)
err(1, "printf");
}
}