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 <string.h>
     33 #include <sys/socket.h>
     34 #include <arpa/inet.h>
     35 #include <net/if.h>
     36 #include <netinet/tcp.h>
     37 #include "test_nlattr.h"
     38 #include <linux/inet_diag.h>
     39 #include <linux/rtnetlink.h>
     40 #include <linux/sock_diag.h>
     41 
     42 static const char address[] = "10.11.12.13";
     43 
     44 static void
     45 init_inet_diag_req(struct nlmsghdr *const nlh, const unsigned int msg_len)
     46 {
     47 	SET_STRUCT(struct nlmsghdr, nlh,
     48 		.nlmsg_len = msg_len,
     49 		.nlmsg_type = TCPDIAG_GETSOCK,
     50 		.nlmsg_flags = NLM_F_REQUEST
     51 	);
     52 
     53 	struct inet_diag_req *const req = NLMSG_DATA(nlh);
     54 	SET_STRUCT(struct inet_diag_req, req,
     55 		.idiag_family = AF_INET,
     56 		.idiag_ext = 1 << (INET_DIAG_TOS - 1),
     57 		.idiag_states = 1 << TCP_LAST_ACK,
     58 		.id.idiag_if = ifindex_lo()
     59 	);
     60 
     61 	if (!inet_pton(AF_INET, address, req->id.idiag_src) ||
     62 	    !inet_pton(AF_INET, address, req->id.idiag_dst))
     63 		perror_msg_and_skip("inet_pton");
     64 }
     65 
     66 static void
     67 print_inet_diag_req(const unsigned int msg_len)
     68 {
     69 	printf("{len=%u, type=TCPDIAG_GETSOCK, flags=NLM_F_REQUEST"
     70 	       ", seq=0, pid=0}, {idiag_family=AF_INET"
     71 	       ", idiag_src_len=0, idiag_dst_len=0"
     72 	       ", idiag_ext=1<<(INET_DIAG_TOS-1)"
     73 	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
     74 	       ", idiag_src=inet_addr(\"%s\")"
     75 	       ", idiag_dst=inet_addr(\"%s\")"
     76 	       ", idiag_if=" IFINDEX_LO_STR
     77 	       ", idiag_cookie=[0, 0]}"
     78 	       ", idiag_states=1<<TCP_LAST_ACK, idiag_dbs=0}",
     79 	       msg_len, address, address);
     80 }
     81 
     82 int
     83 main(void)
     84 {
     85 	skip_if_unavailable("/proc/self/fd/");
     86 
     87 	int fd = create_nl_socket(NETLINK_SOCK_DIAG);
     88 	const unsigned int hdrlen = sizeof(struct inet_diag_req);
     89 	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
     90 
     91 	static char pattern[4096];
     92 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
     93 
     94 	char nla_type_str[256];
     95 	sprintf(nla_type_str, "%#x /* INET_DIAG_REQ_??? */",
     96 		INET_DIAG_REQ_BYTECODE + 1);
     97 	TEST_NLATTR_(fd, nlh0, hdrlen,
     98 		     init_inet_diag_req, print_inet_diag_req,
     99 		     INET_DIAG_REQ_BYTECODE + 1, nla_type_str,
    100 		     4, pattern, 4,
    101 		     print_quoted_hex(pattern, 4));
    102 
    103 	puts("+++ exited with 0 +++");
    104 	return 0;
    105 }
    106