3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2011
7 /* __LICENSE_HEADER_BEGIN__ */
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
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__ */
38 #include <libdabba/packet_rx.h>
39 #include <libdabba/pcap.h>
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
;
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
;
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
) ==
75 if (poll(&pfd
, 1, -1) < 0)
79 if ((mmap_hdr
->tp_h
.tp_status
& 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
;