Add an interactive mode in lookip tool, demonstrate lasting connections
[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, int loop)
79 {
80 lookip_response_t resp;
81 char *label;
82 int res;
83
84 do
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_NOT_FOUND:
106 label = "not found:";
107 break;
108 case LOOKIP_NOTIFY_UP:
109 label = "up:";
110 break;
111 case LOOKIP_NOTIFY_DOWN:
112 label = "down:";
113 break;
114 default:
115 fprintf(stderr, "received invalid message type: %d\n", resp.type);
116 return 1;
117 }
118 resp.vip[sizeof(resp.vip) - 1] = '\0';
119 resp.ip[sizeof(resp.ip) - 1] = '\0';
120 resp.id[sizeof(resp.id) - 1] = '\0';
121 resp.name[sizeof(resp.name) - 1] = '\0';
122
123 printf("%-12s %16s %16s %20s %s\n",
124 label, resp.vip, resp.ip, resp.name, resp.id);
125 }
126 while (loop);
127
128 return 0;
129 }
130
131 /**
132 * Interactive IP lookup shell
133 */
134 static int interactive(int fd)
135 {
136 printf("Enter IP address or 'quit'\n");
137
138 while (1)
139 {
140 char line[64], *pos;
141 int res;
142
143 printf("> ");
144 fflush(stdout);
145
146 if (fgets(line, sizeof(line), stdin))
147 {
148 pos = strchr(line, '\n');
149 if (pos)
150 {
151 *pos = '\0';
152 }
153 if (strlen(line) == 0)
154 {
155 continue;
156 }
157 if (strcmp(line, "quit") == 0)
158 {
159 return send_request(fd, LOOKIP_END, NULL);
160 }
161 res = send_request(fd, LOOKIP_LOOKUP, line);
162 if (res != 0)
163 {
164 return res;
165 }
166 res = receive(fd, 1, 0);
167 if (res != 0)
168 {
169 return res;
170 }
171 }
172 }
173 }
174
175 /**
176 * Print usage information
177 */
178 static void usage(char *cmd)
179 {
180 fprintf(stderr, "Usage:\n");
181 fprintf(stderr, " %s --help\n", cmd);
182 fprintf(stderr, " %s --dump\n", cmd);
183 fprintf(stderr, " %s --lookup <IP>\n", cmd);
184 fprintf(stderr, " %s --listen-up\n", cmd);
185 fprintf(stderr, " %s --listen-down\n", cmd);
186 fprintf(stderr, "Any combination of options is allowed.\n", cmd);
187 }
188
189 int main(int argc, char *argv[])
190 {
191 int fd, res = 0, end = 0;
192 struct option long_opts[] = {
193 { "help", no_argument, NULL, 'h' },
194 { "dump", no_argument, NULL, 'd' },
195 { "lookup", required_argument, NULL, 'l' },
196 { "listen-up", no_argument, NULL, 'u' },
197 { "listen-down", no_argument, NULL, 'c' },
198 { 0,0,0,0 }
199 };
200
201 fd = make_connection();
202 if (fd == -1)
203 {
204 return 1;
205 }
206
207 if (argc == 1)
208 {
209 res = interactive(fd);
210 close(fd);
211 return res;
212 }
213
214 while (res == 0)
215 {
216 switch (getopt_long(argc, argv, "", long_opts, NULL))
217 {
218 case EOF:
219 end = 1;
220 break;
221 case 'h':
222 usage(argv[0]);
223 break;
224 case 'd':
225 res = send_request(fd, LOOKIP_DUMP, NULL);
226 break;
227 case 'l':
228 res = send_request(fd, LOOKIP_LOOKUP, optarg);
229 break;
230 case 'u':
231 res = send_request(fd, LOOKIP_REGISTER_UP, NULL);
232 break;
233 case 'c':
234 res = send_request(fd, LOOKIP_REGISTER_DOWN, NULL);
235 break;
236 default:
237 usage(argv[0]);
238 res = 1;
239 break;
240 }
241 if (end)
242 {
243 break;
244 }
245 if (res == 0)
246 { /* read all currently available results */
247 res = receive(fd, 0, 1);
248 }
249 }
250 if (res == 0)
251 {
252 /* send close message */
253 send_request(fd, LOOKIP_END, NULL);
254 /* read until socket gets closed */
255 res = receive(fd, 1, 1);
256 }
257 close(fd);
258
259 return res;
260 }