4637e7b0b1775e6640568e2165a2cbb559a816e9
[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_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 }
127
128 /**
129 * Print usage information
130 */
131 static void usage(char *cmd)
132 {
133 fprintf(stderr, "Usage:\n");
134 fprintf(stderr, " %s --help\n", cmd);
135 fprintf(stderr, " %s --dump\n", cmd);
136 fprintf(stderr, " %s --lookup <IP>\n", cmd);
137 fprintf(stderr, " %s --listen-up\n", cmd);
138 fprintf(stderr, " %s --listen-down\n", cmd);
139 fprintf(stderr, "Any combination of options is allowed.\n", cmd);
140 }
141
142 int main(int argc, char *argv[])
143 {
144 int fd, res = 0, end = 0;
145 struct option long_opts[] = {
146 { "help", no_argument, NULL, 'h' },
147 { "dump", no_argument, NULL, 'd' },
148 { "lookup", required_argument, NULL, 'l' },
149 { "listen-up", no_argument, NULL, 'u' },
150 { "listen-down", no_argument, NULL, 'c' },
151 { 0,0,0,0 }
152 };
153
154 if (argc == 1)
155 {
156 usage(argv[0]);
157 return 1;
158 }
159
160 fd = make_connection();
161 if (fd == -1)
162 {
163 return 1;
164 }
165
166 while (res == 0)
167 {
168 switch (getopt_long(argc, argv, "", long_opts, NULL))
169 {
170 case EOF:
171 end = 1;
172 break;
173 case 'h':
174 usage(argv[0]);
175 break;
176 case 'd':
177 res = send_request(fd, LOOKIP_DUMP, NULL);
178 break;
179 case 'l':
180 res = send_request(fd, LOOKIP_LOOKUP, optarg);
181 break;
182 case 'u':
183 res = send_request(fd, LOOKIP_REGISTER_UP, NULL);
184 break;
185 case 'c':
186 res = send_request(fd, LOOKIP_REGISTER_DOWN, NULL);
187 break;
188 default:
189 usage(argv[0]);
190 res = 1;
191 break;
192 }
193 if (end)
194 {
195 break;
196 }
197 if (res == 0)
198 { /* read all currently available results */
199 res = receive(fd, 0);
200 }
201 }
202 if (res == 0)
203 {
204 /* send close message */
205 send_request(fd, LOOKIP_END, NULL);
206 /* read until socket gets closed */
207 res = receive(fd, 1);
208 }
209 close(fd);
210
211 return res;
212 }