Home | History | Annotate | Line # | Download | only in mtrace
mtrace.c revision 1.3
      1  1.1  mycroft /*
      2  1.1  mycroft  * mtrace.c
      3  1.1  mycroft  *
      4  1.1  mycroft  * This tool traces the branch of a multicast tree from a source to a
      5  1.1  mycroft  * receiver for a particular multicast group and gives statistics
      6  1.1  mycroft  * about packet rate and loss for each hop along the path.  It can
      7  1.1  mycroft  * usually be invoked just as
      8  1.1  mycroft  *
      9  1.1  mycroft  * 	mtrace source
     10  1.1  mycroft  *
     11  1.1  mycroft  * to trace the route from that source to the local host for a default
     12  1.1  mycroft  * group when only the route is desired and not group-specific packet
     13  1.1  mycroft  * counts.  See the usage line for more complex forms.
     14  1.1  mycroft  *
     15  1.1  mycroft  *
     16  1.1  mycroft  * Released 4 Apr 1995.  This program was adapted by Steve Casner
     17  1.1  mycroft  * (USC/ISI) from a prototype written by Ajit Thyagarajan (UDel and
     18  1.1  mycroft  * Xerox PARC).  It attempts to parallel in command syntax and output
     19  1.1  mycroft  * format the unicast traceroute program written by Van Jacobson (LBL)
     20  1.1  mycroft  * for the parts where that makes sense.
     21  1.1  mycroft  *
     22  1.1  mycroft  * Copyright (c) 1995 by the University of Southern California
     23  1.1  mycroft  * All rights reserved.
     24  1.1  mycroft  *
     25  1.1  mycroft  * Permission to use, copy, modify, and distribute this software and its
     26  1.1  mycroft  * documentation in source and binary forms for non-commercial purposes
     27  1.1  mycroft  * and without fee is hereby granted, provided that the above copyright
     28  1.1  mycroft  * notice appear in all copies and that both the copyright notice and
     29  1.1  mycroft  * this permission notice appear in supporting documentation, and that
     30  1.1  mycroft  * any documentation, advertising materials, and other materials related
     31  1.1  mycroft  * to such distribution and use acknowledge that the software was
     32  1.1  mycroft  * developed by the University of Southern California, Information
     33  1.1  mycroft  * Sciences Institute.  The name of the University may not be used to
     34  1.1  mycroft  * endorse or promote products derived from this software without
     35  1.1  mycroft  * specific prior written permission.
     36  1.1  mycroft  *
     37  1.1  mycroft  * THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
     38  1.1  mycroft  * the suitability of this software for any purpose.  THIS SOFTWARE IS
     39  1.1  mycroft  * PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     40  1.1  mycroft  * INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     41  1.1  mycroft  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     42  1.1  mycroft  *
     43  1.1  mycroft  * Other copyrights might apply to parts of this software and are so
     44  1.1  mycroft  * noted when applicable.
     45  1.1  mycroft  *
     46  1.1  mycroft  * In particular, parts of the prototype version of this program may
     47  1.1  mycroft  * have been derived from mrouted programs sources covered by the
     48  1.1  mycroft  * license in the accompanying file named "LICENSE".
     49  1.1  mycroft  *
     50  1.3  thorpej  * $Id: mtrace.c,v 1.3 1995/06/02 01:02:12 thorpej Exp $
     51  1.1  mycroft  */
     52  1.1  mycroft 
     53  1.2  thorpej #include <sys/filio.h>
     54  1.1  mycroft #include <sys/time.h>
     55  1.2  thorpej #include <netinet/in.h>
     56  1.2  thorpej #include <arpa/inet.h>
     57  1.3  thorpej #include <ctype.h>
     58  1.1  mycroft #include <memory.h>
     59  1.2  thorpej #include <netdb.h>
     60  1.1  mycroft #include <string.h>
     61  1.2  thorpej #include <stdlib.h>
     62  1.2  thorpej #include <unistd.h>
     63  1.2  thorpej 
     64  1.2  thorpej extern int optind;
     65  1.2  thorpej extern char *optarg;
     66  1.2  thorpej 
     67  1.1  mycroft #include "defs.h"
     68  1.1  mycroft 
     69  1.1  mycroft #define DEFAULT_TIMEOUT	3	/* How long to wait before retrying requests */
     70  1.1  mycroft #define DEFAULT_RETRIES 3	/* How many times to try */
     71  1.1  mycroft #define MAXHOPS UNREACHABLE	/* Don't need more hops than max metric */
     72  1.1  mycroft #define UNICAST_TTL 255		/* TTL for unicast response */
     73  1.1  mycroft #define MULTICAST_TTL1 64	/* Default TTL for multicast query/response */
     74  1.1  mycroft #define MULTICAST_TTL_INC 32	/* TTL increment for increase after timeout */
     75  1.1  mycroft #define MULTICAST_TTL_MAX 192	/* Maximum TTL allowed (protect low-BW links */
     76  1.1  mycroft 
     77  1.1  mycroft struct resp_buf {
     78  1.1  mycroft     u_long qtime;		/* Time query was issued */
     79  1.1  mycroft     u_long rtime;		/* Time response was received */
     80  1.1  mycroft     int	len;			/* Number of reports or length of data */
     81  1.1  mycroft     struct igmp igmp;		/* IGMP header */
     82  1.1  mycroft     union {
     83  1.1  mycroft 	struct {
     84  1.1  mycroft 	    struct tr_query q;		/* Query/response header */
     85  1.1  mycroft 	    struct tr_resp r[MAXHOPS];	/* Per-hop reports */
     86  1.1  mycroft 	} t;
     87  1.1  mycroft 	char d[MAX_DVMRP_DATA_LEN];	/* Neighbor data */
     88  1.1  mycroft     } u;
     89  1.1  mycroft } base, incr[2];
     90  1.1  mycroft 
     91  1.1  mycroft #define qhdr u.t.q
     92  1.1  mycroft #define resps u.t.r
     93  1.1  mycroft #define ndata u.d
     94  1.1  mycroft 
     95  1.1  mycroft char names[MAXHOPS][40];
     96  1.1  mycroft 
     97  1.1  mycroft int timeout = DEFAULT_TIMEOUT;
     98  1.1  mycroft int nqueries = DEFAULT_RETRIES;
     99  1.1  mycroft int numeric = FALSE;
    100  1.1  mycroft int debug = 0;
    101  1.1  mycroft int passive = FALSE;
    102  1.1  mycroft int multicast = FALSE;
    103  1.1  mycroft 
    104  1.1  mycroft u_int32_t defgrp;			/* Default group if not specified */
    105  1.1  mycroft u_int32_t query_cast;			/* All routers multicast addr */
    106  1.1  mycroft u_int32_t resp_cast;			/* Mtrace response multicast addr */
    107  1.1  mycroft 
    108  1.1  mycroft u_int32_t lcl_addr = 0;			/* This host address, in NET order */
    109  1.1  mycroft u_int32_t dst_netmask;			/* netmask to go with qdst */
    110  1.1  mycroft 
    111  1.1  mycroft /*
    112  1.1  mycroft  * Query/response parameters, all initialized to zero and set later
    113  1.1  mycroft  * to default values or from options.
    114  1.1  mycroft  */
    115  1.1  mycroft u_int32_t qsrc = 0;
    116  1.1  mycroft u_int32_t qgrp = 0;
    117  1.1  mycroft u_int32_t qdst = 0;
    118  1.1  mycroft u_char qno  = 0;
    119  1.1  mycroft u_int32_t raddr = 0;
    120  1.1  mycroft int    qttl = 0;
    121  1.1  mycroft u_char rttl = 0;
    122  1.1  mycroft u_int32_t gwy = 0;
    123  1.1  mycroft 
    124  1.1  mycroft vifi_t  numvifs;		/* to keep loader happy */
    125  1.1  mycroft 				/* (see kern.c) */
    126  1.1  mycroft extern void k_join();
    127  1.1  mycroft extern void k_leave();
    128  1.1  mycroft extern void k_set_ttl();
    129  1.1  mycroft extern void exit();
    130  1.1  mycroft #ifndef SYSV
    131  1.1  mycroft extern long random();
    132  1.1  mycroft #endif
    133  1.1  mycroft extern int errno;
    134  1.1  mycroft 
    135  1.2  thorpej void
    136  1.2  thorpej usage()
    137  1.2  thorpej {
    138  1.2  thorpej 
    139  1.2  thorpej     printf("\
    140  1.2  thorpej Usage: mtrace [-Mlnps] [-w wait] [-m max_hops] [-q nqueries] [-g gateway]\n\
    141  1.2  thorpej               [-t ttl] [-r resp_dest] [-i if_addr] source [receiver] [group]\n");
    142  1.2  thorpej 	exit(1);
    143  1.2  thorpej }
    144  1.2  thorpej 
    145  1.1  mycroft 
    146  1.2  thorpej char *
    147  1.1  mycroft inet_name(addr)
    148  1.1  mycroft     u_int32_t  addr;
    149  1.1  mycroft {
    150  1.1  mycroft     struct hostent *e;
    151  1.1  mycroft 
    152  1.1  mycroft     e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
    153  1.1  mycroft 
    154  1.1  mycroft     return e ? e->h_name : "?";
    155  1.1  mycroft }
    156  1.1  mycroft 
    157  1.1  mycroft 
    158  1.1  mycroft u_int32_t
    159  1.1  mycroft host_addr(name)
    160  1.1  mycroft     char   *name;
    161  1.1  mycroft {
    162  1.2  thorpej     struct hostent *e;
    163  1.2  thorpej     struct in_addr ina;
    164  1.1  mycroft     int	i, dots = 3;
    165  1.1  mycroft     char	buf[40];
    166  1.1  mycroft     char	*ip = name;
    167  1.1  mycroft     char	*op = buf;
    168  1.1  mycroft 
    169  1.2  thorpej     /*
    170  1.2  thorpej      * Undo the BSD-ism `127.1' == `127.0.0.1'.  We change this to
    171  1.2  thorpej      * `127.1' == `127.1.0.0'.
    172  1.2  thorpej      */
    173  1.2  thorpej 
    174  1.2  thorpej     for (i = sizeof(buf) - 7; i > 0; --i) {
    175  1.2  thorpej         if (*ip == '.')
    176  1.2  thorpej             --dots;
    177  1.2  thorpej         if (*ip == '\0')
    178  1.2  thorpej             break;
    179  1.2  thorpej         *op++ = *ip++;
    180  1.2  thorpej     }
    181  1.2  thorpej     for (i = 0; i < dots; ++i) {
    182  1.2  thorpej         *op++ = '.';
    183  1.2  thorpej 	*op++ = '0';
    184  1.2  thorpej     }
    185  1.2  thorpej     *op = '\0';
    186  1.2  thorpej 
    187  1.2  thorpej     if (inet_aton(buf, &ina) == 0) {
    188  1.2  thorpej         if ((e = gethostbyname(name)) == NULL) {
    189  1.2  thorpej 	    ina.s_addr = 0;
    190  1.1  mycroft 	    printf("Could not parse %s as host name or address\n", name);
    191  1.2  thorpej         } else
    192  1.2  thorpej 	    memcpy((char *)&ina.s_addr, e->h_addr_list[0], e->h_length);
    193  1.1  mycroft     }
    194  1.2  thorpej 
    195  1.2  thorpej     return (ina.s_addr);
    196  1.1  mycroft }
    197  1.1  mycroft 
    198  1.1  mycroft 
    199  1.1  mycroft char *
    200  1.1  mycroft proto_type(type)
    201  1.1  mycroft     u_char type;
    202  1.1  mycroft {
    203  1.1  mycroft     static char buf[80];
    204  1.1  mycroft 
    205  1.1  mycroft     switch (type) {
    206  1.1  mycroft       case PROTO_DVMRP:
    207  1.1  mycroft 	return ("DVMRP");
    208  1.1  mycroft       case PROTO_MOSPF:
    209  1.1  mycroft 	return ("MOSPF");
    210  1.1  mycroft       case PROTO_PIM:
    211  1.1  mycroft 	return ("PIM");
    212  1.1  mycroft       case PROTO_CBT:
    213  1.1  mycroft 	return ("CBT");
    214  1.1  mycroft       default:
    215  1.1  mycroft 	(void) sprintf(buf, "Unknown protocol code %d", type);
    216  1.1  mycroft 	return (buf);
    217  1.1  mycroft     }
    218  1.1  mycroft }
    219  1.1  mycroft 
    220  1.1  mycroft 
    221  1.1  mycroft char *
    222  1.1  mycroft flag_type(type)
    223  1.1  mycroft     u_char type;
    224  1.1  mycroft {
    225  1.1  mycroft     static char buf[80];
    226  1.1  mycroft 
    227  1.1  mycroft     switch (type) {
    228  1.1  mycroft       case TR_NO_ERR:
    229  1.1  mycroft 	return ("");
    230  1.1  mycroft       case TR_WRONG_IF:
    231  1.1  mycroft 	return ("Wrong interface");
    232  1.1  mycroft       case TR_PRUNED:
    233  1.1  mycroft 	return ("Prune sent upstream");
    234  1.1  mycroft       case TR_OPRUNED:
    235  1.1  mycroft 	return ("Output pruned");
    236  1.1  mycroft       case TR_SCOPED:
    237  1.1  mycroft 	return ("Hit scope boundary");
    238  1.1  mycroft       case TR_NO_RTE:
    239  1.1  mycroft 	return ("No route");
    240  1.1  mycroft       case TR_OLD_ROUTER:
    241  1.1  mycroft 	return ("Next router no mtrace");
    242  1.1  mycroft       case TR_NO_FWD:
    243  1.1  mycroft 	return ("Not forwarding");
    244  1.1  mycroft       case TR_NO_SPACE:
    245  1.1  mycroft 	return ("No space in packet");
    246  1.1  mycroft       default:
    247  1.1  mycroft 	(void) sprintf(buf, "Unknown error code %d", type);
    248  1.1  mycroft 	return (buf);
    249  1.1  mycroft     }
    250  1.1  mycroft }
    251  1.1  mycroft 
    252  1.1  mycroft /*
    253  1.1  mycroft  * If destination is on a local net, get the netmask, else set the
    254  1.1  mycroft  * netmask to all ones.  There are two side effects: if the local
    255  1.1  mycroft  * address was not explicitly set, and if the destination is on a
    256  1.1  mycroft  * local net, use that one; in either case, verify that the local
    257  1.1  mycroft  * address is valid.
    258  1.1  mycroft  */
    259  1.1  mycroft 
    260  1.1  mycroft u_int32_t
    261  1.1  mycroft get_netmask(s, dst)
    262  1.1  mycroft     int s;
    263  1.1  mycroft     u_int32_t dst;
    264  1.1  mycroft {
    265  1.2  thorpej     char inbuf[8192];
    266  1.1  mycroft     struct ifconf ifc;
    267  1.1  mycroft     struct ifreq *ifr;
    268  1.2  thorpej     int i;
    269  1.1  mycroft     u_int32_t if_addr, if_mask;
    270  1.1  mycroft     u_int32_t retval = 0xFFFFFFFF;
    271  1.1  mycroft     int found = FALSE;
    272  1.1  mycroft 
    273  1.2  thorpej     ifc.ifc_len = sizeof(inbuf);
    274  1.2  thorpej     ifc.ifc_buf = inbuf;
    275  1.2  thorpej     if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
    276  1.1  mycroft 	perror("ioctl (SIOCGIFCONF)");
    277  1.1  mycroft 	return (retval);
    278  1.1  mycroft     }
    279  1.2  thorpej 
    280  1.2  thorpej     for (i = 0; i < ifc.ifc_len; ) {
    281  1.2  thorpej 	ifr = (struct ifreq *)((char *)ifc.ifc_req + i);
    282  1.2  thorpej 	i += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
    283  1.1  mycroft 	if_addr = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
    284  1.1  mycroft 	if (ioctl(s, SIOCGIFNETMASK, (char *)ifr) >= 0) {
    285  1.1  mycroft 	    if_mask = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
    286  1.1  mycroft 	    if ((dst & if_mask) == (if_addr & if_mask)) {
    287  1.1  mycroft 		retval = if_mask;
    288  1.2  thorpej 		if (lcl_addr == 0)
    289  1.2  thorpej 		    lcl_addr = if_addr;
    290  1.1  mycroft 	    }
    291  1.1  mycroft 	}
    292  1.2  thorpej 	if (lcl_addr == if_addr)
    293  1.2  thorpej 	    found = TRUE;
    294  1.1  mycroft     }
    295  1.1  mycroft     if (!found && lcl_addr != 0) {
    296  1.1  mycroft 	printf("Interface address is not valid\n");
    297  1.1  mycroft 	exit(1);
    298  1.1  mycroft     }
    299  1.1  mycroft     return (retval);
    300  1.1  mycroft }
    301  1.1  mycroft 
    302  1.1  mycroft 
    303  1.1  mycroft int
    304  1.1  mycroft get_ttl(buf)
    305  1.1  mycroft     struct resp_buf *buf;
    306  1.1  mycroft {
    307  1.1  mycroft     register rno;
    308  1.1  mycroft     register struct tr_resp *b;
    309  1.1  mycroft     register ttl;
    310  1.1  mycroft 
    311  1.1  mycroft     if (buf && (rno = buf->len) > 0) {
    312  1.1  mycroft 	b = buf->resps + rno - 1;
    313  1.1  mycroft 	ttl = b->tr_fttl;
    314  1.1  mycroft 
    315  1.1  mycroft 	while (--rno > 0) {
    316  1.1  mycroft 	    --b;
    317  1.2  thorpej 	    if (ttl < b->tr_fttl)
    318  1.2  thorpej 	        ttl = b->tr_fttl;
    319  1.2  thorpej 	    else
    320  1.2  thorpej 	        ++ttl;
    321  1.1  mycroft 	}
    322  1.1  mycroft 	ttl += MULTICAST_TTL_INC;
    323  1.2  thorpej 	if (ttl < MULTICAST_TTL1)
    324  1.2  thorpej 	    ttl = MULTICAST_TTL1;
    325  1.2  thorpej 	if (ttl > MULTICAST_TTL_MAX)
    326  1.2  thorpej 	    ttl = MULTICAST_TTL_MAX;
    327  1.1  mycroft 	return (ttl);
    328  1.2  thorpej     } else
    329  1.2  thorpej         return(MULTICAST_TTL1);
    330  1.1  mycroft }
    331  1.1  mycroft 
    332  1.1  mycroft /*
    333  1.1  mycroft  * Calculate the difference between two 32-bit NTP timestamps and return
    334  1.1  mycroft  * the result in milliseconds.
    335  1.1  mycroft  */
    336  1.1  mycroft int
    337  1.1  mycroft t_diff(a, b)
    338  1.1  mycroft     u_long a, b;
    339  1.1  mycroft {
    340  1.1  mycroft     int d = a - b;
    341  1.1  mycroft 
    342  1.1  mycroft     return ((d * 125) >> 13);
    343  1.1  mycroft }
    344  1.1  mycroft 
    345  1.1  mycroft /*
    346  1.1  mycroft  * Fixup for incorrect time format in 3.3 mrouted.
    347  1.1  mycroft  * This is possible because (JAN_1970 mod 64K) is quite close to 32K,
    348  1.1  mycroft  * so correct and incorrect times will be far apart.
    349  1.1  mycroft  */
    350  1.1  mycroft u_long
    351  1.1  mycroft fixtime(time)
    352  1.1  mycroft     u_long time;
    353  1.1  mycroft {
    354  1.1  mycroft     if (abs((int)(time-base.qtime)) > 0x3FFFFFFF)
    355  1.1  mycroft         time = ((time & 0xFFFF0000) + (JAN_1970 << 16)) +
    356  1.1  mycroft 	       ((time & 0xFFFF) << 14) / 15625;
    357  1.1  mycroft     return (time);
    358  1.1  mycroft }
    359  1.1  mycroft 
    360  1.1  mycroft int
    361  1.1  mycroft send_recv(dst, type, code, tries, save)
    362  1.1  mycroft     u_int32_t dst;
    363  1.1  mycroft     int type, code, tries;
    364  1.1  mycroft     struct resp_buf *save;
    365  1.1  mycroft {
    366  1.1  mycroft     fd_set  fds;
    367  1.1  mycroft     struct timeval tq, tr, tv;
    368  1.1  mycroft     struct ip *ip;
    369  1.1  mycroft     struct igmp *igmp;
    370  1.1  mycroft     struct tr_query *query, *rquery;
    371  1.1  mycroft     int ipdatalen, iphdrlen, igmpdatalen;
    372  1.1  mycroft     u_int32_t local, group;
    373  1.1  mycroft     int datalen;
    374  1.1  mycroft     int count, recvlen, dummy = 0;
    375  1.1  mycroft     int len;
    376  1.2  thorpej     int i;
    377  1.1  mycroft 
    378  1.1  mycroft     if (type == IGMP_MTRACE_QUERY) {
    379  1.1  mycroft 	group = qgrp;
    380  1.1  mycroft 	datalen = sizeof(struct tr_query);
    381  1.1  mycroft     } else {
    382  1.1  mycroft 	group = htonl(MROUTED_LEVEL);
    383  1.1  mycroft 	datalen = 0;
    384  1.1  mycroft     }
    385  1.2  thorpej     if (IN_MULTICAST(ntohl(dst)))
    386  1.2  thorpej         local = lcl_addr;
    387  1.2  thorpej     else
    388  1.2  thorpej         local = INADDR_ANY;
    389  1.1  mycroft 
    390  1.1  mycroft     /*
    391  1.1  mycroft      * If the reply address was not explictly specified, start off
    392  1.1  mycroft      * with the unicast address of this host.  Then, if there is no
    393  1.1  mycroft      * response after trying half the tries with unicast, switch to
    394  1.1  mycroft      * the standard multicast reply address.  If the TTL was also not
    395  1.1  mycroft      * specified, set a multicast TTL and if needed increase it for the
    396  1.1  mycroft      * last quarter of the tries.
    397  1.1  mycroft      */
    398  1.1  mycroft     query = (struct tr_query *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
    399  1.1  mycroft     query->tr_raddr = raddr ? raddr : multicast ? resp_cast : lcl_addr;
    400  1.1  mycroft     query->tr_rttl  = rttl ? rttl :
    401  1.1  mycroft       IN_MULTICAST(ntohl(query->tr_raddr)) ? get_ttl(save) : UNICAST_TTL;
    402  1.1  mycroft 
    403  1.1  mycroft     for (i = tries ; i > 0; --i) {
    404  1.1  mycroft 	if (tries == nqueries && raddr == 0) {
    405  1.1  mycroft 	    if (i == ((nqueries + 1) >> 1)) {
    406  1.1  mycroft 		query->tr_raddr = resp_cast;
    407  1.1  mycroft 		if (rttl == 0) query->tr_rttl = get_ttl(save);
    408  1.1  mycroft 	    }
    409  1.1  mycroft 	    if (i <= ((nqueries + 3) >> 2) && rttl == 0) {
    410  1.1  mycroft 		query->tr_rttl += MULTICAST_TTL_INC;
    411  1.1  mycroft 		if (query->tr_rttl > MULTICAST_TTL_MAX)
    412  1.1  mycroft 		  query->tr_rttl = MULTICAST_TTL_MAX;
    413  1.1  mycroft 	    }
    414  1.1  mycroft 	}
    415  1.1  mycroft 
    416  1.1  mycroft 	/*
    417  1.1  mycroft 	 * Change the qid for each request sent to avoid being confused
    418  1.1  mycroft 	 * by duplicate responses
    419  1.1  mycroft 	 */
    420  1.1  mycroft 	query->tr_qid  = ((u_int32_t)random() >> 8);
    421  1.1  mycroft 
    422  1.1  mycroft 	/*
    423  1.1  mycroft 	 * Set timer to calculate delays, then send query
    424  1.1  mycroft 	 */
    425  1.1  mycroft 	gettimeofday(&tq, 0);
    426  1.1  mycroft 	send_igmp(local, dst, type, code, group, datalen);
    427  1.1  mycroft 
    428  1.1  mycroft 	/*
    429  1.1  mycroft 	 * Wait for response, discarding false alarms
    430  1.1  mycroft 	 */
    431  1.1  mycroft 	while (TRUE) {
    432  1.1  mycroft 	    FD_ZERO(&fds);
    433  1.1  mycroft 	    FD_SET(igmp_socket, &fds);
    434  1.1  mycroft 	    gettimeofday(&tv, 0);
    435  1.1  mycroft 	    tv.tv_sec = tq.tv_sec + timeout - tv.tv_sec;
    436  1.1  mycroft 	    tv.tv_usec = tq.tv_usec - tv.tv_usec;
    437  1.2  thorpej 	    if (tv.tv_usec < 0)
    438  1.2  thorpej 	        tv.tv_usec += 1000000L, --tv.tv_sec;
    439  1.2  thorpej 	    if (tv.tv_sec < 0)
    440  1.2  thorpej 	        tv.tv_sec = tv.tv_usec = 0;
    441  1.1  mycroft 
    442  1.1  mycroft 	    count = select(igmp_socket + 1, &fds, (fd_set *)0, (fd_set *)0,
    443  1.1  mycroft 			   &tv);
    444  1.1  mycroft 
    445  1.1  mycroft 	    if (count < 0) {
    446  1.2  thorpej 		if (errno != EINTR)
    447  1.2  thorpej 		    perror("select");
    448  1.1  mycroft 		continue;
    449  1.1  mycroft 	    } else if (count == 0) {
    450  1.1  mycroft 		printf("* ");
    451  1.1  mycroft 		fflush(stdout);
    452  1.1  mycroft 		break;
    453  1.1  mycroft 	    }
    454  1.1  mycroft 
    455  1.1  mycroft 	    gettimeofday(&tr, 0);
    456  1.1  mycroft 	    recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
    457  1.1  mycroft 			       0, (struct sockaddr *)0, &dummy);
    458  1.1  mycroft 
    459  1.1  mycroft 	    if (recvlen <= 0) {
    460  1.2  thorpej 		if (recvlen && errno != EINTR)
    461  1.2  thorpej 		    perror("recvfrom");
    462  1.1  mycroft 		continue;
    463  1.1  mycroft 	    }
    464  1.1  mycroft 
    465  1.1  mycroft 	    if (recvlen < sizeof(struct ip)) {
    466  1.1  mycroft 		fprintf(stderr,
    467  1.1  mycroft 			"packet too short (%u bytes) for IP header", recvlen);
    468  1.1  mycroft 		continue;
    469  1.1  mycroft 	    }
    470  1.1  mycroft 	    ip = (struct ip *) recv_buf;
    471  1.1  mycroft 	    if (ip->ip_p == 0)	/* ignore cache creation requests */
    472  1.1  mycroft 		continue;
    473  1.1  mycroft 
    474  1.1  mycroft 	    iphdrlen = ip->ip_hl << 2;
    475  1.1  mycroft 	    ipdatalen = ip->ip_len;
    476  1.1  mycroft 	    if (iphdrlen + ipdatalen != recvlen) {
    477  1.1  mycroft 		fprintf(stderr,
    478  1.1  mycroft 			"packet shorter (%u bytes) than hdr+data len (%u+%u)\n",
    479  1.1  mycroft 			recvlen, iphdrlen, ipdatalen);
    480  1.1  mycroft 		continue;
    481  1.1  mycroft 	    }
    482  1.1  mycroft 
    483  1.1  mycroft 	    igmp = (struct igmp *) (recv_buf + iphdrlen);
    484  1.1  mycroft 	    igmpdatalen = ipdatalen - IGMP_MINLEN;
    485  1.1  mycroft 	    if (igmpdatalen < 0) {
    486  1.1  mycroft 		fprintf(stderr,
    487  1.1  mycroft 			"IP data field too short (%u bytes) for IGMP from %s\n",
    488  1.1  mycroft 			ipdatalen, inet_fmt(ip->ip_src.s_addr, s1));
    489  1.1  mycroft 		continue;
    490  1.1  mycroft 	    }
    491  1.1  mycroft 
    492  1.1  mycroft 	    switch (igmp->igmp_type) {
    493  1.1  mycroft 
    494  1.1  mycroft 	      case IGMP_DVMRP:
    495  1.2  thorpej 		if (igmp->igmp_code != DVMRP_NEIGHBORS2)
    496  1.2  thorpej 		    continue;
    497  1.2  thorpej 		if (ip->ip_src.s_addr != dst)
    498  1.2  thorpej 		    continue;
    499  1.1  mycroft 		len = igmpdatalen;
    500  1.1  mycroft 		break;
    501  1.1  mycroft 
    502  1.2  thorpej 	      case IGMP_MTRACE_QUERY:  /* For backward compatibility with 3.3 */
    503  1.1  mycroft 	      case IGMP_MTRACE_REPLY:
    504  1.2  thorpej 		if (igmpdatalen <= QLEN)
    505  1.2  thorpej 		    continue;
    506  1.1  mycroft 		if ((igmpdatalen - QLEN)%RLEN) {
    507  1.1  mycroft 		    printf("packet with incorrect datalen\n");
    508  1.1  mycroft 		    continue;
    509  1.1  mycroft 		}
    510  1.1  mycroft 
    511  1.1  mycroft 		/*
    512  1.1  mycroft 		 * Ignore responses that don't match query.
    513  1.1  mycroft 		 */
    514  1.1  mycroft 		rquery = (struct tr_query *)(igmp + 1);
    515  1.2  thorpej 		if (rquery->tr_qid != query->tr_qid)
    516  1.2  thorpej 		    continue;
    517  1.2  thorpej 		if (rquery->tr_src != qsrc)
    518  1.2  thorpej 		    continue;
    519  1.2  thorpej 		if (rquery->tr_dst != qdst)
    520  1.2  thorpej 		    continue;
    521  1.1  mycroft 		len = (igmpdatalen - QLEN)/RLEN;
    522  1.1  mycroft 
    523  1.1  mycroft 		/*
    524  1.1  mycroft 		 * Ignore trace queries passing through this node when
    525  1.1  mycroft 		 * mtrace is run on an mrouter that is in the path
    526  1.2  thorpej 		 * (needed only because IGMP_MTRACE is accepted above
    527  1.1  mycroft 		 * for backward compatibility with multicast release 3.3).
    528  1.1  mycroft 		 */
    529  1.1  mycroft 		if (igmp->igmp_type == IGMP_MTRACE_QUERY) {
    530  1.1  mycroft 		    struct tr_resp *r = (struct tr_resp *)(rquery+1) + len - 1;
    531  1.1  mycroft 		    u_int32_t smask;
    532  1.1  mycroft 
    533  1.1  mycroft 		    VAL_TO_MASK(smask, r->tr_smask);
    534  1.1  mycroft 		    if (len < code && (r->tr_inaddr & smask) != (qsrc & smask)
    535  1.1  mycroft 			&& r->tr_rmtaddr != 0 && !(r->tr_rflags & 0x80))
    536  1.1  mycroft 		      continue;
    537  1.1  mycroft 		}
    538  1.1  mycroft 
    539  1.1  mycroft 		/*
    540  1.1  mycroft 		 * A match, we'll keep this one.
    541  1.1  mycroft 		 */
    542  1.1  mycroft 		if (len > code) {
    543  1.1  mycroft 		    fprintf(stderr,
    544  1.1  mycroft 			    "Num hops received (%d) exceeds request (%d)\n",
    545  1.1  mycroft 			    len, code);
    546  1.1  mycroft 		}
    547  1.1  mycroft 		rquery->tr_raddr = query->tr_raddr;	/* Insure these are */
    548  1.1  mycroft 		rquery->tr_rttl = query->tr_rttl;	/* as we sent them */
    549  1.1  mycroft 		break;
    550  1.1  mycroft 
    551  1.1  mycroft 	      default:
    552  1.1  mycroft 		continue;
    553  1.1  mycroft 	    }
    554  1.1  mycroft 
    555  1.1  mycroft 	    /*
    556  1.1  mycroft 	     * Most of the sanity checking done at this point.
    557  1.1  mycroft 	     * Return this packet we have been waiting for.
    558  1.1  mycroft 	     */
    559  1.1  mycroft 	    if (save) {
    560  1.1  mycroft 		save->qtime = ((tq.tv_sec + JAN_1970) << 16) +
    561  1.1  mycroft 			      (tq.tv_usec << 10) / 15625;
    562  1.1  mycroft 		save->rtime = ((tr.tv_sec + JAN_1970) << 16) +
    563  1.1  mycroft 			      (tr.tv_usec << 10) / 15625;
    564  1.1  mycroft 		save->len = len;
    565  1.1  mycroft 		bcopy((char *)igmp, (char *)&save->igmp, ipdatalen);
    566  1.1  mycroft 	    }
    567  1.1  mycroft 	    return (recvlen);
    568  1.1  mycroft 	}
    569  1.1  mycroft     }
    570  1.1  mycroft     return (0);
    571  1.1  mycroft }
    572  1.1  mycroft 
    573  1.1  mycroft 
    574  1.1  mycroft char *
    575  1.1  mycroft print_host(addr)
    576  1.1  mycroft     u_int32_t addr;
    577  1.1  mycroft {
    578  1.1  mycroft     char *name;
    579  1.1  mycroft 
    580  1.1  mycroft     if (numeric) {
    581  1.1  mycroft 	printf("%s", inet_fmt(addr, s1));
    582  1.1  mycroft 	return ("");
    583  1.1  mycroft     }
    584  1.1  mycroft     name = inet_name(addr);
    585  1.1  mycroft     printf("%s (%s)", name, inet_fmt(addr, s1));
    586  1.1  mycroft     return (name);
    587  1.1  mycroft }
    588  1.1  mycroft 
    589  1.1  mycroft /*
    590  1.1  mycroft  * Print responses as received (reverse path from dst to src)
    591  1.1  mycroft  */
    592  1.1  mycroft void
    593  1.1  mycroft print_trace(index, buf)
    594  1.1  mycroft     int index;
    595  1.1  mycroft     struct resp_buf *buf;
    596  1.1  mycroft {
    597  1.1  mycroft     struct tr_resp *r;
    598  1.1  mycroft     char *name;
    599  1.1  mycroft     int i;
    600  1.1  mycroft 
    601  1.1  mycroft     i = abs(index);
    602  1.1  mycroft     r = buf->resps + i - 1;
    603  1.1  mycroft 
    604  1.1  mycroft     for (; i <= buf->len; ++i, ++r) {
    605  1.1  mycroft 	if (index > 0) printf("%3d  ", -i);
    606  1.1  mycroft 	name = print_host(r->tr_outaddr);
    607  1.1  mycroft 	printf("  %s  thresh^ %d  %d ms  %s\n", proto_type(r->tr_rproto),
    608  1.1  mycroft 	       r->tr_fttl, t_diff(fixtime(ntohl(r->tr_qarr)), buf->qtime),
    609  1.1  mycroft 	       flag_type(r->tr_rflags));
    610  1.1  mycroft 	memcpy(names[i-1], name, sizeof(names[0]) - 1);
    611  1.1  mycroft 	names[i-1][sizeof(names[0])-1] = '\0';
    612  1.1  mycroft     }
    613  1.1  mycroft }
    614  1.1  mycroft 
    615  1.1  mycroft /*
    616  1.1  mycroft  * See what kind of router is the next hop
    617  1.1  mycroft  */
    618  1.1  mycroft void
    619  1.1  mycroft what_kind(buf)
    620  1.1  mycroft     struct resp_buf *buf;
    621  1.1  mycroft {
    622  1.1  mycroft     u_int32_t smask;
    623  1.1  mycroft     int recvlen;
    624  1.1  mycroft     int hops = buf->len;
    625  1.1  mycroft     struct tr_resp *r = buf->resps + hops - 1;
    626  1.1  mycroft     u_int32_t next = r->tr_rmtaddr;
    627  1.1  mycroft 
    628  1.1  mycroft     recvlen = send_recv(next, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0]);
    629  1.1  mycroft     print_host(next);
    630  1.1  mycroft     if (recvlen) {
    631  1.1  mycroft 	u_int32_t version = ntohl(incr[0].igmp.igmp_group.s_addr);
    632  1.1  mycroft 	u_int32_t *p = (u_int32_t *)incr[0].ndata;
    633  1.1  mycroft 	u_int32_t *ep = p + (incr[0].len >> 2);
    634  1.1  mycroft 	printf(" [%s%d.%d] didn't respond\n",
    635  1.1  mycroft 	       (version == 1) ? "proteon/mrouted " :
    636  1.1  mycroft 	       ((version & 0xff) == 2) ? "mrouted " :
    637  1.1  mycroft 	       ((version & 0xff) == 3) ? "mrouted " :
    638  1.1  mycroft 	       ((version & 0xff) == 4) ? "mrouted " :
    639  1.1  mycroft 	       ((version & 0xff) == 10) ? "cisco " : "",
    640  1.1  mycroft 	       version & 0xff, (version >> 8) & 0xff);
    641  1.1  mycroft 	VAL_TO_MASK(smask, r->tr_smask);
    642  1.1  mycroft 	while (p < ep) {
    643  1.1  mycroft 	    register u_int32_t laddr = *p++;
    644  1.1  mycroft 	    register int n = ntohl(*p++) & 0xFF;
    645  1.1  mycroft 	    if ((laddr & smask) == (qsrc & smask)) {
    646  1.1  mycroft 		printf("%3d  ", -(hops+2));
    647  1.1  mycroft 		print_host(qsrc);
    648  1.1  mycroft 		printf("\n");
    649  1.1  mycroft 		break;
    650  1.1  mycroft 	    }
    651  1.1  mycroft 	    p += n;
    652  1.1  mycroft 	}
    653  1.1  mycroft 	return;
    654  1.1  mycroft     }
    655  1.1  mycroft     printf(" didn't respond\n");
    656  1.1  mycroft }
    657  1.1  mycroft 
    658  1.1  mycroft 
    659  1.1  mycroft char *
    660  1.1  mycroft scale(hop)
    661  1.1  mycroft     int *hop;
    662  1.1  mycroft {
    663  1.1  mycroft     if (*hop > -1000 && *hop < 10000) return (" ms");
    664  1.1  mycroft     *hop /= 1000;
    665  1.1  mycroft     if (*hop > -1000 && *hop < 10000) return (" s ");
    666  1.1  mycroft     return ("s ");
    667  1.1  mycroft }
    668  1.1  mycroft 
    669  1.1  mycroft /*
    670  1.1  mycroft  * Calculate and print one line of packet loss and packet rate statistics.
    671  1.1  mycroft  * Checks for count of all ones from mrouted 2.3 that doesn't have counters.
    672  1.1  mycroft  */
    673  1.1  mycroft #define NEITHER 0
    674  1.1  mycroft #define INS     1
    675  1.1  mycroft #define OUTS    2
    676  1.1  mycroft #define BOTH    3
    677  1.1  mycroft void
    678  1.1  mycroft stat_line(r, s, have_next)
    679  1.1  mycroft     struct tr_resp *r, *s;
    680  1.1  mycroft     int have_next;
    681  1.1  mycroft {
    682  1.1  mycroft     register timediff = (fixtime(ntohl(s->tr_qarr)) -
    683  1.1  mycroft 			 fixtime(ntohl(r->tr_qarr))) >> 16;
    684  1.1  mycroft     register v_lost, v_pct;
    685  1.1  mycroft     register g_lost, g_pct;
    686  1.1  mycroft     register v_out = ntohl(s->tr_vifout) - ntohl(r->tr_vifout);
    687  1.1  mycroft     register g_out = ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt);
    688  1.1  mycroft     register v_pps, g_pps;
    689  1.1  mycroft     char v_str[8], g_str[8];
    690  1.1  mycroft     register have = NEITHER;
    691  1.1  mycroft 
    692  1.2  thorpej     if (timediff == 0)
    693  1.2  thorpej         timediff = 1;
    694  1.1  mycroft     v_pps = v_out / timediff;
    695  1.1  mycroft     g_pps = g_out / timediff;
    696  1.1  mycroft 
    697  1.2  thorpej     if (v_out || s->tr_vifout != 0xFFFFFFFF)
    698  1.2  thorpej         have |= OUTS;
    699  1.1  mycroft 
    700  1.1  mycroft     if (have_next) {
    701  1.1  mycroft 	--r,  --s;
    702  1.1  mycroft 	if (s->tr_vifin != 0xFFFFFFFF || r->tr_vifin != 0xFFFFFFFF)
    703  1.1  mycroft 	  have |= INS;
    704  1.1  mycroft     }
    705  1.1  mycroft 
    706  1.1  mycroft     switch (have) {
    707  1.1  mycroft       case BOTH:
    708  1.1  mycroft 	v_lost = v_out - (ntohl(s->tr_vifin) - ntohl(r->tr_vifin));
    709  1.2  thorpej 	if (v_out)
    710  1.2  thorpej 	    v_pct = (v_lost * 100 + (v_out >> 1)) / v_out;
    711  1.2  thorpej 	else
    712  1.2  thorpej 	    v_pct = 0;
    713  1.1  mycroft 	if (-100 < v_pct && v_pct < 101 && v_out > 10)
    714  1.2  thorpej 	    sprintf(v_str, "%3d", v_pct);
    715  1.2  thorpej 	else
    716  1.2  thorpej 	    memcpy(v_str, " --", 4);
    717  1.1  mycroft 
    718  1.1  mycroft 	g_lost = g_out - (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
    719  1.2  thorpej 	if (g_out)
    720  1.2  thorpej 	    g_pct = (g_lost * 100 + (g_out >> 1))/ g_out;
    721  1.2  thorpej 	else
    722  1.2  thorpej 	    g_pct = 0;
    723  1.1  mycroft 	if (-100 < g_pct && g_pct < 101 && g_out > 10)
    724  1.2  thorpej 	    sprintf(g_str, "%3d", g_pct);
    725  1.2  thorpej 	else
    726  1.2  thorpej 	    memcpy(g_str, " --", 4);
    727  1.1  mycroft 
    728  1.1  mycroft 	printf("%6d/%-5d=%s%%%4d pps%6d/%-5d=%s%%%4d pps\n",
    729  1.1  mycroft 	       v_lost, v_out, v_str, v_pps, g_lost, g_out, g_str, g_pps);
    730  1.1  mycroft 	if (debug > 2) {
    731  1.1  mycroft 	    printf("\t\t\t\tv_in: %ld ", ntohl(s->tr_vifin));
    732  1.1  mycroft 	    printf("v_out: %ld ", ntohl(s->tr_vifout));
    733  1.1  mycroft 	    printf("pkts: %ld\n", ntohl(s->tr_pktcnt));
    734  1.1  mycroft 	    printf("\t\t\t\tv_in: %ld ", ntohl(r->tr_vifin));
    735  1.1  mycroft 	    printf("v_out: %ld ", ntohl(r->tr_vifout));
    736  1.1  mycroft 	    printf("pkts: %ld\n", ntohl(r->tr_pktcnt));
    737  1.1  mycroft 	    printf("\t\t\t\tv_in: %ld ",ntohl(s->tr_vifin)-ntohl(r->tr_vifin));
    738  1.1  mycroft 	    printf("v_out: %ld ", ntohl(s->tr_vifout) - ntohl(r->tr_vifout));
    739  1.1  mycroft 	    printf("pkts: %ld ", ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
    740  1.1  mycroft 	    printf("time: %d\n", timediff);
    741  1.1  mycroft 	}
    742  1.1  mycroft 	break;
    743  1.1  mycroft 
    744  1.1  mycroft       case INS:
    745  1.1  mycroft 	v_out = (ntohl(s->tr_vifin) - ntohl(r->tr_vifin));
    746  1.1  mycroft 	g_out = (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
    747  1.1  mycroft 	v_pps = v_out / timediff;
    748  1.1  mycroft 	g_pps = g_out / timediff;
    749  1.1  mycroft 	/* Fall through */
    750  1.1  mycroft 
    751  1.1  mycroft       case OUTS:
    752  1.1  mycroft 	printf("       %-5d     %4d pps       %-5d     %4d pps\n",
    753  1.1  mycroft 	       v_out, v_pps, g_out, g_pps);
    754  1.1  mycroft 	break;
    755  1.1  mycroft 
    756  1.1  mycroft       case NEITHER:
    757  1.1  mycroft 	printf("\n");
    758  1.1  mycroft 	break;
    759  1.1  mycroft     }
    760  1.1  mycroft }
    761  1.1  mycroft 
    762  1.1  mycroft /*
    763  1.1  mycroft  * A fixup to check if any pktcnt has been reset.
    764  1.1  mycroft  */
    765  1.1  mycroft void
    766  1.1  mycroft fixup_stats(base, new)
    767  1.1  mycroft     struct resp_buf *base, *new;
    768  1.1  mycroft {
    769  1.1  mycroft     register rno = base->len;
    770  1.1  mycroft     register struct tr_resp *b = base->resps + rno;
    771  1.1  mycroft     register struct tr_resp *n = new->resps + rno;
    772  1.1  mycroft 
    773  1.1  mycroft     while (--rno >= 0)
    774  1.2  thorpej         if (ntohl((--n)->tr_pktcnt) < ntohl((--b)->tr_pktcnt))
    775  1.2  thorpej             break;
    776  1.1  mycroft 
    777  1.2  thorpej     if (rno < 0)
    778  1.2  thorpej         return;
    779  1.1  mycroft 
    780  1.1  mycroft     rno = base->len;
    781  1.1  mycroft     b = base->resps + rno;
    782  1.1  mycroft     n = new->resps + rno;
    783  1.1  mycroft 
    784  1.2  thorpej     while (--rno >= 0)
    785  1.2  thorpej         (--b)->tr_pktcnt = (--n)->tr_pktcnt;
    786  1.1  mycroft }
    787  1.1  mycroft 
    788  1.1  mycroft /*
    789  1.1  mycroft  * Print responses with statistics for forward path (from src to dst)
    790  1.1  mycroft  */
    791  1.1  mycroft void
    792  1.1  mycroft print_stats(base, prev, new)
    793  1.1  mycroft     struct resp_buf *base, *prev, *new;
    794  1.1  mycroft {
    795  1.1  mycroft     int rtt, hop;
    796  1.1  mycroft     register char *ms;
    797  1.1  mycroft     register u_int32_t smask;
    798  1.1  mycroft     register rno = base->len - 1;
    799  1.1  mycroft     register struct tr_resp *b = base->resps + rno;
    800  1.1  mycroft     register struct tr_resp *p = prev->resps + rno;
    801  1.1  mycroft     register struct tr_resp *n = new->resps + rno;
    802  1.1  mycroft     register u_long resptime = new->rtime;
    803  1.1  mycroft     register u_long qarrtime = fixtime(ntohl(n->tr_qarr));
    804  1.1  mycroft     register ttl = n->tr_fttl;
    805  1.1  mycroft 
    806  1.1  mycroft     VAL_TO_MASK(smask, b->tr_smask);
    807  1.1  mycroft     printf("  Source        Response Dest");
    808  1.1  mycroft     printf("    Packet Statistics For     Only For Traffic\n");
    809  1.1  mycroft     printf("%-15s %-15s  All Multicast Traffic     From %s\n",
    810  1.1  mycroft 	   ((b->tr_inaddr & smask) == (qsrc & smask)) ? s1 : "   * * *       ",
    811  1.1  mycroft 	   inet_fmt(base->qhdr.tr_raddr, s2), inet_fmt(qsrc, s1));
    812  1.1  mycroft     rtt = t_diff(resptime, new->qtime);
    813  1.1  mycroft     ms = scale(&rtt);
    814  1.1  mycroft     printf("     |       __/  rtt%5d%s    Lost/Sent = Pct  Rate       To %s\n",
    815  1.1  mycroft 	   rtt, ms, inet_fmt(qgrp, s2));
    816  1.1  mycroft     hop = t_diff(resptime, qarrtime);
    817  1.1  mycroft     ms = scale(&hop);
    818  1.1  mycroft     printf("     v      /     hop%5d%s", hop, ms);
    819  1.1  mycroft     printf("    ---------------------     --------------------\n");
    820  1.1  mycroft     if (debug > 2) {
    821  1.1  mycroft 	printf("\t\t\t\tv_in: %ld ", ntohl(n->tr_vifin));
    822  1.1  mycroft 	printf("v_out: %ld ", ntohl(n->tr_vifout));
    823  1.1  mycroft 	printf("pkts: %ld\n", ntohl(n->tr_pktcnt));
    824  1.1  mycroft 	printf("\t\t\t\tv_in: %ld ", ntohl(b->tr_vifin));
    825  1.1  mycroft 	printf("v_out: %ld ", ntohl(b->tr_vifout));
    826  1.1  mycroft 	printf("pkts: %ld\n", ntohl(b->tr_pktcnt));
    827  1.1  mycroft 	printf("\t\t\t\tv_in: %ld ", ntohl(n->tr_vifin) - ntohl(b->tr_vifin));
    828  1.1  mycroft 	printf("v_out: %ld ", ntohl(n->tr_vifout) - ntohl(b->tr_vifout));
    829  1.1  mycroft 	printf("pkts: %ld\n", ntohl(n->tr_pktcnt) - ntohl(b->tr_pktcnt));
    830  1.1  mycroft     }
    831  1.1  mycroft 
    832  1.1  mycroft     while (TRUE) {
    833  1.1  mycroft 	if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_inaddr != b->tr_inaddr)) {
    834  1.1  mycroft 	    printf("Route changed, start again.\n");
    835  1.1  mycroft 	    exit(1);
    836  1.1  mycroft 	}
    837  1.1  mycroft 	if ((n->tr_inaddr != n->tr_outaddr))
    838  1.1  mycroft 	  printf("%-15s\n", inet_fmt(n->tr_inaddr, s1));
    839  1.1  mycroft 	printf("%-15s %-14s %s\n", inet_fmt(n->tr_outaddr, s1), names[rno],
    840  1.1  mycroft 		 flag_type(n->tr_rflags));
    841  1.1  mycroft 
    842  1.1  mycroft 	if (rno-- < 1) break;
    843  1.1  mycroft 
    844  1.1  mycroft 	printf("     |     ^      ttl%5d   ", ttl);
    845  1.2  thorpej 	if (prev == new)
    846  1.2  thorpej 	    printf("\n");
    847  1.2  thorpej 	else
    848  1.2  thorpej 	    stat_line(p, n, TRUE);
    849  1.1  mycroft 	resptime = qarrtime;
    850  1.1  mycroft 	qarrtime = fixtime(ntohl((n-1)->tr_qarr));
    851  1.1  mycroft 	hop = t_diff(resptime, qarrtime);
    852  1.1  mycroft 	ms = scale(&hop);
    853  1.1  mycroft 	printf("     v     |      hop%5d%s", hop, ms);
    854  1.1  mycroft 	stat_line(b, n, TRUE);
    855  1.1  mycroft 
    856  1.1  mycroft 	--b, --p, --n;
    857  1.2  thorpej 	if (ttl < n->tr_fttl)
    858  1.2  thorpej 	    ttl = n->tr_fttl;
    859  1.2  thorpej 	else
    860  1.2  thorpej 	    ++ttl;
    861  1.1  mycroft     }
    862  1.1  mycroft 
    863  1.1  mycroft     printf("     |      \\__   ttl%5d   ", ttl);
    864  1.2  thorpej     if (prev == new)
    865  1.2  thorpej         printf("\n");
    866  1.2  thorpej     else
    867  1.2  thorpej         stat_line(p, n, FALSE);
    868  1.1  mycroft     hop = t_diff(qarrtime, new->qtime);
    869  1.1  mycroft     ms = scale(&hop);
    870  1.1  mycroft     printf("     v         \\  hop%5d%s", hop, ms);
    871  1.1  mycroft     stat_line(b, n, FALSE);
    872  1.1  mycroft     printf("%-15s %s\n", inet_fmt(qdst, s1), inet_fmt(lcl_addr, s2));
    873  1.1  mycroft     printf("  Receiver      Query Source\n\n");
    874  1.1  mycroft }
    875  1.1  mycroft 
    876  1.1  mycroft 
    877  1.1  mycroft /***************************************************************************
    878  1.1  mycroft  *	main
    879  1.1  mycroft  ***************************************************************************/
    880  1.1  mycroft 
    881  1.1  mycroft int
    882  1.1  mycroft main(argc, argv)
    883  1.1  mycroft int argc;
    884  1.1  mycroft char *argv[];
    885  1.1  mycroft {
    886  1.1  mycroft     int udp;
    887  1.1  mycroft     struct sockaddr_in addr;
    888  1.1  mycroft     int addrlen = sizeof(addr);
    889  1.1  mycroft     int recvlen;
    890  1.1  mycroft     struct timeval tv;
    891  1.1  mycroft     struct resp_buf *prev, *new;
    892  1.1  mycroft     struct tr_query *query;
    893  1.1  mycroft     struct tr_resp *r;
    894  1.1  mycroft     u_int32_t smask;
    895  1.1  mycroft     int rno;
    896  1.1  mycroft     int hops, tries;
    897  1.1  mycroft     int numstats = 1;
    898  1.1  mycroft     int waittime;
    899  1.1  mycroft     int seed;
    900  1.2  thorpej     int ch;
    901  1.1  mycroft 
    902  1.1  mycroft     if (geteuid() != 0) {
    903  1.1  mycroft 	fprintf(stderr, "mtrace: must be root\n");
    904  1.1  mycroft 	exit(1);
    905  1.1  mycroft     }
    906  1.1  mycroft 
    907  1.3  thorpej     while ((ch = getopt(argc, argv, "d:g:i:lMm:npq:r:st:w:")) != -1) {
    908  1.2  thorpej         switch (ch) {
    909  1.2  thorpej 	case 'd':			/* Unlisted debug print option */
    910  1.2  thorpej 	    if (!isdigit(*optarg))
    911  1.2  thorpej 		usage();
    912  1.2  thorpej 	    debug = atoi(optarg);
    913  1.2  thorpej 	    if (debug < 0)
    914  1.2  thorpej 	         debug = 0;
    915  1.2  thorpej 	    else if (debug > 3)
    916  1.2  thorpej 	        debug = 3;
    917  1.2  thorpej 	    break;
    918  1.2  thorpej 
    919  1.2  thorpej         case 'M':			/* Use multicast for reponse */
    920  1.2  thorpej 	    multicast = TRUE;
    921  1.2  thorpej 	    break;
    922  1.2  thorpej 
    923  1.2  thorpej 	case 'l':			/* Loop updating stats indefinitely */
    924  1.2  thorpej 	    numstats = 3153600;
    925  1.2  thorpej 	    break;
    926  1.2  thorpej 
    927  1.2  thorpej 	case 'n':			/* Don't reverse map host addresses */
    928  1.2  thorpej 	    numeric = TRUE;
    929  1.2  thorpej 	    break;
    930  1.2  thorpej 
    931  1.2  thorpej 	case 'p':			/* Passive listen for traces */
    932  1.2  thorpej 	    passive = TRUE;
    933  1.2  thorpej 	    break;
    934  1.2  thorpej 
    935  1.2  thorpej 	case 's':			/* Short form, don't wait for stats */
    936  1.2  thorpej 	    numstats = 0;
    937  1.2  thorpej 	    break;
    938  1.2  thorpej 
    939  1.2  thorpej 	case 'w':			/* Time to wait for packet arrival */
    940  1.2  thorpej 	    if (!isdigit(*optarg))
    941  1.2  thorpej 		usage();
    942  1.2  thorpej 	    timeout = atoi(optarg);
    943  1.2  thorpej 	    if (timeout < 1)
    944  1.2  thorpej 	        timeout = 1;
    945  1.2  thorpej 	    break;
    946  1.2  thorpej 
    947  1.2  thorpej 	case 'm':			/* Max number of hops to trace */
    948  1.2  thorpej 	    if (!isdigit(*optarg))
    949  1.2  thorpej 		usage();
    950  1.2  thorpej 	    qno = atoi(optarg);
    951  1.2  thorpej 	    if (qno > MAXHOPS)
    952  1.2  thorpej 	        qno = MAXHOPS;
    953  1.2  thorpej 	    else if (qno < 1)
    954  1.2  thorpej 	        qno = 0;
    955  1.2  thorpej 	    break;
    956  1.2  thorpej 
    957  1.2  thorpej 	case 'q':			/* Number of query retries */
    958  1.2  thorpej 	    if (!isdigit(*optarg))
    959  1.2  thorpej 		usage();
    960  1.2  thorpej 	    nqueries = atoi(optarg);
    961  1.2  thorpej 	    if (nqueries < 1)
    962  1.2  thorpej 	        nqueries = 1;
    963  1.2  thorpej 	    break;
    964  1.2  thorpej 
    965  1.2  thorpej 	case 'g':			/* Last-hop gateway (dest of query) */
    966  1.2  thorpej 	    if ((gwy = host_addr(optarg)) == 0)
    967  1.2  thorpej 		usage();
    968  1.2  thorpej 	    break;
    969  1.2  thorpej 
    970  1.2  thorpej 	case 't':			/* TTL for query packet */
    971  1.2  thorpej 	    if (!isdigit(*optarg))
    972  1.2  thorpej 		usage();
    973  1.2  thorpej 	    qttl = atoi(optarg);
    974  1.2  thorpej 	    if (qttl < 1)
    975  1.2  thorpej 	        qttl = 1;
    976  1.2  thorpej 	    rttl = qttl;
    977  1.2  thorpej 	    break;
    978  1.2  thorpej 
    979  1.2  thorpej 	case 'r':			/* Dest for response packet */
    980  1.2  thorpej 	    if ((raddr = host_addr(optarg)) == 0)
    981  1.2  thorpej 		usage();
    982  1.2  thorpej 	    break;
    983  1.2  thorpej 
    984  1.2  thorpej 	case 'i':			/* Local interface address */
    985  1.2  thorpej 	    if ((lcl_addr = host_addr(optarg)) == 0)
    986  1.2  thorpej 		usage();
    987  1.2  thorpej 	    break;
    988  1.2  thorpej 
    989  1.2  thorpej 	default:
    990  1.2  thorpej 	    usage();
    991  1.2  thorpej 	} /* switch */
    992  1.2  thorpej     } /* while */
    993  1.2  thorpej     argv += optind;
    994  1.2  thorpej     argc -= optind;
    995  1.2  thorpej 
    996  1.2  thorpej     switch (argc) {
    997  1.2  thorpej     case 3:		/* Path via group */
    998  1.2  thorpej 	if ((qgrp = host_addr(argv[2])) == 0)
    999  1.2  thorpej 		usage();
   1000  1.2  thorpej 	/* FALLTHROUGH */
   1001  1.2  thorpej     case 2:		/* dest of path */
   1002  1.2  thorpej 	if ((qdst = host_addr(argv[1])) == 0)
   1003  1.2  thorpej 		usage();
   1004  1.2  thorpej 	/* FALLTHROUGH */
   1005  1.2  thorpej     case 1:		/* source of path */
   1006  1.2  thorpej 	if ((qsrc = host_addr(argv[0])) == 0 || IN_MULTICAST(ntohl(qsrc)))
   1007  1.2  thorpej 	    usage();
   1008  1.2  thorpej 	break;
   1009  1.1  mycroft 
   1010  1.2  thorpej     default:
   1011  1.2  thorpej 	usage();
   1012  1.1  mycroft     }
   1013  1.1  mycroft 
   1014  1.2  thorpej     /*
   1015  1.2  thorpej      * If argc is > 1 and the second argument is a multicast address,
   1016  1.2  thorpej      * assume that the second argument is actually qgrp and the third
   1017  1.2  thorpej      * (if any) is qdst; in this case, the third argument is not allowed
   1018  1.2  thorpej      * to be a multicast address.
   1019  1.2  thorpej      */
   1020  1.2  thorpej     if (argc > 1) {
   1021  1.2  thorpej 	if (IN_MULTICAST(ntohl(qdst))) {
   1022  1.2  thorpej 	    u_int32_t temp = qdst;
   1023  1.2  thorpej 	    qdst = qgrp;
   1024  1.2  thorpej 	    qgrp = temp;
   1025  1.2  thorpej 	    if (IN_MULTICAST(ntohl(qdst)))
   1026  1.2  thorpej 		usage();
   1027  1.2  thorpej 	} else if (qgrp != 0 && !IN_MULTICAST(ntohl(qgrp)))
   1028  1.2  thorpej 	    usage();
   1029  1.1  mycroft     }
   1030  1.1  mycroft 
   1031  1.2  thorpej     if (qsrc == 0)
   1032  1.2  thorpej 	usage();
   1033  1.1  mycroft 
   1034  1.1  mycroft     init_igmp();
   1035  1.1  mycroft 
   1036  1.1  mycroft     /*
   1037  1.1  mycroft      * Set useful defaults for as many parameters as possible.
   1038  1.1  mycroft      */
   1039  1.1  mycroft 
   1040  1.1  mycroft     defgrp = htonl(0xE0020001);		/* MBone Audio (224.2.0.1) */
   1041  1.1  mycroft     query_cast = htonl(0xE0000002);	/* All routers multicast addr */
   1042  1.1  mycroft     resp_cast = htonl(0xE0000120);	/* Mtrace response multicast addr */
   1043  1.2  thorpej     if (qgrp == 0)
   1044  1.2  thorpej         qgrp = defgrp;
   1045  1.1  mycroft 
   1046  1.1  mycroft     /*
   1047  1.1  mycroft      * Get default local address for multicasts to use in setting defaults.
   1048  1.1  mycroft      */
   1049  1.1  mycroft     addr.sin_family = AF_INET;
   1050  1.1  mycroft #if (defined(BSD) && (BSD >= 199103))
   1051  1.1  mycroft     addr.sin_len = sizeof(addr);
   1052  1.1  mycroft #endif
   1053  1.1  mycroft     addr.sin_addr.s_addr = qgrp;
   1054  1.1  mycroft     addr.sin_port = htons(2000);	/* Any port above 1024 will do */
   1055  1.1  mycroft 
   1056  1.1  mycroft     if (((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) ||
   1057  1.1  mycroft 	(connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0) ||
   1058  1.1  mycroft 	getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) {
   1059  1.1  mycroft 	perror("Determining local address");
   1060  1.1  mycroft 	exit(-1);
   1061  1.1  mycroft     }
   1062  1.1  mycroft 
   1063  1.1  mycroft     /*
   1064  1.1  mycroft      * Default destination for path to be queried is the local host.
   1065  1.1  mycroft      */
   1066  1.2  thorpej     if (qdst == 0)
   1067  1.2  thorpej         qdst = lcl_addr ? lcl_addr : addr.sin_addr.s_addr;
   1068  1.1  mycroft 
   1069  1.1  mycroft     /*
   1070  1.1  mycroft      * If the destination is on the local net, the last-hop router can
   1071  1.1  mycroft      * be found by multicast to the all-routers multicast group.
   1072  1.1  mycroft      * Otherwise, use the group address that is the subject of the
   1073  1.1  mycroft      * query since by definition the last hop router will be a member.
   1074  1.1  mycroft      * Set default TTLs for local remote multicasts.
   1075  1.1  mycroft      */
   1076  1.1  mycroft     dst_netmask = get_netmask(udp, qdst);
   1077  1.1  mycroft     close(udp);
   1078  1.2  thorpej     if (lcl_addr == 0)
   1079  1.2  thorpej         lcl_addr = addr.sin_addr.s_addr;
   1080  1.1  mycroft     if (gwy == 0)
   1081  1.2  thorpej         if ((qdst & dst_netmask) == (lcl_addr & dst_netmask))
   1082  1.2  thorpej             gwy = query_cast;
   1083  1.2  thorpej         else
   1084  1.2  thorpej             gwy = qgrp;
   1085  1.1  mycroft 
   1086  1.1  mycroft     if (IN_MULTICAST(ntohl(gwy))) {
   1087  1.1  mycroft       k_set_loop(1);	/* If I am running on a router, I need to hear this */
   1088  1.2  thorpej       if (gwy == query_cast)
   1089  1.2  thorpej           k_set_ttl(qttl ? qttl : 1);
   1090  1.2  thorpej       else
   1091  1.2  thorpej           k_set_ttl(qttl ? qttl : MULTICAST_TTL1);
   1092  1.1  mycroft     } else
   1093  1.1  mycroft       if (send_recv(gwy, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0]))
   1094  1.1  mycroft 	if (ntohl(incr[0].igmp.igmp_group.s_addr) == 0x0303) {
   1095  1.1  mycroft 	    printf("Don't use -g to address an mrouted 3.3, it might crash\n");
   1096  1.1  mycroft 	    exit(0);
   1097  1.1  mycroft 	}
   1098  1.1  mycroft 
   1099  1.1  mycroft     printf("Mtrace from %s to %s via group %s\n",
   1100  1.1  mycroft 	   inet_fmt(qsrc, s1), inet_fmt(qdst, s2), inet_fmt(qgrp, s3));
   1101  1.1  mycroft 
   1102  1.1  mycroft     if ((qdst & dst_netmask) == (qsrc & dst_netmask)) {
   1103  1.1  mycroft 	printf("Source & receiver are directly connected, no path to trace\n");
   1104  1.1  mycroft 	exit(0);
   1105  1.1  mycroft     }
   1106  1.1  mycroft 
   1107  1.1  mycroft     /*
   1108  1.1  mycroft      * Make up the IGMP_MTRACE_QUERY query packet to send (some parameters
   1109  1.1  mycroft      * are set later), including initializing the seed for random
   1110  1.1  mycroft      * query identifiers.
   1111  1.1  mycroft      */
   1112  1.1  mycroft     query = (struct tr_query *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
   1113  1.1  mycroft     query->tr_src   = qsrc;
   1114  1.1  mycroft     query->tr_dst   = qdst;
   1115  1.1  mycroft 
   1116  1.1  mycroft     gettimeofday(&tv, 0);
   1117  1.1  mycroft     seed = tv.tv_usec ^ lcl_addr;
   1118  1.1  mycroft     srandom(seed);
   1119  1.1  mycroft 
   1120  1.1  mycroft     /*
   1121  1.1  mycroft      * If the response is to be a multicast address, make sure we
   1122  1.1  mycroft      * are listening on that multicast address.
   1123  1.1  mycroft      */
   1124  1.2  thorpej     if (raddr && IN_MULTICAST(ntohl(raddr)))
   1125  1.2  thorpej         k_join(raddr, lcl_addr);
   1126  1.2  thorpej     else
   1127  1.2  thorpej         k_join(resp_cast, lcl_addr);
   1128  1.1  mycroft 
   1129  1.1  mycroft     /*
   1130  1.1  mycroft      * Try a query at the requested number of hops or MAXOPS if unspecified.
   1131  1.1  mycroft      */
   1132  1.1  mycroft     if (qno == 0) {
   1133  1.1  mycroft 	hops = MAXHOPS;
   1134  1.1  mycroft 	tries = 1;
   1135  1.1  mycroft 	printf("Querying full reverse path... ");
   1136  1.1  mycroft 	fflush(stdout);
   1137  1.1  mycroft     } else {
   1138  1.1  mycroft 	hops = qno;
   1139  1.1  mycroft 	tries = nqueries;
   1140  1.1  mycroft 	printf("Querying reverse path, maximum %d hops... ", qno);
   1141  1.1  mycroft 	fflush(stdout);
   1142  1.2  thorpej     }
   1143  1.1  mycroft     base.rtime = 0;
   1144  1.1  mycroft     base.len = 0;
   1145  1.1  mycroft 
   1146  1.1  mycroft     recvlen = send_recv(gwy, IGMP_MTRACE_QUERY, hops, tries, &base);
   1147  1.1  mycroft 
   1148  1.1  mycroft     /*
   1149  1.1  mycroft      * If the initial query was successful, print it.  Otherwise, if
   1150  1.1  mycroft      * the query max hop count is the default of zero, loop starting
   1151  1.1  mycroft      * from one until a timeout occurs.
   1152  1.1  mycroft      */
   1153  1.1  mycroft     if (recvlen) {
   1154  1.1  mycroft 	printf("\n  0  ");
   1155  1.1  mycroft 	print_host(qdst);
   1156  1.1  mycroft 	printf("\n");
   1157  1.1  mycroft 	print_trace(1, &base);
   1158  1.1  mycroft 	r = base.resps + base.len - 1;
   1159  1.1  mycroft 	if (r->tr_rflags == TR_OLD_ROUTER) {
   1160  1.1  mycroft 	    printf("%3d  ", -(base.len+1));
   1161  1.1  mycroft 	    fflush(stdout);
   1162  1.1  mycroft 	    what_kind(&base);
   1163  1.1  mycroft 	} else {
   1164  1.1  mycroft 	    VAL_TO_MASK(smask, r->tr_smask);
   1165  1.1  mycroft 	    if ((r->tr_inaddr & smask) == (qsrc & smask)) {
   1166  1.1  mycroft 		printf("%3d  ", -(base.len+1));
   1167  1.1  mycroft 		print_host(qsrc);
   1168  1.1  mycroft 		printf("\n");
   1169  1.1  mycroft 	    }
   1170  1.1  mycroft 	}
   1171  1.1  mycroft     } else if (qno == 0) {
   1172  1.1  mycroft 	printf("switching to hop-by-hop:\n  0  ");
   1173  1.1  mycroft 	print_host(qdst);
   1174  1.1  mycroft 	printf("\n");
   1175  1.1  mycroft 
   1176  1.1  mycroft 	for (hops = 1; hops <= MAXHOPS; ++hops) {
   1177  1.1  mycroft 	    printf("%3d  ", -hops);
   1178  1.1  mycroft 	    fflush(stdout);
   1179  1.1  mycroft 
   1180  1.1  mycroft 	    recvlen = send_recv(gwy, IGMP_MTRACE_QUERY, hops, nqueries, &base);
   1181  1.1  mycroft 
   1182  1.1  mycroft 	    if (recvlen == 0) {
   1183  1.1  mycroft 		if (--hops == 0) break;
   1184  1.1  mycroft 		what_kind(&base);
   1185  1.1  mycroft 		break;
   1186  1.1  mycroft 	    }
   1187  1.1  mycroft 	    r = base.resps + base.len - 1;
   1188  1.2  thorpej 	    if (base.len == hops)
   1189  1.2  thorpej 	        print_trace(-hops, &base);
   1190  1.1  mycroft 	    else {
   1191  1.1  mycroft 		hops = base.len;
   1192  1.1  mycroft 		if (r->tr_rflags == TR_OLD_ROUTER) {
   1193  1.1  mycroft 		    what_kind(&base);
   1194  1.1  mycroft 		    break;
   1195  1.1  mycroft 		}
   1196  1.1  mycroft 		if (r->tr_rflags == TR_NO_SPACE) {
   1197  1.1  mycroft 		    printf("No space left in trace packet for further hops\n");
   1198  1.1  mycroft 		    break;	/* XXX could do segmented trace */
   1199  1.1  mycroft 		}
   1200  1.1  mycroft 		printf("Route must have changed...\n\n");
   1201  1.1  mycroft 		print_trace(1, &base);
   1202  1.1  mycroft 	    }
   1203  1.1  mycroft 
   1204  1.1  mycroft 	    VAL_TO_MASK(smask, r->tr_smask);
   1205  1.1  mycroft 	    if ((r->tr_inaddr & smask) == (qsrc & smask)) {
   1206  1.1  mycroft 		printf("%3d  ", -(hops+1));
   1207  1.1  mycroft 		print_host(qsrc);
   1208  1.1  mycroft 		printf("\n");
   1209  1.1  mycroft 		break;
   1210  1.1  mycroft 	    }
   1211  1.2  thorpej 	    if (r->tr_rmtaddr == 0 || (r->tr_rflags & 0x80))
   1212  1.2  thorpej 	        break;
   1213  1.1  mycroft 	}
   1214  1.1  mycroft     }
   1215  1.1  mycroft 
   1216  1.1  mycroft     if (base.rtime == 0) {
   1217  1.1  mycroft 	printf("Timed out receiving responses\n");
   1218  1.1  mycroft 	if (IN_MULTICAST(ntohl(gwy)))
   1219  1.1  mycroft 	  if (gwy == query_cast)
   1220  1.1  mycroft 	    printf("Perhaps no local router has a route for source %s\n",
   1221  1.1  mycroft 		   inet_fmt(qsrc, s1));
   1222  1.1  mycroft 	  else
   1223  1.1  mycroft 	    printf("Perhaps receiver %s is not a member of group %s,\n\
   1224  1.1  mycroft or no router local to it has a route for source %s,\n\
   1225  1.1  mycroft or multicast at ttl %d doesn't reach its last-hop router for that source\n",
   1226  1.1  mycroft 		   inet_fmt(qdst, s2), inet_fmt(qgrp, s3), inet_fmt(qsrc, s1),
   1227  1.1  mycroft 		   qttl ? qttl : MULTICAST_TTL1);
   1228  1.1  mycroft 	exit(1);
   1229  1.1  mycroft     }
   1230  1.1  mycroft 
   1231  1.1  mycroft     printf("Round trip time %d ms\n\n", t_diff(base.rtime, base.qtime));
   1232  1.1  mycroft 
   1233  1.1  mycroft     /*
   1234  1.1  mycroft      * Use the saved response which was the longest one received,
   1235  1.1  mycroft      * and make additional probes after delay to measure loss.
   1236  1.1  mycroft      */
   1237  1.1  mycroft     raddr = base.qhdr.tr_raddr;
   1238  1.1  mycroft     rttl = base.qhdr.tr_rttl;
   1239  1.1  mycroft     gettimeofday(&tv, 0);
   1240  1.1  mycroft     waittime = 10 - (((tv.tv_sec + JAN_1970) & 0xFFFF) - (base.qtime >> 16));
   1241  1.1  mycroft     prev = new = &incr[numstats&1];
   1242  1.1  mycroft 
   1243  1.1  mycroft     while (numstats--) {
   1244  1.1  mycroft 	if (waittime < 1) printf("\n");
   1245  1.1  mycroft 	else {
   1246  1.1  mycroft 	    printf("Waiting to accumulate statistics... ");
   1247  1.1  mycroft 	    fflush(stdout);
   1248  1.1  mycroft 	    sleep((unsigned)waittime);
   1249  1.1  mycroft 	}
   1250  1.1  mycroft 	rno = base.len;
   1251  1.1  mycroft 	recvlen = send_recv(gwy, IGMP_MTRACE_QUERY, rno, nqueries, new);
   1252  1.1  mycroft 
   1253  1.1  mycroft 	if (recvlen == 0) {
   1254  1.1  mycroft 	    printf("Timed out.\n");
   1255  1.1  mycroft 	    exit(1);
   1256  1.1  mycroft 	}
   1257  1.1  mycroft 
   1258  1.1  mycroft 	if (rno != new->len) {
   1259  1.1  mycroft 	    printf("Trace length doesn't match.\n");
   1260  1.1  mycroft 	    exit(1);
   1261  1.1  mycroft 	}
   1262  1.1  mycroft 
   1263  1.1  mycroft 	printf("Results after %d seconds:\n\n",
   1264  1.1  mycroft 	       (new->qtime - base.qtime) >> 16);
   1265  1.1  mycroft 	fixup_stats(&base, new);
   1266  1.1  mycroft 	print_stats(&base, prev, new);
   1267  1.1  mycroft 	prev = new;
   1268  1.1  mycroft 	new = &incr[numstats&1];
   1269  1.1  mycroft 	waittime = 10;
   1270  1.1  mycroft     }
   1271  1.1  mycroft 
   1272  1.1  mycroft     /*
   1273  1.1  mycroft      * If the response was multicast back, leave the group
   1274  1.1  mycroft      */
   1275  1.1  mycroft     if (raddr && IN_MULTICAST(ntohl(raddr))) k_leave(raddr, lcl_addr);
   1276  1.1  mycroft     else k_leave(resp_cast, lcl_addr);
   1277  1.1  mycroft 
   1278  1.1  mycroft     return (0);
   1279  1.1  mycroft }
   1280  1.1  mycroft 
   1281  1.1  mycroft void
   1282  1.1  mycroft check_vif_state()
   1283  1.1  mycroft {
   1284  1.1  mycroft     log(LOG_WARNING, errno, "sendto");
   1285  1.1  mycroft }
   1286  1.1  mycroft 
   1287  1.1  mycroft /*
   1288  1.1  mycroft  * Log errors and other messages to stderr, according to the severity
   1289  1.1  mycroft  * of the message and the current debug level.  For errors of severity
   1290  1.1  mycroft  * LOG_ERR or worse, terminate the program.
   1291  1.1  mycroft  */
   1292  1.1  mycroft /*VARARGS3*/
   1293  1.1  mycroft void
   1294  1.1  mycroft log(severity, syserr, format, a, b, c, d, e)
   1295  1.1  mycroft     int severity, syserr;
   1296  1.1  mycroft     char *format;
   1297  1.1  mycroft     int a, b, c, d, e;
   1298  1.1  mycroft {
   1299  1.1  mycroft     char fmt[100];
   1300  1.1  mycroft 
   1301  1.1  mycroft     switch (debug) {
   1302  1.2  thorpej 	case 0:
   1303  1.2  thorpej 	    if (severity > LOG_WARNING)
   1304  1.2  thorpej 	        return;
   1305  1.2  thorpej 	case 1:
   1306  1.2  thorpej 	    if (severity > LOG_NOTICE)
   1307  1.2  thorpej 	        return;
   1308  1.2  thorpej 	case 2:
   1309  1.2  thorpej 	    if (severity > LOG_INFO)
   1310  1.2  thorpej 	        return;
   1311  1.1  mycroft 	default:
   1312  1.1  mycroft 	    fmt[0] = '\0';
   1313  1.1  mycroft 	    if (severity == LOG_WARNING) strcat(fmt, "warning - ");
   1314  1.1  mycroft 	    strncat(fmt, format, 80);
   1315  1.1  mycroft 	    fprintf(stderr, fmt, a, b, c, d, e);
   1316  1.1  mycroft 	    if (syserr == 0)
   1317  1.1  mycroft 		fprintf(stderr, "\n");
   1318  1.1  mycroft 	    else if(syserr < sys_nerr)
   1319  1.1  mycroft 		fprintf(stderr, ": %s\n", sys_errlist[syserr]);
   1320  1.1  mycroft 	    else
   1321  1.1  mycroft 		fprintf(stderr, ": errno %d\n", syserr);
   1322  1.1  mycroft     }
   1323  1.2  thorpej     if (severity <= LOG_ERR)
   1324  1.2  thorpej         exit(-1);
   1325  1.1  mycroft }
   1326  1.1  mycroft 
   1327  1.1  mycroft /* dummies */
   1328  1.1  mycroft 
   1329  1.1  mycroft /*VARARGS*/
   1330  1.1  mycroft void accept_probe() {} /*VARARGS*/
   1331  1.1  mycroft void accept_group_report() {} /*VARARGS*/
   1332  1.1  mycroft void accept_neighbors() {} /*VARARGS*/
   1333  1.1  mycroft void accept_neighbors2() {} /*VARARGS*/
   1334  1.1  mycroft void accept_neighbor_request() {} /*VARARGS*/
   1335  1.1  mycroft void accept_neighbor_request2() {} /*VARARGS*/
   1336  1.1  mycroft void accept_report() {} /*VARARGS*/
   1337  1.1  mycroft void accept_prune() {} /*VARARGS*/
   1338  1.1  mycroft void accept_graft() {} /*VARARGS*/
   1339  1.1  mycroft void accept_g_ack() {} /*VARARGS*/
   1340  1.1  mycroft void add_table_entry() {} /*VARARGS*/
   1341  1.1  mycroft void accept_mtrace() {} /*VARARGS*/
   1342  1.1  mycroft void accept_leave_message() {} /*VARARGS*/
   1343  1.1  mycroft void accept_membership_query() {} /*VARARGS*/
   1344