Coverage Report

Created: 2026-08-25 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/apps/db_dump/json.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 <assert.h>
20
#include <errno.h>
21
#include <fcntl.h>
22
#include <inttypes.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <sys/stat.h>
27
#include <time.h>
28
#include <cjson/cJSON.h>
29
30
#include "db_dump.h"
31
#include "json_help.h"
32
#include <mosquitto_broker_internal.h>
33
#include <persist.h>
34
35
#define mosquitto_malloc(A) malloc((A))
36
0
#define mosquitto_free(A) free((A))
37
#define _mosquitto_malloc(A) malloc((A))
38
#define _mosquitto_free(A) free((A))
39
#include <uthash.h>
40
41
#include "db_dump.h"
42
43
cJSON *j_tree = NULL;
44
cJSON *j_base_msgs = NULL;
45
cJSON *j_clients = NULL;
46
cJSON *j_client_msgs = NULL;
47
cJSON *j_retained_msgs = NULL;
48
cJSON *j_subscriptions = NULL;
49
50
51
void json_init(void)
52
0
{
53
0
  j_tree = cJSON_CreateObject();
54
0
  if(!j_tree){
55
0
    fprintf(stderr, "Error: Out of memory.\n");
56
0
    exit(1);
57
0
  }
58
59
0
  if((j_base_msgs = cJSON_AddArrayToObject(j_tree, "base-messages")) == NULL
60
0
      || (j_clients = cJSON_AddArrayToObject(j_tree, "clients")) == NULL
61
0
      || (j_client_msgs = cJSON_AddArrayToObject(j_tree, "client-messages")) == NULL
62
0
      || (j_retained_msgs = cJSON_AddArrayToObject(j_tree, "retained-messages")) == NULL
63
0
      || (j_subscriptions = cJSON_AddArrayToObject(j_tree, "subscriptions")) == NULL
64
0
      ){
65
66
0
    fprintf(stderr, "Error: Out of memory.\n");
67
0
    exit(1);
68
0
  }
69
0
}
70
71
72
void json_print(void)
73
0
{
74
0
  char *jstr = cJSON_Print(j_tree);
75
0
  printf("%s\n", jstr);
76
0
  free(jstr);
77
0
}
78
79
80
void json_cleanup(void)
81
0
{
82
0
  cJSON_Delete(j_tree);
83
0
}
84
85
86
void json_add_base_msg(struct P_base_msg *chunk)
87
0
{
88
0
  cJSON *j_base_msg = NULL;
89
90
0
  j_base_msg = cJSON_CreateObject();
91
0
  cJSON_AddItemToArray(j_base_msgs, j_base_msg);
92
93
0
  cJSON_AddUIntToObject(j_base_msg, "storeid", chunk->F.store_id);
94
0
  cJSON_AddIntToObject(j_base_msg, "expiry-time", chunk->F.expiry_time);
95
0
  cJSON_AddNumberToObject(j_base_msg, "source-mid", chunk->F.source_mid);
96
0
  cJSON_AddNumberToObject(j_base_msg, "source-port", chunk->F.source_port);
97
0
  cJSON_AddNumberToObject(j_base_msg, "qos", chunk->F.qos);
98
0
  cJSON_AddNumberToObject(j_base_msg, "retain", chunk->F.retain);
99
0
  cJSON_AddStringToObject(j_base_msg, "topic", chunk->topic);
100
0
  if(chunk->source.id){
101
0
    cJSON_AddStringToObject(j_base_msg, "clientid", chunk->source.id);
102
0
  }
103
0
  if(chunk->source.username){
104
0
    cJSON_AddStringToObject(j_base_msg, "username", chunk->source.username);
105
0
  }
106
0
  if(chunk->F.payloadlen > 0){
107
0
    char *payload;
108
0
    if(mosquitto_base64_encode(chunk->payload, chunk->F.payloadlen, &payload) == MOSQ_ERR_SUCCESS){
109
0
      cJSON_AddStringToObject(j_base_msg, "payload", payload);
110
0
      mosquitto_free(payload);
111
0
    }
112
0
  }
113
0
  if(chunk->properties){
114
0
    cJSON *j_props = mosquitto_properties_to_json(chunk->properties);
115
0
    if(j_props){
116
0
      cJSON_AddItemToObject(j_base_msg, "properties", j_props);
117
0
    }
118
0
  }
119
0
}
120
121
122
void json_add_client(struct P_client *chunk)
123
0
{
124
0
  cJSON *j_client;
125
126
0
  j_client = cJSON_CreateObject();
127
0
  cJSON_AddItemToArray(j_clients, j_client);
128
129
0
  cJSON_AddStringToObject(j_client, "clientid", chunk->clientid);
130
0
  if(chunk->username){
131
0
    cJSON_AddStringToObject(j_client, "username", chunk->username);
132
0
  }
133
0
  cJSON_AddIntToObject(j_client, "session-expiry-time", chunk->F.session_expiry_time);
134
0
  cJSON_AddNumberToObject(j_client, "session-expiry-interval", chunk->F.session_expiry_interval);
135
0
  cJSON_AddNumberToObject(j_client, "last-mid", chunk->F.last_mid);
136
0
  cJSON_AddNumberToObject(j_client, "listener-port", chunk->F.listener_port);
137
138
0
}
139
140
141
void json_add_client_msg(struct P_client_msg *chunk)
142
0
{
143
0
  cJSON *j_client_msg;
144
145
0
  j_client_msg = cJSON_CreateObject();
146
0
  cJSON_AddItemToArray(j_client_msgs, j_client_msg);
147
148
0
  cJSON_AddStringToObject(j_client_msg, "clientid", chunk->clientid);
149
0
  cJSON_AddNumberToObject(j_client_msg, "storeid", chunk->subscription_identifier);
150
0
  cJSON_AddNumberToObject(j_client_msg, "mid", chunk->F.mid);
151
0
  cJSON_AddNumberToObject(j_client_msg, "qos", chunk->F.qos);
152
0
  cJSON_AddNumberToObject(j_client_msg, "state", chunk->F.state);
153
0
  cJSON_AddNumberToObject(j_client_msg, "retain-dup", chunk->F.retain_dup);
154
0
  cJSON_AddNumberToObject(j_client_msg, "direction", chunk->F.direction);
155
0
  cJSON_AddNumberToObject(j_client_msg, "subscription-identifier", chunk->subscription_identifier);
156
0
}
157
158
159
void json_add_subscription(struct P_sub *chunk)
160
0
{
161
0
  cJSON *j_subscription;
162
163
0
  j_subscription = cJSON_CreateObject();
164
0
  cJSON_AddItemToArray(j_subscriptions, j_subscription);
165
166
0
  cJSON_AddStringToObject(j_subscription, "clientid", chunk->clientid);
167
0
  cJSON_AddStringToObject(j_subscription, "topic", chunk->topic);
168
0
  cJSON_AddNumberToObject(j_subscription, "qos", chunk->F.qos);
169
0
  cJSON_AddNumberToObject(j_subscription, "options", chunk->F.options);
170
0
  cJSON_AddNumberToObject(j_subscription, "identifier", chunk->F.identifier);
171
0
}
172
173
174
void json_add_retained_msg(struct P_retain *chunk)
175
0
{
176
0
  cJSON *j_retained_msg;
177
178
0
  j_retained_msg = cJSON_CreateObject();
179
0
  cJSON_AddItemToArray(j_retained_msgs, j_retained_msg);
180
0
  cJSON_AddUIntToObject(j_retained_msg, "storeid", chunk->F.store_id);
181
0
}