-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnmsgjsonseqsrc.c
More file actions
295 lines (266 loc) · 7.01 KB
/
nmsgjsonseqsrc.c
File metadata and controls
295 lines (266 loc) · 7.01 KB
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
/*
* Copyright (c) 2017 by Farsight Security, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#include <nmsg.h>
static int get_socket(const char *);
static void signal_handler(int sig __attribute__((unused)));
static void setup_signals(void);
static nmsg_io_t io;
static void
callback(nmsg_message_t msg, void *user __attribute__((__unused__)))
{
nmsg_res res;
char *json_blob;
/* Serialize NMSG as JSON, freeing the malloc'd string if successful. */
res = nmsg_message_to_json(msg, &json_blob);
if (res != nmsg_res_success)
{
fprintf(stderr, "x");
}
else
{
fprintf(stderr, "%s\n", json_blob);
free(json_blob);
}
nmsg_message_destroy(&msg);
}
int
main(int argc, char **argv)
{
nmsg_res res; /* reusuable NMSG response holder */
nmsg_input_t input; /* NMSG input object */
nmsg_output_t output; /* NMSG output object */
uint64_t count;
int c, sock = -1;
while ((c = getopt(argc, argv, "l:")) != -1)
{
switch (c)
{
case 'l':
sock = get_socket(optarg);
break;
default:
fprintf(stderr,
"usage: %s "
"{-l address/port}\n", argv[0]);
exit(1);
}
}
if (sock == -1)
{
fprintf(stderr, "-l must be specified\n");
exit(1);
}
printf("nmsg JSON emitter / seqsrc example program\n");
/* Iniitialize nmsg. */
res = nmsg_init();
if (res != nmsg_res_success)
{
fprintf(stderr, "unable to initialize libnmsg\n");
return (EXIT_FAILURE);
}
printf("nmsg initialized\n");
/* Iniitialize nmsg IO engine. */
io = nmsg_io_init();
if (io == NULL)
{
fprintf(stderr, "nmsg_io_init() failed\n");
return (EXIT_FAILURE);
}
printf("nmsg io engine initialized\n");
/* Initialize a new nmsg stream input. */
input = nmsg_input_open_sock(sock);
if (input == NULL)
{
fprintf(stderr, "nmsg_input_open_file() failed\n");
return (EXIT_FAILURE);
}
printf("nmsg socket input initialized\n");
/* Instruct io engine to enable container loss tracking. */
res = nmsg_input_set_verify_seqsrc(input, true);
if (res != nmsg_res_success)
{
fprintf(stderr, "nmsg_input_set_verify_seqsrc(): %s\n",
nmsg_res_lookup(res));
return (EXIT_FAILURE);
}
printf("verify seqsrc enabled\n");
/* Add an nmsg input to an nmsg_io_t object. */
res = nmsg_io_add_input(io, input, NULL);
if (res != nmsg_res_success)
{
fprintf(stderr, "nmsg_io_add_input(): %s\n",
nmsg_res_lookup(res));
return (EXIT_FAILURE);
}
printf("socket input added to io engine\n");
/* Initialize a new nmsg output closure.
* This allows a user-provided callback to function as an nmsg
* output, for instance to participate in an nmsg_io loop. The
* callback is responsible for disposing of each nmsg message. */
output = nmsg_output_open_callback(callback, NULL);
if (output == NULL)
{
fprintf(stderr, "nmsg_output_open_callback() failed\n");
return (EXIT_FAILURE);
}
printf("callback initialized\n");
/* Add an nmsg output to an nmsg_io_t object.
* When nmsg_io_loop() is called, the input threads will cycle over
* and write payloads to the available outputs. */
res = nmsg_io_add_output(io, output, NULL);
if (res != nmsg_res_success)
{
fprintf(stderr, "nmsg_io_add_output() failed: %s\n",
nmsg_res_lookup(res));
return (EXIT_FAILURE);
}
printf("callback added to io engine\n");
/* from nmsgtool */
setup_signals();
/* Begin processing the data specified by the configured inputs and
* outputs.
* One processing thread is created for each input. nmsg_io_loop()
* does not return until these threads finish and are destroyed. Only
* nmsg_io_breakloop() may be called asynchronously while
* nmsg_io_loop() is executing. nmsg_io_loop() invalidates an
* nmsg_io_t object. nmsg_io_destroy() should then be called. */
printf("entering io loop, <ctrl-c> to quit...\n");
res = nmsg_io_loop(io);
if (res != nmsg_res_success)
{
fprintf(stderr, "nmsg_io_loop() failed: %s\n",
nmsg_res_lookup(res));
return (EXIT_FAILURE);
}
/* Retrieve the total number of NMSG containers that have been
* received since the nmsg_input_t object was created. */
res = nmsg_input_get_count_container_received(input, &count);
if (res != nmsg_res_success)
{
fprintf(stderr,
"nmsg_input_get_count_container_received(): %s\n",
nmsg_res_lookup(res));
return (EXIT_FAILURE);
}
printf("\nreceived %" PRIu64 " containers\n", count);
/* Retrieve the total number of NMSG containers that have been
* dropped since the nmsg_input_t object was created. */
res = nmsg_input_get_count_container_dropped(input, &count);
if (res != nmsg_res_success)
{
fprintf(stderr,
"nmsg_input_get_count_container_dropped(): %s\n",
nmsg_res_lookup(res));
return (EXIT_FAILURE);
}
printf("dropped %" PRIu64 " containers\n", count);
/* Deallocate the resources associated with an nmsg_io_t object. */
nmsg_io_destroy(&io);
return (EXIT_SUCCESS);
}
static int
get_socket(const char *spec)
{
char *p, *addr;
struct sockaddr_in6 sa6;
struct sockaddr_in sa4;
struct sockaddr *sap;
int pf, sock, port, len;
if ((p = strchr(spec, '/')) == NULL)
{
p = strchr(spec, ',');
}
if (p == NULL)
{
fprintf(stderr, "socket spec is addr,port or addr/port\n");
return (-1);
}
port = atoi(p + 1);
if (port == 0)
{
fprintf(stderr, "port number '%s' is not valid\n", p + 1);
return (-1);
}
addr = strndup(spec, p - spec);
if (inet_pton(AF_INET6, addr, &sa6.sin6_addr) > 0)
{
len = sizeof sa6;
sa6.sin6_family = AF_INET6;
/* sa6.sin6_len = len; */
sa6.sin6_port = htons(port);
pf = PF_INET6;
sap = (struct sockaddr *) &sa6;
}
else if (inet_pton(AF_INET, addr, &sa4.sin_addr) > 0)
{
len = sizeof sa4;
sa4.sin_family = AF_INET;
/* sa4.sin_len = len; */
sa4.sin_port = htons(port);
pf = PF_INET;
sap = (struct sockaddr *) &sa4;
}
else
{
fprintf(stderr, "address '%s' is not valid\n", addr);
free(addr);
return (-1);
}
free(addr);
sock = socket(pf, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return (-1);
}
if (bind(sock, sap, len) == -1)
{
perror("bind");
return (-1);
}
return (sock);
}
static void
signal_handler(int sig __attribute__((unused)))
{
nmsg_io_breakloop(io);
}
static void
setup_signals(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGINT);
sigaddset(&sa.sa_mask, SIGTERM);
sa.sa_handler = &signal_handler;
if (sigaction(SIGINT, &sa, NULL) != 0)
{
perror("sigaction");
exit(EXIT_FAILURE);
}
if (sigaction(SIGTERM, &sa, NULL) != 0)
{
perror("sigaction");
exit(EXIT_FAILURE);
}
}