1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup net_app
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Generic application error printing functions implementation.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include "print_error.h"
|
---|
38 |
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <errno.h>
|
---|
41 |
|
---|
42 | #include <net/icmp_codes.h>
|
---|
43 |
|
---|
44 | /** Prints the specific ICMP error description.
|
---|
45 | *
|
---|
46 | * @param[in] output The description output stream. May be NULL.
|
---|
47 | * @param[in] error_code The ICMP error code.
|
---|
48 | * @param[in] prefix The error description prefix. May be NULL.
|
---|
49 | * @param[in] suffix The error description suffix. May be NULL.
|
---|
50 | */
|
---|
51 | void icmp_print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
|
---|
52 | {
|
---|
53 | if (!output)
|
---|
54 | return;
|
---|
55 |
|
---|
56 | if (prefix)
|
---|
57 | fprintf(output, "%s", prefix);
|
---|
58 |
|
---|
59 | switch (error_code) {
|
---|
60 | case ICMP_DEST_UNREACH:
|
---|
61 | fprintf(output, "ICMP Destination Unreachable (%d) error", error_code);
|
---|
62 | break;
|
---|
63 | case ICMP_SOURCE_QUENCH:
|
---|
64 | fprintf(output, "ICMP Source Quench (%d) error", error_code);
|
---|
65 | break;
|
---|
66 | case ICMP_REDIRECT:
|
---|
67 | fprintf(output, "ICMP Redirect (%d) error", error_code);
|
---|
68 | break;
|
---|
69 | case ICMP_ALTERNATE_ADDR:
|
---|
70 | fprintf(output, "ICMP Alternate Host Address (%d) error", error_code);
|
---|
71 | break;
|
---|
72 | case ICMP_ROUTER_ADV:
|
---|
73 | fprintf(output, "ICMP Router Advertisement (%d) error", error_code);
|
---|
74 | break;
|
---|
75 | case ICMP_ROUTER_SOL:
|
---|
76 | fprintf(output, "ICMP Router Solicitation (%d) error", error_code);
|
---|
77 | break;
|
---|
78 | case ICMP_TIME_EXCEEDED:
|
---|
79 | fprintf(output, "ICMP Time Exceeded (%d) error", error_code);
|
---|
80 | break;
|
---|
81 | case ICMP_PARAMETERPROB:
|
---|
82 | fprintf(output, "ICMP Paramenter Problem (%d) error", error_code);
|
---|
83 | break;
|
---|
84 | case ICMP_CONVERSION_ERROR:
|
---|
85 | fprintf(output, "ICMP Datagram Conversion Error (%d) error", error_code);
|
---|
86 | break;
|
---|
87 | case ICMP_REDIRECT_MOBILE:
|
---|
88 | fprintf(output, "ICMP Mobile Host Redirect (%d) error", error_code);
|
---|
89 | break;
|
---|
90 | case ICMP_SKIP:
|
---|
91 | fprintf(output, "ICMP SKIP (%d) error", error_code);
|
---|
92 | break;
|
---|
93 | case ICMP_PHOTURIS:
|
---|
94 | fprintf(output, "ICMP Photuris (%d) error", error_code);
|
---|
95 | break;
|
---|
96 | default:
|
---|
97 | fprintf(output, "Other (%d) error", error_code);
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (suffix)
|
---|
101 | fprintf(output, "%s", suffix);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Prints the error description.
|
---|
105 | *
|
---|
106 | * Supports ICMP and socket error codes.
|
---|
107 | *
|
---|
108 | * @param[in] output The description output stream. May be NULL.
|
---|
109 | * @param[in] error_code The error code.
|
---|
110 | * @param[in] prefix The error description prefix. May be NULL.
|
---|
111 | * @param[in] suffix The error description suffix. May be NULL.
|
---|
112 | */
|
---|
113 | void print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
|
---|
114 | {
|
---|
115 | if (IS_ICMP_ERROR(error_code))
|
---|
116 | icmp_print_error(output, error_code, prefix, suffix);
|
---|
117 | else if(IS_SOCKET_ERROR(error_code))
|
---|
118 | socket_print_error(output, error_code, prefix, suffix);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Prints the specific socket error description.
|
---|
122 | *
|
---|
123 | * @param[in] output The description output stream. May be NULL.
|
---|
124 | * @param[in] error_code The socket error code.
|
---|
125 | * @param[in] prefix The error description prefix. May be NULL.
|
---|
126 | * @param[in] suffix The error description suffix. May be NULL.
|
---|
127 | */
|
---|
128 | void socket_print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
|
---|
129 | {
|
---|
130 | if (!output)
|
---|
131 | return;
|
---|
132 |
|
---|
133 | if (prefix)
|
---|
134 | fprintf(output, "%s", prefix);
|
---|
135 |
|
---|
136 | switch (error_code) {
|
---|
137 | case ENOTSOCK:
|
---|
138 | fprintf(output, "Not a socket (%d) error", error_code);
|
---|
139 | break;
|
---|
140 | case EPROTONOSUPPORT:
|
---|
141 | fprintf(output, "Protocol not supported (%d) error", error_code);
|
---|
142 | break;
|
---|
143 | case ESOCKTNOSUPPORT:
|
---|
144 | fprintf(output, "Socket type not supported (%d) error", error_code);
|
---|
145 | break;
|
---|
146 | case EPFNOSUPPORT:
|
---|
147 | fprintf(output, "Protocol family not supported (%d) error", error_code);
|
---|
148 | break;
|
---|
149 | case EAFNOSUPPORT:
|
---|
150 | fprintf(output, "Address family not supported (%d) error", error_code);
|
---|
151 | break;
|
---|
152 | case EADDRINUSE:
|
---|
153 | fprintf(output, "Address already in use (%d) error", error_code);
|
---|
154 | break;
|
---|
155 | case ENOTCONN:
|
---|
156 | fprintf(output, "Socket not connected (%d) error", error_code);
|
---|
157 | break;
|
---|
158 | case NO_DATA:
|
---|
159 | fprintf(output, "No data (%d) error", error_code);
|
---|
160 | break;
|
---|
161 | case EINPROGRESS:
|
---|
162 | fprintf(output, "Another operation in progress (%d) error", error_code);
|
---|
163 | break;
|
---|
164 | case EDESTADDRREQ:
|
---|
165 | fprintf(output, "Destination address required (%d) error", error_code);
|
---|
166 | case TRY_AGAIN:
|
---|
167 | fprintf(output, "Try again (%d) error", error_code);
|
---|
168 | default:
|
---|
169 | fprintf(output, "Other (%d) error", error_code);
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (suffix)
|
---|
173 | fprintf(output, "%s", suffix);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** @}
|
---|
177 | */
|
---|
178 |
|
---|