]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/duplicheck/duplicheck.c
2 * Copyright (C) 2011 Martin Willi
4 * Copyright (C) secunet Security Networks AG
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>.
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
17 #include <sys/socket.h>
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
28 #include "duplicheck_msg.h"
31 * Connect to the daemon, return FD
33 static int make_connection()
36 struct sockaddr_un un
;
37 struct sockaddr_in in
;
42 if (getenv("TCP_PORT"))
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
);
51 addr
.un
.sun_family
= AF_UNIX
;
52 strcpy(addr
.un
.sun_path
, DUPLICHECK_SOCKET
);
54 len
= offsetof(struct sockaddr_un
, sun_path
) + strlen(addr
.un
.sun_path
);
56 fd
= socket(addr
.sa
.sa_family
, SOCK_STREAM
, 0);
59 fprintf(stderr
, "opening socket failed: %s\n", strerror(errno
));
62 if (connect(fd
, &addr
.sa
, len
) < 0)
64 fprintf(stderr
, "connecting failed: %s\n", strerror(errno
));
71 int main(int argc
, char *argv
[])
77 fd
= make_connection();
84 len
= recv(fd
, &msglen
, sizeof(msglen
), 0);
85 if (len
!= sizeof(msglen
))
89 msglen
= ntohs(msglen
);
92 if (sizeof(buf
) > msglen
)
100 len
= recv(fd
, &buf
, len
, 0);
106 printf("%.*s", len
, buf
);
114 fprintf(stderr
, "reading from socket failed: %s\n", strerror(errno
));