libexplain  1.4.D001
libexplain/buffer/errno/getsockopt.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2009, 2011, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License as published by
00007  * the Free Software Foundation; either version 3 of the License, or (at
00008  * 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/errno.h>
00020 
00021 #include <libexplain/buffer/ebadf.h>
00022 #include <libexplain/buffer/efault.h>
00023 #include <libexplain/buffer/einval.h>
00024 #include <libexplain/buffer/enoprotoopt.h>
00025 #include <libexplain/buffer/enotsock.h>
00026 #include <libexplain/buffer/errno/generic.h>
00027 #include <libexplain/buffer/errno/getsockopt.h>
00028 #include <libexplain/buffer/fildes_to_pathname.h>
00029 #include <libexplain/buffer/gettext.h>
00030 #include <libexplain/buffer/pointer.h>
00031 #include <libexplain/buffer/socklen.h>
00032 #include <libexplain/buffer/sockopt_level.h>
00033 #include <libexplain/buffer/sockopt_name.h>
00034 #include <libexplain/explanation.h>
00035 #include <libexplain/is_efault.h>
00036 
00037 
00038 static void
00039 explain_buffer_errno_getsockopt_system_call(explain_string_buffer_t *sb,
00040     int errnum, int fildes, int level, int name, void *data,
00041     socklen_t *data_size)
00042 {
00043     (void)errnum;
00044     explain_string_buffer_printf(sb, "getsockopt(fildes = %d", fildes);
00045     explain_buffer_fildes_to_pathname(sb, fildes);
00046     explain_string_buffer_puts(sb, ", level = ");
00047     explain_buffer_sockopt_level(sb, level);
00048     explain_string_buffer_puts(sb, ", name = ");
00049     explain_buffer_sockopt_name(sb, level, name);
00050     explain_string_buffer_puts(sb, ", data = ");
00051     explain_buffer_pointer(sb, data);
00052     explain_string_buffer_puts(sb, ", data_size = ");
00053     explain_buffer_socklen_star(sb, data_size);
00054     explain_string_buffer_putc(sb, ')');
00055 }
00056 
00057 
00058 static void
00059 explain_buffer_errno_getsockopt_explanation(explain_string_buffer_t *sb,
00060     int errnum, int fildes, int level, int name, void *data,
00061     socklen_t *data_size)
00062 {
00063     /*
00064      * http://www.opengroup.org/onlinepubs/009695399/functions/getsockopt.html
00065      */
00066     (void)level;
00067     (void)name;
00068     switch (errnum)
00069     {
00070     case EBADF:
00071         explain_buffer_ebadf(sb, fildes, "fildes");
00072         break;
00073 
00074     case EFAULT:
00075         if (explain_is_efault_pointer(data_size, sizeof(*data_size)))
00076         {
00077             explain_buffer_efault(sb, "data_size");
00078             break;
00079         }
00080         if (explain_is_efault_pointer(data, *data_size))
00081         {
00082             explain_buffer_efault(sb, "data");
00083             break;
00084         }
00085         goto dunno;
00086 
00087     case EINVAL:
00088         if (explain_is_efault_pointer(data_size, sizeof(*data_size)))
00089             goto dunno;
00090         explain_buffer_einval_too_small(sb, "data_size", *data_size);
00091         break;
00092 
00093     case ENOPROTOOPT:
00094         explain_buffer_enoprotoopt(sb, "name");
00095         break;
00096 
00097     case ENOTSOCK:
00098         explain_buffer_enotsock(sb, fildes, "fildes");
00099         break;
00100 
00101     default:
00102         dunno:
00103         explain_buffer_errno_generic(sb, errnum, "getsockopt");
00104         break;
00105     }
00106 }
00107 
00108 
00109 void
00110 explain_buffer_errno_getsockopt(explain_string_buffer_t *sb, int errnum,
00111     int fildes, int level, int name, void *data, socklen_t *data_size)
00112 {
00113     explain_explanation_t exp;
00114 
00115     explain_explanation_init(&exp, errnum);
00116     explain_buffer_errno_getsockopt_system_call
00117     (
00118         &exp.system_call_sb,
00119         errnum,
00120         fildes,
00121         level,
00122         name,
00123         data,
00124         data_size
00125     );
00126     explain_buffer_errno_getsockopt_explanation
00127     (
00128         &exp.explanation_sb,
00129         errnum,
00130         fildes,
00131         level,
00132         name,
00133         data,
00134         data_size
00135     );
00136     explain_explanation_assemble(&exp, sb);
00137 }
00138 
00139 /* vim: set ts=8 sw=4 et : */