(root)/
strace-6.5/
tests/
nlattr_inet_diag_req_compat.c
       1  /*
       2   * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
       3   * Copyright (c) 2017-2021 The strace developers.
       4   * All rights reserved.
       5   *
       6   * SPDX-License-Identifier: GPL-2.0-or-later
       7   */
       8  
       9  #include "tests.h"
      10  
      11  #include <stdio.h>
      12  #include <string.h>
      13  #include <sys/socket.h>
      14  #include <arpa/inet.h>
      15  #include <net/if.h>
      16  #include <netinet/tcp.h>
      17  #include "test_nlattr.h"
      18  #include <linux/inet_diag.h>
      19  #include <linux/rtnetlink.h>
      20  #include <linux/sock_diag.h>
      21  
      22  static const char address[] = "10.11.12.13";
      23  
      24  static void
      25  init_inet_diag_req(struct nlmsghdr *const nlh, const unsigned int msg_len)
      26  {
      27  	SET_STRUCT(struct nlmsghdr, nlh,
      28  		.nlmsg_len = msg_len,
      29  		.nlmsg_type = TCPDIAG_GETSOCK,
      30  		.nlmsg_flags = NLM_F_REQUEST
      31  	);
      32  
      33  	struct inet_diag_req *const req = NLMSG_DATA(nlh);
      34  	SET_STRUCT(struct inet_diag_req, req,
      35  		.idiag_family = AF_INET,
      36  		.idiag_ext = 1 << (INET_DIAG_TOS - 1),
      37  		.idiag_states = 1 << TCP_LAST_ACK,
      38  		.id.idiag_if = ifindex_lo()
      39  	);
      40  
      41  	if (!inet_pton(AF_INET, address, req->id.idiag_src) ||
      42  	    !inet_pton(AF_INET, address, req->id.idiag_dst))
      43  		perror_msg_and_skip("inet_pton");
      44  }
      45  
      46  static void
      47  print_inet_diag_req(const unsigned int msg_len)
      48  {
      49  	printf("{nlmsg_len=%u, nlmsg_type=TCPDIAG_GETSOCK"
      50  	       ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
      51  	       ", {idiag_family=AF_INET, idiag_src_len=0, idiag_dst_len=0"
      52  	       ", idiag_ext=1<<(INET_DIAG_TOS-1)"
      53  	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
      54  	       ", idiag_src=inet_addr(\"%s\")"
      55  	       ", idiag_dst=inet_addr(\"%s\")"
      56  	       ", idiag_if=" IFINDEX_LO_STR
      57  	       ", idiag_cookie=[0, 0]}"
      58  	       ", idiag_states=1<<TCP_LAST_ACK, idiag_dbs=0}",
      59  	       msg_len, address, address);
      60  }
      61  
      62  int
      63  main(void)
      64  {
      65  	skip_if_unavailable("/proc/self/fd/");
      66  
      67  	int fd = create_nl_socket(NETLINK_SOCK_DIAG);
      68  	const unsigned int hdrlen = sizeof(struct inet_diag_req);
      69  	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4);
      70  
      71  	static char pattern[4096];
      72  	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
      73  
      74  	char nla_type_str[256];
      75  	sprintf(nla_type_str, "%#x /* INET_DIAG_REQ_??? */",
      76  		INET_DIAG_REQ_PROTOCOL + 1);
      77  	TEST_NLATTR_(fd, nlh0, hdrlen,
      78  		     init_inet_diag_req, print_inet_diag_req,
      79  		     INET_DIAG_REQ_PROTOCOL + 1, nla_type_str,
      80  		     4, pattern, 4,
      81  		     print_quoted_hex(pattern, 4));
      82  
      83  	puts("+++ exited with 0 +++");
      84  	return 0;
      85  }