Home | History | Annotate | Download | only in tests-mx32
      1 /*
      2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) 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 #include <stdio.h>
     32 #include <netinet/in.h>
     33 #include <arpa/inet.h>
     34 #include "test_nlattr.h"
     35 #ifdef HAVE_LINUX_NEIGHBOUR_H
     36 # include <linux/neighbour.h>
     37 #endif
     38 #include <linux/rtnetlink.h>
     39 
     40 #define NDA_PORT 6
     41 
     42 static void
     43 init_ndmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
     44 {
     45 	SET_STRUCT(struct nlmsghdr, nlh,
     46 		.nlmsg_len = msg_len,
     47 		.nlmsg_type = RTM_GETNEIGH,
     48 		.nlmsg_flags = NLM_F_DUMP
     49 	);
     50 
     51 	struct ndmsg *const msg = NLMSG_DATA(nlh);
     52 	SET_STRUCT(struct ndmsg, msg,
     53 		.ndm_family = AF_UNIX,
     54 		.ndm_ifindex = ifindex_lo(),
     55 		.ndm_state = NUD_PERMANENT,
     56 		.ndm_flags = NTF_PROXY,
     57 		.ndm_type = RTN_UNSPEC
     58 	);
     59 }
     60 
     61 static void
     62 print_ndmsg(const unsigned int msg_len)
     63 {
     64 	printf("{len=%u, type=RTM_GETNEIGH, flags=NLM_F_DUMP"
     65 	       ", seq=0, pid=0}, {ndm_family=AF_UNIX"
     66 	       ", ndm_ifindex=" IFINDEX_LO_STR
     67 	       ", ndm_state=NUD_PERMANENT"
     68 	       ", ndm_flags=NTF_PROXY"
     69 	       ", ndm_type=RTN_UNSPEC}",
     70 	       msg_len);
     71 }
     72 
     73 int
     74 main(void)
     75 {
     76 	skip_if_unavailable("/proc/self/fd/");
     77 
     78 	const int fd = create_nl_socket(NETLINK_ROUTE);
     79 	const unsigned int hdrlen = sizeof(struct ndmsg);
     80 	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
     81 
     82 	static char pattern[4096];
     83 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
     84 
     85 	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
     86 	char nla_type_str[256];
     87 	sprintf(nla_type_str, "%#x /* NDA_??? */", nla_type);
     88 	TEST_NLATTR_(fd, nlh0, hdrlen,
     89 		     init_ndmsg, print_ndmsg,
     90 		     nla_type, nla_type_str,
     91 		     4, pattern, 4,
     92 		     print_quoted_hex(pattern, 4));
     93 
     94 	TEST_NLATTR(fd, nlh0, hdrlen,
     95 		    init_ndmsg, print_ndmsg,
     96 		    NDA_DST, 4, pattern, 4,
     97 		    print_quoted_hex(pattern, 4));
     98 
     99 	static const struct nda_cacheinfo ci = {
    100 		.ndm_confirmed = 0xabcdedad,
    101 		.ndm_used = 0xbcdaedad,
    102 		.ndm_updated = 0xcdbadeda,
    103 		.ndm_refcnt = 0xdeadbeda
    104 	};
    105 
    106 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    107 			   init_ndmsg, print_ndmsg,
    108 			   NDA_CACHEINFO, pattern, ci,
    109 			   PRINT_FIELD_U("{", ci, ndm_confirmed);
    110 			   PRINT_FIELD_U(", ", ci, ndm_used);
    111 			   PRINT_FIELD_U(", ", ci, ndm_updated);
    112 			   PRINT_FIELD_U(", ", ci, ndm_refcnt);
    113 			   printf("}"));
    114 
    115 	const uint16_t port = 0xabcd;
    116 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    117 			   init_ndmsg, print_ndmsg,
    118 			   NDA_PORT, pattern, port,
    119 			   printf("htons(%u)", ntohs(port)));
    120 
    121 	puts("+++ exited with 0 +++");
    122 	return 0;
    123 }
    124