print_error.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lukas Mejdrech
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include "print_error.h"
00038 
00039 #include <stdio.h>
00040 #include <errno.h>
00041 
00042 #include <net/icmp_codes.h>
00043 
00051 void icmp_print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
00052 {
00053         if (!output)
00054                 return;
00055         
00056         if (prefix)
00057                 fprintf(output, "%s", prefix);
00058                 
00059         switch (error_code) {
00060         case ICMP_DEST_UNREACH:
00061                 fprintf(output, "ICMP Destination Unreachable (%d) error", error_code);
00062                 break;
00063         case ICMP_SOURCE_QUENCH:
00064                 fprintf(output, "ICMP Source Quench (%d) error", error_code);
00065                 break;
00066         case ICMP_REDIRECT:
00067                 fprintf(output, "ICMP Redirect (%d) error", error_code);
00068                 break;
00069         case ICMP_ALTERNATE_ADDR:
00070                 fprintf(output, "ICMP Alternate Host Address (%d) error", error_code);
00071                 break;
00072         case ICMP_ROUTER_ADV:
00073                 fprintf(output, "ICMP Router Advertisement (%d) error", error_code);
00074                 break;
00075         case ICMP_ROUTER_SOL:
00076                 fprintf(output, "ICMP Router Solicitation (%d) error", error_code);
00077                 break;
00078         case ICMP_TIME_EXCEEDED:
00079                 fprintf(output, "ICMP Time Exceeded (%d) error", error_code);
00080                 break;
00081         case ICMP_PARAMETERPROB:
00082                 fprintf(output, "ICMP Paramenter Problem (%d) error", error_code);
00083                 break;
00084         case ICMP_CONVERSION_ERROR:
00085                 fprintf(output, "ICMP Datagram Conversion Error (%d) error", error_code);
00086                 break;
00087         case ICMP_REDIRECT_MOBILE:
00088                 fprintf(output, "ICMP Mobile Host Redirect (%d) error", error_code);
00089                 break;
00090         case ICMP_SKIP:
00091                 fprintf(output, "ICMP SKIP (%d) error", error_code);
00092                 break;
00093         case ICMP_PHOTURIS:
00094                 fprintf(output, "ICMP Photuris (%d) error", error_code);
00095                 break;
00096         default:
00097                 fprintf(output, "Other (%d) error", error_code);
00098         }
00099 
00100         if (suffix)
00101                 fprintf(output, "%s", suffix);
00102 }
00103 
00113 void print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
00114 {
00115         if (IS_ICMP_ERROR(error_code))
00116                 icmp_print_error(output, error_code, prefix, suffix);
00117         else if(IS_SOCKET_ERROR(error_code))
00118                 socket_print_error(output, error_code, prefix, suffix);
00119 }
00120 
00128 void socket_print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
00129 {
00130         if (!output)
00131                 return;
00132 
00133         if (prefix)
00134                 fprintf(output, "%s", prefix);
00135 
00136         switch (error_code) {
00137         case ENOTSOCK:
00138                 fprintf(output, "Not a socket (%d) error", error_code);
00139                 break;
00140         case EPROTONOSUPPORT:
00141                 fprintf(output, "Protocol not supported (%d) error", error_code);
00142                 break;
00143         case ESOCKTNOSUPPORT:
00144                 fprintf(output, "Socket type not supported (%d) error", error_code);
00145                 break;
00146         case EPFNOSUPPORT:
00147                 fprintf(output, "Protocol family not supported (%d) error", error_code);
00148                 break;
00149         case EAFNOSUPPORT:
00150                 fprintf(output, "Address family not supported (%d) error", error_code);
00151                 break;
00152         case EADDRINUSE:
00153                 fprintf(output, "Address already in use (%d) error", error_code);
00154                 break;
00155         case ENOTCONN:
00156                 fprintf(output, "Socket not connected (%d) error", error_code);
00157                 break;
00158         case NO_DATA:
00159                 fprintf(output, "No data (%d) error", error_code);
00160                 break;
00161         case EINPROGRESS:
00162                 fprintf(output, "Another operation in progress (%d) error", error_code);
00163                 break;
00164         case EDESTADDRREQ:
00165                 fprintf(output, "Destination address required (%d) error", error_code);
00166         case EAGAIN:
00167                 fprintf(output, "Try again (%d) error", error_code);
00168         default:
00169                 fprintf(output, "Other (%d) error", error_code);
00170         }
00171 
00172         if (suffix)
00173                 fprintf(output, "%s", suffix);
00174 }
00175 

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7