summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/error_notify/error_notify.c
diff options
context:
space:
mode:
authorYves-Alexis Perez <corsac@debian.org>2013-08-25 15:37:27 +0200
committerYves-Alexis Perez <corsac@debian.org>2013-08-25 15:37:27 +0200
commitc7307e752d8f47c68f834e22ee2ce0a14a70e695 (patch)
treefbb442a20ab54aad511b46a070e65b8d09c22791 /src/libcharon/plugins/error_notify/error_notify.c
parentf74c6d77c3efb529e7403eeef0613c061eb895b3 (diff)
parent6b99c8d9cff7b3e8ae8f3204b99e7ea40f791349 (diff)
downloadvyos-strongswan-c7307e752d8f47c68f834e22ee2ce0a14a70e695.tar.gz
vyos-strongswan-c7307e752d8f47c68f834e22ee2ce0a14a70e695.zip
Merge tag 'upstream/5.1.0'
Upstream version 5.1.0
Diffstat (limited to 'src/libcharon/plugins/error_notify/error_notify.c')
-rw-r--r--src/libcharon/plugins/error_notify/error_notify.c79
1 files changed, 61 insertions, 18 deletions
diff --git a/src/libcharon/plugins/error_notify/error_notify.c b/src/libcharon/plugins/error_notify/error_notify.c
index fec35a45d..e68f8a4a5 100644
--- a/src/libcharon/plugins/error_notify/error_notify.c
+++ b/src/libcharon/plugins/error_notify/error_notify.c
@@ -16,46 +16,89 @@
#include "error_notify_msg.h"
#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
+#include <arpa/inet.h>
/**
- * Example of a simple notification listener
+ * Connect to the daemon, return FD
*/
-int main(int argc, char *argv[])
+static int make_connection()
{
- struct sockaddr_un addr;
- error_notify_msg_t msg;
- int s;
+ union {
+ struct sockaddr_un un;
+ struct sockaddr_in in;
+ struct sockaddr sa;
+ } addr;
+ int fd, len;
- addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, ERROR_NOTIFY_SOCKET);
+ if (getenv("TCP_PORT"))
+ {
+ addr.in.sin_family = AF_INET;
+ addr.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ addr.in.sin_port = htons(atoi(getenv("TCP_PORT")));
+ len = sizeof(addr.in);
+ }
+ else
+ {
+ addr.un.sun_family = AF_UNIX;
+ strcpy(addr.un.sun_path, ERROR_NOTIFY_SOCKET);
- s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
- if (s < 0)
+ len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.un.sun_path);
+ }
+ fd = socket(addr.sa.sa_family, SOCK_STREAM, 0);
+ if (fd < 0)
{
fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
- return 1;
+ return -1;
+ }
+ if (connect(fd, &addr.sa, len) < 0)
+ {
+ fprintf(stderr, "connecting failed: %s\n", strerror(errno));
+ close(fd);
+ return -1;
}
- if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
+ return fd;
+}
+
+/**
+ * Example of a simple notification listener
+ */
+int main(int argc, char *argv[])
+{
+ error_notify_msg_t msg;
+ int s, len, total;
+ void *pos;
+
+ s = make_connection();
+ if (s < 0)
{
- fprintf(stderr, "connect failed: %s\n", strerror(errno));
- close(s);
return 1;
}
while (1)
{
- if (read(s, &msg, sizeof(msg)) != sizeof(msg))
+ total = 0;
+ pos = &msg;
+
+ while (total < sizeof(msg))
{
- fprintf(stderr, "read failed: %s\n", strerror(errno));
- close(s);
- return 1;
+ len = read(s, pos, sizeof(msg) - total);
+ if (len < 0)
+ {
+ fprintf(stderr, "read failed: %s\n", strerror(errno));
+ close(s);
+ return 1;
+ }
+ total += len;
+ pos += len;
}
printf("%d %s %s %s %s\n",
- msg.type, msg.name, msg.id, msg.ip, msg.str);
+ ntohl(msg.type), msg.name, msg.id, msg.ip, msg.str);
}
close(s);
return 0;