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