-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
pub_shared.c
136 lines (117 loc) · 2.97 KB
/
pub_shared.c
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
/*
Copyright (c) 2009-2020 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
https://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <time.h>
#else
#include <process.h>
#include <winsock2.h>
#define snprintf sprintf_s
#endif
#include <mosquitto.h>
#include <mqtt_protocol.h>
#include "client_shared.h"
#include "pub_shared.h"
/* Global variables for use in callbacks. See sub_client.c for an example of
* using a struct to hold variables for use in callbacks. */
int mid_sent = -1;
struct mosq_config cfg;
void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str)
{
UNUSED(mosq);
UNUSED(obj);
UNUSED(level);
printf("%s\n", str);
}
int load_stdin(void)
{
size_t pos = 0, rlen;
char buf[1024];
char *aux_message = NULL;
cfg.pub_mode = MSGMODE_STDIN_FILE;
while(!feof(stdin)){
rlen = fread(buf, 1, 1024, stdin);
aux_message = realloc(cfg.message, pos+rlen);
if(!aux_message){
err_printf(&cfg, "Error: Out of memory.\n");
free(cfg.message);
return 1;
} else
{
cfg.message = aux_message;
}
memcpy(&(cfg.message[pos]), buf, rlen);
pos += rlen;
}
if(pos > MQTT_MAX_PAYLOAD){
err_printf(&cfg, "Error: Message length must be less than %u bytes.\n\n", MQTT_MAX_PAYLOAD);
free(cfg.message);
return 1;
}
cfg.msglen = (int )pos;
if(!cfg.msglen){
err_printf(&cfg, "Error: Zero length input.\n");
return 1;
}
return 0;
}
int load_file(const char *filename)
{
size_t pos, rlen;
FILE *fptr = NULL;
long flen;
fptr = fopen(filename, "rb");
if(!fptr){
err_printf(&cfg, "Error: Unable to open file \"%s\".\n", filename);
return 1;
}
cfg.pub_mode = MSGMODE_FILE;
fseek(fptr, 0, SEEK_END);
flen = ftell(fptr);
if(flen > MQTT_MAX_PAYLOAD){
fclose(fptr);
err_printf(&cfg, "Error: File must be less than %u bytes.\n\n", MQTT_MAX_PAYLOAD);
free(cfg.message);
return 1;
}else if(flen == 0){
fclose(fptr);
cfg.message = NULL;
cfg.msglen = 0;
return 0;
}else if(flen < 0){
fclose(fptr);
err_printf(&cfg, "Error: Unable to determine size of file \"%s\".\n", filename);
return 1;
}
cfg.msglen = (int )flen;
fseek(fptr, 0, SEEK_SET);
cfg.message = malloc((size_t )cfg.msglen);
if(!cfg.message){
fclose(fptr);
err_printf(&cfg, "Error: Out of memory.\n");
return 1;
}
pos = 0;
while(pos < cfg.msglen){
rlen = fread(&(cfg.message[pos]), sizeof(char), (size_t )cfg.msglen-pos, fptr);
pos += rlen;
}
fclose(fptr);
return 0;
}