/src/mosquitto/lib/strings_mosq.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2010-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include <errno.h> |
22 | | #include <string.h> |
23 | | |
24 | | #ifndef WIN32 |
25 | | # include <strings.h> |
26 | | #endif |
27 | | |
28 | | #include "mosquitto.h" |
29 | | #include "mqtt_protocol.h" |
30 | | |
31 | | const char *mosquitto_strerror(int mosq_errno) |
32 | 0 | { |
33 | 0 | switch(mosq_errno){ |
34 | 0 | case MOSQ_ERR_QUOTA_EXCEEDED: |
35 | 0 | return "Quota exceeded"; |
36 | 0 | case MOSQ_ERR_AUTH_DELAYED: |
37 | 0 | return "Authentication delayed"; |
38 | 0 | case MOSQ_ERR_AUTH_CONTINUE: |
39 | 0 | return "Continue with authentication"; |
40 | 0 | case MOSQ_ERR_NO_SUBSCRIBERS: |
41 | 0 | return "No subscribers"; |
42 | 0 | case MOSQ_ERR_SUB_EXISTS: |
43 | 0 | return "Subscription already exists"; |
44 | 0 | case MOSQ_ERR_CONN_PENDING: |
45 | 0 | return "Connection pending"; |
46 | 0 | case MOSQ_ERR_SUCCESS: |
47 | 0 | return "No error"; |
48 | 0 | case MOSQ_ERR_NOMEM: |
49 | 0 | return "Out of memory"; |
50 | 0 | case MOSQ_ERR_PROTOCOL: |
51 | 0 | return "A network protocol error occurred when communicating with the broker"; |
52 | 0 | case MOSQ_ERR_INVAL: |
53 | 0 | return "Invalid input"; |
54 | 0 | case MOSQ_ERR_NO_CONN: |
55 | 0 | return "The client is not currently connected"; |
56 | 0 | case MOSQ_ERR_CONN_REFUSED: |
57 | 0 | return "The connection was refused"; |
58 | 0 | case MOSQ_ERR_NOT_FOUND: |
59 | 0 | return "Message not found (internal error)"; |
60 | 0 | case MOSQ_ERR_CONN_LOST: |
61 | 0 | return "The connection was lost"; |
62 | 0 | case MOSQ_ERR_TLS: |
63 | 0 | return "A TLS error occurred"; |
64 | 0 | case MOSQ_ERR_PAYLOAD_SIZE: |
65 | 0 | return "Payload too large"; |
66 | 0 | case MOSQ_ERR_NOT_SUPPORTED: |
67 | 0 | return "This feature is not supported"; |
68 | 0 | case MOSQ_ERR_AUTH: |
69 | 0 | return "Authorisation failed"; |
70 | 0 | case MOSQ_ERR_ACL_DENIED: |
71 | 0 | return "Access denied by ACL"; |
72 | 0 | case MOSQ_ERR_UNKNOWN: |
73 | 0 | return "Unknown error"; |
74 | 0 | case MOSQ_ERR_ERRNO: |
75 | 0 | return strerror(errno); |
76 | 0 | case MOSQ_ERR_EAI: |
77 | 0 | return "Lookup error"; |
78 | 0 | case MOSQ_ERR_PROXY: |
79 | 0 | return "Proxy error"; |
80 | 0 | case MOSQ_ERR_PLUGIN_DEFER: |
81 | 0 | return "Plugin deferring result"; |
82 | 0 | case MOSQ_ERR_MALFORMED_UTF8: |
83 | 0 | return "Malformed UTF-8"; |
84 | 0 | case MOSQ_ERR_KEEPALIVE: |
85 | 0 | return "Keepalive exceeded"; |
86 | 0 | case MOSQ_ERR_LOOKUP: |
87 | 0 | return "DNS Lookup failed"; |
88 | 0 | case MOSQ_ERR_MALFORMED_PACKET: |
89 | 0 | return "Malformed packet"; |
90 | 0 | case MOSQ_ERR_DUPLICATE_PROPERTY: |
91 | 0 | return "Duplicate property in property list"; |
92 | 0 | case MOSQ_ERR_TLS_HANDSHAKE: |
93 | 0 | return "TLS handshake failed"; |
94 | 0 | case MOSQ_ERR_QOS_NOT_SUPPORTED: |
95 | 0 | return "Requested QoS not supported on server"; |
96 | 0 | case MOSQ_ERR_OVERSIZE_PACKET: |
97 | 0 | return "Packet larger than supported by the server"; |
98 | 0 | case MOSQ_ERR_OCSP: |
99 | 0 | return "OCSP error"; |
100 | 0 | case MOSQ_ERR_TIMEOUT: |
101 | 0 | return "Timeout"; |
102 | 0 | case MOSQ_ERR_ALREADY_EXISTS: |
103 | 0 | return "Entry already exists"; |
104 | 0 | case MOSQ_ERR_PLUGIN_IGNORE: |
105 | 0 | return "Ignore plugin"; |
106 | | |
107 | 0 | case MOSQ_ERR_UNSPECIFIED: |
108 | 0 | return "Unspecified error"; |
109 | 0 | case MOSQ_ERR_IMPLEMENTATION_SPECIFIC: |
110 | 0 | return "Implementaion specific error"; |
111 | 0 | case MOSQ_ERR_CLIENT_IDENTIFIER_NOT_VALID: |
112 | 0 | return "Client identifier not valid"; |
113 | 0 | case MOSQ_ERR_BAD_USERNAME_OR_PASSWORD: |
114 | 0 | return "Bad username or password"; |
115 | 0 | case MOSQ_ERR_SERVER_UNAVAILABLE: |
116 | 0 | return "Server unavailable"; |
117 | 0 | case MOSQ_ERR_SERVER_BUSY: |
118 | 0 | return "Server busy"; |
119 | 0 | case MOSQ_ERR_BANNED: |
120 | 0 | return "Banned"; |
121 | 0 | case MOSQ_ERR_BAD_AUTHENTICATION_METHOD: |
122 | 0 | return "Bad authentication method"; |
123 | 0 | case MOSQ_ERR_SESSION_TAKEN_OVER: |
124 | 0 | return "Session taken over"; |
125 | 0 | case MOSQ_ERR_RECEIVE_MAXIMUM_EXCEEDED: |
126 | 0 | return "Receive maximum exceeded"; |
127 | 0 | case MOSQ_ERR_TOPIC_ALIAS_INVALID: |
128 | 0 | return "Topic alias invalid"; |
129 | 0 | case MOSQ_ERR_ADMINISTRATIVE_ACTION: |
130 | 0 | return "Administrative action"; |
131 | 0 | case MOSQ_ERR_RETAIN_NOT_SUPPORTED: |
132 | 0 | return "Retain not supported"; |
133 | 0 | case MOSQ_ERR_CONNECTION_RATE_EXCEEDED: |
134 | 0 | return "Connection rate exceeded"; |
135 | 0 | default: |
136 | 0 | return "Unknown error"; |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | | const char *mosquitto_connack_string(int connack_code) |
141 | 0 | { |
142 | 0 | switch(connack_code){ |
143 | 0 | case 0: |
144 | 0 | return "Connection Accepted"; |
145 | 0 | case 1: |
146 | 0 | return "Connection Refused: unacceptable protocol version"; |
147 | 0 | case 2: |
148 | 0 | return "Connection Refused: identifier rejected"; |
149 | 0 | case 3: |
150 | 0 | return "Connection Refused: broker unavailable"; |
151 | 0 | case 4: |
152 | 0 | return "Connection Refused: bad user name or password"; |
153 | 0 | case 5: |
154 | 0 | return "Connection Refused: not authorised"; |
155 | 0 | default: |
156 | 0 | return "Connection Refused: unknown reason"; |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | const char *mosquitto_reason_string(int reason_code) |
161 | 124 | { |
162 | 124 | switch(reason_code){ |
163 | 0 | case MQTT_RC_SUCCESS: |
164 | 0 | return "Success"; |
165 | 6 | case MQTT_RC_GRANTED_QOS1: |
166 | 6 | return "Granted QoS 1"; |
167 | 3 | case MQTT_RC_GRANTED_QOS2: |
168 | 3 | return "Granted QoS 2"; |
169 | 1 | case MQTT_RC_DISCONNECT_WITH_WILL_MSG: |
170 | 1 | return "Disconnect with Will Message"; |
171 | 2 | case MQTT_RC_NO_MATCHING_SUBSCRIBERS: |
172 | 2 | return "No matching subscribers"; |
173 | 1 | case MQTT_RC_NO_SUBSCRIPTION_EXISTED: |
174 | 1 | return "No subscription existed"; |
175 | 2 | case MQTT_RC_CONTINUE_AUTHENTICATION: |
176 | 2 | return "Continue authentication"; |
177 | 2 | case MQTT_RC_REAUTHENTICATE: |
178 | 2 | return "Re-authenticate"; |
179 | | |
180 | 1 | case MQTT_RC_UNSPECIFIED: |
181 | 1 | return "Unspecified error"; |
182 | 6 | case MQTT_RC_MALFORMED_PACKET: |
183 | 6 | return "Malformed Packet"; |
184 | 1 | case MQTT_RC_PROTOCOL_ERROR: |
185 | 1 | return "Protocol Error"; |
186 | 1 | case MQTT_RC_IMPLEMENTATION_SPECIFIC: |
187 | 1 | return "Implementation specific error"; |
188 | 1 | case MQTT_RC_UNSUPPORTED_PROTOCOL_VERSION: |
189 | 1 | return "Unsupported Protocol Version"; |
190 | 1 | case MQTT_RC_CLIENTID_NOT_VALID: |
191 | 1 | return "Client Identifier not valid"; |
192 | 1 | case MQTT_RC_BAD_USERNAME_OR_PASSWORD: |
193 | 1 | return "Bad User Name or Password"; |
194 | 2 | case MQTT_RC_NOT_AUTHORIZED: |
195 | 2 | return "Not authorized"; |
196 | 4 | case MQTT_RC_SERVER_UNAVAILABLE: |
197 | 4 | return "Server unavailable"; |
198 | 2 | case MQTT_RC_SERVER_BUSY: |
199 | 2 | return "Server busy"; |
200 | 1 | case MQTT_RC_BANNED: |
201 | 1 | return "Banned"; |
202 | 2 | case MQTT_RC_SERVER_SHUTTING_DOWN: |
203 | 2 | return "Server shutting down"; |
204 | 2 | case MQTT_RC_BAD_AUTHENTICATION_METHOD: |
205 | 2 | return "Bad authentication method"; |
206 | 1 | case MQTT_RC_KEEP_ALIVE_TIMEOUT: |
207 | 1 | return "Keep Alive timeout"; |
208 | 1 | case MQTT_RC_SESSION_TAKEN_OVER: |
209 | 1 | return "Session taken over"; |
210 | 2 | case MQTT_RC_TOPIC_FILTER_INVALID: |
211 | 2 | return "Topic Filter invalid"; |
212 | 2 | case MQTT_RC_TOPIC_NAME_INVALID: |
213 | 2 | return "Topic Name invalid"; |
214 | 3 | case MQTT_RC_PACKET_ID_IN_USE: |
215 | 3 | return "Packet Identifier in use"; |
216 | 1 | case MQTT_RC_PACKET_ID_NOT_FOUND: |
217 | 1 | return "Packet Identifier not found"; |
218 | 1 | case MQTT_RC_RECEIVE_MAXIMUM_EXCEEDED: |
219 | 1 | return "Receive Maximum exceeded"; |
220 | 1 | case MQTT_RC_TOPIC_ALIAS_INVALID: |
221 | 1 | return "Topic Alias invalid"; |
222 | 1 | case MQTT_RC_PACKET_TOO_LARGE: |
223 | 1 | return "Packet too large"; |
224 | 1 | case MQTT_RC_MESSAGE_RATE_TOO_HIGH: |
225 | 1 | return "Message rate too high"; |
226 | 1 | case MQTT_RC_QUOTA_EXCEEDED: |
227 | 1 | return "Quota exceeded"; |
228 | 1 | case MQTT_RC_ADMINISTRATIVE_ACTION: |
229 | 1 | return "Administrative action"; |
230 | 1 | case MQTT_RC_PAYLOAD_FORMAT_INVALID: |
231 | 1 | return "Payload format invalid"; |
232 | 0 | case MQTT_RC_RETAIN_NOT_SUPPORTED: |
233 | 0 | return "Retain not supported"; |
234 | 0 | case MQTT_RC_QOS_NOT_SUPPORTED: |
235 | 0 | return "QoS not supported"; |
236 | 1 | case MQTT_RC_USE_ANOTHER_SERVER: |
237 | 1 | return "Use another server"; |
238 | 1 | case MQTT_RC_SERVER_MOVED: |
239 | 1 | return "Server moved"; |
240 | 2 | case MQTT_RC_SHARED_SUBS_NOT_SUPPORTED: |
241 | 2 | return "Shared Subscriptions not supported"; |
242 | 2 | case MQTT_RC_CONNECTION_RATE_EXCEEDED: |
243 | 2 | return "Connection rate exceeded"; |
244 | 3 | case MQTT_RC_MAXIMUM_CONNECT_TIME: |
245 | 3 | return "Maximum connect time"; |
246 | 6 | case MQTT_RC_SUBSCRIPTION_IDS_NOT_SUPPORTED: |
247 | 6 | return "Subscription identifiers not supported"; |
248 | 3 | case MQTT_RC_WILDCARD_SUBS_NOT_SUPPORTED: |
249 | 3 | return "Wildcard Subscriptions not supported"; |
250 | 47 | default: |
251 | 47 | return "Unknown reason"; |
252 | 124 | } |
253 | 124 | } |
254 | | |
255 | | |
256 | | int mosquitto_string_to_command(const char *str, int *cmd) |
257 | 0 | { |
258 | 0 | if(!strcasecmp(str, "connect")){ |
259 | 0 | *cmd = CMD_CONNECT; |
260 | 0 | }else if(!strcasecmp(str, "connack")){ |
261 | 0 | *cmd = CMD_CONNACK; |
262 | 0 | }else if(!strcasecmp(str, "publish")){ |
263 | 0 | *cmd = CMD_PUBLISH; |
264 | 0 | }else if(!strcasecmp(str, "puback")){ |
265 | 0 | *cmd = CMD_PUBACK; |
266 | 0 | }else if(!strcasecmp(str, "pubrec")){ |
267 | 0 | *cmd = CMD_PUBREC; |
268 | 0 | }else if(!strcasecmp(str, "pubrel")){ |
269 | 0 | *cmd = CMD_PUBREL; |
270 | 0 | }else if(!strcasecmp(str, "pubcomp")){ |
271 | 0 | *cmd = CMD_PUBCOMP; |
272 | 0 | }else if(!strcasecmp(str, "subscribe")){ |
273 | 0 | *cmd = CMD_SUBSCRIBE; |
274 | 0 | }else if(!strcasecmp(str, "suback")){ |
275 | 0 | *cmd = CMD_SUBACK; |
276 | 0 | }else if(!strcasecmp(str, "unsubscribe")){ |
277 | 0 | *cmd = CMD_UNSUBSCRIBE; |
278 | 0 | }else if(!strcasecmp(str, "unsuback")){ |
279 | 0 | *cmd = CMD_UNSUBACK; |
280 | 0 | }else if(!strcasecmp(str, "disconnect")){ |
281 | 0 | *cmd = CMD_DISCONNECT; |
282 | 0 | }else if(!strcasecmp(str, "auth")){ |
283 | 0 | *cmd = CMD_AUTH; |
284 | 0 | }else if(!strcasecmp(str, "will")){ |
285 | 0 | *cmd = CMD_WILL; |
286 | 0 | }else{ |
287 | 0 | *cmd = 0; |
288 | 0 | return MOSQ_ERR_INVAL; |
289 | 0 | } |
290 | 0 | return MOSQ_ERR_SUCCESS; |
291 | 0 | } |