]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/error_notify/error_notify.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libcharon / plugins / error_notify / error_notify.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "error_notify_msg.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <errno.h>
27 #include <arpa/inet.h>
28
29 /**
30 * Connect to the daemon, return FD
31 */
32 static int make_connection()
33 {
34 union {
35 struct sockaddr_un un;
36 struct sockaddr_in in;
37 struct sockaddr sa;
38 } addr;
39 int fd, len;
40
41 if (getenv("TCP_PORT"))
42 {
43 addr.in.sin_family = AF_INET;
44 addr.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
45 addr.in.sin_port = htons(atoi(getenv("TCP_PORT")));
46 len = sizeof(addr.in);
47 }
48 else
49 {
50 addr.un.sun_family = AF_UNIX;
51 strcpy(addr.un.sun_path, ERROR_NOTIFY_SOCKET);
52
53 len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.un.sun_path);
54 }
55 fd = socket(addr.sa.sa_family, SOCK_STREAM, 0);
56 if (fd < 0)
57 {
58 fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
59 return -1;
60 }
61 if (connect(fd, &addr.sa, len) < 0)
62 {
63 fprintf(stderr, "connecting failed: %s\n", strerror(errno));
64 close(fd);
65 return -1;
66 }
67 return fd;
68 }
69
70 /**
71 * Example of a simple notification listener
72 */
73 int main(int argc, char *argv[])
74 {
75 error_notify_msg_t msg;
76 int s, len, total;
77 void *pos;
78
79 s = make_connection();
80 if (s < 0)
81 {
82 return 1;
83 }
84 while (1)
85 {
86 total = 0;
87 pos = &msg;
88
89 while (total < sizeof(msg))
90 {
91 len = read(s, pos, sizeof(msg) - total);
92 if (len < 0)
93 {
94 fprintf(stderr, "read failed: %s\n", strerror(errno));
95 close(s);
96 return 1;
97 }
98 total += len;
99 pos += len;
100 }
101 printf("%d %s %s %s %s\n",
102 ntohl(msg.type), msg.name, msg.id, msg.ip, msg.str);
103 }
104 close(s);
105 return 0;
106 }