Coverage Report

Created: 2025-08-29 06:24

/src/mosquitto/libcommon/strings_common.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 "mosquitto/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
0
    case MOSQ_ERR_HTTP_BAD_ORIGIN:
107
0
      return "Bad http origin";
108
109
0
    case MOSQ_ERR_UNSPECIFIED:
110
0
      return "Unspecified error";
111
0
    case MOSQ_ERR_IMPLEMENTATION_SPECIFIC:
112
0
      return "Implementaion specific error";
113
0
    case MOSQ_ERR_CLIENT_IDENTIFIER_NOT_VALID:
114
0
      return "Client identifier not valid";
115
0
    case MOSQ_ERR_BAD_USERNAME_OR_PASSWORD:
116
0
      return "Bad username or password";
117
0
    case MOSQ_ERR_SERVER_UNAVAILABLE:
118
0
      return "Server unavailable";
119
0
    case MOSQ_ERR_SERVER_BUSY:
120
0
      return "Server busy";
121
0
    case MOSQ_ERR_BANNED:
122
0
      return "Banned";
123
0
    case MOSQ_ERR_BAD_AUTHENTICATION_METHOD:
124
0
      return "Bad authentication method";
125
0
    case MOSQ_ERR_SESSION_TAKEN_OVER:
126
0
      return "Session taken over";
127
0
    case MOSQ_ERR_RECEIVE_MAXIMUM_EXCEEDED:
128
0
      return "Receive maximum exceeded";
129
0
    case MOSQ_ERR_TOPIC_ALIAS_INVALID:
130
0
      return "Topic alias invalid";
131
0
    case MOSQ_ERR_ADMINISTRATIVE_ACTION:
132
0
      return "Administrative action";
133
0
    case MOSQ_ERR_RETAIN_NOT_SUPPORTED:
134
0
      return "Retain not supported";
135
0
    case MOSQ_ERR_CONNECTION_RATE_EXCEEDED:
136
0
      return "Connection rate exceeded";
137
0
    default:
138
0
      if(mosq_errno >= 128) {
139
        // If mosq_errno is greater than 127,
140
        // a mqtt5_return_code error was used
141
0
        return mosquitto_reason_string(mosq_errno);
142
0
      } else {
143
0
        return "Unknown error";
144
0
      }
145
0
  }
