Coverage Report

Created: 2025-11-14 07:11

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