libexplain  1.4.D001
libexplain/buffer/hostent.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2013, 2014 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/netdb.h>
00020 #include <libexplain/ac/sys/socket.h> /* for FreeBSD, for AF_INET */
00021 #include <libexplain/ac/string.h>
00022 
00023 #include <libexplain/buffer/address_family.h>
00024 #include <libexplain/buffer/hexdump.h>
00025 #include <libexplain/buffer/hostent.h>
00026 #include <libexplain/buffer/in6_addr.h>
00027 #include <libexplain/buffer/int.h>
00028 #include <libexplain/buffer/pointer.h>
00029 #include <libexplain/buffer/sockaddr.h>
00030 #include <libexplain/is_efault.h>
00031 
00032 
00033 void
00034 explain_buffer_hostent(explain_string_buffer_t *sb,
00035     const struct hostent *data)
00036 {
00037     if (explain_is_efault_pointer(data, sizeof(*data)))
00038     {
00039         explain_buffer_pointer(sb, data);
00040         return;
00041     }
00042 
00043     explain_string_buffer_puts(sb, "{ h_name = ");
00044     explain_string_buffer_puts_quoted(sb, data->h_name);
00045     if (data->h_aliases && data->h_aliases[0])
00046     {
00047         char            **sp;
00048 
00049         explain_string_buffer_puts(sb, ", h_aliases = { ");
00050         for (sp = data->h_aliases; *sp; ++sp)
00051         {
00052             if (sp != data->h_aliases)
00053                 explain_string_buffer_puts(sb, ", ");
00054             explain_string_buffer_puts_quoted(sb, *sp);
00055         }
00056         explain_string_buffer_puts(sb, " }");
00057     }
00058     explain_string_buffer_puts(sb, ", h_addrtype = ");
00059     explain_buffer_address_family(sb, data->h_addrtype);
00060     if (data->h_length)
00061     {
00062         explain_string_buffer_puts(sb, ", h_length = ");
00063         explain_buffer_int(sb, data->h_length);
00064     }
00065     if (data->h_length > 0 && data->h_addr_list && data->h_addr_list[0])
00066     {
00067         char            **ap;
00068 
00069         explain_string_buffer_puts(sb, ", h_addr_list = { ");
00070         for (ap = data->h_addr_list; *ap; ++ap)
00071         {
00072             if (ap != data->h_addr_list)
00073                 explain_string_buffer_puts(sb, ", ");
00074             switch (data->h_addrtype)
00075             {
00076             case AF_INET:
00077                 explain_buffer_in_addr(sb, (const struct in_addr *)*ap);
00078                 break;
00079 
00080             case AF_INET6:
00081                 explain_buffer_in6_addr(sb, (const struct in6_addr *)*ap);
00082                 break;
00083 
00084             default:
00085                 /* This is documented as impossible.  Yeah, right. */
00086                 explain_buffer_hexdump(sb, *ap, data->h_length);
00087                 break;
00088             }
00089         }
00090         explain_string_buffer_puts(sb, " }");
00091     }
00092     explain_string_buffer_puts(sb, " }");
00093 }
00094 
00095 
00096 /* vim: set ts=8 sw=4 et : */