/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 | 157k | { |
31 | 157k | int i; |
32 | | |
33 | 157k | if(properties == NULL){ |
34 | 156k | return; |
35 | 156k | } |
36 | | |
37 | 1.28k | printf("\tProperties:\n"); |
38 | | |
39 | 3.09k | while(properties){ |
40 | 1.80k | switch(mosquitto_property_identifier(properties)){ |
41 | | /* Only properties for base messages are valid for saving */ |
42 | 314 | case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR: |
43 | 314 | printf("\t\tPayload format indicator: %d\n", mosquitto_property_byte_value(properties)); |
44 | 314 | break; |
45 | | |
46 | 316 | case MQTT_PROP_CONTENT_TYPE: |
47 | 316 | printf("\t\tContent type: %s\n", mosquitto_property_string_value(properties)); |
48 | 316 | break; |
49 | | |
50 | 295 | case MQTT_PROP_RESPONSE_TOPIC: |
51 | 295 | printf("\t\tResponse topic: %s\n", mosquitto_property_string_value(properties)); |
52 | 295 | break; |
53 | | |
54 | 402 | case MQTT_PROP_CORRELATION_DATA: |
55 | 402 | printf("\t\tCorrelation data: "); |
56 | 402 | const uint8_t *bin = mosquitto_property_binary_value(properties); |
57 | 1.01k | for(i=0; i<mosquitto_property_binary_value_length(properties); i++){ |
58 | 614 | printf("%02X", bin[i]); |
59 | 614 | } |
60 | 402 | printf("\n"); |
61 | 402 | break; |
62 | | |
63 | 195 | case MQTT_PROP_USER_PROPERTY: |
64 | 195 | printf("\t\tUser property: %s , %s\n", mosquitto_property_string_name(properties), mosquitto_property_string_value(properties)); |
65 | 195 | break; |
66 | | |
67 | 286 | default: |
68 | 286 | printf("\t\tInvalid property type: %d\n", mosquitto_property_identifier(properties)); |
69 | 286 | break; |
70 | 1.80k | } |
71 | | |
72 | 1.80k | properties = mosquitto_property_next(properties); |
73 | 1.80k | } |
74 | 1.28k | } |
75 | | |
76 | | |
77 | | void print__client(struct P_client *chunk, uint32_t length) |
78 | 1.90k | { |
79 | 1.90k | printf("DB_CHUNK_CLIENT:\n"); |
80 | 1.90k | printf("\tLength: %d\n", length); |
81 | 1.90k | printf("\tClient ID: %s\n", chunk->clientid); |
82 | 1.90k | if(chunk->username){ |
83 | 466 | printf("\tUsername: %s\n", chunk->username); |
84 | 466 | } |
85 | 1.90k | if(chunk->F.listener_port > 0){ |
86 | 737 | printf("\tListener port: %u\n", chunk->F.listener_port); |
87 | 737 | } |
88 | 1.90k | printf("\tLast MID: %d\n", chunk->F.last_mid); |
89 | 1.90k | printf("\tSession expiry time: %" PRIu64 "\n", chunk->F.session_expiry_time); |
90 | 1.90k | printf("\tSession expiry interval: %u\n", chunk->F.session_expiry_interval); |
91 | 1.90k | } |
92 | | |
93 | | |
94 | | void print__client_msg(struct P_client_msg *chunk, uint32_t length) |
95 | 2.57k | { |
96 | 2.57k | printf("DB_CHUNK_CLIENT_MSG:\n"); |
97 | 2.57k | printf("\tLength: %d\n", length); |
98 | 2.57k | printf("\tClient ID: %s\n", chunk->clientid); |
99 | 2.57k | printf("\tStore ID: %" PRIu64 "\n", chunk->F.store_id); |
100 | 2.57k | printf("\tMID: %d\n", chunk->F.mid); |
101 | 2.57k | printf("\tQoS: %d\n", chunk->F.qos); |
102 | 2.57k | printf("\tRetain: %d\n", (chunk->F.retain_dup&0xF0)>>4); |
103 | 2.57k | printf("\tDirection: %d\n", chunk->F.direction); |
104 | 2.57k | printf("\tState: %d\n", chunk->F.state); |
105 | 2.57k | printf("\tDup: %d\n", chunk->F.retain_dup&0x0F); |
106 | 2.57k | if(chunk->subscription_identifier){ |
107 | 598 | printf("\tSubscription identifier: %d\n", chunk->subscription_identifier); |
108 | 598 | } |
109 | 2.57k | } |
110 | | |
111 | | |
112 | | void print__base_msg(struct P_base_msg *chunk, uint32_t length) |
113 | 157k | { |
114 | 157k | uint8_t *payload; |
115 | | |
116 | 157k | printf("DB_CHUNK_BASE_MSG:\n"); |
117 | 157k | printf("\tLength: %d\n", length); |
118 | 157k | 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 | 157k | printf("\tSource Port: %d\n", chunk->F.source_port); |
122 | 157k | printf("\tSource MID: %d\n", chunk->F.source_mid); |
123 | 157k | printf("\tTopic: %s\n", chunk->topic); |
124 | 157k | printf("\tQoS: %d\n", chunk->F.qos); |
125 | 157k | printf("\tRetain: %d\n", chunk->F.retain); |
126 | 157k | printf("\tPayload Length: %d\n", chunk->F.payloadlen); |
127 | 157k | printf("\tExpiry Time: %" PRIu64 "\n", chunk->F.expiry_time); |
128 | | |
129 | 157k | payload = chunk->payload; |
130 | 157k | if(chunk->F.payloadlen < 256){ |
131 | | /* Print payloads with UTF-8 data below an arbitrary limit of 256 bytes */ |
132 | 156k | if(mosquitto_validate_utf8((char *)payload, (uint16_t)chunk->F.payloadlen) == MOSQ_ERR_SUCCESS){ |
133 | 516 | printf("\tPayload: %s\n", payload); |
134 | 516 | } |
135 | 156k | } |
136 | 157k | print__properties(chunk->properties); |
137 | 157k | } |
138 | | |
139 | | |
140 | | void print__sub(struct P_sub *chunk, uint32_t length) |
141 | 1.38k | { |
142 | 1.38k | printf("DB_CHUNK_SUB:\n"); |
143 | 1.38k | printf("\tLength: %u\n", length); |
144 | 1.38k | printf("\tClient ID: %s\n", chunk->clientid); |
145 | 1.38k | printf("\tTopic: %s\n", chunk->topic); |
146 | 1.38k | printf("\tQoS: %d\n", chunk->F.qos); |
147 | 1.38k | printf("\tSubscription ID: %d\n", chunk->F.identifier); |
148 | 1.38k | printf("\tOptions: 0x%02X\n", chunk->F.options); |
149 | 1.38k | } |
150 | | |
151 | | |