nmsg  0.9.0
input_nullnmsg.c
1 /*
2  * Copyright (c) 2012 by Farsight Security, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* Import. */
18 
19 #include "private.h"
20 
21 /* Internal functions. */
22 
24 _input_nmsg_read_null(nmsg_input_t input __attribute__((unused)),
25  nmsg_message_t *msg __attribute__((unused)))
26 {
27  return (nmsg_res_failure);
28 }
29 
31 _input_nmsg_loop_null(nmsg_input_t input __attribute__((unused)),
32  int cnt __attribute__((unused)),
33  nmsg_cb_message cb __attribute__((unused)),
34  void *user __attribute__((unused)))
35 {
36  return (nmsg_res_failure);
37 }
38 
39 /* Export. */
40 
42 nmsg_input_read_null(nmsg_input_t input, uint8_t *buf, size_t buf_len,
43  struct timespec *ts, nmsg_message_t **msgarray, size_t *n_msg)
44 {
45  nmsg_res res;
46  ssize_t msgsize;
47 
48  assert(input->stream->type == nmsg_stream_type_null);
49 
50  /* use caller-supplied time, else retrieve the current time */
51  if (ts != NULL)
52  memcpy(&input->stream->now, ts, sizeof(*ts));
53  else
54  nmsg_timespec_get(&input->stream->now);
55 
56  /* deserialize the NMSG header */
57  res = _input_nmsg_deserialize_header(buf, buf_len, &msgsize, &input->stream->flags);
58  if (res != nmsg_res_success)
59  return (res);
60  buf += NMSG_HDRLSZ_V2;
61 
62  /* the entire NMSG container must be present */
63  if ((size_t) msgsize != buf_len - NMSG_HDRLSZ_V2)
64  return (nmsg_res_parse_error);
65 
66  /* unpack message container */
67  res = _input_nmsg_unpack_container(input, &input->stream->nmsg, buf, msgsize);
68 
69  /* expire old outstanding fragments */
70  _input_frag_gc(input->stream);
71 
72  /* convert NMSG payloads to nmsg_message_t objects */
73  if (input->stream->nmsg != NULL) {
74  int msgarray_idx = 0;
75 
76  *msgarray = malloc(input->stream->nmsg->n_payloads * sizeof(void *));
77  if (*msgarray == NULL) {
78  nmsg__nmsg__free_unpacked(input->stream->nmsg, NULL);
79  input->stream->nmsg = NULL;
80  return (nmsg_res_memfail);
81  }
82  *n_msg = input->stream->nmsg->n_payloads;
83 
84  for (unsigned i = 0; i < input->stream->nmsg->n_payloads; i++) {
85  Nmsg__NmsgPayload *np;
86  nmsg_message_t msg;
87 
88  /* detach payload */
89  np = input->stream->nmsg->payloads[i];
90  input->stream->nmsg->payloads[i] = NULL;
91 
92  /* filter payload */
93  if (!_input_nmsg_filter(input, i, np)) {
94  _nmsg_payload_free(&np);
95  *n_msg -= 1;
96  continue;
97  }
98 
99  /* convert payload to message object */
100  msg = _nmsg_message_from_payload(np);
101  if (msg == NULL) {
102  free(*msgarray);
103  *msgarray = NULL;
104  *n_msg = 0;
105  nmsg__nmsg__free_unpacked(input->stream->nmsg, NULL);
106  input->stream->nmsg = NULL;
107  return (nmsg_res_memfail);
108  }
109  (*msgarray)[msgarray_idx] = msg;
110  msgarray_idx += 1;
111  }
112 
113  input->stream->nmsg->n_payloads = 0;
114  free(input->stream->nmsg->payloads);
115  input->stream->nmsg->payloads = NULL;
116  nmsg__nmsg__free_unpacked(input->stream->nmsg, NULL);
117  input->stream->nmsg = NULL;
118  } else {
119  *msgarray = NULL;
120  *n_msg = 0;
121  }
122 
123  return (res);
124 }
void nmsg_timespec_get(struct timespec *ts)
Get the current time.
Definition: timespec.c:24
nmsg_res
nmsg result code
Definition: res.h:25
success
Definition: res.h:26
void(* nmsg_cb_message)(nmsg_message_t msg, void *user)
Callback function for processing nmsg messages.
Definition: nmsg.h:69
out of memory
Definition: res.h:29
#define NMSG_HDRLSZ_V2
Number of octets in an NMSG header (magic + version + length).
Definition: constants.h:42
generic failure
Definition: res.h:27
unable to parse input
Definition: res.h:36
nmsg_res nmsg_input_read_null(nmsg_input_t input, uint8_t *buf, size_t buf_len, struct timespec *ts, nmsg_message_t **msg, size_t *n_msg)
Read zero, one, or more NMSG messages from a "null source" input.