libexplain  1.4.D001
libexplain/buffer/socket_type.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 
00022 #include <libexplain/parse_bits.h>
00023 #include <libexplain/buffer/socket_type.h>
00024 #include <libexplain/sizeof.h>
00025 #include <libexplain/string_buffer.h>
00026 
00027 
00028 static const explain_parse_bits_table_t table[] =
00029 {
00030 #ifdef SOCK_STREAM
00031     { "SOCK_STREAM", SOCK_STREAM },
00032 #endif
00033 #ifdef SOCK_DGRAM
00034     { "SOCK_DGRAM", SOCK_DGRAM },
00035 #endif
00036 #ifdef SOCK_RAW
00037     { "SOCK_RAW", SOCK_RAW },
00038 #endif
00039 #ifdef SOCK_RDM
00040     { "SOCK_RDM", SOCK_RDM },
00041 #endif
00042 #ifdef SOCK_SEQPACKET
00043     { "SOCK_SEQPACKET", SOCK_SEQPACKET },
00044 #endif
00045 #ifdef SOCK_PACKET
00046     { "SOCK_PACKET", SOCK_PACKET },
00047 #endif
00048 };
00049 
00050 
00051 void
00052 explain_buffer_socket_type(explain_string_buffer_t *sb, int type)
00053 {
00054     const explain_parse_bits_table_t *tp;
00055 
00056     tp = explain_parse_bits_find_by_value(type, table, SIZEOF(table));
00057     if (tp)
00058         explain_string_buffer_puts(sb, tp->name);
00059     else
00060         explain_string_buffer_printf(sb, "%d", type);
00061 }
00062 
00063 
00064 int
00065 explain_parse_socket_type_or_die(const char *text, const char *caption)
00066 {
00067     return explain_parse_bits_or_die(text, table, SIZEOF(table), caption);
00068 }
00069 
00070 
00071 void
00072 explain_buffer_socket_type_from_fildes(explain_string_buffer_t *sb,
00073     int fildes)
00074 {
00075     int             val;
00076     socklen_t       valsiz;
00077 
00078     valsiz = sizeof(val);
00079     if (getsockopt(fildes, SOL_SOCKET, SO_TYPE, &val, &valsiz) >= 0)
00080     {
00081         explain_string_buffer_puts(sb, " (");
00082         explain_buffer_socket_type(sb, val);
00083         explain_string_buffer_putc(sb, ')');
00084     }
00085 }
00086 
00087 
00088 /* vim: set ts=8 sw=4 et : */