1 #include "tests.h"
2 #include <stdio.h>
3 #include <sys/socket.h>
4 #include <linux/if_packet.h>
5
6 static const char *errstr;
7
8 static long
set_tpacket_req(int optname,void * optval,socklen_t len)9 set_tpacket_req(int optname, void *optval, socklen_t len)
10 {
11 long rc = setsockopt(-1, SOL_PACKET, optname, optval, len);
12 errstr = sprintrc(rc);
13 return rc;
14 }
15
16 static void
test_tpacket_req(const int optname,const char * const optname_str)17 test_tpacket_req(const int optname, const char *const optname_str)
18 {
19 TAIL_ALLOC_OBJECT_CONST_PTR(struct tpacket_req, tpreq);
20 socklen_t len;
21
22 /* setsockopt with optname unknown */
23 set_tpacket_req(-1, NULL, 0);
24 printf("setsockopt(-1, SOL_PACKET, %#x /* PACKET_??? */, NULL, 0) = %s\n",
25 -1, errstr);
26
27 /* classic setsockopt */
28 len = sizeof(struct tpacket_req);
29 set_tpacket_req(optname, tpreq, len);
30 printf("setsockopt(-1, SOL_PACKET, %s, {tp_block_size=%u,"
31 " tp_block_nr=%u, tp_frame_size=%u, tp_frame_nr=%u}, %d) = %s\n",
32 optname_str, tpreq->tp_block_size, tpreq->tp_block_nr,
33 tpreq->tp_frame_size, tpreq->tp_frame_nr, len, errstr);
34
35 /* setsockopt with optlen larger than usual */
36 len = len + 1;
37 set_tpacket_req(optname, tpreq, len);
38 printf("setsockopt(-1, SOL_PACKET, %s, %p,"
39 " %d) = %s\n", optname_str, tpreq, len, errstr);
40 }
41
42 int
main(void)43 main(void)
44 {
45 test_tpacket_req(ARG_STR(PACKET_RX_RING));
46 #ifdef PACKET_TX_RING
47 test_tpacket_req(ARG_STR(PACKET_TX_RING));
48 #endif
49
50 puts("+++ exited with 0 +++");
51 return 0;
52 }
53