1/******************************************************************************
2			S O C K E V _ C L I . C
3Copyright (c) 2013, The Linux Foundation. All rights reserved.
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7	* Redistributions of source code must retain the above copyright
8	  notice, this list of conditions and the following disclaimer.
9	* Redistributions in binary form must reproduce the above
10	  copyright notice, this list of conditions and the following
11	  disclaimer in the documentation and/or other materials provided
12	  with the distribution.
13	* Neither the name of The Linux Foundation nor the names of its
14	  contributors may be used to endorse or promote products derived
15	  from this software without specific prior written permission.
16THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27******************************************************************************/
28
29/******************************************************************************
30  @file    sockev_cli.c
31  @brief   command line test utility to receive sockev netlink messages.
32******************************************************************************/
33
34#include <sys/socket.h>
35#include <linux/netlink.h>
36#include <linux/sockev.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <arpa/inet.h>
40#include <unistd.h>
41#include <string.h>
42
43#define SOCKEVCLI_ERROR -1
44
45int main(void)
46{
47	int skfd, rc;
48	socklen_t addrlen;
49	struct sockaddr_nl my_addr, src_addr;
50	struct nlmsghdr *nlh = NULL;
51	struct sknlsockevmsg *msg;
52
53	nlh = (struct nlmsghdr *)
54		malloc(NLMSG_SPACE(sizeof(struct sknlsockevmsg) + 16));
55	if (!nlh) {
56		fprintf(stderr, "malloc() failed\n");
57		return SOCKEVCLI_ERROR;
58	}
59
60	skfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCKEV);
61	if (skfd < 0) {
62		fprintf(stderr, "nl_open_sock: socket failed\n");
63		return SOCKEVCLI_ERROR;
64	}
65
66	memset(&my_addr, 0, sizeof(struct sockaddr_nl));
67
68	my_addr.nl_family = AF_NETLINK;
69	my_addr.nl_pid = getpid();
70	my_addr.nl_groups = SKNLGRP_SOCKEV;
71
72	rc = bind(skfd, (struct sockaddr *)&my_addr,
73		  sizeof(struct sockaddr_nl));
74	if (rc < 0) {
75		fprintf(stderr, "nl_open_sock: bind failed\n");
76		close(skfd);
77		return SOCKEVCLI_ERROR;
78	}
79
80	while (1) {
81		recvfrom(skfd, nlh, sizeof(struct sknlsockevmsg) + 16, 0,
82			 (struct sockaddr *)&src_addr, &addrlen);
83		msg = NLMSG_DATA(nlh);
84		printf("----------------------------\n");
85		printf("pid:\t%d\n", msg->pid);
86		printf("event:\t%s\n", msg->event);
87		printf("skfamily:\t0x%04X\n", msg->skfamily);
88		printf("skstate:\t%03d\n", msg->skstate);
89		printf("skprotocol:\t%03d\n", msg->skprotocol);
90		printf("sktype:\t0x%04X\n", msg->sktype);
91		printf("skflags:\t0x%016llX\n", msg->skflags);
92	}
93
94	return 0;
95}
96
97