-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog_client_mod.c
271 lines (233 loc) · 5.81 KB
/
syslog_client_mod.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
#include "syslog_client_mod.h"
// printf()
#include <stdio.h>
// strlen()
#include <string.h>
// malloc(), free()
#include <stdlib.h>
#ifdef _DEBUG
// intXX_t, uintXX_t
#include <inttypes.h>
#endif
// close()
#include <unistd.h>
// regular expression operations
#include <regex.h>
// inet_ntoa()
#include <arpa/inet.h>
// struct sockaddr, socket(), sendto()
#include <sys/socket.h>
// BSD socket errors
#include <errno.h>
int
syslog_client_is_rfc5424_syslog_message(const char* const message)
{
if (message == NULL || *message == '\0')
return 0;
#ifdef _DEBUG
printf("syslog_client_is_rfc5424_syslog_message()\n\tMessage: %s\n", message);
#endif
// Finds the space (%d32) between header and structured-data.
char* ptr = (char*)message;
for (uint8_t counter = 0; counter < 6; ++counter, ++ptr)
if ((ptr = strchr(ptr, (int)' ')) == NULL)
return 0;
char* header = (char*)malloc(ptr - message);
if (header == NULL)
exit(MEMORY_ALLOCATION_FAILED_CODE);
memcpy(header, message, ptr - message);
header[ptr - message - 1] = '\0';
#ifdef _DEBUG
printf("syslog_client_is_rfc5424_syslog_message()\n\tHeader: %s\n", header);
#endif
regex_t regex_header;
// Compiles regular expression to check the header.
int result = regcomp(
®ex_header,
"^"
"<([0-9]|[1-9][0-9]|1[1-8][0-9]|19[01])>"
"[1-9][0-9]{0,2}"
" "
"("
"-"
"|"
"("
"[0-9]{4}"
"-"
"(0[1-9]|1[012])"
"-"
"([012][0-9]|3[01])"
"T"
"([01][0-9]|2[0-3])"
":"
"[0-5][0-9]"
":"
"[0-5][0-9]"
"(\\.[0-9]{1,6})?"
"(Z|([+-]([01][0-9]|2[0-3]):[0-5][0-9]))"
")"
")"
" "
"("
"-"
"|"
"[[:print:]]{1,255}"
")"
" "
"("
"-"
"|"
"[[:print:]]{1,48}"
")"
" "
"("
"-"
"|"
"[[:print:]]{1,128}"
")"
" "
"("
"-"
"|"
"[[:print:]]{1,32}"
")"
"$",
REG_EXTENDED | REG_NOSUB
);
if (result != 0) {
#ifdef _DEBUG
char buffer[100] = {'\0'};
regerror(result, ®ex_header, buffer, sizeof(buffer));
printf("syslog_client_is_rfc5424_syslog_message()\n\tCompiling the regex_header failed: %s\n", buffer);
#endif
free(header);
return 0;
}
// Executes the regular expression on 'header'.
result = regexec(®ex_header, header, (size_t)0, NULL, 0);
// Frees resources.
regfree(®ex_header);
free(header);
// Checks the result of matching the header.
switch (result) {
case 0:
break;
case REG_NOMATCH:
default:
#ifdef _DEBUG
{
char buffer[100] = {'\0'};
regerror(result, ®ex_header, buffer, sizeof(buffer));
printf("syslog_client_is_rfc5424_syslog_message()\n\tRegex_header failed: %s\n", buffer);
}
#endif
return 0;
} // -- end of switch ---
#ifdef _DEBUG
printf("syslog_client_is_rfc5424_syslog_message()\n\tData: %s\n", ptr);
#endif
regex_t regex_data;
// Compiles regular expression to check structured data and message (if presented).
result = regcomp(
®ex_data,
"^"
"("
"-"
"|"
"(\\[[^= \"]{1,32}( [^= \"]{1,32}=\".*\")*\\])+"
")"
"("
" .*"
"){0,1}"
"$",
REG_EXTENDED | REG_NOSUB
);
if (result != 0) {
#ifdef _DEBUG
char buffer[100] = {'\0'};
regerror(result, ®ex_data, buffer, sizeof(buffer));
printf("syslog_client_is_rfc5424_syslog_message()\n\tCompiling the regex_data failed: %s\n", buffer);
#endif
return 0;
}
// Executes the regular expression on 'header'.
result = regexec(®ex_data, ptr, (size_t)0, NULL, 0);
// Frees resources.
regfree(®ex_data);
// Checks the result of matching structured data and message (if presented).
switch (result) {
case 0:
break;
case REG_NOMATCH:
default:
#ifdef _DEBUG
{
char buffer[100] = {'\0'};
regerror(result, ®ex_data, buffer, sizeof(buffer));
printf("syslog_client_is_rfc5424_syslog_message()\n\tRegex_data failed: %s\n", buffer);
}
#endif
return 0;
} // -- end of switch ---
//
return 1;
}
int
syslog_client_send_message_to_syslog_server(const packet_t* const packet)
{
if (packet == NULL)
return PACKET_POINTER_IS_NULL_CODE;
if (packet->message == NULL)
return MESSAGE_POINTER_IS_NULL_CODE;
if (*(packet->message) == '\0')
return MESSAGE_EMPTY_CODE;
#ifdef _DEBUG
printf("syslog_client_send_message_to_syslog_server()\n\tIP address: %s\n\tPort: %"PRIu16"\n\tMessage: %s\n",
inet_ntoa(packet->connection_data.sin_addr), packet->connection_data.sin_port, packet->message);
#endif
int server_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (server_socket == -1)
return SOCKET_CREATION_FAILED_CODE;
// Sets to transmit the message without prior connection established
// and without blocking.
// TO-DO: use poll() to check whether the socket is ready to transmit.
ssize_t result = sendto(
server_socket,
packet->message,
strlen(packet->message),
MSG_DONTWAIT,
(struct sockaddr*)&packet->connection_data,
sizeof(packet->connection_data)
);
if (close(server_socket) == -1)
return CLOSING_SOCKET_FAILED_CODE;
if (result == (ssize_t)-1) {
switch (errno) {
case EAGAIN:
break;
case EACCES:
return SYSLOG_SERVER_ADDRESS_IS_BROADCAST_ADDRESS_CODE;
case ENOBUFS:
return CLIENT_SIDE_NETWORK_ERROR_CODE;
case ECONNREFUSED:
case EHOSTUNREACH:
case EHOSTDOWN:
case ENETDOWN:
return SYSLOG_SERVER_IS_NOT_AVAILABLE;
case EMSGSIZE:
return MESSAGE_IS_TO_LONG_CODE;
default:
#ifdef _DEBUG
printf("syslog_client_send_message_to_syslog_server()\n\terrno = %d\n", errno);
#endif
return OTHER_REASON_FOR_SEND_TO_FAIL_CODE;
}
} else {
if (result > 0 && (size_t)result != strlen(packet->message))
return PARTIALLY_SENT_MESSAGE_CODE;
}
#ifdef _DEBUG
printf("syslog_client_send_message_to_syslog_server()\n\tsent data = %ld bytes\n", (long int)result);
#endif
return SUCCESSFULLY_SENT_CODE;
}