Debian Codegraph @

ax25/mheard.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8
9 #include <config.h>
10 #include <scm-version.h>
11
12 #include <netax25/ax25.h>
13 #include <netrose/rose.h>
14
15 #include <netax25/axlib.h>
16 #include <netax25/mheard.h>
17
18 #include "../pathnames.h"
19
20 struct PortRecord {
21 struct mheard_struct entry;
22 struct PortRecord *Next;
23 };
24
25 static char *types[] = {
26 "SABM",
27 "SABME",
28 "DISC",
29 "UA",
30 "DM",
31 "RR",
32 "RNR",
33 "REJ",
34 "FRMR",
35 "I",
36 "UI",
37 "????"};
38
39 static struct PortRecord *PortList;
40
41 static void PrintHeader(int data)
42 {
43 switch (data) {
44 case 0:
45 printf("Callsign Port Packets Last Heard\n");
46 break;
47 case 1:
48 printf("Callsign Port\n");
49 break;
50 case 2:
51 printf("Callsign Port #I #S #U First Heard Last Heard\n");
52 break;
53 case 3:
54 printf("Callsign Port Packets Type PIDs\n");
55 break;
56 case 4:
57 printf("Callsign Port #I #S #U First Heard Last Heard Packets Type PIDs Targets\n");
58 break;
59 }
60 }
61
62 static void PrintPortEntry(struct PortRecord *pr, int data)
63 {
64 char lh[20], fh[20], *call, *s;
65 char buffer[80];
66 int i;
67 struct tm *loc;
68
69
70 /* port record data can be garbled. We cannot trust the data.
71 time operations may returns NULL,
72 also assure that pr->entry.type is < sizeof(types), and in 'case 4:',
73 only up to 8 digipeaters are copied.
74 */
75
76 switch (data) {
77 case 0:
78 if (!pr->entry.count)
79 return;
80 if (pr->entry.last_heard < 0)
81 return;
82 if ((loc = localtime(&pr->entry.last_heard)) == NULL ||
83 strftime(lh, sizeof(lh),"%Y-%m-%d %H:%M:%S", loc) == 0)
84 return;
85 call = ax25_ntoa(&pr->entry.from_call);
86 s = strstr(call, "-0");
87 if (s != NULL)
88 *s = '\0';
89 printf("%-9s %-5s %8u %s\n",
90 call, pr->entry.portname, pr->entry.count, lh);
91 break;
92 case 1:
93 buffer[0] = '\0';
94 call = ax25_ntoa(&pr->entry.from_call);
95 s = strstr(call, "-0");
96 if (s != NULL)
97 *s = '\0';
98 strcat(buffer, call);
99 call = ax25_ntoa(&pr->entry.to_call);
100 s = strstr(call, "-0");
101 if (s != NULL)
102 *s = '\0';
103 strcat(buffer, ">");
104 strcat(buffer, call);
105 for (i = 0; i < pr->entry.ndigis && i < 4; i++) {
106 strcat(buffer, ",");
107 call = ax25_ntoa(&pr->entry.digis[i]);
108 s = strstr(call, "-0");
109 if (s != NULL)
110 *s = '\0';
111 strcat(buffer, call);
112 }
113 if (pr->entry.ndigis >= 4)
114 strcat(buffer, ",...");
115 printf("%-70s %-5s\n",
116 buffer, pr->entry.portname);
117 break;
118 case 2:
119 if (pr->entry.last_heard < 0 || pr->entry.first_heard < 0)
120 return;
121 if ((loc = localtime(&pr->entry.last_heard)) == NULL ||
122 strftime(lh, sizeof(lh),"%Y-%m-%d %H:%M:%S", loc) == 0)
123 return;
124 if ((loc = localtime(&pr->entry.first_heard)) == NULL ||
125 strftime(fh, sizeof(fh),"%Y-%m-%d %H:%M:%S", loc) == 0)
126 return;
127 call = ax25_ntoa(&pr->entry.from_call);
128 s = strstr(call, "-0");
129 if (s != NULL)
130 *s = '\0';
131 printf("%-9s %-5s %8u %8u %8u %s %s\n",
132 call, pr->entry.portname, pr->entry.iframes, pr->entry.sframes, pr->entry.uframes, fh, lh);
133 break;
134 case 3:
135 if (!pr->entry.count)
136 return;
137 call = ax25_ntoa(&pr->entry.from_call);
138 s = strstr(call, "-0");
139 if (s != NULL)
140 *s = '\0';
141 if (pr->entry.type > sizeof(types))
142 return;
143 printf("%-9s %-5s %8u %5s ",
144 call, pr->entry.portname, pr->entry.count, types[pr->entry.type]);
145 if (pr->entry.mode & MHEARD_MODE_ARP)
146 printf(" ARP");
147 if (pr->entry.mode & MHEARD_MODE_FLEXNET)
148 printf(" FlexNet");
149 if (pr->entry.mode & MHEARD_MODE_IP_DG)
150 printf(" IP-DG");
151 if (pr->entry.mode & MHEARD_MODE_IP_VC)
152 printf(" IP-VC");
153 if (pr->entry.mode & MHEARD_MODE_NETROM)
154 printf(" NET/ROM");
155 if (pr->entry.mode & MHEARD_MODE_ROSE)
156 printf(" Rose");
157 if (pr->entry.mode & MHEARD_MODE_SEGMENT)
158 printf(" Segment");
159 if (pr->entry.mode & MHEARD_MODE_TEXNET)
160 printf(" TexNet");
161 if (pr->entry.mode & MHEARD_MODE_TEXT)
162 printf(" Text");
163 if (pr->entry.mode & MHEARD_MODE_PSATFT)
164 printf(" PacsatFT");
165 if (pr->entry.mode & MHEARD_MODE_PSATPB)
166 printf(" PacsatPB");
167 if (pr->entry.mode & MHEARD_MODE_UNKNOWN)
168 printf(" Unknown");
169 printf("\n");
170 break;
171 case 4:
172 if (!pr->entry.count)
173 return;
174 if (pr->entry.type > sizeof(types))
175 return;
176 if (pr->entry.last_heard < 0 || pr->entry.first_heard < 0)
177 return;
178 if ((loc = localtime(&pr->entry.last_heard)) == NULL ||
179 strftime(lh, sizeof(lh),"%Y-%m-%d %H:%M:%S", loc) == 0)
180 return;
181 if ((loc = localtime(&pr->entry.first_heard)) == NULL ||
182 strftime(fh, sizeof(fh),"%Y-%m-%d %H:%M:%S", loc) == 0)
183 return;
184 call = ax25_ntoa(&pr->entry.from_call);
185 s = strstr(call, "-0");
186 if (s != NULL)
187 *s = '\0';
188 printf("%-9s %-5s %8u %8u %8u %s %s %8u %5s ",
189 call, pr->entry.portname, pr->entry.iframes, pr->entry.sframes, pr->entry.uframes, fh, lh, pr->entry.count, types[pr->entry.type]);
190 if (pr->entry.mode & MHEARD_MODE_ARP)
191 printf(" ARP ");
192 if (pr->entry.mode & MHEARD_MODE_FLEXNET)
193 printf(" FlexNet ");
194 if (pr->entry.mode & MHEARD_MODE_IP_DG)
195 printf(" IP-DG ");
196 if (pr->entry.mode & MHEARD_MODE_IP_VC)
197 printf(" IP-VC ");
198 if (pr->entry.mode & MHEARD_MODE_NETROM)
199 printf(" NET/ROM ");
200 if (pr->entry.mode & MHEARD_MODE_ROSE)
201 printf(" Rose ");
202 if (pr->entry.mode & MHEARD_MODE_SEGMENT)
203 printf(" Segment ");
204 if (pr->entry.mode & MHEARD_MODE_TEXNET)
205 printf(" TexNet ");
206 if (pr->entry.mode & MHEARD_MODE_TEXT)
207 printf(" Text ");
208 if (pr->entry.mode & MHEARD_MODE_PSATFT)
209 printf(" PacsatFT ");
210 if (pr->entry.mode & MHEARD_MODE_PSATPB)
211 printf(" PacsatPB ");
212 if (pr->entry.mode & MHEARD_MODE_UNKNOWN)
213 printf(" Unknown ");
214 if (pr->entry.mode == 0)
215 printf(" ");
216
217 buffer[0] = '\0';
218 for (i = 0; i < pr->entry.ndigis && i < 8; i++) {
219 if (i)
220 strcat(buffer, ",");
221 call = ax25_ntoa(&pr->entry.digis[i]);
222 s = strstr(call, "-0");
223 if (s != NULL)
224 *s = '\0';
225 strcat(buffer, call);
226 }
227 printf("%s\n", buffer);
228 break;
229 }
230 }
231
232 static void ListAllPorts(int data)
233 {
234 struct PortRecord *pr;
235
236 for (pr = PortList; pr != NULL; pr = pr->Next)
237 PrintPortEntry(pr, data);
238 }
239
240 static void ListOnlyPort(char *name, int data)
241 {
242 struct PortRecord *pr;
243
244 for (pr = PortList; pr != NULL; pr = pr->Next)
245 if (strcmp(pr->entry.portname, name) == 0)
246 PrintPortEntry(pr, data);
247 }
248
249 static void LoadPortData(void)
250 {
251 FILE *fp;
252 struct PortRecord *pr;
253 struct mheard_struct mheard;
254
255 fp = fopen(DATA_MHEARD_FILE, "r");
256 if (fp == NULL) {
257 fprintf(stderr, "mheard: cannot open mheard data file\n");
258 exit(1);
259 }
260
261 while (fread(&mheard, sizeof(struct mheard_struct), 1, fp) == 1) {
262 pr = malloc(sizeof(struct PortRecord));
263 pr->entry = mheard;
264 /* assure 0-terminated portname in case the record is garbled */
265 pr->entry.portname[19] = '\0';
266 pr->Next = PortList;
267 PortList = pr;
268 }
269
270 fclose(fp);
271 }
272
273 static void SortByTime(void)
274 {
275 struct PortRecord *p = PortList;
276 struct PortRecord *n;
277 PortList = NULL;
278
279 while (p != NULL) {
280 struct PortRecord *w = PortList;
281
282 n = p->Next;
283
284 if (w == NULL || p->entry.last_heard > w->entry.last_heard) {
285 p->Next = w;
286 PortList = p;
287 p = n;
288 continue;
289 }
290
291 while (w->Next != NULL && p->entry.last_heard <= w->Next->entry.last_heard)
292 w = w->Next;
293
294 p->Next = w->Next;
295 w->Next = p;
296 p = n;
297 }
298 }
299
300
301 static void SortByPort(void)
302 {
303 struct PortRecord *p = PortList;
304 struct PortRecord *n;
305 PortList = NULL;
306
307 while (p != NULL) {
308 struct PortRecord *w = PortList;
309
310 n = p->Next;
311
312 if (w == NULL || strcmp(p->entry.portname, w->entry.portname) < 0) {
313 p->Next = w;
314 PortList = p;
315 p = n;
316 continue;
317 }
318
319 while (w->Next != NULL && strcmp(p->entry.portname, w->Next->entry.portname) >= 0)
320 w = w->Next;
321
322 p->Next = w->Next;
323 w->Next = p;
324 p = n;
325 }
326 }
327
328 static void SortByCall(void)
329 {
330 struct PortRecord *p = PortList;
331 struct PortRecord *n;
332 PortList = NULL;
333
334 while (p != NULL) {
335 struct PortRecord *w = PortList;
336
337 n = p->Next;
338
339 if (w == NULL || memcmp(&p->entry.from_call, &w->entry.from_call, sizeof(ax25_address)) < 0) {
340 p->Next = w;
341 PortList = p;
342 p = n;
343 continue;
344 }
345
346 while (w->Next != NULL && memcmp(&p->entry.from_call, &w->Next->entry.from_call, sizeof(ax25_address)) >= 0)
347 w = w->Next;
348
349 p->Next = w->Next;
350 w->Next = p;
351 p = n;
352 }
353 }
354
355 static void SortByFrame(void)
356 {
357 struct PortRecord *p = PortList;
358 struct PortRecord *n;
359 PortList = NULL;
360
361 while (p != NULL) {
362 struct PortRecord *w = PortList;
363
364 n = p->Next;
365
366 if (w == NULL || p->entry.count > w->entry.count) {
367 p->Next = w;
368 PortList = p;
369 p = n;
370 continue;
371 }
372
373 while (w->Next != NULL && p->entry.count <= w->Next->entry.count)
374 w = w->Next;
375
376 p->Next = w->Next;
377 w->Next = p;
378 p = n;
379 }
380 }
381
382 int main(int argc, char *argv[])
383 {
384 int headers = TRUE;
385 int mode = 0;
386 int data = 0;
387 int c;
388
389 while ((c = getopt(argc, argv, "d:no:v")) != -1) {
390 switch (c) {
391 case 'd':
392 switch (*optarg) {
393 case 'c':
394 data = 1;
395 break;
396 case 'm':
397 data = 3;
398 break;
399 case 'n':
400 data = 0;
401 break;
402 case 's':
403 data = 2;
404 break;
405 case 'a':
406 data = 4; /* s + c */
407 break;
408 default:
409 fprintf(stderr, "mheard: invalid display type '%s'\n", optarg);
410 return 1;
411 }
412 break;
413 case 'n':
414 headers = FALSE;
415 break;
416 case 'o':
417 switch (*optarg) {
418 case 'c':
419 mode = 2;
420 break;
421 case 'f':
422 mode = 3;
423 break;
424 case 'p':
425 mode = 1;
426 break;
427 case 't':
428 mode = 0;
429 break;
430 default:
431 fprintf(stderr, "mheard: invalid ordering type '%s'\n", optarg);
432 return 1;
433 }
434 break;
435 case 'v':
436 printf("mheard: %s\n", FULL_VER);
437 return 0;
438 case '?':
439 case ':':
440 fprintf(stderr, "Usage: %s [-d cmnsa] [-n] [-o cfpt] [-v] [port ...]\n", argv[0]);
441 return 1;
442 }
443 }
444
445 LoadPortData();
446
447 switch (mode) {
448 case 0: SortByTime(); break;
449 case 1: SortByPort(); break;
450 case 2: SortByCall(); break;
451 case 3: SortByFrame(); break;
452 }
453
454 if (argc == optind) {
455 if (headers)
456 PrintHeader(data);
457 ListAllPorts(data);
458 } else {
459 while (argv[optind] != NULL) {
460 if (headers) {
461 printf("Port %s:\n", argv[optind]);
462 PrintHeader(data);
463 }
464 ListOnlyPort(argv[optind], data);
465 optind++;
466 }
467 }
468
469 return 0;
470 }