146
0
}
147
148
const char *mosquitto_connack_string(int connack_code)
149
0
{
150
0
  switch(connack_code){
151
0
    case 0:
152
0
      return "Connection Accepted";
153
0
    case 1:
154
0
      return "Connection Refused: unacceptable protocol version";
155
0
    case 2:
156
0
      return "Connection Refused: identifier rejected";
157
0
    case 3:
158
0
      return "Connection Refused: broker unavailable";
159
0
    case 4:
160
0
      return "Connection Refused: bad user name or password";
161
0
    case 5:
162
0
      return "Connection Refused: not authorised";
163
0
    default:
164
0
      return "Connection Refused: unknown reason";
165
0
  }
166
0
}
167
168
const char *mosquitto_reason_string(int reason_code)
169
101
{
170
101
  switch(reason_code){
171
0
    case MQTT_RC_SUCCESS:
172
0
      return "Success";
173
2
    case MQTT_RC_GRANTED_QOS1:
174
2
      return "Granted QoS 1";
175
2
    case MQTT_RC_GRANTED_QOS2:
176
2
      return "Granted QoS 2";
177
2
    case MQTT_RC_DISCONNECT_WITH_WILL_MSG:
178
2
      return "Disconnect with Will Message";
179
2
    case MQTT_RC_NO_MATCHING_SUBSCRIBERS:
180
2
      return "No matching subscribers";
181
5
    case MQTT_RC_NO_SUBSCRIPTION_EXISTED:
182
5
      return "No subscription existed";
183
1
    case MQTT_RC_CONTINUE_AUTHENTICATION:
184
1
      return "Continue authentication";
185
2
    case MQTT_RC_REAUTHENTICATE:
186
2
      return "Re-authenticate";
187
188
2
    case MQTT_RC_UNSPECIFIED:
189
2
      return "Unspecified error";
190
2
    case MQTT_RC_MALFORMED_PACKET:
191
2
      return "Malformed Packet";
192
1
    case MQTT_RC_PROTOCOL_ERROR:
193
1
      return "Protocol Error";
194
3
    case MQTT_RC_IMPLEMENTATION_SPECIFIC:
195
3
      return "Implementation specific error";
196
1
    case MQTT_RC_UNSUPPORTED_PROTOCOL_VERSION:
197
1
      return "Unsupported Protocol Version";
198
1
    case MQTT_RC_CLIENTID_NOT_VALID:
199
1
      return "Client Identifier not valid";
200
1
    case MQTT_RC_BAD_USERNAME_OR_PASSWORD:
201
1
      return "Bad User Name or Password";
202
2
    case MQTT_RC_NOT_AUTHORIZED:
203
2
      return "Not authorized";
204
1
    case MQTT_RC_SERVER_UNAVAILABLE:
205
1
      return "Server unavailable";
206
3
    case MQTT_RC_SERVER_BUSY:
207
3
      return "Server busy";
208
3
    case MQTT_RC_BANNED:
209
3
      return "Banned";
210
2
    case MQTT_RC_SERVER_SHUTTING_DOWN:
211
2
      return "Server shutting down";
212
1
    case MQTT_RC_BAD_AUTHENTICATION_METHOD:
213
1
      return "Bad authentication method";
214
5
    case MQTT_RC_KEEP_ALIVE_TIMEOUT:
215
5
      return "Keep Alive timeout";
216
3
    case MQTT_RC_SESSION_TAKEN_OVER:
217
3
      return "Session taken over";
218
1
    case MQTT_RC_TOPIC_FILTER_INVALID:
219
1
      return "Topic Filter invalid";
220
1
    case MQTT_RC_TOPIC_NAME_INVALID:
221
1
      return "Topic Name invalid";
222
1
    case MQTT_RC_PACKET_ID_IN_USE:
223
1
      return "Packet Identifier in use";
224
2
    case MQTT_RC_PACKET_ID_NOT_FOUND:
225
2
      return "Packet Identifier not found";
226
3
    case MQTT_RC_RECEIVE_MAXIMUM_EXCEEDED:
227
3
      return "Receive Maximum exceeded";
228
2
    case MQTT_RC_TOPIC_ALIAS_INVALID:
229
2
      return "Topic Alias invalid";
230
2
    case MQTT_RC_PACKET_TOO_LARGE:
231
2
      return "Packet too large";
232
2
    case MQTT_RC_MESSAGE_RATE_TOO_HIGH:
233
2
      return "Message rate too high";
234
1
    case MQTT_RC_QUOTA_EXCEEDED:
235
1
      return "Quota exceeded";
236
1
    case MQTT_RC_ADMINISTRATIVE_ACTION:
237
1
      return "Administrative action";
238
1
    case MQTT_RC_PAYLOAD_FORMAT_INVALID:
239
1
      return "Payload format invalid";
240
0
    case MQTT_RC_RETAIN_NOT_SUPPORTED:
241
0
      return "Retain not supported";
242
0
    case MQTT_RC_QOS_NOT_SUPPORTED:
243
0
      return "QoS not supported";
244
1
    case MQTT_RC_USE_ANOTHER_SERVER:
245
1
      return "Use another server";
246
1
    case MQTT_RC_SERVER_MOVED:
247
1
      return "Server moved";
248
2
    case MQTT_RC_SHARED_SUBS_NOT_SUPPORTED:
249
2
      return "Shared Subscriptions not supported";
250
2
    case MQTT_RC_CONNECTION_RATE_EXCEEDED:
251
2
      return "Connection rate exceeded";
252
3
    case MQTT_RC_MAXIMUM_CONNECT_TIME:
253
3
      return "Maximum connect time";
254
2
    case MQTT_RC_SUBSCRIPTION_IDS_NOT_SUPPORTED:
255
2
      return "Subscription identifiers not supported";
256
2
    case MQTT_RC_WILDCARD_SUBS_NOT_SUPPORTED:
257
2
      return "Wildcard Subscriptions not supported";
258
24
    default:
259
24
      return "Unknown reason";
260
101
  }
261
101
}
262
263
264
int mosquitto_string_to_command(const char *str, int *cmd)
265
0
{
266
0
  if(!strcasecmp(str, "connect")){
267
0
    *cmd = CMD_CONNECT;
268
0
  }else if(!strcasecmp(str, "connack")){
269
0
    *cmd = CMD_CONNACK;
270
0
  }else if(!strcasecmp(str, "publish")){
271
0
    *cmd = CMD_PUBLISH;
272
0
  }else if(!strcasecmp(str, "puback")){
273
0
    *cmd = CMD_PUBACK;
274
0
  }else if(!strcasecmp(str, "pubrec")){
275
0
    *cmd = CMD_PUBREC;
276
0
  }else if(!strcasecmp(str, "pubrel")){
277
0
    *cmd = CMD_PUBREL;
278
0
  }else if(!strcasecmp(str, "pubcomp")){
279
0
    *cmd = CMD_PUBCOMP;
280
0
  }else if(!strcasecmp(str, "subscribe")){
281
0
    *cmd = CMD_SUBSCRIBE;
282
0
  }else if(!strcasecmp(str, "suback")){
283
0
    *cmd = CMD_SUBACK;
284
0
  }else if(!strcasecmp(str, "unsubscribe")){
285
0
    *cmd = CMD_UNSUBSCRIBE;
286
0
  }else if(!strcasecmp(str, "unsuback")){
287
0
    *cmd = CMD_UNSUBACK;
288
0
  }else if(!strcasecmp(str, "disconnect")){
289
0
    *cmd = CMD_DISCONNECT;
290
0
  }else if(!strcasecmp(str, "auth")){
291
0
    *cmd = CMD_AUTH;
292
0
  }else if(!strcasecmp(str, "will")){
293
0
    *cmd = CMD_WILL;
294
0
  }else{
295
0
    *cmd = 0;
296
0
    return MOSQ_ERR_INVAL;
297
0
  }
298
0
  return MOSQ_ERR_SUCCESS;
299
0
}