-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg.c
301 lines (240 loc) · 6.07 KB
/
msg.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <sys/param.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "syslogd.h"
SLIST_HEAD(msg_queue_list_head, msg_queue);
/** SLIST HEAD of all msg lists created per every SG group */
static struct msg_queue_list_head msg_list_head;
static syslog_msg_t msg_counter;
extern Debug;
static msg_queue_t * msg_queue_find_sgid(syslog_sg_t);
static void msg_queue_dump(struct msg_queue_head *msg_head);
static void
msg_queue_dump(struct msg_queue_head *msg_head)
{
msg_t *msg;
dprintf("-----------------------------------------\n");
dprintf("Messages in queue:\n");
TAILQ_FOREACH(msg, msg_head, next){
dprintf("%s %s %s\n", msg->header, msg->sd, msg->line);
dprintf("\t%s\n", msg->hash);
}
dprintf("-----------------------------------------\n");
}
/** Search for Signature Group ID in a msg list. */
static msg_queue_t *
msg_queue_find_sgid(syslog_sg_t sg_id)
{
msg_queue_t *msgq;
SLIST_FOREACH(msgq, &msg_list_head, next) {
if (sg_id == msgq->sg_id)
return msgq;
}
return 0;
}
/*!
* Add Message to message queue identified by signature id
* @param sg_id Signature Group ID
* @param msg poiter to msg description structure
* @bug Free all messages in msg_t::msg_queue after sending hash msg
* @bug Implement sending message with hashes.
* @see logmsg()
*/
int
syslog_msg_queue_add(syslog_sg_t sg_id, msg_t *msg)
{
msg_queue_t *msgq;
assert (msg != NULL);
if ((msgq = msg_queue_find_sgid(sg_id)) == NULL)
return ENOENT;
TAILQ_INSERT_TAIL(&msgq->msg_queue, msg, next);
msgq->msgs++;
msg_queue_dump(&msgq->msg_queue);
if (msgq->msgs == msgq->queue_size) {
dprintf("Sending hash message SG %d\n", sg_id);
syslog_sign_msg_create(&msgq->msg_queue);
msgq->msgs = 0;
/* initialize message queue to NULL after removing all msgs from it */
TAILQ_INIT(&msgq->msg_queue);
}
return 0;
}
/** Initialize message queue head */
void
syslog_msg_queue_init(void)
{
SLIST_INIT(&msg_list_head);
}
/*!
* Initialize message queue for size and SG.
* @param size Size of message queue, when mesage queue is full message with hashes is sent.
* @param sg_id Signature Group ID.
*/
int
syslog_msg_queue_create(size_t size, syslog_sg_t sg_id)
{
msg_queue_t *msgq;
assert(size > 0);
dprintf("!!! Creating MEssage queue for SG %d with size %d\n", sg_id, size);
msgq = malloc(sizeof(msg_queue_t));
msgq->sg_id = sg_id;
msgq->queue_size = size;
msgq->msgs = 0;
TAILQ_INIT(&msgq->msg_queue);
SLIST_INSERT_HEAD(&msg_list_head, msgq, next);
return 0;
}
/*!
* Destroy message queue selected with sg_id
* @bug Destroy all msgs in msg_t::mds_queue when freeing this struct.
*/
int
syslog_msg_queue_destroy(syslog_sg_t sg_id)
{
msg_queue_t *msgq;
/* Find message queue by signature ID */
msgq = msg_queue_find_sgid(sg_id);
if (msgq == NULL)
return ENOENT;
/* I have to desotry all message in a queue when I'm removing sg msg queue */
if (!TAILQ_EMPTY(&msgq->msg_queue))
return EBUSY;
free(msgq);
return 0;
}
/*********************************************************************************
* Message creating functions
*********************************************************************************/
/*!
* Allocate Message
*/
msg_t *
syslog_msg_create()
{
msg_t *msg;
if ((msg = malloc(sizeof(msg_t))) == NULL)
return NULL;
memset(msg, '0', sizeof(msg_t));
msg->header = NULL;
msg->line = NULL;
msg->sd = NULL;
msg->msg_id = msg_counter++;
return msg;
}
/*!
* Destroy message, before this message must be removed from all
* msg lists.
* @see syslog_msg_queue_add()
* @see syslog_msg_queue_destroy()
*/
void
syslog_msg_destroy(msg_t *msg)
{
assert (msg != NULL);
if (msg->header != NULL)
free(msg->header);
if (msg->line != NULL)
free(msg->line);
if (msg->sd != NULL)
free(msg->sd);
if (msg->hash != NULL)
free(msg->hash);
}
/** Set message header */
void
syslog_msg_set_header(char *header, msg_t *msg)
{
assert(msg != NULL);
if (msg->header != NULL)
free(msg->header);
msg->header = header;
}
/** Set message line/text */
void
syslog_msg_set_line(char *line, msg_t *msg)
{
assert(msg != NULL);
if (msg->line != NULL)
free(msg->line);
msg->line = line;
}
/** Set message sd elements */
void
syslog_msg_set_sd(char *sd, msg_t *msg)
{
assert(msg != NULL);
if (msg->sd != NULL)
free(msg->sd);
msg->sd = sd;
}
/** Set message hash */
void
syslog_msg_set_hash(char *hash, msg_t *msg)
{
assert(msg != NULL);
strncpy(msg->hash, hash, sizeof(msg->hash));
}
/*!
* Alloc message header
* @param pri Priority value
* @param hostname Name/IP address of logger
* @param times Timestamp when was message created
* @param app_name Name of Aplication which created this message
* @param proc_id Pid or other process identificator
* @param msg_id Message id -> it is message counter.
* @see logmsg()
* @bug this interface is broken probably it need some more work
*/
char *
syslog_msg_header_create(int pri, char *hostname, char *times,
char *app_name, syslog_pid_t proc_id, syslog_msg_t msg_id)
{
char *header;
char *proc, *msgid;
size_t size;
if (proc_id < 0)
asprintf(&proc, "-");
else
asprintf(&proc, "%d", proc_id);
if (msg_id < 0)
asprintf(&msgid, "%"PRIu64, msg_counter++);
else
asprintf(&msgid, "%"PRIu64, msg_id);
size = asprintf(&header, "<%d>%d %s %s %s %s %s", pri, SYSLOG_VERSION, times,
hostname, app_name, proc, msgid);
dprintf("Message header: %s\n", header);
free(proc);
free(msgid);
return header;
}
/*!
* Alloc SD elements string for msg.
* @bug Add normal support for SD elements
*/
char *
syslog_msg_sd_create()
{
char *sde;
asprintf(&sde,"[exampleSDID@]");
dprintf("Created sd elements: %s\n", sde);
return sde;
}
char *
syslog_msg_message_create(msg_t *msg)
{
char *msg_buf;
dprintf("%s %s %s\n", msg->header, msg->sd, msg->line);
asprintf(&msg_buf,"%s %s %s", msg->header, msg->sd, msg->line);
syslog_sha1_msg(msg, msg_buf, strlen(msg_buf));
dprintf("Message: %s\n", msg_buf);
dprintf("Hash: %s\n", msg->hash);
return msg_buf;
}