]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/duplicheck/duplicheck.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libcharon / plugins / duplicheck / duplicheck.c
1 /*
2 * Copyright (C) 2011 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 <sys/socket.h>
18 #include <sys/un.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27
28 #include "duplicheck_msg.h"
29
30 /**
31 * Connect to the daemon, return FD
32 */
33 static int make_connection()
34 {
35 union {
36 struct sockaddr_un un;
37 struct sockaddr_in in;
38 struct sockaddr sa;
39 } addr;
40 int fd, len;
41
42 if (getenv("TCP_PORT"))
43 {
44 addr.in.sin_family = AF_INET;
45 addr.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
46 addr.in.sin_port = htons(atoi(getenv("TCP_PORT")));
47 len = sizeof(addr.in);
48 }
49 else
50 {
51 addr.un.sun_family = AF_UNIX;
52 strcpy(addr.un.sun_path, DUPLICHECK_SOCKET);
53
54 len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.un.sun_path);
55 }
56 fd = socket(addr.sa.sa_family, SOCK_STREAM, 0);
57 if (fd < 0)
58 {
59 fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
60 return -1;
61 }
62 if (connect(fd, &addr.sa, len) < 0)
63 {
64 fprintf(stderr, "connecting failed: %s\n", strerror(errno));
65 close(fd);
66 return -1;
67 }
68 return fd;
69 }
70
71 int main(int argc, char *argv[])
72 {
73 char buf[128];
74 int fd, len;
75 uint16_t msglen;
76
77 fd = make_connection();
78 if (fd < 0)
79 {
80 return 1;
81 }
82 while (1)
83 {
84 len = recv(fd, &msglen, sizeof(msglen), 0);
85 if (len != sizeof(msglen))
86 {
87 break;
88 }
89 msglen = ntohs(msglen);
90 while (msglen)
91 {
92 if (sizeof(buf) > msglen)
93 {
94 len = msglen;
95 }
96 else
97 {
98 len = sizeof(buf);
99 }
100 len = recv(fd, &buf, len, 0);
101 if (len < 0)
102 {
103 break;
104 }
105 msglen -= len;
106 printf("%.*s", len, buf);
107 }
108 printf("\n");
109 if (len < 0)
110 {
111 break;
112 }
113 }
114 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
115 close(fd);
116 return 1;
117 }