home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / implog / implog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-19  |  12.1 KB  |  521 lines

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