Add an error-notify sample application to listen to error notifications
[strongswan.git] / src / libcharon / plugins / error_notify / error_notify.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 * Copyright (C) 2012 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "error_notify_msg.h"
17
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <errno.h>
24
25 /**
26 * Example of a simple notification listener
27 */
28 int main(int argc, char *argv[])
29 {
30 struct sockaddr_un addr;
31 error_notify_msg_t msg;
32 int s;
33
34 addr.sun_family = AF_UNIX;
35 strcpy(addr.sun_path, ERROR_NOTIFY_SOCKET);
36
37 s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
38 if (s < 0)
39 {
40 fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
41 return 1;
42 }
43 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
44 {
45 fprintf(stderr, "connect failed: %s\n", strerror(errno));
46 close(s);
47 return 1;
48 }
49 while (1)
50 {
51 if (read(s, &msg, sizeof(msg)) != sizeof(msg))
52 {
53 fprintf(stderr, "read failed: %s\n", strerror(errno));
54 close(s);
55 return 1;
56 }
57 printf("%d %s %s %s %s\n",
58 msg.type, msg.name, msg.id, msg.ip, msg.str);
59 }
60 close(s);
61 return 0;
62 }