Coverage Report

Created: 2026-05-29 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/apps/db_dump/print.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 <inttypes.h>
20
#include <stdio.h>
21
22
#include "db_dump.h"
23
#include <mosquitto_broker_internal.h>
24
#include <mosquitto/mqtt_protocol.h>
25
#include <persist.h>
26
#include <property_mosq.h>
27
28
29
static void print__properties(mosquitto_property *properties)
30
33.0k
{
31
33.0k
  int i;
32
33
33.0k
  if(properties == NULL){
34
29.7k
    return;
35
29.7k
  }
36
37
3.37k
  printf("\tProperties:\n");
38
39
7.26k
  while(properties){
40
3.89k
    switch(mosquitto_property_identifier(properties)){
41
      /* Only properties for base messages are valid for saving */
42
1.45k
      case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
43
1.45k
        printf("\t\tPayload format indicator: %d\n", mosquitto_property_byte_value(properties));
44
1.45k
        break;
45
46
317
      case MQTT_PROP_CONTENT_TYPE:
47
317
        printf("\t\tContent type: %s\n", mosquitto_property_string_value(properties));
48
317
        break;
49
50
543
      case MQTT_PROP_RESPONSE_TOPIC:
51
543
        printf("\t\tResponse topic: %s\n", mosquitto_property_string_value(properties));
52
543
        break;
53
54
1.04k
      case MQTT_PROP_CORRELATION_DATA:
55
1.04k
        printf("\t\tCorrelation data: ");
56
1.04k
        const uint8_t *bin = mosquitto_property_binary_value(properties);
57
3.20k
        for(i=0; i<mosquitto_property_binary_value_length(properties); i++){
58
2.15k
          printf("%02X", bin[i]);
59
2.15k
        }
60
1.04k
        printf("\n");
61
1.04k
        break;
62
63
67
      case MQTT_PROP_USER_PROPERTY:
64
67
        printf("\t\tUser property: %s , %s\n", mosquitto_property_string_name(properties), mosquitto_property_string_value(properties));
65
67
        break;
66
67
470
      default:
68
470
        printf("\t\tInvalid property type: %d\n", mosquitto_property_identifier(properties));
69
470
        break;
70
3.89k
    }
71
72
3.89k
    properties = mosquitto_property_next(properties);
73
3.89k
  }
74
3.37k
}
75
76
77
void print__client(struct P_client *chunk, uint32_t length)
78
1.93k
{
79
1.93k
  printf("DB_CHUNK_CLIENT:\n");
80
1.93k
  printf("\tLength: %d\n", length);
81
1.93k
  printf("\tClient ID: %s\n", chunk->clientid);
82
1.93k
  if(chunk->username){
83
465
    printf("\tUsername: %s\n", chunk->username);
84
465
  }
85
1.93k
  if(chunk->F.listener_port > 0){
86
832
    printf("\tListener port: %u\n", chunk->F.listener_port);
87
832
  }
88
1.93k
  printf("\tLast MID: %d\n", chunk->F.last_mid);
89
1.93k
  printf("\tSession expiry time: %" PRIu64 "\n", chunk->F.session_expiry_time);
90
1.93k
  printf("\tSession expiry interval: %u\n", chunk->F.session_expiry_interval);
91
1.93k
}
92
93
94
void print__client_msg(struct P_client_msg *chunk, uint32_t length)
95
2.05k
{
96
2.05k
  printf("DB_CHUNK_CLIENT_MSG:\n");
97
2.05k
  printf("\tLength: %d\n", length);
98
2.05k
  printf("\tClient ID: %s\n", chunk->clientid);
99
2.05k
  printf("\tStore ID: %" PRIu64 "\n", chunk->F.store_id);
100
2.05k
  printf("\tMID: %d\n", chunk->F.mid);
101
2.05k
  printf("\tQoS: %d\n", chunk->F.qos);
102
2.05k
  printf("\tRetain: %d\n", (chunk->F.retain_dup&0xF0)>>4);
103
2.05k
  printf("\tDirection: %d\n", chunk->F.direction);
104
2.05k
  printf("\tState: %d\n", chunk->F.state);
105
2.05k
  printf("\tDup: %d\n", chunk->F.retain_dup&0x0F);
106
2.05k
  if(chunk->subscription_identifier){
107
645
    printf("\tSubscription identifier: %d\n", chunk->subscription_identifier);
108
645
  }
109
2.05k
}
110
111
112
void print__base_msg(struct P_base_msg *chunk, uint32_t length)
113
33.0k
{
114
33.0k
  uint8_t *payload;
115
116
33.0k
  printf("DB_CHUNK_BASE_MSG:\n");
117
33.0k
  printf("\tLength: %d\n", length);
118
33.0k
  printf("\tStore ID: %" PRIu64 "\n", chunk->F.store_id);
119
  /* printf("\tSource ID: %s\n", chunk->source_id); */
120
  /* printf("\tSource Username: %s\n", chunk->source_username); */
121
33.0k
  printf("\tSource Port: %d\n", chunk->F.source_port);
122
33.0k
  printf("\tSource MID: %d\n", chunk->F.source_mid);
123
33.0k
  printf("\tTopic: %s\n", chunk->topic);
124
33.0k
  printf("\tQoS: %d\n", chunk->F.qos);
125
33.0k
  printf("\tRetain: %d\n", chunk->F.retain);
126
33.0k
  printf("\tPayload Length: %d\n", chunk->F.payloadlen);
127
33.0k
  printf("\tExpiry Time: %" PRIu64 "\n", chunk->F.expiry_time);
128
129
33.0k
  payload = chunk->payload;
130
33.0k
  if(chunk->F.payloadlen < 256){
131
    /* Print payloads with UTF-8 data below an arbitrary limit of 256 bytes */
132
17.3k
    if(mosquitto_validate_utf8((char *)payload, (uint16_t)chunk->F.payloadlen) == MOSQ_ERR_SUCCESS){
133
1.14k
      printf("\tPayload: %s\n", payload);
134
1.14k
    }
135
17.3k
  }
136
33.0k
  print__properties(chunk->properties);
137
33.0k
}
138
139
140
void print__sub(struct P_sub *chunk, uint32_t length)
141
1.49k
{
142
1.49k
  printf("DB_CHUNK_SUB:\n");
143
1.49k
  printf("\tLength: %u\n", length);
144
1.49k
  printf("\tClient ID: %s\n", chunk->clientid);
145
1.49k
  printf("\tTopic: %s\n", chunk->topic);
146
1.49k
  printf("\tQoS: %d\n", chunk->F.qos);
147
1.49k
  printf("\tSubscription ID: %d\n", chunk->F.identifier);
148
1.49k
  printf("\tOptions: 0x%02X\n", chunk->F.options);
149
1.49k
}
150
151