Add a simple command line utility to query the lookip plugin
[strongswan.git] / src / libcharon / plugins / lookip / lookip.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 "lookip_msg.h"
17
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <unistd.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <getopt.h>
25
26 /**
27 * Connect to the daemon, return FD
28 */
29 static int make_connection()
30 {
31 struct sockaddr_un addr;
32 int fd;
33
34 addr.sun_family = AF_UNIX;
35 strcpy(addr.sun_path, LOOKIP_SOCKET);
36
37 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
38 if (fd < 0)
39 {
40 fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
41 return -1;
42 }
43 if (connect(fd, (struct sockaddr *)&addr,
44 offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path)) < 0)
45 {
46 fprintf(stderr, "connecting to %s failed: %s\n",
47 LOOKIP_SOCKET, strerror(errno));
48 close(fd);
49 return -1;
50 }
51 return fd;
52 }
53
54 /**
55 * Send a request message
56 */
57 static int send_request(int fd, int type, char *vip)
58 {
59 lookip_request_t req = {
60 .type = type,
61 };
62
63 if (vip)
64 {
65 snprintf(req.vip, sizeof(req.vip), "%s", vip);
66 }
67 if (send(fd, &req, sizeof(req), 0) != sizeof(req))
68 {
69 fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
70 return 2;
71 }
72 return 0;
73 }
74
75 /**
76 * Receive entries from fd. If block is != 0, the call blocks until closed
77 */
78 static int receive(int fd, int block)
79 {
80 lookip_response_t resp;
81 char *label;
82 int res;
83
84 for (;;)
85 {
86 res = recv(fd, &resp, sizeof(resp), block ? 0 : MSG_DONTWAIT);
87 if (res == 0)
88 { /* closed by server */
89 return 0;
90 }
91 if (res != sizeof(resp))
92 {
93 if (!block && (errno == EAGAIN || errno == EWOULDBLOCK))
94 { /* call would block, but we don't */
95 return 0;
96 }
97 fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
98 return 1;
99 }
100 switch (resp.type)
101 {
102 case LOOKIP_ENTRY:
103 label = "lookup:";
104 break;
105 case LOOKIP_NOTIFY_UP:
106 label = "up:";
107 break;
108 case LOOKIP_NOTIFY_DOWN:
109 label = "down:";
110 break;
111 default:
112 fprintf(stderr, "received invalid message type: %d\n", resp.type);
113 return 1;
114 }
115 resp.vip[sizeof(resp.vip) - 1] = '\0';
116 resp.ip[sizeof(resp.ip) - 1] = '\0';
117 resp.id[sizeof(resp.id) - 1] = '\0';
118 resp.name[sizeof(resp.name) - 1] = '\0';
119
120 printf("%-8s %16s %16s %20s %s\n",
121 label, resp.vip, resp.ip, resp.name, resp.id);
122 }
123 }
124
125 /**
126 * Print usage information
127 */
128 static void usage(char *cmd)
129 {
130 fprintf(stderr, "Usage:\n");
131 fprintf(stderr, " %s --help\n", cmd);
132 fprintf(stderr, " %s --dump\n", cmd);
133 fprintf(stderr, " %s --lookup <IP>\n", cmd);
134 fprintf(stderr, " %s --listen-up\n", cmd);
135 fprintf(stderr, " %s --listen-down\n", cmd);
136 fprintf(stderr, "Any combination of options is allowed.\n", cmd);
137 }
138
139 int main(int argc, char *argv[])
140 {
141 int fd, res = 0, end = 0;
142 struct option long_opts[] = {
143 { "help", no_argument, NULL, 'h' },
144 { "dump", no_argument, NULL, 'd' },
145 { "lookup", required_argument, NULL, 'l' },
146 { "listen-up", no_argument, NULL, 'u' },
147 { "listen-down", no_argument, NULL, 'c' },
148 { 0,0,0,0 }
149 };
150
151 if (argc == 1)
152 {
153 usage(argv[0]);
154 return 1;
155 }
156
157 fd = make_connection();
158 if (fd == -1)
159 {
160 return 1;
161 }
162
163 while (res == 0)
164 {
165 switch (getopt_long(argc, argv, "", long_opts, NULL))
166 {
167 case EOF:
168 end = 1;
169 break;
170 case 'h':
171 usage(argv[0]);
172 break;
173 case 'd':
174 res = send_request(fd, LOOKIP_DUMP, NULL);
175 break;
176 case 'l':
177 res = send_request(fd, LOOKIP_LOOKUP, optarg);
178 break;
179 case 'u':
180 res = send_request(fd, LOOKIP_REGISTER_UP, NULL);
181 break;
182 case 'c':
183 res = send_request(fd, LOOKIP_REGISTER_DOWN, NULL);
184 break;
185 default:
186 usage(argv[0]);
187 res = 1;
188 break;
189 }
190 if (end)
191 {
192 break;
193 }
194 if (res == 0)
195 { /* read all currently available results */
196 res = receive(fd, 0);
197 }
198 }
199 if (res == 0)
200 {
201 /* send close message */
202 send_request(fd, LOOKIP_END, NULL);
203 /* read until socket gets closed */
204 res = receive(fd, 1);
205 }
206 close(fd);
207
208 return res;
209 }