Added postinst script for debian packaging.
[dabba.git] / libdabba / packet_rx.c
blob4f860da0dce354244f99660ab178d92b50bcfdf7
1 /**
2 * \file packet_rx.c
3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2011
4 * \date 2011
5 */
7 /* __LICENSE_HEADER_BEGIN__ */
9 /*
10 * Copyright (C) 2011 Emmanuel Roullit <emmanuel.roullit@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 /* __LICENSE_HEADER_END__ */
30 #define _GNU_SOURCE
32 #include <assert.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/uio.h>
36 #include <poll.h>
38 #include <libdabba/packet_rx.h>
39 #include <libdabba/pcap.h>
41 /**
42 * \brief Receive packets coming from a packet mmap RX ring
43 * \param[in] arg Pointer to packet rx thread structure
44 * \return Always return NULL
46 * This function will \c poll(2) until some packets are received on the configured
47 * interface. For now this function does not much but it can be starting point
48 * for PCAP function to dump the frames into a file or for a packet dissectors.
51 void *packet_rx(void *arg)
53 struct packet_rx_thread *thread = arg;
54 struct packet_mmap *pkt_rx = &thread->pkt_rx;
55 struct pollfd pfd;
56 size_t index = 0;
58 if (!arg)
59 goto out;
61 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
63 memset(&pfd, 0, sizeof(pfd));
65 pfd.events = POLLIN | POLLRDNORM | POLLERR;
66 pfd.fd = pkt_rx->pf_sock;
68 for (;;) {
69 for (index = 0; index < pkt_rx->layout.tp_frame_nr; index++) {
70 struct packet_mmap_header *mmap_hdr =
71 pkt_rx->vec[index].iov_base;
73 if ((mmap_hdr->tp_h.tp_status & TP_STATUS_KERNEL) ==
74 TP_STATUS_KERNEL) {
75 if (poll(&pfd, 1, -1) < 0)
76 continue;
79 if ((mmap_hdr->tp_h.tp_status & TP_STATUS_USER) ==
80 TP_STATUS_USER) {
81 if (thread->pcap_fd > 0
82 && mmap_hdr->tp_h.tp_len <
83 pkt_rx->layout.tp_frame_size) {
84 pcap_write(thread->pcap_fd,
85 (uint8_t *) mmap_hdr +
86 mmap_hdr->tp_h.tp_mac,
87 mmap_hdr->tp_h.tp_len,
88 mmap_hdr->tp_h.tp_snaplen,
89 mmap_hdr->tp_h.tp_sec,
90 mmap_hdr->tp_h.tp_usec);
93 mmap_hdr->tp_h.tp_status = TP_STATUS_KERNEL;
98 out:
99 pthread_exit(NULL);