1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #ifdef HAVE_STRUCT_DCBMSG
32 
33 # include <stdio.h>
34 # include "test_nlattr.h"
35 # include <linux/dcbnl.h>
36 # include <linux/rtnetlink.h>
37 
38 static void
39 init_dcbmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
40 {
41 	SET_STRUCT(struct nlmsghdr, nlh,
42 		.nlmsg_len = msg_len,
43 		.nlmsg_type = RTM_GETDCB,
44 		.nlmsg_flags = NLM_F_DUMP
45 	);
46 
47 	struct dcbmsg *const msg = NLMSG_DATA(nlh);
48 	SET_STRUCT(struct dcbmsg, msg,
49 		.dcb_family = AF_UNIX,
50 		.cmd = DCB_CMD_UNDEFINED
51 	);
52 }
53 
54 static void
55 print_dcbmsg(const unsigned int msg_len)
56 {
57 	printf("{len=%u, type=RTM_GETDCB, flags=NLM_F_DUMP"
58 	       ", seq=0, pid=0}, {dcb_family=AF_UNIX"
59 	       ", cmd=DCB_CMD_UNDEFINED}",
60 	       msg_len);
61 }
62 
63 int
64 main(void)
65 {
66 	skip_if_unavailable("/proc/self/fd/");
67 
68 	const int fd = create_nl_socket(NETLINK_ROUTE);
69 	const unsigned int hdrlen = sizeof(struct dcbmsg);
70 	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
71 
72 	static char pattern[4096];
73 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
74 
75 	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
76 	char nla_type_str[256];
77 	sprintf(nla_type_str, "%#x /* DCB_ATTR_??? */", nla_type);
78 	TEST_NLATTR_(fd, nlh0, hdrlen,
79 		     init_dcbmsg, print_dcbmsg,
80 		     nla_type, nla_type_str,
81 		     4, pattern, 4,
82 		     print_quoted_hex(pattern, 4));
83 
84 	puts("+++ exited with 0 +++");
85 	return 0;
86 }
87 
88 #else
89 
90 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_DCBMSG")
91 
92 #endif
93