Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) gmail.com>
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. The name of the author may not be used to endorse or promote products
     14  *    derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "tests.h"
     29 #include <stdio.h>
     30 #include <string.h>
     31 #include <stdint.h>
     32 #include <unistd.h>
     33 #include <sys/socket.h>
     34 #include "netlink.h"
     35 #include <linux/xfrm.h>
     36 
     37 static void
     38 test_nlmsg_type(const int fd)
     39 {
     40 	long rc;
     41 	struct nlmsghdr nlh = {
     42 		.nlmsg_len = sizeof(nlh),
     43 		.nlmsg_type = XFRM_MSG_NEWSA,
     44 		.nlmsg_flags = NLM_F_REQUEST,
     45 	};
     46 
     47 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
     48 	printf("sendto(%d, {len=%u, type=XFRM_MSG_NEWSA"
     49 	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
     50 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
     51 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
     52 }
     53 
     54 static void
     55 test_nlmsg_flags(const int fd)
     56 {
     57 	long rc;
     58 	struct nlmsghdr nlh = {
     59 		.nlmsg_len = sizeof(nlh),
     60 	};
     61 
     62 	nlh.nlmsg_type = XFRM_MSG_GETSA;
     63 	nlh.nlmsg_flags = NLM_F_DUMP;
     64 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
     65 	printf("sendto(%d, {len=%u, type=XFRM_MSG_GETSA"
     66 	       ", flags=NLM_F_DUMP, seq=0, pid=0}"
     67 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
     68 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
     69 
     70 	nlh.nlmsg_type = XFRM_MSG_NEWSA;
     71 	nlh.nlmsg_flags = NLM_F_REPLACE;
     72 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
     73 	printf("sendto(%d, {len=%u, type=XFRM_MSG_NEWSA"
     74 	       ", flags=NLM_F_REPLACE, seq=0, pid=0}"
     75 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
     76 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
     77 
     78 	nlh.nlmsg_type = XFRM_MSG_DELSA;
     79 	nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_NONREC;
     80 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
     81 	printf("sendto(%d, {len=%u, type=XFRM_MSG_DELSA"
     82 	       ", flags=NLM_F_ECHO|NLM_F_NONREC, seq=0, pid=0}"
     83 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
     84 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
     85 
     86 	nlh.nlmsg_type = XFRM_MSG_ALLOCSPI;
     87 	nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_REPLACE;
     88 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
     89 	printf("sendto(%d, {len=%u, type=XFRM_MSG_ALLOCSPI"
     90 	       ", flags=NLM_F_ECHO|%#x, seq=0, pid=0}"
     91 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
     92 	       fd, nlh.nlmsg_len, NLM_F_REPLACE,
     93 	       (unsigned) sizeof(nlh), sprintrc(rc));
     94 }
     95 
     96 int main(void)
     97 {
     98 	skip_if_unavailable("/proc/self/fd/");
     99 
    100 	int fd = create_nl_socket(NETLINK_XFRM);
    101 
    102 	test_nlmsg_type(fd);
    103 	test_nlmsg_flags(fd);
    104 
    105 	printf("+++ exited with 0 +++\n");
    106 
    107 	return 0;
    108 }
    109