libexplain  1.4.D001
libexplain/buffer/errno/getsockname.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/enobufs.h>
00025 #include <libexplain/buffer/enotconn.h>
00026 #include <libexplain/buffer/enotsock.h>
00027 #include <libexplain/buffer/errno/generic.h>
00028 #include <libexplain/buffer/errno/getsockname.h>
00029 #include <libexplain/buffer/fildes_to_pathname.h>
00030 #include <libexplain/buffer/pointer.h>
00031 #include <libexplain/buffer/socklen.h>
00032 #include <libexplain/explanation.h>
00033 #include <libexplain/is_efault.h>
00034 
00035 
00036 static void
00037 explain_buffer_errno_getsockname_system_call(explain_string_buffer_t *sb,
00038     int errnum, int fildes, struct sockaddr *sock_addr,
00039     socklen_t *sock_addr_size)
00040 {
00041     (void)errnum;
00042     explain_string_buffer_printf(sb, "getsockname(fildes = %d", fildes);
00043     explain_buffer_fildes_to_pathname(sb, fildes);
00044     explain_string_buffer_puts(sb, ", sock_addr = ");
00045     explain_buffer_pointer(sb, sock_addr);
00046     explain_string_buffer_puts(sb, ", sock_addr_size = ");
00047     explain_buffer_socklen_star(sb, sock_addr_size);
00048     explain_string_buffer_putc(sb, ')');
00049 }
00050 
00051 
00052 static void
00053 explain_buffer_errno_getsockname_explanation(explain_string_buffer_t *sb,
00054     int errnum, int fildes, struct sockaddr *sock_addr,
00055     socklen_t *sock_addr_size)
00056 {
00057     /*
00058      * http://www.opengroup.org/onlinepubs/009695399/functions/getsockname.html
00059      */
00060     (void)sock_addr;
00061     switch (errnum)
00062     {
00063     case EBADF:
00064         explain_buffer_ebadf(sb, fildes, "fildes");
00065         break;
00066 
00067     case EFAULT:
00068         if (explain_is_efault_pointer(sock_addr_size, sizeof(*sock_addr_size)))
00069             explain_buffer_efault(sb, "sock_addr_size");
00070         else
00071             explain_buffer_efault(sb, "sock_addr");
00072         break;
00073 
00074     case EINVAL:
00075         if
00076         (
00077             explain_is_efault_pointer
00078             (
00079                 sock_addr_size,
00080                 sizeof(*sock_addr_size)
00081             )
00082         )
00083             goto dunno;
00084         explain_buffer_einval_too_small
00085         (
00086             sb,
00087             "sock_addr_size",
00088             (int)*sock_addr_size
00089         );
00090         break;
00091 
00092     case ENOBUFS:
00093         explain_buffer_enobufs(sb);
00094         break;
00095 
00096     case ENOTCONN:
00097         explain_buffer_enotconn(sb, "fildes");
00098         break;
00099 
00100     case ENOTSOCK:
00101         explain_buffer_enotsock(sb, fildes, "fildes");
00102         break;
00103 
00104     default:
00105         dunno:
00106         explain_buffer_errno_generic(sb, errnum, "getsockname");
00107         break;
00108     }
00109 }
00110 
00111 
00112 void
00113 explain_buffer_errno_getsockname(explain_string_buffer_t *sb, int errnum,
00114     int fildes, struct sockaddr *sock_addr, socklen_t *sock_addr_size)
00115 {
00116     explain_explanation_t exp;
00117 
00118     explain_explanation_init(&exp, errnum);
00119     explain_buffer_errno_getsockname_system_call
00120     (
00121         &exp.system_call_sb,
00122         errnum,
00123         fildes,
00124         sock_addr,
00125         sock_addr_size
00126     );
00127     explain_buffer_errno_getsockname_explanation
00128     (
00129         &exp.explanation_sb,
00130         errnum,
00131         fildes,
00132         sock_addr,
00133         sock_addr_size
00134     );
00135     explain_explanation_assemble(&exp, sb);
00136 }
00137 
00138 /* vim: set ts=8 sw=4 et : */