nmsg  0.9.0
input_pres.c
1 /*
2  * Copyright (c) 2009-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_pres_read(nmsg_input_t input, nmsg_message_t *msg) {
25  char line[1024];
26  nmsg_res res;
27  size_t sz;
28  struct timespec ts;
29  uint8_t *pbuf;
30 
31  while (fgets(line, sizeof(line), input->pres->fp) != NULL) {
32  res = nmsg_msgmod_pres_to_payload(input->msgmod, input->clos,
33  line);
34  if (res == nmsg_res_failure)
35  return (res);
36  if (res == nmsg_res_success)
37  continue;
38  if (res != nmsg_res_pbuf_ready)
39  return (res);
40 
41  /* payload ready, finalize and convert to nmsg payload */
42  nmsg_timespec_get(&ts);
43  res = nmsg_msgmod_pres_to_payload_finalize(input->msgmod,
44  input->clos,
45  &pbuf, &sz);
46  if (res != nmsg_res_success)
47  return (res);
48  *msg = nmsg_message_from_raw_payload(input->msgmod->plugin->vendor.id,
49  input->msgmod->plugin->msgtype.id,
50  pbuf, sz, &ts);
51  if (*msg == NULL) {
52  free(pbuf);
53  return (nmsg_res_memfail);
54  }
55 
56  return (nmsg_res_success);
57  }
58 
59  return (nmsg_res_eof);
60 }
void nmsg_timespec_get(struct timespec *ts)
Get the current time.
Definition: timespec.c:24
nmsg_res nmsg_msgmod_pres_to_payload_finalize(nmsg_msgmod_t mod, void *clos, uint8_t **pbuf, size_t *sz)
After a call to nmsg_msgmod_pres_to_payload() returns nmsg_res_pbuf_ready, this function will return ...
nmsg_res
nmsg result code
Definition: res.h:25
success
Definition: res.h:26
out of memory
Definition: res.h:29
nmsg_res nmsg_msgmod_pres_to_payload(nmsg_msgmod_t mod, void *clos, const char *pres)
Convert a presentation format line to an NMSG payload.
generic failure
Definition: res.h:27
a pbuf is ready to be written
Definition: res.h:32
nmsg_message_t nmsg_message_from_raw_payload(unsigned vid, unsigned msgtype, uint8_t *data, size_t sz, const struct timespec *ts)
Initialize a new message object from an opaque payload blob.
end of file
Definition: res.h:28