Debian Codegraph @

ax25rtd/ax25rtctl.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1996 Joerg Reuter ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <time.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/un.h>
31
32 #include <config.h>
33 #include "../pathnames.h"
34
35 static const struct option lopts[] = {
36 {"add", 1, NULL, 'a'},
37 {"del", 1, NULL, 'd'},
38 {"list", 1, NULL, 'l'},
39 {"expire", 1, NULL, 'e'},
40 {"save", 0, NULL, 's'},
41 {"reload", 0, NULL, 'r'},
42 {"shutdown", 0, NULL, 'q'},
43 {"Version", 0, NULL, 'V'},
44 {"help", 0, NULL, 'h'},
45 {"debug", 0, NULL, 'x'},
46 {"version", 0, NULL, 'v'},
47 {NULL, 0, NULL, 0}
48 };
49
50 static const char *sopts = "a:d:l:e:srqvVh";
51
52 static void usage(void)
53 {
54 fprintf(stderr, "usage:\n");
55 fprintf(stderr,
56 "ax25rtctl -a|--add ax25 <callsign> <dev> <time> [digipeater]\n");
57 fprintf(stderr,
58 " -a|--add ip <ip> <dev> <time> <call> <ipmode>\n");
59 fprintf(stderr, " -d|--del ax25 <callsign> <dev>\n");
60 fprintf(stderr, " -d|--del ip <ip>\n");
61 fprintf(stderr, " -l|--list ax25|ip\n");
62 fprintf(stderr, " -e|--expire <minutes>\n");
63 fprintf(stderr, " -s|--save\n");
64 fprintf(stderr, " -r|--reload\n");
65 fprintf(stderr, " -q|--shutdown\n");
66 fprintf(stderr, " -V|--Version\n");
67 fprintf(stderr, " -h|--help\n");
68 fprintf(stderr, " -v|--version\n");
69 exit(1);
70 }
71
72 static int open_socket(void)
73 {
74 int sock, addrlen;
75 struct sockaddr_un addr;
76
77 sock = socket(AF_UNIX, SOCK_STREAM, 0);
78 if (sock < 0) {
79 perror("ax25rtctl socket");
80 exit(1);
81 }
82
83 addr.sun_family = AF_UNIX;
84 strcpy(addr.sun_path, DATA_AX25ROUTED_CTL_SOCK);
85 addrlen =
86 strlen(DATA_AX25ROUTED_CTL_SOCK) + sizeof(addr.sun_family);
87
88 if (connect(sock, (struct sockaddr *) &addr, addrlen) < 0) {
89 perror("ax25rtctl connect");
90 exit(1);
91 }
92
93 return sock;
94 }
95
96 static int wsock(int sock, char *s)
97 {
98 return write(sock, s, strlen(s));
99 }
100
101 static char *get_next_arg(char **p)
102 {
103 char *p2;
104
105 if (p == NULL || *p == NULL)
106 return NULL;
107
108 p2 = *p;
109 for (; *p2 && *p2 == ' '; p2++);
110 if (!*p2)
111 return NULL;
112
113 *p = strchr(p2, ' ');
114 if (*p != NULL) {
115 **p = '\0';
116 (*p)++;
117 }
118
119 return p2;
120 }
121
122 static void list_ax25(void)
123 {
124 int sock, len, offs;
125 char buf[512], *b, *s, *digi, *call, *dev, *ct;
126 time_t t;
127
128 sock = open_socket();
129
130 wsock(sock, "list ax25\n");
131
132 offs = 0;
133
134 printf("Callsign Port Last update Path\n");
135
136 /*
137 DB0PRA-15 scc3 Tue Aug 6 16:35:38 1996
138 */
139
140 while (1) {
141 len = read(sock, buf + offs, sizeof(buf) - offs - 1);
142 if (len <= 0) {
143 close(sock);
144 return;
145 }
146
147 buf[len + offs] = '\0';
148 s = buf;
149 for (s = buf; (b = strchr(s, '\n')) != NULL; s = b + 1) {
150 *b = '\0';
151
152 if (s[0] == '.') {
153 close(sock);
154 return;
155 }
156
157 call = get_next_arg(&s);
158 dev = get_next_arg(&s);
159 t = strtol(get_next_arg(&s), NULL, 16);
160
161 if (t == 0) {
162 ct = "(permanent)";
163 ct = strdup(ct);
164 } else {
165 ct = strdup(ctime(&t));
166 ct[strlen(ct) - 6] = '\0';
167 }
168
169 printf("%-9s %-6s %s", call, dev, ct);
170
171 free(ct);
172
173 while ((digi = get_next_arg(&s)) != NULL)
174 printf(" %s", digi);
175
176 printf("\n");
177 }
178
179 if (b == NULL && s != NULL) {
180 offs = strlen(s);
181 if (offs)
182 memcpy(buf, s, offs);
183 }
184 }
185
186 close(sock);
187 }
188
189 static void list_ip(void)
190 {
191 int sock, len, offs;
192 char buf[512], *b, *s, *ip, *call, *dev, *ct, *mode;
193 time_t t;
194
195 sock = open_socket();
196
197 wsock(sock, "list ip\n");
198
199 offs = 0;
200 printf("IP Address Port Callsign Mode Last update\n");
201
202 /*
203 255.255.255.255 scc3 DB0PRA-15 v Thu Jan 7 06:54:19 1971
204 */
205 while (1) {
206 len = read(sock, buf + offs, sizeof(buf) - offs - 1);
207 if (len <= 0) {
208 close(sock);
209 return;
210 }
211
212 buf[len + offs] = '\0';
213 s = buf;
214 for (s = buf; (b = strchr(s, '\n')) != NULL; s = b + 1) {
215 *b = '\0';
216
217 if (s[0] == '.') {
218 close(sock);
219 return;
220 }
221
222 ip = get_next_arg(&s);
223 dev = get_next_arg(&s);
224 t = strtol(get_next_arg(&s), NULL, 16);
225 call = get_next_arg(&s);
226 mode = get_next_arg(&s);
227
228 if (t == 0) {
229 ct = "(permanent)";
230 } else {
231 ct = ctime(&t);
232 ct[strlen(ct) - 6] = '\0';
233 }
234
235 printf("%-15s %-6s %-9s %-4s %s\n", ip, dev, call,
236 mode, ct);
237 }
238
239 if (b == NULL && s != NULL) {
240 offs = strlen(s);
241 if (offs)
242 memcpy(buf, s, offs);
243 }
244 }
245
246 close(sock);
247 }
248
249 static void Version(void)
250 {
251 int sock;
252 char buf[256];
253
254 printf("ax25rtctl version " VERSION "\n");
255 sock = open_socket();
256 wsock(sock, "version\n");
257 read(sock, buf, sizeof(buf));
258 printf("%s", buf);
259 close(sock);
260 }
261
262 static void debug(void)
263 {
264 int sock, n, k;
265 unsigned char buf[256];
266 fd_set read_fds, write_fds;
267 struct timeval tv;
268
269 sock = open_socket();
270
271 while (1) {
272 FD_ZERO(&read_fds);
273 FD_SET(0, &read_fds);
274 FD_SET(sock, &read_fds);
275
276 FD_ZERO(&write_fds);
277 FD_SET(sock, &write_fds);
278
279 tv.tv_sec = 0;
280 tv.tv_usec = 0;
281 if (select(sock + 1, &read_fds, NULL, &write_fds, &tv) < 0) {
282 perror("socket gone");
283 exit(1);
284 }
285
286 if (FD_ISSET(sock, &write_fds))
287 exit(0);
288
289 if (FD_ISSET(0, &read_fds)) {
290 n = read(0, buf, sizeof(buf));
291 if (n) {
292 k = write(sock, buf, n);
293 if (k <= 0)
294 exit(0);
295 }
296 }
297
298 if (FD_ISSET(sock, &read_fds)) {
299 n = read(sock, buf, sizeof(buf));
300 if (n)
301 write(0, buf, n);
302 }
303 }
304 }
305
306 int main(int argc, char **argv)
307 {
308 int sock, cmd, k, len;
309 int opt_ind = 0;
310 char buf[256];
311 long when;
312
313 cmd = getopt_long(argc, argv, sopts, lopts, &opt_ind);
314 switch (cmd) {
315 case 'a':
316 if (!strcmp(optarg, "ax25")) {
317 if (argc < optind + 3)
318 usage();
319
320 len = sprintf(buf, "add ax25");
321 for (k = optind; k < argc; k++)
322 len += sprintf(buf + len, " %s", argv[k]);
323 sprintf(buf + len, "\n");
324
325 sock = open_socket();
326 wsock(sock, buf);
327 close(sock);
328 } else if (!strcmp(optarg, "ip")) {
329 if (argc < optind + 5)
330 usage();
331
332 len = sprintf(buf, "add ip");
333 for (k = optind; k < argc; k++)
334 len += sprintf(buf + len, " %s", argv[k]);
335 sprintf(buf + len, "\n");
336
337 sock = open_socket();
338 wsock(sock, buf);
339 close(sock);
340 } else
341 usage();
342 return 0;
343 case 'd':
344 if (!strcmp(optarg, "ax25")) {
345 if (argc < optind + 2)
346 usage();
347
348 sprintf(buf, "del ax25 %s %s\n", argv[optind],
349 argv[optind + 1]);
350
351 sock = open_socket();
352 wsock(sock, buf);
353 close(sock);
354 } else if (!strcmp(optarg, "ip")) {
355 if (argc < optind + 1)
356 usage();
357
358 sprintf(buf, "del ip %s\n", argv[optind]);
359
360 sock = open_socket();
361 wsock(sock, buf);
362 close(sock);
363 } else
364 usage();
365 return 0;
366 case 'l':
367 if (!strcmp(optarg, "ax25"))
368 list_ax25();
369 else if (!strcmp(optarg, "ip"))
370 list_ip();
371 else
372 usage();
373 return 0;
374 case 'e':
375 when = atoi(optarg);
376 if (when <= 0)
377 usage();
378 sock = open_socket();
379 sprintf(buf, "expire %ld\n", when);
380 wsock(sock, buf);
381 close(sock);
382 return 0;
383 case 's':
384 sock = open_socket();
385 wsock(sock, "save\n");
386 close(sock);
387 return 0;
388 case 'r':
389 sock = open_socket();
390 wsock(sock, "reload\n");
391 close(sock);
392 return 0;
393 case 'q':
394 sock = open_socket();
395 wsock(sock, "shutdown\n");
396 close(sock);
397 return 0;
398 case 'V':
399 Version();
400 return 0;
401 case 'v':
402 printf("ax25rtctl: %s\n", VERSION);
403 return 0;
404 case 'x':
405 debug();
406 case ':':
407 case 'h':
408 case '?':
409 default:
410 usage();
411 }
412 usage();
413 return 1;
414 }