nmsg  0.9.0
input_pcap.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_pcap_read(nmsg_input_t input, nmsg_message_t *msg) {
25  nmsg_res res;
26  size_t sz;
27  struct nmsg_ipdg dg;
28  struct timespec ts;
29  uint8_t *pbuf;
30 
31  /* get next ip datagram from pcap source */
32  res = nmsg_pcap_input_read(input->pcap, &dg, &ts);
33  if (res != nmsg_res_success)
34  return (res);
35 
36  /* convert ip datagram to payload */
37  res = nmsg_msgmod_ipdg_to_payload(input->msgmod, input->clos, &dg,
38  &pbuf, &sz);
39  if (res != nmsg_res_pbuf_ready)
40  return (res);
41 
42  /* encapsulate nmsg payload */
43  *msg = nmsg_message_from_raw_payload(input->msgmod->plugin->vendor.id,
44  input->msgmod->plugin->msgtype.id,
45  pbuf, sz, &ts);
46  if (*msg == NULL) {
47  free(pbuf);
48  return (nmsg_res_memfail);
49  }
50 
51  return (nmsg_res_success);
52 }
53 
55 _input_pcap_read_raw(nmsg_input_t input, nmsg_message_t *msg) {
56  return (nmsg_msgmod_pkt_to_payload(input->msgmod, input->clos, input->pcap, msg));
57 }
nmsg_res
nmsg result code
Definition: res.h:25
success
Definition: res.h:26
nmsg_res nmsg_msgmod_ipdg_to_payload(nmsg_msgmod_t mod, void *clos, const struct nmsg_ipdg *dg, uint8_t **pbuf, size_t *sz)
Convert an IP datagram to an NMSG payload.
out of memory
Definition: res.h:29
Parsed IP datagram.
Definition: ipdg.h:34
nmsg_res nmsg_msgmod_pkt_to_payload(struct nmsg_msgmod *mod, void *clos, nmsg_pcap_t pcap, nmsg_message_t *m)
Read a raw packet and optionally convert it to an NMSG payload.
a pbuf is ready to be written
Definition: res.h:32
nmsg_res nmsg_pcap_input_read(nmsg_pcap_t pcap, struct nmsg_ipdg *dg, struct timespec *ts)
Read an IP datagram from an nmsg_pcap_t input, performing reassembly if necessary.
Definition: pcap_input.c:70
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.