|
|
1.1 root 1: /*
2: * Copyright (c) 1983, 1988 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted provided
6: * that: (1) source distributions retain this entire copyright notice and
7: * comment, and (2) distributions including binaries display the following
8: * acknowledgement: ``This product includes software developed by the
9: * University of California, Berkeley and its contributors'' in the
10: * documentation or other materials provided with the distribution and in
11: * all advertising materials mentioning features or use of this software.
12: * Neither the name of the University nor the names of its contributors may
13: * be used to endorse or promote products derived from this software without
14: * specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: char copyright[] =
22: "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
23: All rights reserved.\n";
24: #endif /* not lint */
25:
26: #ifndef lint
27: static char sccsid[] = "@(#)implog.c 5.13 (Berkeley) 6/1/90";
28: #endif /* not lint */
29:
30: #include <sys/param.h>
31: #include <sys/time.h>
32: #include <sys/signal.h>
33: #include <sys/file.h>
34: #include <sys/stat.h>
35: #include <sys/socket.h>
36:
37: #include <net/if.h>
38:
39: #include <netinet/in.h>
40: #define IMPMESSAGES
41: #define IMPLEADERS
42: #include <netimp/if_imp.h>
43:
44: #include <sgtty.h>
45: #include <stdio.h>
46: #include "pathnames.h"
47:
48: u_char buf[1024];
49: int showdata = 1;
50: int showcontents = 0;
51: int rawheader = 0;
52: int follow = 0;
53: int skip = 0;
54: int link = -1;
55: int host = -1;
56: int imp = -1;
57: int packettype = -1;
58: extern int errno;
59: int log;
60:
61: /*
62: * Socket address, internet style, with
63: * unused space taken by timestamp and packet
64: * size.
65: */
66: struct sockstamp {
67: short sin_family;
68: u_short sin_port;
69: struct in_addr sin_addr;
70: time_t sin_time;
71: int sin_cc;
72: };
73: struct sockstamp from;
74:
75: main(argc, argv)
76: int argc;
77: char **argv;
78: {
79: extern int errno, optind;
80: extern char *optarg;
81: struct stat b;
82: off_t size, lseek();
83: char *logfile, *strerror();
84: int ch;
85: long hostfrom, impfrom;
86:
87: while ((ch = getopt(argc, argv, "DFLcfh:i:l:rt:")) != EOF)
88: switch(ch) {
89: case 'D':
90: showdata = 0;
91: break;
92: case 'F':
93: skip++;
94: /* FALLTHROUGH */
95: case 'f':
96: follow++;
97: break;
98: case 'L':
99: link = IMPLINK_IP;
100: break;
101: case 'c':
102: showcontents++;
103: break;
104: case 'h':
105: host = atoi(optarg);
106: break;
107: case 'i':
108: imp = atoi(optarg);
109: break;
110: case 'l':
111: link = atoi(optarg);
112: break;
113: case 'r':
114: rawheader++;
115: break;
116: case 't':
117: packettype = atoi(optarg);
118: break;
119: case '?':
120: default:
121: fprintf(stderr,
122: "usage: implog [-DFLcfr] [-h host] [-i imp] [-l link] [-t type] [logfile]\n");
123: exit(2);
124: }
125: argc -= optind;
126: argv += optind;
127:
128: logfile = argc ? *argv : _PATH_IMPLOG;
129: log = open(logfile, O_RDONLY, 0);
130: if (log < 0 || fstat(log, &b)) {
131: fprintf(stderr, "implog: %s: %s\n", logfile, strerror(errno));
132: exit(1);
133: }
134: size = b.st_size;
135: if (skip)
136: (void)lseek(log, size, L_SET);
137: again:
138: while (read(log, (char *)&from, sizeof(from)) == sizeof(from)) {
139: if (from.sin_family == 0) {
140: printf("restarted: %.24s\n", ctime(&from.sin_time));
141: continue;
142: }
143: if (host >= 0 || imp >= 0) {
144: long addr = ntohl(from.sin_addr.s_addr);
145:
146: if (IN_CLASSA(addr)) {
147: hostfrom = ((addr>>16) & 0xFF);
148: impfrom = addr & 0xFF;
149: } else if (IN_CLASSB(addr)) {
150: hostfrom = ((addr>>8) & 0xFF);
151: impfrom = addr & 0xFF;
152: } else {
153: hostfrom = ((addr>>4) & 0xF);
154: impfrom = addr & 0xF;
155: }
156: }
157: if (host >= 0 && hostfrom != host) {
158: (void)lseek(log, (long)from.sin_cc, L_INCR);
159: continue;
160: }
161: if (imp >= 0 && impfrom != imp) {
162: (void)lseek(log, (long)from.sin_cc, L_INCR);
163: continue;
164: }
165: process(log, &from);
166: }
167: while (follow) {
168: (void)fflush(stdout);
169: (void)sleep(5);
170: (void)fstat(log, &b);
171: if (b.st_size > size) {
172: size = b.st_size;
173: goto again;
174: }
175: }
176: }
177:
178: int impdata(), impbadleader(), impdown(), impnoop();
179: int imprfnm(), impincomplete(), imphostdead(), imphostunreach();
180: int impbaddata(), impreset(), impretry(), impnotify(), imptrying();
181: int impready(), impundef();
182:
183: struct messages {
184: u_char m_type; /* type of message */
185: int (*m_func)(); /* routine to process message */
186: } mtypes[] = {
187: { IMPTYPE_DATA, impdata },
188: { IMPTYPE_BADLEADER, impbadleader },
189: { IMPTYPE_DOWN, impdown },
190: { IMPTYPE_NOOP, impnoop },
191: { IMPTYPE_RFNM, imprfnm },
192: { IMPTYPE_INCOMPLETE, impincomplete },
193: { IMPTYPE_HOSTDEAD, imphostdead },
194: { IMPTYPE_HOSTUNREACH, imphostunreach },
195: { IMPTYPE_BADDATA, impbaddata },
196: { IMPTYPE_RESET, impreset },
197: { IMPTYPE_RETRY, impretry },
198: { IMPTYPE_NOTIFY, impnotify },
199: { IMPTYPE_TRYING, imptrying },
200: { IMPTYPE_READY, impready },
201: { -1, impundef }
202: };
203:
204: /*
205: * Print a packet.
206: */
207: process(l, f)
208: int l;
209: struct sockstamp *f;
210: {
211: register struct messages *mp;
212: struct imp_leader *ip;
213: int (*fn)();
214:
215: if (read(l, (char *)buf, f->sin_cc) != f->sin_cc) {
216: perror("implog: read");
217: return;
218: }
219: ip = (struct imp_leader *)buf;
220: ip->il_imp = ntohs(ip->il_imp);
221: if (ip->il_format != IMP_NFF)
222: fn = impundef;
223: else {
224: for (mp = mtypes; mp->m_type != (u_char)-1; mp++)
225: if (mp->m_type == ip->il_mtype)
226: break;
227: fn = mp->m_func;
228: }
229: if (ip->il_mtype == IMPTYPE_DATA) {
230: if (link >= 0 && ip->il_link != link)
231: return;
232: if (!showdata)
233: return;
234: }
235: if (packettype >= 0 && ip->il_mtype != packettype)
236: return;
237: printf("%.24s: ", ctime(&f->sin_time));
238: if (f->sin_cc < sizeof(struct control_leader))
239: printf("(truncated header, %d bytes): ", f->sin_cc);
240: (*fn)(ip, f->sin_cc);
241: if (rawheader && fn != impundef) {
242: putchar('\t');
243: impundef(ip, f->sin_cc);
244: }
245: }
246:
247: impdata(ip, cc)
248: register struct imp_leader *ip;
249: int cc;
250: {
251: printf("<DATA, source=%d/%u, link=", ip->il_host, (u_short)ip->il_imp);
252: if (ip->il_link == IMPLINK_IP)
253: printf("ip,");
254: else
255: printf("%d,", ip->il_link);
256: printf(" len=%u bytes>\n", ntohs((u_short)ip->il_length) >> 3);
257: if (showcontents) {
258: register u_char *cp = ((u_char *)ip) + sizeof(*ip);
259: register int i;
260:
261: i = (ntohs(ip->il_length) >> 3) - sizeof(struct imp_leader);
262: cc = MIN(i, cc);
263: printf("data: (%d bytes)", cc);
264: for (i = 0; i < cc; i++, cp++) {
265: if (i % 25 == 0)
266: printf("\n");
267: printf("%02x ", *cp);
268: }
269: putchar('\n');
270: }
271: }
272:
273: char *badleader[] = {
274: "error flip-flop set",
275: "message < 80 bits",
276: "illegal type field",
277: "opposite leader type"
278: };
279:
280: /* ARGSUSED */
281: impbadleader(ip, cc)
282: register struct imp_leader *ip;
283: int cc;
284: {
285: printf("bad leader: ");
286: if (ip->il_subtype > IMPLEADER_OPPOSITE)
287: printf("%x\n", ip->il_subtype);
288: else
289: printf("%s\n", badleader[ip->il_subtype]);
290: }
291:
292: /* ARGSUSED */
293: impdown(ip, cc)
294: register struct imp_leader *ip;
295: int cc;
296: {
297: int tdown, tbackup;
298:
299: printf("imp going down %s", impmessage[ip->il_link & IMP_DMASK]);
300: tdown = ((ip->il_link >> IMPDOWN_WHENSHIFT) & IMPDOWN_WHENMASK) *
301: IMPDOWN_WHENUNIT;
302: if ((ip->il_link & IMP_DMASK) != IMPDOWN_GOING)
303: printf(" in %d minutes", tdown);
304: tbackup = ip->il_subtype * IMPDOWN_WHENUNIT;
305: printf(": back up ");
306: if (tbackup)
307: printf("%d minutes\n", tbackup);
308: else
309: printf("immediately\n");
310: }
311:
312: /* ARGSUSED */
313: impnoop(ip, cc)
314: register struct imp_leader *ip;
315: int cc;
316: {
317: printf("noop: host %d, imp %u\n", ip->il_host, ip->il_imp);
318: }
319:
320: /* ARGSUSED */
321: imprfnm(ip, cc)
322: register struct imp_leader *ip;
323: int cc;
324: {
325: printf("rfnm: htype=%x, source=%d/%u, link=",
326: ip->il_htype, ip->il_host, ip->il_imp);
327: if (ip->il_link == IMPLINK_IP)
328: printf("ip,");
329: else
330: printf("%d,", ip->il_link);
331: printf(" subtype=%x\n", ip->il_subtype);
332: }
333:
334: char *hostdead[] = {
335: "#0",
336: "ready-line negated",
337: "tardy receiving messages",
338: "ncc doesn't know host",
339: "imp software won't allow messages",
340: "host down for scheduled pm",
341: "host down for hardware work",
342: "host down for software work",
343: "host down for emergency restart",
344: "host down because of power outage",
345: "host stopped at a breakpoint",
346: "host down due to hardware failure",
347: "host not scheduled to be up",
348: "#13",
349: "#14",
350: "host in the process of coming up"
351: };
352:
353: /* ARGSUSED */
354: imphostdead(ip, cc)
355: register struct imp_leader *ip;
356: int cc;
357: {
358: printf("host %u/%u dead: ", ip->il_host, ip->il_imp);
359: if (ip->il_link & IMP_DMASK)
360: printf("down %s, ", impmessage[ip->il_link & IMP_DMASK]);
361: if (ip->il_subtype <= IMPHOST_COMINGUP)
362: printf("%s\n", hostdead[ip->il_subtype]);
363: else
364: printf("subtype=%x\n", ip->il_subtype);
365: }
366:
367: char *hostunreach[] = {
368: "destination imp can't be reached",
369: "destination host isn't up",
370: "host doesn't support long leader",
371: "communication is prohibited"
372: };
373:
374: /* ARGSUSED */
375: imphostunreach(ip, cc)
376: register struct imp_leader *ip;
377: int cc;
378: {
379: printf("host %u/%u unreachable: ", ip->il_host, ip->il_imp);
380: if (ip->il_subtype <= IMPREACH_PROHIBITED)
381: printf("%s\n", hostunreach[ip->il_subtype]);
382: else
383: printf("subtype=%x\n", ip->il_subtype);
384: }
385:
386: /* ARGSUSED */
387: impbaddata(ip, cc)
388: register struct imp_leader *ip;
389: int cc;
390: {
391: printf("error in data: htype=%x, source=%u/%u, link=",
392: ip->il_htype, ip->il_host, ip->il_imp);
393: if (ip->il_link == IMPLINK_IP)
394: printf("ip, ");
395: else
396: printf("%d, ", ip->il_link);
397: printf("subtype=%x\n", ip->il_subtype);
398: }
399:
400: char *incomplete[] = {
401: "host didn't take data fast enough",
402: "message was too long",
403: "message transmission time > 15 seconds",
404: "imp/circuit failure",
405: "no resources within 15 seconds",
406: "source imp i/o failure during receipt"
407: };
408:
409: /* ARGSUSED */
410: impincomplete(ip, cc)
411: register struct imp_leader *ip;
412: int cc;
413: {
414: printf("incomplete: htype=%x, source=%u/%u, link=",
415: ip->il_htype, ip->il_host, ip->il_imp);
416: if (ip->il_link == IMPLINK_IP)
417: printf("ip,");
418: else
419: printf("%d,", ip->il_link);
420: if (ip->il_subtype <= IMPCOMPLETE_IMPIO)
421: printf(" %s\n", incomplete[ip->il_subtype]);
422: else
423: printf(" subtype=%x\n", ip->il_subtype);
424: }
425:
426: /* ARGSUSED */
427: impreset(ip, cc)
428: struct imp_leader *ip;
429: int cc;
430: {
431: printf("reset complete\n");
432: }
433:
434: char *retry[] = {
435: "imp buffer wasn't available",
436: "connection block unavailable"
437: };
438:
439: /* ARGSUSED */
440: impretry(ip, cc)
441: register struct imp_leader *ip;
442: int cc;
443: {
444: printf("refused, try again: ");
445: if (ip->il_subtype <= IMPRETRY_BLOCK)
446: printf("%s\n", retry[ip->il_subtype]);
447: else
448: printf("subtype=%x\n", ip->il_subtype);
449: }
450:
451: char *notify[] = {
452: "#0",
453: "#1",
454: "connection not available",
455: "reassembly space not available at destination",
456: "message number not available",
457: "transaction block for message not available"
458: };
459:
460: /* ARGSUSED */
461: impnotify(ip, cc)
462: register struct imp_leader *ip;
463: int cc;
464: {
465: printf("refused, will notify: ");
466: if (ip->il_subtype <= 5)
467: printf("%s\n", notify[ip->il_subtype]);
468: else
469: printf("subtype=%x\n", ip->il_subtype);
470: }
471:
472: /* ARGSUSED */
473: imptrying(ip, cc)
474: struct imp_leader *ip;
475: int cc;
476: {
477: printf("refused, still trying\n");
478: }
479:
480: /* ARGSUSED */
481: impready(ip, cc)
482: struct imp_leader *ip;
483: int cc;
484: {
485: printf("ready\n");
486: }
487:
488: /* ARGSUSED */
489: impundef(ip, cc)
490: register struct imp_leader *ip;
491: int cc;
492: {
493: printf("<fmt=%x, net=%x, flags=%x, mtype=", ip->il_format,
494: ip->il_network, ip->il_flags);
495: printf("%x, htype=%x,\n\t host=%d(x%x), imp=%u(x%x), link=",
496: ip->il_mtype, ip->il_htype, ip->il_host, ip->il_host,
497: ip->il_imp, ip->il_imp);
498: if (ip->il_link == IMPLINK_IP)
499: printf("ip,");
500: else
501: printf("%d (x%x),", ip->il_link, ip->il_link);
502: printf(" subtype=%x", ip->il_subtype);
503: if (cc >= sizeof(struct imp_leader) && ip->il_length)
504: printf(" len=%u bytes", ntohs((u_short)ip->il_length) >> 3);
505: printf(">\n");
506: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.