APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Copyright (c) 2010-2018 Roger Light <roger@atchoo.org>
 
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
 
The Eclipse Public License is available at
   http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
  http://www.eclipse.org/org/documents/edl-v10.php.
 
Contributors:
   Roger Light - initial implementation and documentation.
*/
 
#include "config.h"
 
#ifdef WITH_PERSISTENCE
 
#ifndef WIN32
#include <arpa/inet.h>
#endif
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
 
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "persist.h"
#include "packet_mosq.h"
#include "property_mosq.h"
#include "time_mosq.h"
#include "util_mosq.h"
 
int persist__chunk_cfg_write_v5(FILE *db_fptr, struct PF_cfg *chunk)
{
    struct PF_header header;
 
    header.chunk = htonl(DB_CHUNK_CFG);
    header.length = htonl(sizeof(struct PF_cfg));
    write_e(db_fptr, &header, sizeof(struct PF_header));
    write_e(db_fptr, chunk, sizeof(struct PF_cfg));
 
    return MOSQ_ERR_SUCCESS;
error:
    log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
    return 1;
}
 
 
int persist__chunk_client_write_v5(FILE *db_fptr, struct P_client *chunk)
{
    struct PF_header header;
    uint16_t id_len = chunk->F.id_len;
 
    chunk->F.session_expiry_interval = htonl(chunk->F.session_expiry_interval);
    chunk->F.last_mid = htons(chunk->F.last_mid);
    chunk->F.id_len = htons(chunk->F.id_len);
 
    header.chunk = htonl(DB_CHUNK_CLIENT);
    header.length = htonl(sizeof(struct PF_client)+id_len);
 
    write_e(db_fptr, &header, sizeof(struct PF_header));
    write_e(db_fptr, &chunk->F, sizeof(struct PF_client));
 
    write_e(db_fptr, chunk->client_id, id_len);
 
    return MOSQ_ERR_SUCCESS;
error:
    log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
    return 1;
}
 
 
int persist__chunk_client_msg_write_v5(FILE *db_fptr, struct P_client_msg *chunk)
{
    struct PF_header header;
    struct mosquitto__packet prop_packet;
    uint16_t id_len = chunk->F.id_len;
    uint32_t proplen = 0;
    int rc;
 
    memset(&prop_packet, 0, sizeof(struct mosquitto__packet));
    if(chunk->properties){
        proplen = property__get_length_all(chunk->properties);
        proplen += packet__varint_bytes(proplen);
    }
 
    chunk->F.mid = htons(chunk->F.mid);
    chunk->F.id_len = htons(chunk->F.id_len);
 
    header.chunk = htonl(DB_CHUNK_CLIENT_MSG);
    header.length = htonl(sizeof(struct PF_client_msg) + id_len + proplen);
 
    write_e(db_fptr, &header, sizeof(struct PF_header));
    write_e(db_fptr, &chunk->F, sizeof(struct PF_client_msg));
    write_e(db_fptr, chunk->client_id, id_len);
    if(chunk->properties){
        if(proplen > 0){
            prop_packet.remaining_length = proplen;
            prop_packet.packet_length = proplen;
            prop_packet.payload = mosquitto__malloc(proplen);
            if(!prop_packet.payload){
                return MOSQ_ERR_NOMEM;
            }
            rc = property__write_all(&prop_packet, chunk->properties, true);
            if(rc) return rc;
 
            write_e(db_fptr, prop_packet.payload, proplen);
            mosquitto__free(prop_packet.payload);
        }
    }
 
    return MOSQ_ERR_SUCCESS;
error:
    log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
    return 1;
}
 
 
int persist__chunk_message_store_write_v5(FILE *db_fptr, struct P_msg_store *chunk)
{
    struct PF_header header;
    uint32_t payloadlen = chunk->F.payloadlen;
    uint16_t source_id_len = chunk->F.source_id_len;
    uint16_t source_username_len = chunk->F.source_username_len;
    uint16_t topic_len = chunk->F.topic_len;
    uint32_t proplen = 0;
    struct mosquitto__packet prop_packet;
    int rc;
 
    memset(&prop_packet, 0, sizeof(struct mosquitto__packet));
    if(chunk->properties){
        proplen = property__get_length_all(chunk->properties);
        proplen += packet__varint_bytes(proplen);
    }
 
    chunk->F.payloadlen = htonl(chunk->F.payloadlen);
    chunk->F.source_mid = htons(chunk->F.source_mid);
    chunk->F.source_id_len = htons(chunk->F.source_id_len);
    chunk->F.source_username_len = htons(chunk->F.source_username_len);
    chunk->F.topic_len = htons(chunk->F.topic_len);
    chunk->F.source_port = htons(chunk->F.source_port);
 
    header.chunk = htonl(DB_CHUNK_MSG_STORE);
    header.length = htonl(sizeof(struct PF_msg_store) +
            topic_len + payloadlen +
            source_id_len + source_username_len + proplen);
 
    write_e(db_fptr, &header, sizeof(struct PF_header));
    write_e(db_fptr, &chunk->F, sizeof(struct PF_msg_store));
    if(source_id_len){
        write_e(db_fptr, chunk->source.id, source_id_len);
    }
    if(source_username_len){
        write_e(db_fptr, chunk->source.username, source_username_len);
    }
    write_e(db_fptr, chunk->topic, topic_len);
    if(payloadlen){
        write_e(db_fptr, UHPA_ACCESS(chunk->payload, payloadlen), (unsigned int)payloadlen);
    }
    if(chunk->properties){
        if(proplen > 0){
            prop_packet.remaining_length = proplen;
            prop_packet.packet_length = proplen;
            prop_packet.payload = mosquitto__malloc(proplen);
            if(!prop_packet.payload){
                return MOSQ_ERR_NOMEM;
            }
            rc = property__write_all(&prop_packet, chunk->properties, true);
            if(rc) return rc;
 
            write_e(db_fptr, prop_packet.payload, proplen);
            mosquitto__free(prop_packet.payload);
        }
    }
 
    return MOSQ_ERR_SUCCESS;
error:
    log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
    mosquitto__free(prop_packet.payload);
    return 1;
}
 
 
int persist__chunk_retain_write_v5(FILE *db_fptr, struct P_retain *chunk)
{
    struct PF_header header;
 
    header.chunk = htonl(DB_CHUNK_RETAIN);
    header.length = htonl(sizeof(struct PF_retain));
 
    write_e(db_fptr, &header, sizeof(struct PF_header));
    write_e(db_fptr, &chunk->F, sizeof(struct PF_retain));
 
    return MOSQ_ERR_SUCCESS;
error:
    log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
    return 1;
}
 
 
int persist__chunk_sub_write_v5(FILE *db_fptr, struct P_sub *chunk)
{
    struct PF_header header;
    uint16_t id_len = chunk->F.id_len;
    uint16_t topic_len = chunk->F.topic_len;
 
    chunk->F.identifier = htonl(chunk->F.identifier);
    chunk->F.id_len = htons(chunk->F.id_len);
    chunk->F.topic_len = htons(chunk->F.topic_len);
 
    header.chunk = htonl(DB_CHUNK_SUB);
    header.length = htonl(sizeof(struct PF_sub) +
            id_len + topic_len);
 
    write_e(db_fptr, &header, sizeof(struct PF_header));
    write_e(db_fptr, &chunk->F, sizeof(struct PF_sub));
    write_e(db_fptr, chunk->client_id, id_len);
    write_e(db_fptr, chunk->topic, topic_len);
 
    return MOSQ_ERR_SUCCESS;
error:
    log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
    return 1;
}
#endif