libexplain  1.4.D001
libexplain/buffer/socket_protocol.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as
00008  * published by the Free Software Foundation; either version 3 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/sys/socket.h>
00021 #include <libexplain/ac/netdb.h>
00022 #include <libexplain/ac/netinet/in.h>
00023 
00024 #include <libexplain/parse_bits.h>
00025 #include <libexplain/buffer/socket_protocol.h>
00026 #include <libexplain/sizeof.h>
00027 #include <libexplain/string_buffer.h>
00028 
00029 
00030 static const explain_parse_bits_table_t table[] =
00031 {
00032 #ifdef IPPROTO_IP
00033     { "IPPROTO_IP", IPPROTO_IP },
00034 #endif
00035 #ifdef IPPROTO_ICMP
00036     { "IPPROTO_ICMP", IPPROTO_ICMP },
00037 #endif
00038 #ifdef IPPROTO_IGMP
00039     { "IPPROTO_IGMP", IPPROTO_IGMP },
00040 #endif
00041 #ifdef IPPROTO_IPIP
00042     { "IPPROTO_IPIP", IPPROTO_IPIP },
00043 #endif
00044 #ifdef IPPROTO_TCP
00045     { "IPPROTO_TCP", IPPROTO_TCP },
00046 #endif
00047 #ifdef IPPROTO_EGP
00048     { "IPPROTO_EGP", IPPROTO_EGP },
00049 #endif
00050 #ifdef IPPROTO_PUP
00051     { "IPPROTO_PUP", IPPROTO_PUP },
00052 #endif
00053 #ifdef IPPROTO_UDP
00054     { "IPPROTO_UDP", IPPROTO_UDP },
00055 #endif
00056 #ifdef IPPROTO_IDP
00057     { "IPPROTO_IDP", IPPROTO_IDP },
00058 #endif
00059 #ifdef IPPROTO_TP
00060     { "IPPROTO_TP", IPPROTO_TP },
00061 #endif
00062 #ifdef IPPROTO_IPV6
00063     { "IPPROTO_IPV6", IPPROTO_IPV6 },
00064 #endif
00065 #ifdef IPPROTO_ROUTING
00066     { "IPPROTO_ROUTING", IPPROTO_ROUTING },
00067 #endif
00068 #ifdef IPPROTO_FRAGMENT
00069     { "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT },
00070 #endif
00071 #ifdef IPPROTO_RSVP
00072     { "IPPROTO_RSVP", IPPROTO_RSVP },
00073 #endif
00074 #ifdef IPPROTO_GRE
00075     { "IPPROTO_GRE", IPPROTO_GRE },
00076 #endif
00077 #ifdef IPPROTO_ESP
00078     { "IPPROTO_ESP", IPPROTO_ESP },
00079 #endif
00080 #ifdef IPPROTO_AH
00081     { "IPPROTO_AH", IPPROTO_AH },
00082 #endif
00083 #ifdef IPPROTO_ICMPV6
00084     { "IPPROTO_ICMPV6", IPPROTO_ICMPV6 },
00085 #endif
00086 #ifdef IPPROTO_NONE
00087     { "IPPROTO_NONE", IPPROTO_NONE },
00088 #endif
00089 #ifdef IPPROTO_MTP
00090     { "IPPROTO_MTP", IPPROTO_MTP },
00091 #endif
00092 #ifdef IPPROTO_ENCAP
00093     { "IPPROTO_ENCAP", IPPROTO_ENCAP },
00094 #endif
00095 #ifdef IPPROTO_PIM
00096     { "IPPROTO_PIM", IPPROTO_PIM },
00097 #endif
00098 #ifdef IPPROTO_COMP
00099     { "IPPROTO_COMP", IPPROTO_COMP },
00100 #endif
00101 #ifdef IPPROTO_SCTP
00102     { "IPPROTO_SCTP", IPPROTO_SCTP },
00103 #endif
00104 #ifdef IPPROTO_RAW
00105     { "IPPROTO_RAW", IPPROTO_RAW },
00106 #endif
00107 };
00108 
00109 
00110 void
00111 explain_buffer_socket_protocol(explain_string_buffer_t *sb, int protocol)
00112 {
00113     const explain_parse_bits_table_t *tp;
00114     struct protoent *pep;
00115 
00116     tp = explain_parse_bits_find_by_value(protocol, table, SIZEOF(table));
00117     if (tp)
00118     {
00119         explain_string_buffer_puts(sb, tp->name);
00120         return;
00121     }
00122 
00123     explain_string_buffer_printf(sb, "%d", protocol);
00124 
00125     pep = getprotobynumber(protocol);
00126     if (pep)
00127     {
00128         explain_string_buffer_putc(sb, ' ');
00129         explain_string_buffer_puts_quoted(sb, pep->p_name);
00130     }
00131 }
00132 
00133 
00134 int
00135 explain_parse_socket_protocol_or_die(const char *text, const char *caption)
00136 {
00137     struct protoent *pep;
00138 
00139     pep = getprotobyname(text);
00140     if (pep)
00141         return pep->p_proto;
00142     return explain_parse_bits_or_die(text, table, SIZEOF(table), caption);
00143 }
00144 
00145 
00146 /* vim: set ts=8 sw=4 et : */