Home | History | Annotate | Line # | Download | only in mtrace
mtrace.c revision 1.15
      1  1.15       is /*	$NetBSD: mtrace.c,v 1.15 2000/10/11 20:23:53 is Exp $	*/
      2   1.4  thorpej 
      3   1.1  mycroft /*
      4   1.1  mycroft  * mtrace.c
      5   1.1  mycroft  *
      6   1.1  mycroft  * This tool traces the branch of a multicast tree from a source to a
      7   1.1  mycroft  * receiver for a particular multicast group and gives statistics
      8   1.1  mycroft  * about packet rate and loss for each hop along the path.  It can
      9   1.1  mycroft  * usually be invoked just as
     10   1.1  mycroft  *
     11   1.1  mycroft  * 	mtrace source
     12   1.1  mycroft  *
     13   1.1  mycroft  * to trace the route from that source to the local host for a default
     14   1.1  mycroft  * group when only the route is desired and not group-specific packet
     15   1.1  mycroft  * counts.  See the usage line for more complex forms.
     16   1.1  mycroft  *
     17   1.1  mycroft  *
     18   1.1  mycroft  * Released 4 Apr 1995.  This program was adapted by Steve Casner
     19   1.1  mycroft  * (USC/ISI) from a prototype written by Ajit Thyagarajan (UDel and
     20   1.1  mycroft  * Xerox PARC).  It attempts to parallel in command syntax and output
     21   1.1  mycroft  * format the unicast traceroute program written by Van Jacobson (LBL)
     22   1.1  mycroft  * for the parts where that makes sense.
     23   1.1  mycroft  *
     24   1.1  mycroft  * Copyright (c) 1995 by the University of Southern California
     25   1.1  mycroft  * All rights reserved.
     26   1.1  mycroft  *
     27   1.1  mycroft  * Permission to use, copy, modify, and distribute this software and its
     28   1.1  mycroft  * documentation in source and binary forms for non-commercial purposes
     29   1.1  mycroft  * and without fee is hereby granted, provided that the above copyright
     30   1.1  mycroft  * notice appear in all copies and that both the copyright notice and
     31   1.1  mycroft  * this permission notice appear in supporting documentation, and that
     32   1.1  mycroft  * any documentation, advertising materials, and other materials related
     33   1.1  mycroft  * to such distribution and use acknowledge that the software was
     34   1.1  mycroft  * developed by the University of Southern California, Information
     35   1.1  mycroft  * Sciences Institute.  The name of the University may not be used to
     36   1.1  mycroft  * endorse or promote products derived from this software without
     37   1.1  mycroft  * specific prior written permission.
     38   1.1  mycroft  *
     39   1.1  mycroft  * THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
     40   1.1  mycroft  * the suitability of this software for any purpose.  THIS SOFTWARE IS
     41   1.1  mycroft  * PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     42   1.1  mycroft  * INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     43   1.1  mycroft  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     44   1.1  mycroft  *
     45   1.1  mycroft  * Other copyrights might apply to parts of this software and are so
     46   1.1  mycroft  * noted when applicable.
     47   1.1  mycroft  *
     48   1.1  mycroft  * In particular, parts of the prototype version of this program may
     49   1.1  mycroft  * have been derived from mrouted programs sources covered by the
     50   1.1  mycroft  * license in the accompanying file named "LICENSE".
     51   1.1  mycroft  */
     52   1.1  mycroft 
     53   1.8    lukem #include <sys/cdefs.h>
     54   1.5  mycroft #ifndef lint
     55  1.15       is __RCSID("$NetBSD: mtrace.c,v 1.15 2000/10/11 20:23:53 is Exp $");
     56   1.5  mycroft #endif
     57   1.5  mycroft 
     58   1.8    lukem #include <sys/types.h>
     59   1.8    lukem #include <sys/ioctl.h>
     60   1.1  mycroft #include <sys/time.h>
     61   1.8    lukem #include <netinet/in.h>
     62   1.8    lukem #include <arpa/inet.h>
     63   1.8    lukem #include <ctype.h>
     64   1.1  mycroft #include <memory.h>
     65   1.8    lukem #include <netdb.h>
     66   1.1  mycroft #include <string.h>
     67   1.1  mycroft #include "defs.h"
     68   1.8    lukem 
     69   1.5  mycroft #ifdef __STDC__
     70   1.5  mycroft #include <stdarg.h>
     71   1.5  mycroft #else
     72   1.5  mycroft #include <varargs.h>
     73   1.5  mycroft #endif
     74   1.5  mycroft #ifdef SUNOS5
     75   1.5  mycroft #include <sys/systeminfo.h>
     76   1.5  mycroft #endif
     77   1.1  mycroft 
     78   1.1  mycroft #define DEFAULT_TIMEOUT	3	/* How long to wait before retrying requests */
     79   1.1  mycroft #define DEFAULT_RETRIES 3	/* How many times to try */
     80   1.1  mycroft #define MAXHOPS UNREACHABLE	/* Don't need more hops than max metric */
     81   1.1  mycroft #define UNICAST_TTL 255		/* TTL for unicast response */
     82   1.1  mycroft #define MULTICAST_TTL1 64	/* Default TTL for multicast query/response */
     83   1.1  mycroft #define MULTICAST_TTL_INC 32	/* TTL increment for increase after timeout */
     84   1.1  mycroft #define MULTICAST_TTL_MAX 192	/* Maximum TTL allowed (protect low-BW links */
     85   1.1  mycroft 
     86   1.1  mycroft struct resp_buf {
     87   1.1  mycroft     u_long qtime;		/* Time query was issued */
     88   1.1  mycroft     u_long rtime;		/* Time response was received */
     89   1.1  mycroft     int	len;			/* Number of reports or length of data */
     90   1.1  mycroft     struct igmp igmp;		/* IGMP header */
     91   1.1  mycroft     union {
     92   1.1  mycroft 	struct {
     93   1.1  mycroft 	    struct tr_query q;		/* Query/response header */
     94   1.1  mycroft 	    struct tr_resp r[MAXHOPS];	/* Per-hop reports */
     95   1.1  mycroft 	} t;
     96   1.1  mycroft 	char d[MAX_DVMRP_DATA_LEN];	/* Neighbor data */
     97   1.1  mycroft     } u;
     98   1.1  mycroft } base, incr[2];
     99   1.1  mycroft 
    100   1.1  mycroft #define qhdr u.t.q
    101   1.1  mycroft #define resps u.t.r
    102   1.1  mycroft #define ndata u.d
    103   1.1  mycroft 
    104   1.1  mycroft char names[MAXHOPS][40];
    105   1.5  mycroft int reset[MAXHOPS];			/* To get around 3.4 bug, ... */
    106   1.5  mycroft int swaps[MAXHOPS];			/* To get around 3.6 bug, ... */
    107   1.1  mycroft 
    108   1.1  mycroft int timeout = DEFAULT_TIMEOUT;
    109   1.1  mycroft int nqueries = DEFAULT_RETRIES;
    110   1.1  mycroft int numeric = FALSE;
    111   1.1  mycroft int debug = 0;
    112   1.1  mycroft int passive = FALSE;
    113   1.1  mycroft int multicast = FALSE;
    114   1.5  mycroft int statint = 10;
    115   1.5  mycroft int verbose = 0;
    116   1.1  mycroft 
    117   1.1  mycroft u_int32_t defgrp;			/* Default group if not specified */
    118   1.1  mycroft u_int32_t query_cast;			/* All routers multicast addr */
    119   1.1  mycroft u_int32_t resp_cast;			/* Mtrace response multicast addr */
    120   1.1  mycroft 
    121   1.1  mycroft u_int32_t lcl_addr = 0;			/* This host address, in NET order */
    122   1.1  mycroft u_int32_t dst_netmask;			/* netmask to go with qdst */
    123   1.1  mycroft 
    124   1.1  mycroft /*
    125   1.1  mycroft  * Query/response parameters, all initialized to zero and set later
    126   1.1  mycroft  * to default values or from options.
    127   1.1  mycroft  */
    128   1.5  mycroft u_int32_t qsrc = 0;		/* Source address in the query */
    129   1.5  mycroft u_int32_t qgrp = 0;		/* Group address in the query */
    130   1.5  mycroft u_int32_t qdst = 0;		/* Destination (receiver) address in query */
    131   1.5  mycroft u_char qno  = 0;		/* Max number of hops to query */
    132   1.5  mycroft u_int32_t raddr = 0;		/* Address where response should be sent */
    133   1.5  mycroft int    qttl = 0;		/* TTL for the query packet */
    134   1.5  mycroft u_char rttl = 0;		/* TTL for the response packet */
    135   1.5  mycroft u_int32_t gwy = 0;		/* User-supplied last-hop router address */
    136   1.5  mycroft u_int32_t tdst = 0;		/* Address where trace is sent (last-hop) */
    137   1.1  mycroft 
    138   1.1  mycroft vifi_t  numvifs;		/* to keep loader happy */
    139   1.1  mycroft 				/* (see kern.c) */
    140   1.1  mycroft 
    141   1.8    lukem u_long			byteswap __P((u_long));
    142   1.5  mycroft char *			inet_name __P((u_int32_t addr));
    143   1.5  mycroft u_int32_t			host_addr __P((char *name));
    144   1.5  mycroft /* u_int is promoted u_char */
    145   1.5  mycroft char *			proto_type __P((u_int type));
    146   1.5  mycroft char *			flag_type __P((u_int type));
    147   1.5  mycroft 
    148   1.8    lukem u_int32_t		get_netmask __P((int s, u_int32_t dst));
    149   1.5  mycroft int			get_ttl __P((struct resp_buf *buf));
    150   1.5  mycroft int			t_diff __P((u_long a, u_long b));
    151   1.5  mycroft u_long			fixtime __P((u_long time));
    152   1.5  mycroft int			send_recv __P((u_int32_t dst, int type, int code,
    153   1.5  mycroft 					int tries, struct resp_buf *save));
    154   1.5  mycroft char *			print_host __P((u_int32_t addr));
    155   1.5  mycroft char *			print_host2 __P((u_int32_t addr1, u_int32_t addr2));
    156   1.5  mycroft void			print_trace __P((int index, struct resp_buf *buf));
    157   1.5  mycroft int			what_kind __P((struct resp_buf *buf, char *why));
    158   1.5  mycroft char *			scale __P((int *hop));
    159   1.5  mycroft void			stat_line __P((struct tr_resp *r, struct tr_resp *s,
    160   1.5  mycroft 					int have_next, int *res));
    161   1.5  mycroft void			fixup_stats __P((struct resp_buf *base,
    162   1.5  mycroft 					struct resp_buf *prev,
    163   1.5  mycroft 					struct resp_buf *new));
    164   1.5  mycroft int			print_stats __P((struct resp_buf *base,
    165   1.5  mycroft 					struct resp_buf *prev,
    166   1.5  mycroft 					struct resp_buf *new));
    167   1.5  mycroft void			check_vif_state __P((void));
    168   1.8    lukem void			passive_mode __P((void));
    169   1.5  mycroft 
    170   1.5  mycroft int			main __P((int argc, char *argv[]));
    171  1.15       is #ifdef __STDC__
    172  1.15       is void log(int severity, int syserr, char *format, ...)
    173  1.15       is 	__attribute__((__format__(__printf__, 3, 4)));
    174  1.15       is #endif
    175   1.2  thorpej 
    176   1.2  thorpej 
    177   1.1  mycroft 
    178   1.5  mycroft char   *
    179   1.1  mycroft inet_name(addr)
    180   1.1  mycroft     u_int32_t  addr;
    181   1.1  mycroft {
    182   1.1  mycroft     struct hostent *e;
    183   1.1  mycroft 
    184   1.1  mycroft     e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
    185   1.1  mycroft 
    186   1.1  mycroft     return e ? e->h_name : "?";
    187   1.1  mycroft }
    188   1.1  mycroft 
    189   1.1  mycroft 
    190   1.1  mycroft u_int32_t
    191   1.1  mycroft host_addr(name)
    192   1.1  mycroft     char   *name;
    193   1.1  mycroft {
    194   1.5  mycroft     struct hostent *e = (struct hostent *)0;
    195   1.5  mycroft     u_int32_t  addr;
    196   1.1  mycroft     int	i, dots = 3;
    197   1.1  mycroft     char	buf[40];
    198   1.1  mycroft     char	*ip = name;
    199   1.1  mycroft     char	*op = buf;
    200   1.1  mycroft 
    201   1.2  thorpej     /*
    202   1.5  mycroft      * Undo BSD's favor -- take fewer than 4 octets as net/subnet address
    203   1.5  mycroft      * if the name is all numeric.
    204   1.2  thorpej      */
    205   1.2  thorpej     for (i = sizeof(buf) - 7; i > 0; --i) {
    206   1.5  mycroft 	if (*ip == '.') --dots;
    207   1.5  mycroft 	else if (*ip == '\0') break;
    208   1.5  mycroft 	else if (!isdigit(*ip)) dots = 0;  /* Not numeric, don't add zeroes */
    209   1.5  mycroft 	*op++ = *ip++;
    210   1.2  thorpej     }
    211   1.2  thorpej     for (i = 0; i < dots; ++i) {
    212   1.5  mycroft 	*op++ = '.';
    213   1.2  thorpej 	*op++ = '0';
    214   1.2  thorpej     }
    215   1.2  thorpej     *op = '\0';
    216   1.2  thorpej 
    217   1.5  mycroft     if (dots <= 0) e = gethostbyname(name);
    218  1.10      mrg     if (e) memcpy((char *)&addr, e->h_addr_list[0], sizeof(addr));
    219   1.5  mycroft     else {
    220   1.5  mycroft 	addr = inet_addr(buf);
    221   1.5  mycroft 	if (addr == -1) {
    222   1.5  mycroft 	    addr = 0;
    223   1.1  mycroft 	    printf("Could not parse %s as host name or address\n", name);
    224   1.5  mycroft 	}
    225   1.1  mycroft     }
    226   1.5  mycroft     return addr;
    227   1.1  mycroft }
    228   1.1  mycroft 
    229   1.1  mycroft 
    230   1.1  mycroft char *
    231   1.1  mycroft proto_type(type)
    232   1.5  mycroft     u_int type;
    233   1.1  mycroft {
    234   1.1  mycroft     static char buf[80];
    235   1.1  mycroft 
    236   1.1  mycroft     switch (type) {
    237   1.1  mycroft       case PROTO_DVMRP:
    238   1.1  mycroft 	return ("DVMRP");
    239   1.1  mycroft       case PROTO_MOSPF:
    240   1.1  mycroft 	return ("MOSPF");
    241   1.1  mycroft       case PROTO_PIM:
    242   1.1  mycroft 	return ("PIM");
    243   1.1  mycroft       case PROTO_CBT:
    244   1.1  mycroft 	return ("CBT");
    245  1.12       he       case PROTO_PIM_SPEC:
    246  1.12       he 	return ("PIM-special");
    247  1.12       he       case PROTO_PIM_STAT:
    248  1.12       he 	return ("PIM-static");
    249  1.12       he       case PROTO_DVMRP_STAT:
    250  1.12       he 	return ("DVMRP-static");
    251  1.12       he       case PROTO_PIM_MBGP:
    252  1.12       he 	return ("PIM/MBGP");
    253   1.1  mycroft       default:
    254   1.7      mrg 	(void)snprintf(buf, sizeof buf, "Unknown protocol code %d", type);
    255   1.1  mycroft 	return (buf);
    256   1.1  mycroft     }
    257   1.1  mycroft }
    258   1.1  mycroft 
    259   1.1  mycroft 
    260   1.1  mycroft char *
    261   1.1  mycroft flag_type(type)
    262   1.5  mycroft     u_int type;
    263   1.1  mycroft {
    264   1.1  mycroft     static char buf[80];
    265   1.1  mycroft 
    266   1.1  mycroft     switch (type) {
    267   1.1  mycroft       case TR_NO_ERR:
    268   1.1  mycroft 	return ("");
    269   1.1  mycroft       case TR_WRONG_IF:
    270   1.1  mycroft 	return ("Wrong interface");
    271   1.1  mycroft       case TR_PRUNED:
    272   1.1  mycroft 	return ("Prune sent upstream");
    273   1.1  mycroft       case TR_OPRUNED:
    274   1.1  mycroft 	return ("Output pruned");
    275   1.1  mycroft       case TR_SCOPED:
    276   1.1  mycroft 	return ("Hit scope boundary");
    277   1.1  mycroft       case TR_NO_RTE:
    278   1.1  mycroft 	return ("No route");
    279   1.1  mycroft       case TR_OLD_ROUTER:
    280   1.1  mycroft 	return ("Next router no mtrace");
    281   1.1  mycroft       case TR_NO_FWD:
    282   1.1  mycroft 	return ("Not forwarding");
    283   1.1  mycroft       case TR_NO_SPACE:
    284   1.1  mycroft 	return ("No space in packet");
    285  1.12       he       case TR_RP_OR_CORE:
    286  1.12       he 	return ("RP/Core");
    287  1.12       he       case TR_RPF_INT:
    288  1.12       he 	return ("Trace packet on RPT interface");
    289  1.12       he       case TR_NO_MULTICAST:
    290  1.12       he 	return ("Trace packet on non-MC interface");
    291  1.12       he       case TR_ADMIN_DENY:
    292  1.12       he 	return ("Trace admin-denied");
    293   1.1  mycroft       default:
    294   1.7      mrg 	(void)snprintf(buf, sizeof buf, "Unknown error code %d", type);
    295   1.1  mycroft 	return (buf);
    296   1.1  mycroft     }
    297   1.1  mycroft }
    298   1.1  mycroft 
    299   1.1  mycroft /*
    300   1.1  mycroft  * If destination is on a local net, get the netmask, else set the
    301   1.1  mycroft  * netmask to all ones.  There are two side effects: if the local
    302   1.1  mycroft  * address was not explicitly set, and if the destination is on a
    303   1.1  mycroft  * local net, use that one; in either case, verify that the local
    304   1.1  mycroft  * address is valid.
    305   1.1  mycroft  */
    306   1.1  mycroft 
    307   1.1  mycroft u_int32_t
    308   1.1  mycroft get_netmask(s, dst)
    309   1.1  mycroft     int s;
    310   1.1  mycroft     u_int32_t dst;
    311   1.1  mycroft {
    312   1.5  mycroft     unsigned int i;
    313   1.5  mycroft     char ifbuf[5000];
    314   1.1  mycroft     struct ifconf ifc;
    315   1.1  mycroft     struct ifreq *ifr;
    316   1.1  mycroft     u_int32_t if_addr, if_mask;
    317   1.1  mycroft     u_int32_t retval = 0xFFFFFFFF;
    318   1.1  mycroft     int found = FALSE;
    319   1.1  mycroft 
    320   1.5  mycroft     ifc.ifc_buf = ifbuf;
    321   1.5  mycroft     ifc.ifc_len = sizeof(ifbuf);
    322   1.5  mycroft     if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) {
    323   1.1  mycroft 	perror("ioctl (SIOCGIFCONF)");
    324   1.1  mycroft 	return (retval);
    325   1.1  mycroft     }
    326   1.2  thorpej     for (i = 0; i < ifc.ifc_len; ) {
    327   1.2  thorpej 	ifr = (struct ifreq *)((char *)ifc.ifc_req + i);
    328   1.2  thorpej 	i += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
    329   1.1  mycroft 	if_addr = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
    330   1.1  mycroft 	if (ioctl(s, SIOCGIFNETMASK, (char *)ifr) >= 0) {
    331   1.1  mycroft 	    if_mask = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
    332   1.1  mycroft 	    if ((dst & if_mask) == (if_addr & if_mask)) {
    333   1.1  mycroft 		retval = if_mask;
    334   1.5  mycroft 		if (lcl_addr == 0) lcl_addr = if_addr;
    335   1.1  mycroft 	    }
    336   1.1  mycroft 	}
    337   1.5  mycroft 	if (lcl_addr == if_addr) found = TRUE;
    338   1.1  mycroft     }
    339   1.1  mycroft     if (!found && lcl_addr != 0) {
    340   1.1  mycroft 	printf("Interface address is not valid\n");
    341   1.1  mycroft 	exit(1);
    342   1.1  mycroft     }
    343   1.1  mycroft     return (retval);
    344   1.1  mycroft }
    345   1.1  mycroft 
    346   1.1  mycroft 
    347   1.1  mycroft int
    348   1.1  mycroft get_ttl(buf)
    349   1.1  mycroft     struct resp_buf *buf;
    350   1.1  mycroft {
    351   1.5  mycroft     int rno;
    352   1.5  mycroft     struct tr_resp *b;
    353   1.5  mycroft     u_int ttl;
    354   1.1  mycroft 
    355   1.1  mycroft     if (buf && (rno = buf->len) > 0) {
    356   1.1  mycroft 	b = buf->resps + rno - 1;
    357   1.1  mycroft 	ttl = b->tr_fttl;
    358   1.1  mycroft 
    359   1.1  mycroft 	while (--rno > 0) {
    360   1.1  mycroft 	    --b;
    361   1.5  mycroft 	    if (ttl < b->tr_fttl) ttl = b->tr_fttl;
    362   1.5  mycroft 	    else ++ttl;
    363   1.1  mycroft 	}
    364   1.1  mycroft 	ttl += MULTICAST_TTL_INC;
    365   1.5  mycroft 	if (ttl < MULTICAST_TTL1) ttl = MULTICAST_TTL1;
    366   1.5  mycroft 	if (ttl > MULTICAST_TTL_MAX) ttl = MULTICAST_TTL_MAX;
    367   1.1  mycroft 	return (ttl);
    368   1.5  mycroft     } else return(MULTICAST_TTL1);
    369   1.1  mycroft }
    370   1.1  mycroft 
    371   1.1  mycroft /*
    372   1.1  mycroft  * Calculate the difference between two 32-bit NTP timestamps and return
    373   1.1  mycroft  * the result in milliseconds.
    374   1.1  mycroft  */
    375   1.1  mycroft int
    376   1.1  mycroft t_diff(a, b)
    377   1.1  mycroft     u_long a, b;
    378   1.1  mycroft {
    379   1.1  mycroft     int d = a - b;
    380   1.1  mycroft 
    381   1.1  mycroft     return ((d * 125) >> 13);
    382   1.1  mycroft }
    383   1.1  mycroft 
    384   1.1  mycroft /*
    385   1.1  mycroft  * Fixup for incorrect time format in 3.3 mrouted.
    386   1.1  mycroft  * This is possible because (JAN_1970 mod 64K) is quite close to 32K,
    387   1.1  mycroft  * so correct and incorrect times will be far apart.
    388   1.1  mycroft  */
    389   1.1  mycroft u_long
    390   1.1  mycroft fixtime(time)
    391   1.1  mycroft     u_long time;
    392   1.1  mycroft {
    393   1.1  mycroft     if (abs((int)(time-base.qtime)) > 0x3FFFFFFF)
    394   1.1  mycroft         time = ((time & 0xFFFF0000) + (JAN_1970 << 16)) +
    395   1.1  mycroft 	       ((time & 0xFFFF) << 14) / 15625;
    396   1.1  mycroft     return (time);
    397   1.1  mycroft }
    398   1.1  mycroft 
    399   1.5  mycroft /*
    400   1.5  mycroft  * Swap bytes for poor little-endian machines that don't byte-swap
    401   1.5  mycroft  */
    402   1.5  mycroft u_long
    403   1.5  mycroft byteswap(v)
    404   1.5  mycroft     u_long v;
    405   1.5  mycroft {
    406   1.5  mycroft     return ((v << 24) | ((v & 0xff00) << 8) |
    407   1.5  mycroft 	    ((v >> 8) & 0xff00) | (v >> 24));
    408   1.5  mycroft }
    409   1.5  mycroft 
    410   1.1  mycroft int
    411   1.1  mycroft send_recv(dst, type, code, tries, save)
    412   1.1  mycroft     u_int32_t dst;
    413   1.1  mycroft     int type, code, tries;
    414   1.1  mycroft     struct resp_buf *save;
    415   1.1  mycroft {
    416   1.1  mycroft     fd_set  fds;
    417   1.1  mycroft     struct timeval tq, tr, tv;
    418   1.1  mycroft     struct ip *ip;
    419   1.1  mycroft     struct igmp *igmp;
    420   1.1  mycroft     struct tr_query *query, *rquery;
    421   1.1  mycroft     int ipdatalen, iphdrlen, igmpdatalen;
    422   1.1  mycroft     u_int32_t local, group;
    423   1.1  mycroft     int datalen;
    424   1.1  mycroft     int count, recvlen, dummy = 0;
    425   1.1  mycroft     int len;
    426   1.2  thorpej     int i;
    427   1.1  mycroft 
    428   1.1  mycroft     if (type == IGMP_MTRACE_QUERY) {
    429   1.1  mycroft 	group = qgrp;
    430   1.1  mycroft 	datalen = sizeof(struct tr_query);
    431   1.1  mycroft     } else {
    432   1.1  mycroft 	group = htonl(MROUTED_LEVEL);
    433   1.1  mycroft 	datalen = 0;
    434   1.1  mycroft     }
    435   1.5  mycroft     if (IN_MULTICAST(ntohl(dst))) local = lcl_addr;
    436   1.5  mycroft     else local = INADDR_ANY;
    437   1.1  mycroft 
    438   1.1  mycroft     /*
    439   1.1  mycroft      * If the reply address was not explictly specified, start off
    440   1.1  mycroft      * with the unicast address of this host.  Then, if there is no
    441   1.1  mycroft      * response after trying half the tries with unicast, switch to
    442   1.1  mycroft      * the standard multicast reply address.  If the TTL was also not
    443   1.1  mycroft      * specified, set a multicast TTL and if needed increase it for the
    444   1.1  mycroft      * last quarter of the tries.
    445   1.1  mycroft      */
    446   1.1  mycroft     query = (struct tr_query *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
    447   1.1  mycroft     query->tr_raddr = raddr ? raddr : multicast ? resp_cast : lcl_addr;
    448   1.1  mycroft     query->tr_rttl  = rttl ? rttl :
    449   1.1  mycroft       IN_MULTICAST(ntohl(query->tr_raddr)) ? get_ttl(save) : UNICAST_TTL;
    450   1.5  mycroft     query->tr_src   = qsrc;
    451   1.5  mycroft     query->tr_dst   = qdst;
    452   1.1  mycroft 
    453   1.1  mycroft     for (i = tries ; i > 0; --i) {
    454   1.1  mycroft 	if (tries == nqueries && raddr == 0) {
    455   1.1  mycroft 	    if (i == ((nqueries + 1) >> 1)) {
    456   1.1  mycroft 		query->tr_raddr = resp_cast;
    457   1.1  mycroft 		if (rttl == 0) query->tr_rttl = get_ttl(save);
    458   1.1  mycroft 	    }
    459   1.1  mycroft 	    if (i <= ((nqueries + 3) >> 2) && rttl == 0) {
    460   1.1  mycroft 		query->tr_rttl += MULTICAST_TTL_INC;
    461   1.1  mycroft 		if (query->tr_rttl > MULTICAST_TTL_MAX)
    462   1.1  mycroft 		  query->tr_rttl = MULTICAST_TTL_MAX;
    463   1.1  mycroft 	    }
    464   1.1  mycroft 	}
    465   1.1  mycroft 
    466   1.1  mycroft 	/*
    467   1.1  mycroft 	 * Change the qid for each request sent to avoid being confused
    468   1.1  mycroft 	 * by duplicate responses
    469   1.1  mycroft 	 */
    470   1.5  mycroft #ifdef SYSV
    471   1.5  mycroft 	query->tr_qid  = ((u_int32_t)lrand48() >> 8);
    472   1.5  mycroft #else
    473   1.1  mycroft 	query->tr_qid  = ((u_int32_t)random() >> 8);
    474   1.5  mycroft #endif
    475   1.1  mycroft 
    476   1.1  mycroft 	/*
    477   1.1  mycroft 	 * Set timer to calculate delays, then send query
    478   1.1  mycroft 	 */
    479   1.1  mycroft 	gettimeofday(&tq, 0);
    480   1.1  mycroft 	send_igmp(local, dst, type, code, group, datalen);
    481   1.1  mycroft 
    482   1.1  mycroft 	/*
    483   1.1  mycroft 	 * Wait for response, discarding false alarms
    484   1.1  mycroft 	 */
    485   1.1  mycroft 	while (TRUE) {
    486   1.1  mycroft 	    FD_ZERO(&fds);
    487   1.1  mycroft 	    FD_SET(igmp_socket, &fds);
    488   1.1  mycroft 	    gettimeofday(&tv, 0);
    489   1.1  mycroft 	    tv.tv_sec = tq.tv_sec + timeout - tv.tv_sec;
    490   1.1  mycroft 	    tv.tv_usec = tq.tv_usec - tv.tv_usec;
    491   1.5  mycroft 	    if (tv.tv_usec < 0) tv.tv_usec += 1000000L, --tv.tv_sec;
    492   1.5  mycroft 	    if (tv.tv_sec < 0) tv.tv_sec = tv.tv_usec = 0;
    493   1.1  mycroft 
    494   1.1  mycroft 	    count = select(igmp_socket + 1, &fds, (fd_set *)0, (fd_set *)0,
    495   1.1  mycroft 			   &tv);
    496   1.1  mycroft 
    497   1.1  mycroft 	    if (count < 0) {
    498   1.5  mycroft 		if (errno != EINTR) perror("select");
    499   1.1  mycroft 		continue;
    500   1.1  mycroft 	    } else if (count == 0) {
    501   1.1  mycroft 		printf("* ");
    502   1.1  mycroft 		fflush(stdout);
    503   1.1  mycroft 		break;
    504   1.1  mycroft 	    }
    505   1.1  mycroft 
    506   1.1  mycroft 	    gettimeofday(&tr, 0);
    507   1.1  mycroft 	    recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
    508   1.1  mycroft 			       0, (struct sockaddr *)0, &dummy);
    509   1.1  mycroft 
    510   1.1  mycroft 	    if (recvlen <= 0) {
    511   1.5  mycroft 		if (recvlen && errno != EINTR) perror("recvfrom");
    512   1.1  mycroft 		continue;
    513   1.1  mycroft 	    }
    514   1.1  mycroft 
    515   1.1  mycroft 	    if (recvlen < sizeof(struct ip)) {
    516   1.1  mycroft 		fprintf(stderr,
    517   1.1  mycroft 			"packet too short (%u bytes) for IP header", recvlen);
    518   1.1  mycroft 		continue;
    519   1.1  mycroft 	    }
    520   1.1  mycroft 	    ip = (struct ip *) recv_buf;
    521   1.1  mycroft 	    if (ip->ip_p == 0)	/* ignore cache creation requests */
    522   1.1  mycroft 		continue;
    523   1.1  mycroft 
    524   1.1  mycroft 	    iphdrlen = ip->ip_hl << 2;
    525   1.1  mycroft 	    ipdatalen = ip->ip_len;
    526   1.1  mycroft 	    if (iphdrlen + ipdatalen != recvlen) {
    527   1.1  mycroft 		fprintf(stderr,
    528   1.1  mycroft 			"packet shorter (%u bytes) than hdr+data len (%u+%u)\n",
    529   1.1  mycroft 			recvlen, iphdrlen, ipdatalen);
    530   1.1  mycroft 		continue;
    531   1.1  mycroft 	    }
    532   1.1  mycroft 
    533   1.1  mycroft 	    igmp = (struct igmp *) (recv_buf + iphdrlen);
    534   1.1  mycroft 	    igmpdatalen = ipdatalen - IGMP_MINLEN;
    535   1.1  mycroft 	    if (igmpdatalen < 0) {
    536   1.1  mycroft 		fprintf(stderr,
    537   1.1  mycroft 			"IP data field too short (%u bytes) for IGMP from %s\n",
    538   1.1  mycroft 			ipdatalen, inet_fmt(ip->ip_src.s_addr, s1));
    539   1.1  mycroft 		continue;
    540   1.1  mycroft 	    }
    541   1.1  mycroft 
    542   1.1  mycroft 	    switch (igmp->igmp_type) {
    543   1.1  mycroft 
    544   1.1  mycroft 	      case IGMP_DVMRP:
    545   1.5  mycroft 		if (igmp->igmp_code != DVMRP_NEIGHBORS2) continue;
    546   1.1  mycroft 		len = igmpdatalen;
    547   1.5  mycroft 		/*
    548   1.5  mycroft 		 * Accept DVMRP_NEIGHBORS2 response if it comes from the
    549   1.5  mycroft 		 * address queried or if that address is one of the local
    550   1.5  mycroft 		 * addresses in the response.
    551   1.5  mycroft 		 */
    552   1.5  mycroft 		if (ip->ip_src.s_addr != dst) {
    553   1.5  mycroft 		    u_int32_t *p = (u_int32_t *)(igmp + 1);
    554   1.5  mycroft 		    u_int32_t *ep = p + (len >> 2);
    555   1.5  mycroft 		    while (p < ep) {
    556   1.5  mycroft 			u_int32_t laddr = *p++;
    557   1.5  mycroft 			int n = ntohl(*p++) & 0xFF;
    558   1.5  mycroft 			if (laddr == dst) {
    559   1.5  mycroft 			    ep = p + 1;		/* ensure p < ep after loop */
    560   1.5  mycroft 			    break;
    561   1.5  mycroft 			}
    562   1.5  mycroft 			p += n;
    563   1.5  mycroft 		    }
    564   1.5  mycroft 		    if (p >= ep) continue;
    565   1.5  mycroft 		}
    566   1.1  mycroft 		break;
    567   1.1  mycroft 
    568   1.5  mycroft 	      case IGMP_MTRACE_QUERY:	    /* For backward compatibility with 3.3 */
    569   1.1  mycroft 	      case IGMP_MTRACE_REPLY:
    570   1.5  mycroft 		if (igmpdatalen <= QLEN) continue;
    571   1.1  mycroft 		if ((igmpdatalen - QLEN)%RLEN) {
    572   1.1  mycroft 		    printf("packet with incorrect datalen\n");
    573   1.1  mycroft 		    continue;
    574   1.1  mycroft 		}
    575   1.1  mycroft 
    576   1.1  mycroft 		/*
    577   1.1  mycroft 		 * Ignore responses that don't match query.
    578   1.1  mycroft 		 */
    579   1.1  mycroft 		rquery = (struct tr_query *)(igmp + 1);
    580   1.5  mycroft 		if (rquery->tr_qid != query->tr_qid) continue;
    581   1.5  mycroft 		if (rquery->tr_src != qsrc) continue;
    582   1.5  mycroft 		if (rquery->tr_dst != qdst) continue;
    583   1.1  mycroft 		len = (igmpdatalen - QLEN)/RLEN;
    584   1.1  mycroft 
    585   1.1  mycroft 		/*
    586   1.1  mycroft 		 * Ignore trace queries passing through this node when
    587   1.1  mycroft 		 * mtrace is run on an mrouter that is in the path
    588   1.5  mycroft 		 * (needed only because IGMP_MTRACE_QUERY is accepted above
    589   1.1  mycroft 		 * for backward compatibility with multicast release 3.3).
    590   1.1  mycroft 		 */
    591   1.1  mycroft 		if (igmp->igmp_type == IGMP_MTRACE_QUERY) {
    592   1.1  mycroft 		    struct tr_resp *r = (struct tr_resp *)(rquery+1) + len - 1;
    593   1.1  mycroft 		    u_int32_t smask;
    594   1.1  mycroft 
    595   1.1  mycroft 		    VAL_TO_MASK(smask, r->tr_smask);
    596   1.1  mycroft 		    if (len < code && (r->tr_inaddr & smask) != (qsrc & smask)
    597   1.1  mycroft 			&& r->tr_rmtaddr != 0 && !(r->tr_rflags & 0x80))
    598   1.1  mycroft 		      continue;
    599   1.1  mycroft 		}
    600   1.1  mycroft 
    601   1.1  mycroft 		/*
    602   1.1  mycroft 		 * A match, we'll keep this one.
    603   1.1  mycroft 		 */
    604   1.1  mycroft 		if (len > code) {
    605   1.1  mycroft 		    fprintf(stderr,
    606   1.1  mycroft 			    "Num hops received (%d) exceeds request (%d)\n",
    607   1.1  mycroft 			    len, code);
    608   1.1  mycroft 		}
    609   1.1  mycroft 		rquery->tr_raddr = query->tr_raddr;	/* Insure these are */
    610   1.1  mycroft 		rquery->tr_rttl = query->tr_rttl;	/* as we sent them */
    611   1.1  mycroft 		break;
    612   1.1  mycroft 
    613   1.1  mycroft 	      default:
    614   1.1  mycroft 		continue;
    615   1.1  mycroft 	    }
    616   1.1  mycroft 
    617   1.1  mycroft 	    /*
    618   1.1  mycroft 	     * Most of the sanity checking done at this point.
    619   1.1  mycroft 	     * Return this packet we have been waiting for.
    620   1.1  mycroft 	     */
    621   1.1  mycroft 	    if (save) {
    622   1.1  mycroft 		save->qtime = ((tq.tv_sec + JAN_1970) << 16) +
    623   1.1  mycroft 			      (tq.tv_usec << 10) / 15625;
    624   1.1  mycroft 		save->rtime = ((tr.tv_sec + JAN_1970) << 16) +
    625   1.1  mycroft 			      (tr.tv_usec << 10) / 15625;
    626   1.1  mycroft 		save->len = len;
    627   1.1  mycroft 		bcopy((char *)igmp, (char *)&save->igmp, ipdatalen);
    628   1.1  mycroft 	    }
    629   1.1  mycroft 	    return (recvlen);
    630   1.1  mycroft 	}
    631   1.1  mycroft     }
    632   1.1  mycroft     return (0);
    633   1.1  mycroft }
    634   1.1  mycroft 
    635   1.5  mycroft /*
    636   1.5  mycroft  * Most of this code is duplicated elsewhere.  I'm not sure if
    637   1.5  mycroft  * the duplication is absolutely required or not.
    638   1.5  mycroft  *
    639   1.5  mycroft  * Ideally, this would keep track of ongoing statistics
    640   1.5  mycroft  * collection and print out statistics.  (& keep track
    641   1.5  mycroft  * of h-b-h traces and only print the longest)  For now,
    642   1.5  mycroft  * it just snoops on what traces it can.
    643   1.5  mycroft  */
    644   1.5  mycroft void
    645   1.5  mycroft passive_mode()
    646   1.5  mycroft {
    647   1.5  mycroft     struct timeval tr;
    648   1.5  mycroft     struct ip *ip;
    649   1.5  mycroft     struct igmp *igmp;
    650   1.5  mycroft     struct tr_resp *r;
    651   1.5  mycroft     int ipdatalen, iphdrlen, igmpdatalen;
    652   1.5  mycroft     int len, recvlen, dummy = 0;
    653   1.5  mycroft     u_int32_t smask;
    654   1.5  mycroft 
    655   1.5  mycroft     init_igmp();
    656   1.5  mycroft 
    657   1.5  mycroft     if (raddr) {
    658   1.5  mycroft 	if (IN_MULTICAST(ntohl(raddr))) k_join(raddr, INADDR_ANY);
    659   1.5  mycroft     } else k_join(htonl(0xE0000120), INADDR_ANY);
    660   1.5  mycroft 
    661   1.5  mycroft     while (1) {
    662   1.5  mycroft 	recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
    663   1.5  mycroft 			   0, (struct sockaddr *)0, &dummy);
    664   1.5  mycroft 	gettimeofday(&tr,0);
    665   1.5  mycroft 
    666   1.5  mycroft 	if (recvlen <= 0) {
    667   1.5  mycroft 	    if (recvlen && errno != EINTR) perror("recvfrom");
    668   1.5  mycroft 	    continue;
    669   1.5  mycroft 	}
    670   1.5  mycroft 
    671   1.5  mycroft 	if (recvlen < sizeof(struct ip)) {
    672   1.5  mycroft 	    fprintf(stderr,
    673   1.5  mycroft 		    "packet too short (%u bytes) for IP header", recvlen);
    674   1.5  mycroft 	    continue;
    675   1.5  mycroft 	}
    676   1.5  mycroft 	ip = (struct ip *) recv_buf;
    677   1.5  mycroft 	if (ip->ip_p == 0)	/* ignore cache creation requests */
    678   1.5  mycroft 	    continue;
    679   1.5  mycroft 
    680   1.5  mycroft 	iphdrlen = ip->ip_hl << 2;
    681   1.5  mycroft 	ipdatalen = ip->ip_len;
    682   1.5  mycroft 	if (iphdrlen + ipdatalen != recvlen) {
    683   1.5  mycroft 	    fprintf(stderr,
    684   1.5  mycroft 		    "packet shorter (%u bytes) than hdr+data len (%u+%u)\n",
    685   1.5  mycroft 		    recvlen, iphdrlen, ipdatalen);
    686   1.5  mycroft 	    continue;
    687   1.5  mycroft 	}
    688   1.5  mycroft 
    689   1.5  mycroft 	igmp = (struct igmp *) (recv_buf + iphdrlen);
    690   1.5  mycroft 	igmpdatalen = ipdatalen - IGMP_MINLEN;
    691   1.5  mycroft 	if (igmpdatalen < 0) {
    692   1.5  mycroft 	    fprintf(stderr,
    693   1.5  mycroft 		    "IP data field too short (%u bytes) for IGMP from %s\n",
    694   1.5  mycroft 		    ipdatalen, inet_fmt(ip->ip_src.s_addr, s1));
    695   1.5  mycroft 	    continue;
    696   1.5  mycroft 	}
    697   1.5  mycroft 
    698   1.5  mycroft 	switch (igmp->igmp_type) {
    699   1.5  mycroft 
    700   1.5  mycroft 	  case IGMP_MTRACE_QUERY:	    /* For backward compatibility with 3.3 */
    701   1.5  mycroft 	  case IGMP_MTRACE_REPLY:
    702   1.5  mycroft 	    if (igmpdatalen < QLEN) continue;
    703   1.5  mycroft 	    if ((igmpdatalen - QLEN)%RLEN) {
    704   1.5  mycroft 		printf("packet with incorrect datalen\n");
    705   1.5  mycroft 		continue;
    706   1.5  mycroft 	    }
    707   1.5  mycroft 
    708   1.5  mycroft 	    len = (igmpdatalen - QLEN)/RLEN;
    709   1.5  mycroft 
    710   1.5  mycroft 	    break;
    711   1.5  mycroft 
    712   1.5  mycroft 	  default:
    713   1.5  mycroft 	    continue;
    714   1.5  mycroft 	}
    715   1.5  mycroft 
    716   1.5  mycroft 	base.qtime = ((tr.tv_sec + JAN_1970) << 16) +
    717   1.5  mycroft 		      (tr.tv_usec << 10) / 15625;
    718   1.5  mycroft 	base.rtime = ((tr.tv_sec + JAN_1970) << 16) +
    719   1.5  mycroft 		      (tr.tv_usec << 10) / 15625;
    720   1.5  mycroft 	base.len = len;
    721   1.5  mycroft 	bcopy((char *)igmp, (char *)&base.igmp, ipdatalen);
    722   1.5  mycroft 	/*
    723   1.5  mycroft 	 * If the user specified which traces to monitor,
    724   1.5  mycroft 	 * only accept traces that correspond to the
    725   1.5  mycroft 	 * request
    726   1.5  mycroft 	 */
    727   1.5  mycroft 	if ((qsrc != 0 && qsrc != base.qhdr.tr_src) ||
    728   1.5  mycroft 	    (qdst != 0 && qdst != base.qhdr.tr_dst) ||
    729   1.5  mycroft 	    (qgrp != 0 && qgrp != igmp->igmp_group.s_addr))
    730   1.5  mycroft 	    continue;
    731   1.5  mycroft 
    732   1.5  mycroft 	printf("Mtrace from %s to %s via group %s (mxhop=%d)\n",
    733   1.5  mycroft 		inet_fmt(base.qhdr.tr_dst, s1), inet_fmt(base.qhdr.tr_src, s2),
    734   1.5  mycroft 		inet_fmt(igmp->igmp_group.s_addr, s3), igmp->igmp_code);
    735   1.5  mycroft 	if (len == 0)
    736   1.5  mycroft 	    continue;
    737   1.5  mycroft 	printf("  0  ");
    738   1.5  mycroft 	print_host(base.qhdr.tr_dst);
    739   1.5  mycroft 	printf("\n");
    740   1.5  mycroft 	print_trace(1, &base);
    741   1.5  mycroft 	r = base.resps + base.len - 1;
    742   1.5  mycroft 	VAL_TO_MASK(smask, r->tr_smask);
    743   1.5  mycroft 	if ((r->tr_inaddr & smask) == (base.qhdr.tr_src & smask)) {
    744   1.5  mycroft 	    printf("%3d  ", -(base.len+1));
    745   1.5  mycroft 	    print_host(base.qhdr.tr_src);
    746   1.5  mycroft 	    printf("\n");
    747   1.5  mycroft 	} else if (r->tr_rmtaddr != 0) {
    748   1.5  mycroft 	    printf("%3d  ", -(base.len+1));
    749   1.5  mycroft 	    what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ?
    750   1.5  mycroft 				   "doesn't support mtrace"
    751   1.5  mycroft 				 : "is the next hop");
    752   1.5  mycroft 	}
    753   1.5  mycroft 	printf("\n");
    754   1.5  mycroft     }
    755   1.5  mycroft }
    756   1.1  mycroft 
    757   1.1  mycroft char *
    758   1.1  mycroft print_host(addr)
    759   1.1  mycroft     u_int32_t addr;
    760   1.1  mycroft {
    761   1.5  mycroft     return print_host2(addr, 0);
    762   1.5  mycroft }
    763   1.5  mycroft 
    764   1.5  mycroft /*
    765   1.5  mycroft  * On some routers, one interface has a name and the other doesn't.
    766   1.5  mycroft  * We always print the address of the outgoing interface, but can
    767   1.5  mycroft  * sometimes get the name from the incoming interface.  This might be
    768   1.5  mycroft  * confusing but should be slightly more helpful than just a "?".
    769   1.5  mycroft  */
    770   1.5  mycroft char *
    771   1.5  mycroft print_host2(addr1, addr2)
    772   1.5  mycroft     u_int32_t addr1, addr2;
    773   1.5  mycroft {
    774   1.1  mycroft     char *name;
    775   1.1  mycroft 
    776   1.1  mycroft     if (numeric) {
    777   1.5  mycroft 	printf("%s", inet_fmt(addr1, s1));
    778   1.1  mycroft 	return ("");
    779   1.1  mycroft     }
    780   1.5  mycroft     name = inet_name(addr1);
    781   1.5  mycroft     if (*name == '?' && *(name + 1) == '\0' && addr2 != 0)
    782   1.5  mycroft 	name = inet_name(addr2);
    783   1.5  mycroft     printf("%s (%s)", name, inet_fmt(addr1, s1));
    784   1.1  mycroft     return (name);
    785   1.1  mycroft }
    786   1.1  mycroft 
    787   1.1  mycroft /*
    788   1.1  mycroft  * Print responses as received (reverse path from dst to src)
    789   1.1  mycroft  */
    790   1.1  mycroft void
    791   1.1  mycroft print_trace(index, buf)
    792   1.1  mycroft     int index;
    793   1.1  mycroft     struct resp_buf *buf;
    794   1.1  mycroft {
    795   1.1  mycroft     struct tr_resp *r;
    796   1.1  mycroft     char *name;
    797   1.1  mycroft     int i;
    798   1.5  mycroft     int hop;
    799  1.13       he     char *ms, *ft;
    800   1.1  mycroft 
    801   1.1  mycroft     i = abs(index);
    802   1.1  mycroft     r = buf->resps + i - 1;
    803   1.1  mycroft 
    804   1.1  mycroft     for (; i <= buf->len; ++i, ++r) {
    805   1.1  mycroft 	if (index > 0) printf("%3d  ", -i);
    806   1.5  mycroft 	name = print_host2(r->tr_outaddr, r->tr_inaddr);
    807   1.5  mycroft 	printf("  %s  thresh^ %d", proto_type(r->tr_rproto), r->tr_fttl);
    808   1.5  mycroft 	if (verbose) {
    809   1.5  mycroft 	    hop = t_diff(fixtime(ntohl(r->tr_qarr)), buf->qtime);
    810   1.5  mycroft 	    ms = scale(&hop);
    811   1.5  mycroft 	    printf("  %d%s", hop, ms);
    812   1.5  mycroft 	}
    813  1.13       he 	ft = flag_type(r->tr_rflags);
    814  1.13       he 	if (strlen(ft) != 0)
    815  1.13       he 	    printf("  %s", ft);
    816  1.13       he 	printf("\n");
    817   1.1  mycroft 	memcpy(names[i-1], name, sizeof(names[0]) - 1);
    818   1.1  mycroft 	names[i-1][sizeof(names[0])-1] = '\0';
    819   1.1  mycroft     }
    820   1.1  mycroft }
    821   1.1  mycroft 
    822   1.1  mycroft /*
    823   1.1  mycroft  * See what kind of router is the next hop
    824   1.1  mycroft  */
    825   1.5  mycroft int
    826   1.5  mycroft what_kind(buf, why)
    827   1.1  mycroft     struct resp_buf *buf;
    828   1.5  mycroft     char *why;
    829   1.1  mycroft {
    830   1.1  mycroft     u_int32_t smask;
    831   1.5  mycroft     int retval;
    832   1.1  mycroft     int hops = buf->len;
    833   1.1  mycroft     struct tr_resp *r = buf->resps + hops - 1;
    834   1.1  mycroft     u_int32_t next = r->tr_rmtaddr;
    835   1.1  mycroft 
    836   1.5  mycroft     retval = send_recv(next, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0]);
    837   1.1  mycroft     print_host(next);
    838   1.5  mycroft     if (retval) {
    839   1.1  mycroft 	u_int32_t version = ntohl(incr[0].igmp.igmp_group.s_addr);
    840   1.1  mycroft 	u_int32_t *p = (u_int32_t *)incr[0].ndata;
    841   1.1  mycroft 	u_int32_t *ep = p + (incr[0].len >> 2);
    842   1.5  mycroft 	char *type = "";
    843   1.5  mycroft 	retval = 0;
    844   1.5  mycroft 	switch (version & 0xFF) {
    845   1.5  mycroft 	  case 1:
    846   1.5  mycroft 	    type = "proteon/mrouted ";
    847   1.5  mycroft 	    retval = 1;
    848   1.5  mycroft 	    break;
    849   1.5  mycroft 
    850   1.5  mycroft 	  case 2:
    851   1.5  mycroft 	  case 3:
    852   1.5  mycroft 	    if (((version >> 8) & 0xFF) < 3) retval = 1;
    853   1.5  mycroft 				/* Fall through */
    854   1.5  mycroft 	  case 4:
    855   1.5  mycroft 	    type = "mrouted ";
    856   1.5  mycroft 	    break;
    857   1.5  mycroft 
    858   1.5  mycroft 	  case 10:
    859   1.5  mycroft 	    type = "cisco ";
    860   1.5  mycroft 	}
    861   1.5  mycroft 	printf(" [%s%d.%d] %s\n",
    862   1.5  mycroft 	       type, version & 0xFF, (version >> 8) & 0xFF,
    863   1.5  mycroft 	       why);
    864   1.1  mycroft 	VAL_TO_MASK(smask, r->tr_smask);
    865   1.1  mycroft 	while (p < ep) {
    866   1.5  mycroft 	    u_int32_t laddr = *p++;
    867   1.5  mycroft 	    int flags = (ntohl(*p) & 0xFF00) >> 8;
    868   1.5  mycroft 	    int n = ntohl(*p++) & 0xFF;
    869   1.5  mycroft 	    if (!(flags & (DVMRP_NF_DOWN | DVMRP_NF_DISABLED)) &&
    870   1.5  mycroft 		 (laddr & smask) == (qsrc & smask)) {
    871   1.1  mycroft 		printf("%3d  ", -(hops+2));
    872   1.1  mycroft 		print_host(qsrc);
    873   1.1  mycroft 		printf("\n");
    874   1.5  mycroft 		return 1;
    875   1.1  mycroft 	    }
    876   1.1  mycroft 	    p += n;
    877   1.1  mycroft 	}
    878   1.5  mycroft 	return retval;
    879   1.1  mycroft     }
    880   1.5  mycroft     printf(" %s\n", why);
    881   1.5  mycroft     return 0;
    882   1.1  mycroft }
    883   1.1  mycroft 
    884   1.1  mycroft 
    885   1.1  mycroft char *
    886   1.1  mycroft scale(hop)
    887   1.1  mycroft     int *hop;
    888   1.1  mycroft {
    889   1.1  mycroft     if (*hop > -1000 && *hop < 10000) return (" ms");
    890   1.1  mycroft     *hop /= 1000;
    891   1.1  mycroft     if (*hop > -1000 && *hop < 10000) return (" s ");
    892   1.1  mycroft     return ("s ");
    893   1.1  mycroft }
    894   1.1  mycroft 
    895   1.1  mycroft /*
    896   1.1  mycroft  * Calculate and print one line of packet loss and packet rate statistics.
    897   1.1  mycroft  * Checks for count of all ones from mrouted 2.3 that doesn't have counters.
    898   1.1  mycroft  */
    899   1.1  mycroft #define NEITHER 0
    900   1.1  mycroft #define INS     1
    901   1.1  mycroft #define OUTS    2
    902   1.1  mycroft #define BOTH    3
    903   1.1  mycroft void
    904   1.5  mycroft stat_line(r, s, have_next, rst)
    905   1.1  mycroft     struct tr_resp *r, *s;
    906   1.1  mycroft     int have_next;
    907   1.5  mycroft     int *rst;
    908   1.1  mycroft {
    909   1.5  mycroft     int timediff = (fixtime(ntohl(s->tr_qarr)) -
    910   1.1  mycroft 			 fixtime(ntohl(r->tr_qarr))) >> 16;
    911   1.5  mycroft     int v_lost, v_pct;
    912   1.5  mycroft     int g_lost, g_pct;
    913   1.5  mycroft     int v_out = ntohl(s->tr_vifout) - ntohl(r->tr_vifout);
    914   1.5  mycroft     int g_out = ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt);
    915   1.5  mycroft     int v_pps, g_pps;
    916   1.1  mycroft     char v_str[8], g_str[8];
    917   1.5  mycroft     int have = NEITHER;
    918   1.5  mycroft     int res = *rst;
    919   1.1  mycroft 
    920   1.5  mycroft     if (timediff == 0) timediff = 1;
    921   1.1  mycroft     v_pps = v_out / timediff;
    922   1.1  mycroft     g_pps = g_out / timediff;
    923   1.1  mycroft 
    924   1.8    lukem     if ((v_out && (s->tr_vifout != 0xFFFFFFFF && s->tr_vifout != 0)) ||
    925   1.5  mycroft 		 (r->tr_vifout != 0xFFFFFFFF && r->tr_vifout != 0))
    926   1.5  mycroft 	    have |= OUTS;
    927   1.1  mycroft 
    928   1.1  mycroft     if (have_next) {
    929   1.5  mycroft 	--r,  --s,  --rst;
    930   1.5  mycroft 	if ((s->tr_vifin != 0xFFFFFFFF && s->tr_vifin != 0) ||
    931   1.5  mycroft 	    (r->tr_vifin != 0xFFFFFFFF && r->tr_vifin != 0))
    932   1.1  mycroft 	  have |= INS;
    933   1.5  mycroft 	if (*rst)
    934   1.5  mycroft 	  res = 1;
    935   1.1  mycroft     }
    936   1.1  mycroft 
    937   1.1  mycroft     switch (have) {
    938   1.1  mycroft       case BOTH:
    939   1.1  mycroft 	v_lost = v_out - (ntohl(s->tr_vifin) - ntohl(r->tr_vifin));
    940   1.5  mycroft 	if (v_out) v_pct = (v_lost * 100 + (v_out >> 1)) / v_out;
    941   1.5  mycroft 	else v_pct = 0;
    942   1.1  mycroft 	if (-100 < v_pct && v_pct < 101 && v_out > 10)
    943   1.7      mrg 	  (void)snprintf(v_str, sizeof v_str, "%3d", v_pct);
    944   1.5  mycroft 	else memcpy(v_str, " --", 4);
    945   1.1  mycroft 
    946   1.1  mycroft 	g_lost = g_out - (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
    947   1.5  mycroft 	if (g_out) g_pct = (g_lost * 100 + (g_out >> 1))/ g_out;
    948   1.5  mycroft 	else g_pct = 0;
    949   1.1  mycroft 	if (-100 < g_pct && g_pct < 101 && g_out > 10)
    950   1.7      mrg 	  (void)snprintf(g_str, sizeof g_str, "%3d", g_pct);
    951   1.5  mycroft 	else memcpy(g_str, " --", 4);
    952   1.5  mycroft 
    953   1.5  mycroft 	printf("%6d/%-5d=%s%%%4d pps",
    954   1.5  mycroft 	       v_lost, v_out, v_str, v_pps);
    955   1.5  mycroft 	if (res)
    956   1.5  mycroft 	    printf("\n");
    957   1.2  thorpej 	else
    958   1.5  mycroft 	    printf("%6d/%-5d=%s%%%4d pps\n",
    959   1.5  mycroft 		   g_lost, g_out, g_str, g_pps);
    960   1.1  mycroft 	break;
    961   1.1  mycroft 
    962   1.1  mycroft       case INS:
    963   1.5  mycroft 	v_out = ntohl(s->tr_vifin) - ntohl(r->tr_vifin);
    964   1.1  mycroft 	v_pps = v_out / timediff;
    965   1.1  mycroft 	/* Fall through */
    966   1.1  mycroft 
    967   1.1  mycroft       case OUTS:
    968   1.5  mycroft 	printf("       %-5d     %4d pps",
    969   1.5  mycroft 	       v_out, v_pps);
    970   1.5  mycroft 	if (res)
    971   1.5  mycroft 	    printf("\n");
    972   1.5  mycroft 	else
    973   1.5  mycroft 	    printf("       %-5d     %4d pps\n",
    974   1.5  mycroft 		   g_out, g_pps);
    975   1.1  mycroft 	break;
    976   1.1  mycroft 
    977   1.1  mycroft       case NEITHER:
    978   1.1  mycroft 	printf("\n");
    979   1.1  mycroft 	break;
    980   1.1  mycroft     }
    981   1.5  mycroft 
    982   1.5  mycroft     if (debug > 2) {
    983   1.8    lukem 	printf("\t\t\t\tv_in: %ld ", (long)ntohl(s->tr_vifin));
    984   1.8    lukem 	printf("v_out: %ld ", (long)ntohl(s->tr_vifout));
    985   1.8    lukem 	printf("pkts: %ld\n", (long)ntohl(s->tr_pktcnt));
    986   1.8    lukem 	printf("\t\t\t\tv_in: %ld ", (long)ntohl(r->tr_vifin));
    987   1.8    lukem 	printf("v_out: %ld ", (long)ntohl(r->tr_vifout));
    988   1.8    lukem 	printf("pkts: %ld\n", (long)ntohl(r->tr_pktcnt));
    989   1.8    lukem 	printf("\t\t\t\tv_in: %ld ",
    990   1.8    lukem 	    (long)ntohl(s->tr_vifin)-ntohl(r->tr_vifin));
    991   1.8    lukem 	printf("v_out: %ld ",
    992   1.8    lukem 	    (long)(ntohl(s->tr_vifout) - ntohl(r->tr_vifout)));
    993   1.8    lukem 	printf("pkts: %ld ", (long)(ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt)));
    994   1.5  mycroft 	printf("time: %d\n", timediff);
    995   1.5  mycroft 	printf("\t\t\t\tres: %d\n", res);
    996   1.5  mycroft     }
    997   1.1  mycroft }
    998   1.1  mycroft 
    999   1.1  mycroft /*
   1000   1.5  mycroft  * A fixup to check if any pktcnt has been reset, and to fix the
   1001   1.5  mycroft  * byteorder bugs in mrouted 3.6 on little-endian machines.
   1002   1.1  mycroft  */
   1003   1.1  mycroft void
   1004   1.5  mycroft fixup_stats(base, prev, new)
   1005   1.5  mycroft     struct resp_buf *base, *prev, *new;
   1006   1.1  mycroft {
   1007   1.5  mycroft     int rno = base->len;
   1008   1.5  mycroft     struct tr_resp *b = base->resps + rno;
   1009   1.5  mycroft     struct tr_resp *p = prev->resps + rno;
   1010   1.5  mycroft     struct tr_resp *n = new->resps + rno;
   1011   1.5  mycroft     int *r = reset + rno;
   1012   1.5  mycroft     int *s = swaps + rno;
   1013   1.5  mycroft     int res;
   1014   1.5  mycroft 
   1015   1.5  mycroft     /* Check for byte-swappers */
   1016   1.5  mycroft     while (--rno >= 0) {
   1017   1.5  mycroft 	--n; --p; --b; --s;
   1018   1.5  mycroft 	if (*s || abs(ntohl(n->tr_vifout) - ntohl(p->tr_vifout)) > 100000) {
   1019   1.5  mycroft 	    /* This host sends byteswapped reports; swap 'em */
   1020   1.5  mycroft 	    if (!*s) {
   1021   1.5  mycroft 		*s = 1;
   1022   1.5  mycroft 		b->tr_qarr = byteswap(b->tr_qarr);
   1023   1.5  mycroft 		b->tr_vifin = byteswap(b->tr_vifin);
   1024   1.5  mycroft 		b->tr_vifout = byteswap(b->tr_vifout);
   1025   1.5  mycroft 		b->tr_pktcnt = byteswap(b->tr_pktcnt);
   1026   1.5  mycroft 	    }
   1027   1.1  mycroft 
   1028   1.5  mycroft 	    n->tr_qarr = byteswap(n->tr_qarr);
   1029   1.5  mycroft 	    n->tr_vifin = byteswap(n->tr_vifin);
   1030   1.5  mycroft 	    n->tr_vifout = byteswap(n->tr_vifout);
   1031   1.5  mycroft 	    n->tr_pktcnt = byteswap(n->tr_pktcnt);
   1032   1.5  mycroft 	}
   1033   1.5  mycroft     }
   1034   1.1  mycroft 
   1035   1.1  mycroft     rno = base->len;
   1036   1.1  mycroft     b = base->resps + rno;
   1037   1.5  mycroft     p = prev->resps + rno;
   1038   1.1  mycroft     n = new->resps + rno;
   1039   1.1  mycroft 
   1040   1.5  mycroft     while (--rno >= 0) {
   1041   1.5  mycroft 	--n; --p; --b; --r;
   1042   1.5  mycroft 	res = ((ntohl(n->tr_pktcnt) < ntohl(b->tr_pktcnt)) ||
   1043   1.5  mycroft 	       (ntohl(n->tr_pktcnt) < ntohl(p->tr_pktcnt)));
   1044   1.5  mycroft 	if (debug > 2)
   1045   1.5  mycroft     	    printf("\t\tr=%d, res=%d\n", *r, res);
   1046   1.5  mycroft 	if (*r) {
   1047   1.5  mycroft 	    if (res || *r > 1) {
   1048   1.5  mycroft 		/*
   1049   1.5  mycroft 		 * This router appears to be a 3.4 with that nasty ol'
   1050   1.5  mycroft 		 * neighbor version bug, which causes it to constantly
   1051   1.5  mycroft 		 * reset.  Just nuke the statistics for this node, and
   1052   1.5  mycroft 		 * don't even bother giving it the benefit of the
   1053   1.5  mycroft 		 * doubt from now on.
   1054   1.5  mycroft 		 */
   1055   1.5  mycroft 		p->tr_pktcnt = b->tr_pktcnt = n->tr_pktcnt;
   1056   1.8    lukem 		r++;
   1057   1.5  mycroft 	    } else {
   1058   1.5  mycroft 		/*
   1059   1.5  mycroft 		 * This is simply the situation that the original
   1060   1.5  mycroft 		 * fixup_stats was meant to deal with -- that a
   1061   1.5  mycroft 		 * 3.3 or 3.4 router deleted a cache entry while
   1062   1.5  mycroft 		 * traffic was still active.
   1063   1.5  mycroft 		 */
   1064   1.5  mycroft 		*r = 0;
   1065   1.5  mycroft 		break;
   1066   1.5  mycroft 	    }
   1067   1.5  mycroft 	} else
   1068   1.5  mycroft 	    *r = res;
   1069   1.5  mycroft     }
   1070   1.5  mycroft 
   1071   1.5  mycroft     if (rno < 0) return;
   1072   1.5  mycroft 
   1073   1.5  mycroft     rno = base->len;
   1074   1.5  mycroft     b = base->resps + rno;
   1075   1.5  mycroft     p = prev->resps + rno;
   1076   1.5  mycroft 
   1077   1.5  mycroft     while (--rno >= 0) (--b)->tr_pktcnt = (--p)->tr_pktcnt;
   1078   1.1  mycroft }
   1079   1.1  mycroft 
   1080   1.1  mycroft /*
   1081   1.1  mycroft  * Print responses with statistics for forward path (from src to dst)
   1082   1.1  mycroft  */
   1083   1.5  mycroft int
   1084   1.1  mycroft print_stats(base, prev, new)
   1085   1.1  mycroft     struct resp_buf *base, *prev, *new;
   1086   1.1  mycroft {
   1087   1.1  mycroft     int rtt, hop;
   1088   1.5  mycroft     char *ms;
   1089   1.5  mycroft     u_int32_t smask;
   1090   1.5  mycroft     int rno = base->len - 1;
   1091   1.5  mycroft     struct tr_resp *b = base->resps + rno;
   1092   1.5  mycroft     struct tr_resp *p = prev->resps + rno;
   1093   1.5  mycroft     struct tr_resp *n = new->resps + rno;
   1094   1.5  mycroft     int *r = reset + rno;
   1095   1.5  mycroft     u_long resptime = new->rtime;
   1096   1.5  mycroft     u_long qarrtime = fixtime(ntohl(n->tr_qarr));
   1097   1.5  mycroft     u_int ttl = n->tr_fttl;
   1098   1.5  mycroft     int first = (base == prev);
   1099   1.1  mycroft 
   1100   1.1  mycroft     VAL_TO_MASK(smask, b->tr_smask);
   1101   1.1  mycroft     printf("  Source        Response Dest");
   1102   1.1  mycroft     printf("    Packet Statistics For     Only For Traffic\n");
   1103   1.1  mycroft     printf("%-15s %-15s  All Multicast Traffic     From %s\n",
   1104   1.1  mycroft 	   ((b->tr_inaddr & smask) == (qsrc & smask)) ? s1 : "   * * *       ",
   1105   1.1  mycroft 	   inet_fmt(base->qhdr.tr_raddr, s2), inet_fmt(qsrc, s1));
   1106   1.1  mycroft     rtt = t_diff(resptime, new->qtime);
   1107   1.1  mycroft     ms = scale(&rtt);
   1108   1.5  mycroft     printf("     %c       __/  rtt%5d%s    Lost/Sent = Pct  Rate       To %s\n",
   1109   1.5  mycroft 	   first ? 'v' : '|', rtt, ms, inet_fmt(qgrp, s2));
   1110   1.5  mycroft     if (!first) {
   1111   1.5  mycroft 	hop = t_diff(resptime, qarrtime);
   1112   1.5  mycroft 	ms = scale(&hop);
   1113   1.5  mycroft 	printf("     v      /     hop%5d%s", hop, ms);
   1114   1.5  mycroft 	printf("    ---------------------     --------------------\n");
   1115   1.5  mycroft     }
   1116   1.1  mycroft     if (debug > 2) {
   1117   1.8    lukem 	printf("\t\t\t\tv_in: %ld ", (long)ntohl(n->tr_vifin));
   1118   1.8    lukem 	printf("v_out: %ld ", (long)ntohl(n->tr_vifout));
   1119   1.8    lukem 	printf("pkts: %ld\n", (long)ntohl(n->tr_pktcnt));
   1120   1.8    lukem 	printf("\t\t\t\tv_in: %ld ", (long)ntohl(b->tr_vifin));
   1121   1.8    lukem 	printf("v_out: %ld ", (long)ntohl(b->tr_vifout));
   1122   1.8    lukem 	printf("pkts: %ld\n", (long)ntohl(b->tr_pktcnt));
   1123   1.8    lukem 	printf("\t\t\t\tv_in: %ld ",
   1124   1.8    lukem 	    (long)(ntohl(n->tr_vifin) - ntohl(b->tr_vifin)));
   1125   1.8    lukem 	printf("v_out: %ld ",
   1126   1.8    lukem 	    (long)(ntohl(n->tr_vifout) - ntohl(b->tr_vifout)));
   1127   1.8    lukem 	printf("pkts: %ld\n",
   1128   1.8    lukem 	    (long)(ntohl(n->tr_pktcnt) - ntohl(b->tr_pktcnt)));
   1129   1.5  mycroft 	printf("\t\t\t\treset: %d\n", *r);
   1130   1.1  mycroft     }
   1131   1.1  mycroft 
   1132   1.1  mycroft     while (TRUE) {
   1133   1.5  mycroft 	if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_inaddr != b->tr_inaddr))
   1134   1.5  mycroft 	  return 1;		/* Route changed */
   1135   1.5  mycroft 
   1136   1.1  mycroft 	if ((n->tr_inaddr != n->tr_outaddr))
   1137   1.1  mycroft 	  printf("%-15s\n", inet_fmt(n->tr_inaddr, s1));
   1138   1.1  mycroft 	printf("%-15s %-14s %s\n", inet_fmt(n->tr_outaddr, s1), names[rno],
   1139   1.1  mycroft 		 flag_type(n->tr_rflags));
   1140   1.1  mycroft 
   1141   1.1  mycroft 	if (rno-- < 1) break;
   1142   1.1  mycroft 
   1143   1.5  mycroft 	printf("     %c     ^      ttl%5d   ", first ? 'v' : '|', ttl);
   1144   1.5  mycroft 	stat_line(p, n, TRUE, r);
   1145   1.5  mycroft 	if (!first) {
   1146   1.5  mycroft 	    resptime = qarrtime;
   1147   1.5  mycroft 	    qarrtime = fixtime(ntohl((n-1)->tr_qarr));
   1148   1.5  mycroft 	    hop = t_diff(resptime, qarrtime);
   1149   1.5  mycroft 	    ms = scale(&hop);
   1150   1.5  mycroft 	    printf("     v     |      hop%5d%s", hop, ms);
   1151   1.5  mycroft 	    stat_line(b, n, TRUE, r);
   1152   1.5  mycroft 	}
   1153   1.1  mycroft 
   1154   1.5  mycroft 	--b, --p, --n, --r;
   1155   1.5  mycroft 	if (ttl < n->tr_fttl) ttl = n->tr_fttl;
   1156   1.5  mycroft 	else ++ttl;
   1157   1.1  mycroft     }
   1158   1.1  mycroft 
   1159   1.5  mycroft     printf("     %c      \\__   ttl%5d   ", first ? 'v' : '|', ttl);
   1160   1.5  mycroft     stat_line(p, n, FALSE, r);
   1161   1.5  mycroft     if (!first) {
   1162   1.5  mycroft 	hop = t_diff(qarrtime, new->qtime);
   1163   1.5  mycroft 	ms = scale(&hop);
   1164   1.5  mycroft 	printf("     v         \\  hop%5d%s", hop, ms);
   1165   1.5  mycroft 	stat_line(b, n, FALSE, r);
   1166   1.5  mycroft     }
   1167   1.1  mycroft     printf("%-15s %s\n", inet_fmt(qdst, s1), inet_fmt(lcl_addr, s2));
   1168   1.1  mycroft     printf("  Receiver      Query Source\n\n");
   1169   1.5  mycroft     return 0;
   1170   1.1  mycroft }
   1171   1.1  mycroft 
   1172   1.1  mycroft 
   1173   1.1  mycroft /***************************************************************************
   1174   1.1  mycroft  *	main
   1175   1.1  mycroft  ***************************************************************************/
   1176   1.1  mycroft 
   1177   1.1  mycroft int
   1178   1.1  mycroft main(argc, argv)
   1179   1.1  mycroft int argc;
   1180   1.1  mycroft char *argv[];
   1181   1.1  mycroft {
   1182   1.1  mycroft     int udp;
   1183   1.1  mycroft     struct sockaddr_in addr;
   1184   1.1  mycroft     int addrlen = sizeof(addr);
   1185   1.1  mycroft     int recvlen;
   1186   1.1  mycroft     struct timeval tv;
   1187   1.1  mycroft     struct resp_buf *prev, *new;
   1188   1.1  mycroft     struct tr_resp *r;
   1189   1.1  mycroft     u_int32_t smask;
   1190   1.1  mycroft     int rno;
   1191   1.5  mycroft     int hops, nexthop, tries;
   1192   1.5  mycroft     u_int32_t lastout = 0;
   1193   1.1  mycroft     int numstats = 1;
   1194   1.1  mycroft     int waittime;
   1195   1.1  mycroft     int seed;
   1196   1.1  mycroft 
   1197   1.1  mycroft     if (geteuid() != 0) {
   1198   1.1  mycroft 	fprintf(stderr, "mtrace: must be root\n");
   1199   1.1  mycroft 	exit(1);
   1200   1.1  mycroft     }
   1201   1.1  mycroft 
   1202   1.5  mycroft     argv++, argc--;
   1203   1.5  mycroft     if (argc == 0) goto usage;
   1204   1.2  thorpej 
   1205   1.5  mycroft     while (argc > 0 && *argv[0] == '-') {
   1206   1.5  mycroft 	char *p = *argv++;  argc--;
   1207   1.5  mycroft 	p++;
   1208   1.5  mycroft 	do {
   1209   1.5  mycroft 	    char c = *p++;
   1210   1.5  mycroft 	    char *arg = (char *) 0;
   1211   1.5  mycroft 	    if (isdigit(*p)) {
   1212   1.5  mycroft 		arg = p;
   1213   1.5  mycroft 		p = "";
   1214   1.5  mycroft 	    } else if (argc > 0) arg = argv[0];
   1215   1.5  mycroft 	    switch (c) {
   1216   1.5  mycroft 	      case 'd':			/* Unlisted debug print option */
   1217   1.5  mycroft 		if (arg && isdigit(*arg)) {
   1218   1.5  mycroft 		    debug = atoi(arg);
   1219   1.5  mycroft 		    if (debug < 0) debug = 0;
   1220   1.5  mycroft 		    if (debug > 3) debug = 3;
   1221   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1222   1.5  mycroft 		    break;
   1223   1.5  mycroft 		} else
   1224   1.5  mycroft 		    goto usage;
   1225   1.5  mycroft 	      case 'M':			/* Use multicast for reponse */
   1226   1.5  mycroft 		multicast = TRUE;
   1227   1.5  mycroft 		break;
   1228   1.5  mycroft 	      case 'l':			/* Loop updating stats indefinitely */
   1229   1.5  mycroft 		numstats = 3153600;
   1230   1.5  mycroft 		break;
   1231   1.5  mycroft 	      case 'n':			/* Don't reverse map host addresses */
   1232   1.5  mycroft 		numeric = TRUE;
   1233   1.5  mycroft 		break;
   1234   1.5  mycroft 	      case 'p':			/* Passive listen for traces */
   1235   1.5  mycroft 		passive = TRUE;
   1236   1.5  mycroft 		break;
   1237   1.5  mycroft 	      case 'v':			/* Verbosity */
   1238   1.5  mycroft 		verbose = TRUE;
   1239   1.5  mycroft 		break;
   1240   1.5  mycroft 	      case 's':			/* Short form, don't wait for stats */
   1241   1.5  mycroft 		numstats = 0;
   1242   1.5  mycroft 		break;
   1243   1.5  mycroft 	      case 'w':			/* Time to wait for packet arrival */
   1244   1.5  mycroft 		if (arg && isdigit(*arg)) {
   1245   1.5  mycroft 		    timeout = atoi(arg);
   1246   1.5  mycroft 		    if (timeout < 1) timeout = 1;
   1247   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1248   1.5  mycroft 		    break;
   1249   1.5  mycroft 		} else
   1250   1.5  mycroft 		    goto usage;
   1251   1.5  mycroft 	      case 'm':			/* Max number of hops to trace */
   1252   1.5  mycroft 		if (arg && isdigit(*arg)) {
   1253   1.5  mycroft 		    qno = atoi(arg);
   1254   1.5  mycroft 		    if (qno > MAXHOPS) qno = MAXHOPS;
   1255   1.5  mycroft 		    else if (qno < 1) qno = 0;
   1256   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1257   1.5  mycroft 		    break;
   1258   1.5  mycroft 		} else
   1259   1.5  mycroft 		    goto usage;
   1260   1.5  mycroft 	      case 'q':			/* Number of query retries */
   1261   1.5  mycroft 		if (arg && isdigit(*arg)) {
   1262   1.5  mycroft 		    nqueries = atoi(arg);
   1263   1.5  mycroft 		    if (nqueries < 1) nqueries = 1;
   1264   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1265   1.5  mycroft 		    break;
   1266   1.5  mycroft 		} else
   1267   1.5  mycroft 		    goto usage;
   1268   1.5  mycroft 	      case 'g':			/* Last-hop gateway (dest of query) */
   1269   1.5  mycroft 		if (arg && (gwy = host_addr(arg))) {
   1270   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1271   1.5  mycroft 		    break;
   1272   1.5  mycroft 		} else
   1273   1.5  mycroft 		    goto usage;
   1274   1.5  mycroft 	      case 't':			/* TTL for query packet */
   1275   1.5  mycroft 		if (arg && isdigit(*arg)) {
   1276   1.5  mycroft 		    qttl = atoi(arg);
   1277   1.5  mycroft 		    if (qttl < 1) qttl = 1;
   1278   1.5  mycroft 		    rttl = qttl;
   1279   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1280   1.5  mycroft 		    break;
   1281   1.5  mycroft 		} else
   1282   1.5  mycroft 		    goto usage;
   1283   1.5  mycroft 	      case 'r':			/* Dest for response packet */
   1284   1.5  mycroft 		if (arg && (raddr = host_addr(arg))) {
   1285   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1286   1.5  mycroft 		    break;
   1287   1.5  mycroft 		} else
   1288   1.5  mycroft 		    goto usage;
   1289   1.5  mycroft 	      case 'i':			/* Local interface address */
   1290   1.5  mycroft 		if (arg && (lcl_addr = host_addr(arg))) {
   1291   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1292   1.5  mycroft 		    break;
   1293   1.5  mycroft 		} else
   1294   1.5  mycroft 		    goto usage;
   1295   1.5  mycroft 	      case 'S':			/* Stat accumulation interval */
   1296   1.5  mycroft 		if (arg && isdigit(*arg)) {
   1297   1.5  mycroft 		    statint = atoi(arg);
   1298   1.5  mycroft 		    if (statint < 1) statint = 1;
   1299   1.5  mycroft 		    if (arg == argv[0]) argv++, argc--;
   1300   1.5  mycroft 		    break;
   1301   1.5  mycroft 		} else
   1302   1.5  mycroft 		    goto usage;
   1303   1.5  mycroft 	      default:
   1304   1.5  mycroft 		goto usage;
   1305   1.5  mycroft 	    }
   1306   1.5  mycroft 	} while (*p);
   1307   1.5  mycroft     }
   1308   1.2  thorpej 
   1309   1.5  mycroft     if (argc > 0 && (qsrc = host_addr(argv[0]))) {          /* Source of path */
   1310   1.5  mycroft 	if (IN_MULTICAST(ntohl(qsrc))) goto usage;
   1311   1.5  mycroft 	argv++, argc--;
   1312   1.5  mycroft 	if (argc > 0 && (qdst = host_addr(argv[0]))) {      /* Dest of path */
   1313   1.5  mycroft 	    argv++, argc--;
   1314   1.5  mycroft 	    if (argc > 0 && (qgrp = host_addr(argv[0]))) {  /* Path via group */
   1315   1.5  mycroft 		argv++, argc--;
   1316   1.5  mycroft 	    }
   1317   1.5  mycroft 	    if (IN_MULTICAST(ntohl(qdst))) {
   1318   1.5  mycroft 		u_int32_t temp = qdst;
   1319   1.5  mycroft 		qdst = qgrp;
   1320   1.5  mycroft 		qgrp = temp;
   1321   1.5  mycroft 		if (IN_MULTICAST(ntohl(qdst))) goto usage;
   1322   1.5  mycroft 	    } else if (qgrp && !IN_MULTICAST(ntohl(qgrp))) goto usage;
   1323   1.5  mycroft 	}
   1324   1.5  mycroft     }
   1325   1.2  thorpej 
   1326   1.5  mycroft     if (passive) {
   1327   1.5  mycroft 	passive_mode();
   1328   1.5  mycroft 	return(0);
   1329   1.1  mycroft     }
   1330   1.1  mycroft 
   1331   1.5  mycroft     if (argc > 0 || qsrc == 0) {
   1332   1.5  mycroft usage:	printf("\
   1333   1.5  mycroft Usage: mtrace [-Mlnps] [-w wait] [-m max_hops] [-q nqueries] [-g gateway]\n\
   1334   1.5  mycroft               [-S statint] [-t ttl] [-r resp_dest] [-i if_addr] source [receiver] [group]\n");
   1335   1.5  mycroft 	exit(1);
   1336   1.1  mycroft     }
   1337   1.1  mycroft 
   1338   1.1  mycroft     init_igmp();
   1339   1.1  mycroft 
   1340   1.1  mycroft     /*
   1341   1.1  mycroft      * Set useful defaults for as many parameters as possible.
   1342   1.1  mycroft      */
   1343   1.1  mycroft 
   1344   1.1  mycroft     defgrp = htonl(0xE0020001);		/* MBone Audio (224.2.0.1) */
   1345   1.1  mycroft     query_cast = htonl(0xE0000002);	/* All routers multicast addr */
   1346   1.1  mycroft     resp_cast = htonl(0xE0000120);	/* Mtrace response multicast addr */
   1347   1.5  mycroft     if (qgrp == 0) qgrp = defgrp;
   1348   1.1  mycroft 
   1349   1.1  mycroft     /*
   1350   1.1  mycroft      * Get default local address for multicasts to use in setting defaults.
   1351   1.1  mycroft      */
   1352   1.1  mycroft     addr.sin_family = AF_INET;
   1353   1.1  mycroft #if (defined(BSD) && (BSD >= 199103))
   1354   1.1  mycroft     addr.sin_len = sizeof(addr);
   1355   1.1  mycroft #endif
   1356   1.1  mycroft     addr.sin_addr.s_addr = qgrp;
   1357   1.1  mycroft     addr.sin_port = htons(2000);	/* Any port above 1024 will do */
   1358   1.1  mycroft 
   1359   1.1  mycroft     if (((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) ||
   1360   1.1  mycroft 	(connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0) ||
   1361   1.1  mycroft 	getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) {
   1362   1.1  mycroft 	perror("Determining local address");
   1363   1.1  mycroft 	exit(-1);
   1364   1.1  mycroft     }
   1365   1.1  mycroft 
   1366   1.5  mycroft #ifdef SUNOS5
   1367   1.1  mycroft     /*
   1368   1.5  mycroft      * SunOS 5.X prior to SunOS 2.6, getsockname returns 0 for udp socket.
   1369   1.5  mycroft      * This call to sysinfo will return the hostname.
   1370   1.5  mycroft      * If the default multicast interfface (set with the route
   1371   1.5  mycroft      * for 224.0.0.0) is not the same as the hostname,
   1372   1.5  mycroft      * mtrace -i [if_addr] will have to be used.
   1373   1.1  mycroft      */
   1374   1.5  mycroft     if (addr.sin_addr.s_addr == 0) {
   1375   1.5  mycroft 	char myhostname[MAXHOSTNAMELEN];
   1376   1.5  mycroft 	struct hostent *hp;
   1377   1.5  mycroft 	int error;
   1378   1.5  mycroft 
   1379   1.5  mycroft 	error = sysinfo(SI_HOSTNAME, myhostname, sizeof(myhostname));
   1380   1.5  mycroft 	if (error == -1) {
   1381   1.5  mycroft 	    perror("Getting my hostname");
   1382   1.5  mycroft 	    exit(-1);
   1383   1.5  mycroft 	}
   1384   1.5  mycroft 
   1385   1.5  mycroft 	hp = gethostbyname(myhostname);
   1386   1.5  mycroft 	if (hp == NULL || hp->h_addrtype != AF_INET ||
   1387   1.5  mycroft 	    hp->h_length != sizeof(addr.sin_addr)) {
   1388   1.5  mycroft 	    perror("Finding IP address for my hostname");
   1389   1.5  mycroft 	    exit(-1);
   1390   1.5  mycroft 	}
   1391   1.5  mycroft 
   1392  1.10      mrg 	memcpy((char *)&addr.sin_addr.s_addr, hp->h_addr,
   1393  1.10      mrg 	    sizeof(addr.sin_addr.s_addr));
   1394   1.5  mycroft     }
   1395   1.5  mycroft #endif
   1396   1.1  mycroft 
   1397   1.1  mycroft     /*
   1398   1.5  mycroft      * Default destination for path to be queried is the local host.
   1399   1.1  mycroft      */
   1400   1.5  mycroft     if (qdst == 0) qdst = lcl_addr ? lcl_addr : addr.sin_addr.s_addr;
   1401   1.1  mycroft     dst_netmask = get_netmask(udp, qdst);
   1402   1.1  mycroft     close(udp);
   1403   1.5  mycroft     if (lcl_addr == 0) lcl_addr = addr.sin_addr.s_addr;
   1404   1.5  mycroft 
   1405   1.5  mycroft     /*
   1406   1.5  mycroft      * Initialize the seed for random query identifiers.
   1407   1.5  mycroft      */
   1408   1.5  mycroft     gettimeofday(&tv, 0);
   1409   1.5  mycroft     seed = tv.tv_usec ^ lcl_addr;
   1410   1.5  mycroft #ifdef SYSV
   1411   1.5  mycroft     srand48(seed);
   1412   1.5  mycroft #else
   1413   1.5  mycroft     srandom(seed);
   1414   1.5  mycroft #endif
   1415   1.1  mycroft 
   1416   1.5  mycroft     /*
   1417   1.5  mycroft      * Protect against unicast queries to mrouted versions that might crash.
   1418   1.5  mycroft      */
   1419   1.5  mycroft     if (gwy && !IN_MULTICAST(ntohl(gwy)))
   1420   1.5  mycroft       if (send_recv(gwy, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0])) {
   1421   1.5  mycroft 	  int version = ntohl(incr[0].igmp.igmp_group.s_addr) & 0xFFFF;
   1422   1.5  mycroft 	  if (version == 0x0303 || version == 0x0503) {
   1423   1.5  mycroft 	    printf("Don't use -g to address an mrouted 3.%d, it might crash\n",
   1424   1.5  mycroft 		   (version >> 8) & 0xFF);
   1425   1.1  mycroft 	    exit(0);
   1426   1.1  mycroft 	}
   1427   1.5  mycroft       }
   1428   1.1  mycroft 
   1429   1.1  mycroft     printf("Mtrace from %s to %s via group %s\n",
   1430   1.1  mycroft 	   inet_fmt(qsrc, s1), inet_fmt(qdst, s2), inet_fmt(qgrp, s3));
   1431   1.1  mycroft 
   1432   1.1  mycroft     if ((qdst & dst_netmask) == (qsrc & dst_netmask)) {
   1433   1.1  mycroft 	printf("Source & receiver are directly connected, no path to trace\n");
   1434   1.1  mycroft 	exit(0);
   1435   1.1  mycroft     }
   1436   1.1  mycroft 
   1437   1.1  mycroft     /*
   1438   1.5  mycroft      * If the response is to be a multicast address, make sure we
   1439   1.5  mycroft      * are listening on that multicast address.
   1440   1.1  mycroft      */
   1441   1.5  mycroft     if (raddr) {
   1442   1.5  mycroft 	if (IN_MULTICAST(ntohl(raddr))) k_join(raddr, lcl_addr);
   1443   1.5  mycroft     } else k_join(resp_cast, lcl_addr);
   1444   1.1  mycroft 
   1445   1.1  mycroft     /*
   1446   1.5  mycroft      * If the destination is on the local net, the last-hop router can
   1447   1.5  mycroft      * be found by multicast to the all-routers multicast group.
   1448   1.5  mycroft      * Otherwise, use the group address that is the subject of the
   1449   1.5  mycroft      * query since by definition the last-hop router will be a member.
   1450   1.5  mycroft      * Set default TTLs for local remote multicasts.
   1451   1.1  mycroft      */
   1452   1.5  mycroft     restart:
   1453   1.5  mycroft 
   1454   1.5  mycroft     if (gwy == 0)
   1455   1.5  mycroft       if ((qdst & dst_netmask) == (lcl_addr & dst_netmask)) tdst = query_cast;
   1456   1.5  mycroft       else tdst = qgrp;
   1457   1.5  mycroft     else tdst = gwy;
   1458   1.5  mycroft 
   1459   1.5  mycroft     if (IN_MULTICAST(ntohl(tdst))) {
   1460   1.5  mycroft       k_set_loop(1);	/* If I am running on a router, I need to hear this */
   1461   1.5  mycroft       if (tdst == query_cast) k_set_ttl(qttl ? qttl : 1);
   1462   1.5  mycroft       else k_set_ttl(qttl ? qttl : MULTICAST_TTL1);
   1463   1.5  mycroft     }
   1464   1.1  mycroft 
   1465   1.1  mycroft     /*
   1466   1.5  mycroft      * Try a query at the requested number of hops or MAXHOPS if unspecified.
   1467   1.1  mycroft      */
   1468   1.1  mycroft     if (qno == 0) {
   1469   1.1  mycroft 	hops = MAXHOPS;
   1470   1.1  mycroft 	tries = 1;
   1471   1.1  mycroft 	printf("Querying full reverse path... ");
   1472   1.1  mycroft 	fflush(stdout);
   1473   1.1  mycroft     } else {
   1474   1.1  mycroft 	hops = qno;
   1475   1.1  mycroft 	tries = nqueries;
   1476   1.1  mycroft 	printf("Querying reverse path, maximum %d hops... ", qno);
   1477   1.1  mycroft 	fflush(stdout);
   1478   1.2  thorpej     }
   1479   1.1  mycroft     base.rtime = 0;
   1480   1.1  mycroft     base.len = 0;
   1481   1.1  mycroft 
   1482   1.5  mycroft     recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, hops, tries, &base);
   1483   1.1  mycroft 
   1484   1.1  mycroft     /*
   1485   1.1  mycroft      * If the initial query was successful, print it.  Otherwise, if
   1486   1.1  mycroft      * the query max hop count is the default of zero, loop starting
   1487   1.5  mycroft      * from one until there is no response for four hops.  The extra
   1488   1.5  mycroft      * hops allow getting past an mtrace-capable mrouter that can't
   1489   1.5  mycroft      * send multicast packets because all phyints are disabled.
   1490   1.1  mycroft      */
   1491   1.1  mycroft     if (recvlen) {
   1492   1.1  mycroft 	printf("\n  0  ");
   1493   1.1  mycroft 	print_host(qdst);
   1494   1.1  mycroft 	printf("\n");
   1495   1.1  mycroft 	print_trace(1, &base);
   1496   1.1  mycroft 	r = base.resps + base.len - 1;
   1497   1.5  mycroft 	if (r->tr_rflags == TR_OLD_ROUTER || r->tr_rflags == TR_NO_SPACE ||
   1498   1.5  mycroft 		qno != 0) {
   1499   1.1  mycroft 	    printf("%3d  ", -(base.len+1));
   1500   1.5  mycroft 	    what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ?
   1501   1.5  mycroft 				   "doesn't support mtrace"
   1502   1.5  mycroft 				 : "is the next hop");
   1503   1.1  mycroft 	} else {
   1504   1.1  mycroft 	    VAL_TO_MASK(smask, r->tr_smask);
   1505   1.1  mycroft 	    if ((r->tr_inaddr & smask) == (qsrc & smask)) {
   1506   1.1  mycroft 		printf("%3d  ", -(base.len+1));
   1507   1.1  mycroft 		print_host(qsrc);
   1508   1.1  mycroft 		printf("\n");
   1509   1.1  mycroft 	    }
   1510   1.1  mycroft 	}
   1511   1.1  mycroft     } else if (qno == 0) {
   1512   1.1  mycroft 	printf("switching to hop-by-hop:\n  0  ");
   1513   1.1  mycroft 	print_host(qdst);
   1514   1.1  mycroft 	printf("\n");
   1515   1.1  mycroft 
   1516   1.5  mycroft 	for (hops = 1, nexthop = 1; hops <= MAXHOPS; ++hops) {
   1517   1.1  mycroft 	    printf("%3d  ", -hops);
   1518   1.1  mycroft 	    fflush(stdout);
   1519   1.1  mycroft 
   1520   1.5  mycroft 	    /*
   1521   1.5  mycroft 	     * After a successful first hop, try switching to the unicast
   1522   1.5  mycroft 	     * address of the last-hop router instead of multicasting the
   1523   1.5  mycroft 	     * trace query.  This should be safe for mrouted versions 3.3
   1524   1.5  mycroft 	     * and 3.5 because there is a long route timeout with metric
   1525   1.5  mycroft 	     * infinity before a route disappears.  Switching to unicast
   1526   1.5  mycroft 	     * reduces the amount of multicast traffic and avoids a bug
   1527   1.5  mycroft 	     * with duplicate suppression in mrouted 3.5.
   1528   1.5  mycroft 	     */
   1529   1.5  mycroft 	    if (hops == 2 && gwy == 0 &&
   1530   1.5  mycroft 		(recvlen = send_recv(lastout, IGMP_MTRACE_QUERY, hops, 1, &base)))
   1531   1.5  mycroft 	      tdst = lastout;
   1532   1.5  mycroft 	    else recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, hops, nqueries, &base);
   1533   1.1  mycroft 
   1534   1.1  mycroft 	    if (recvlen == 0) {
   1535   1.5  mycroft 		if (hops == 1) break;
   1536   1.5  mycroft 		if (hops == nexthop) {
   1537   1.5  mycroft 		    if (what_kind(&base, "didn't respond")) {
   1538   1.5  mycroft 			/* the ask_neighbors determined that the
   1539   1.5  mycroft 			 * not-responding router is the first-hop. */
   1540   1.5  mycroft 			break;
   1541   1.5  mycroft 		    }
   1542   1.5  mycroft 		} else if (hops < nexthop + 3) {
   1543   1.5  mycroft 		    printf("\n");
   1544   1.5  mycroft 		} else {
   1545   1.5  mycroft 		    printf("...giving up\n");
   1546   1.5  mycroft 		    break;
   1547   1.5  mycroft 		}
   1548   1.5  mycroft 		continue;
   1549   1.1  mycroft 	    }
   1550   1.1  mycroft 	    r = base.resps + base.len - 1;
   1551   1.5  mycroft 	    if (base.len == hops &&
   1552   1.5  mycroft 		(hops == 1 || (base.resps+nexthop-2)->tr_outaddr == lastout)) {
   1553   1.5  mycroft 	    	if (hops == nexthop) {
   1554   1.5  mycroft 		    print_trace(-hops, &base);
   1555   1.5  mycroft 		} else {
   1556   1.5  mycroft 		    printf("\nResuming...\n");
   1557   1.5  mycroft 		    print_trace(nexthop, &base);
   1558   1.1  mycroft 		}
   1559   1.5  mycroft 	    } else {
   1560   1.5  mycroft 		if (base.len < hops) {
   1561   1.5  mycroft 		    /*
   1562   1.5  mycroft 		     * A shorter trace than requested means a fatal error
   1563   1.5  mycroft 		     * occurred along the path, or that the route changed
   1564   1.5  mycroft 		     * to a shorter one.
   1565   1.5  mycroft 		     *
   1566   1.5  mycroft 		     * If the trace is longer than the last one we received,
   1567   1.5  mycroft 		     * then we are resuming from a skipped router (but there
   1568   1.5  mycroft 		     * is still probably a problem).
   1569   1.5  mycroft 		     *
   1570   1.5  mycroft 		     * If the trace is shorter than the last one we
   1571   1.5  mycroft 		     * received, then the route must have changed (and
   1572   1.5  mycroft 		     * there is still probably a problem).
   1573   1.5  mycroft 		     */
   1574   1.5  mycroft 		    if (nexthop <= base.len) {
   1575   1.5  mycroft 			printf("\nResuming...\n");
   1576   1.5  mycroft 			print_trace(nexthop, &base);
   1577   1.5  mycroft 		    } else if (nexthop > base.len + 1) {
   1578   1.5  mycroft 			hops = base.len;
   1579   1.5  mycroft 			printf("\nRoute must have changed...\n");
   1580   1.5  mycroft 			print_trace(1, &base);
   1581   1.5  mycroft 		    }
   1582   1.5  mycroft 		} else {
   1583   1.5  mycroft 		    /*
   1584   1.5  mycroft 		     * The last hop address is not the same as it was;
   1585   1.5  mycroft 		     * the route probably changed underneath us.
   1586   1.5  mycroft 		     */
   1587   1.5  mycroft 		    hops = base.len;
   1588   1.5  mycroft 		    printf("\nRoute must have changed...\n");
   1589   1.5  mycroft 		    print_trace(1, &base);
   1590   1.1  mycroft 		}
   1591   1.1  mycroft 	    }
   1592   1.5  mycroft 	    lastout = r->tr_outaddr;
   1593   1.1  mycroft 
   1594   1.5  mycroft 	    if (base.len < hops ||
   1595   1.5  mycroft 		r->tr_rmtaddr == 0 ||
   1596   1.5  mycroft 		(r->tr_rflags & 0x80)) {
   1597   1.5  mycroft 		VAL_TO_MASK(smask, r->tr_smask);
   1598   1.5  mycroft 		if (r->tr_rmtaddr) {
   1599   1.5  mycroft 		    if (hops != nexthop) {
   1600   1.5  mycroft 			printf("\n%3d  ", -(base.len+1));
   1601   1.5  mycroft 		    }
   1602   1.5  mycroft 		    what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ?
   1603   1.5  mycroft 				"doesn't support mtrace" :
   1604   1.5  mycroft 				"would be the next hop");
   1605   1.5  mycroft 		    /* XXX could do segmented trace if TR_NO_SPACE */
   1606   1.5  mycroft 		} else if (r->tr_rflags == TR_NO_ERR &&
   1607   1.5  mycroft 			   (r->tr_inaddr & smask) == (qsrc & smask)) {
   1608   1.5  mycroft 		    printf("%3d  ", -(hops + 1));
   1609   1.5  mycroft 		    print_host(qsrc);
   1610   1.5  mycroft 		    printf("\n");
   1611   1.5  mycroft 		}
   1612   1.1  mycroft 		break;
   1613   1.1  mycroft 	    }
   1614   1.5  mycroft 
   1615   1.5  mycroft 	    nexthop = hops + 1;
   1616   1.1  mycroft 	}
   1617   1.1  mycroft     }
   1618   1.1  mycroft 
   1619   1.1  mycroft     if (base.rtime == 0) {
   1620   1.1  mycroft 	printf("Timed out receiving responses\n");
   1621  1.11     ross 	if (IN_MULTICAST(ntohl(tdst))) {
   1622   1.5  mycroft 	  if (tdst == query_cast)
   1623   1.1  mycroft 	    printf("Perhaps no local router has a route for source %s\n",
   1624   1.1  mycroft 		   inet_fmt(qsrc, s1));
   1625   1.1  mycroft 	  else
   1626  1.11     ross 	    printf("Perhaps receiver %s is not a member of group %s,\n"
   1627  1.11     ross 		"or no router local to it has a route for source %s,\n"
   1628  1.11     ross 		"or multicast at ttl %d doesn't reach its last-hop router"
   1629  1.11     ross 		" for that source\n",
   1630  1.11     ross 		inet_fmt(qdst, s2), inet_fmt(qgrp, s3), inet_fmt(qsrc, s1),
   1631  1.11     ross 		qttl ? qttl : MULTICAST_TTL1);
   1632  1.11     ross 	}
   1633   1.1  mycroft 	exit(1);
   1634   1.1  mycroft     }
   1635   1.1  mycroft 
   1636   1.1  mycroft     printf("Round trip time %d ms\n\n", t_diff(base.rtime, base.qtime));
   1637   1.1  mycroft 
   1638   1.1  mycroft     /*
   1639   1.1  mycroft      * Use the saved response which was the longest one received,
   1640   1.1  mycroft      * and make additional probes after delay to measure loss.
   1641   1.1  mycroft      */
   1642   1.1  mycroft     raddr = base.qhdr.tr_raddr;
   1643   1.1  mycroft     rttl = base.qhdr.tr_rttl;
   1644   1.1  mycroft     gettimeofday(&tv, 0);
   1645   1.5  mycroft     waittime = statint - (((tv.tv_sec + JAN_1970) & 0xFFFF) - (base.qtime >> 16));
   1646   1.5  mycroft     prev = &base;
   1647   1.5  mycroft     new = &incr[numstats&1];
   1648   1.1  mycroft 
   1649   1.1  mycroft     while (numstats--) {
   1650   1.1  mycroft 	if (waittime < 1) printf("\n");
   1651   1.1  mycroft 	else {
   1652   1.1  mycroft 	    printf("Waiting to accumulate statistics... ");
   1653   1.1  mycroft 	    fflush(stdout);
   1654   1.1  mycroft 	    sleep((unsigned)waittime);
   1655   1.1  mycroft 	}
   1656   1.1  mycroft 	rno = base.len;
   1657   1.5  mycroft 	recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, rno, nqueries, new);
   1658   1.1  mycroft 
   1659   1.1  mycroft 	if (recvlen == 0) {
   1660   1.1  mycroft 	    printf("Timed out.\n");
   1661   1.1  mycroft 	    exit(1);
   1662   1.1  mycroft 	}
   1663   1.1  mycroft 
   1664   1.1  mycroft 	if (rno != new->len) {
   1665   1.5  mycroft 	    printf("Trace length doesn't match:\n");
   1666   1.5  mycroft 	    /*
   1667   1.5  mycroft 	     * XXX Should this trace result be printed, or is that
   1668   1.5  mycroft 	     * too verbose?  Perhaps it should just say restarting.
   1669   1.5  mycroft 	     * But if the path is changing quickly, this may be the
   1670   1.5  mycroft 	     * only snapshot of the current path.  But, if the path
   1671   1.5  mycroft 	     * is changing that quickly, does the current path really
   1672   1.5  mycroft 	     * matter?
   1673   1.5  mycroft 	     */
   1674   1.5  mycroft 	    print_trace(1, new);
   1675   1.5  mycroft 	    printf("Restarting.\n\n");
   1676   1.5  mycroft 	    numstats++;
   1677   1.5  mycroft 	    goto restart;
   1678   1.1  mycroft 	}
   1679   1.1  mycroft 
   1680   1.1  mycroft 	printf("Results after %d seconds:\n\n",
   1681   1.5  mycroft 	       (int)((new->qtime - base.qtime) >> 16));
   1682   1.5  mycroft 	fixup_stats(&base, prev, new);
   1683   1.5  mycroft 	if (print_stats(&base, prev, new)) {
   1684   1.5  mycroft 	    printf("Route changed:\n");
   1685   1.5  mycroft 	    print_trace(1, new);
   1686   1.5  mycroft 	    printf("Restarting.\n\n");
   1687   1.5  mycroft 	    goto restart;
   1688   1.5  mycroft 	}
   1689   1.1  mycroft 	prev = new;
   1690   1.1  mycroft 	new = &incr[numstats&1];
   1691   1.5  mycroft 	waittime = statint;
   1692   1.1  mycroft     }
   1693   1.1  mycroft 
   1694   1.1  mycroft     /*
   1695   1.1  mycroft      * If the response was multicast back, leave the group
   1696   1.1  mycroft      */
   1697   1.5  mycroft     if (raddr) {
   1698   1.5  mycroft 	if (IN_MULTICAST(ntohl(raddr)))	k_leave(raddr, lcl_addr);
   1699   1.5  mycroft     } else k_leave(resp_cast, lcl_addr);
   1700   1.1  mycroft 
   1701   1.1  mycroft     return (0);
   1702   1.1  mycroft }
   1703   1.1  mycroft 
   1704   1.1  mycroft void
   1705   1.1  mycroft check_vif_state()
   1706   1.1  mycroft {
   1707   1.1  mycroft     log(LOG_WARNING, errno, "sendto");
   1708   1.1  mycroft }
   1709   1.1  mycroft 
   1710   1.1  mycroft /*
   1711   1.1  mycroft  * Log errors and other messages to stderr, according to the severity
   1712   1.1  mycroft  * of the message and the current debug level.  For errors of severity
   1713   1.1  mycroft  * LOG_ERR or worse, terminate the program.
   1714   1.1  mycroft  */
   1715   1.5  mycroft #ifdef __STDC__
   1716   1.5  mycroft void
   1717   1.5  mycroft log(int severity, int syserr, char *format, ...)
   1718   1.5  mycroft {
   1719   1.5  mycroft 	va_list ap;
   1720   1.5  mycroft 	char    fmt[100];
   1721   1.5  mycroft 
   1722   1.5  mycroft 	va_start(ap, format);
   1723   1.5  mycroft #else
   1724   1.1  mycroft /*VARARGS3*/
   1725   1.5  mycroft void
   1726   1.5  mycroft log(severity, syserr, format, va_alist)
   1727   1.5  mycroft 	int     severity, syserr;
   1728   1.5  mycroft 	char   *format;
   1729   1.5  mycroft 	va_dcl
   1730   1.1  mycroft {
   1731   1.5  mycroft 	va_list ap;
   1732   1.5  mycroft 	char    fmt[100];
   1733   1.5  mycroft 
   1734   1.5  mycroft 	va_start(ap);
   1735   1.5  mycroft #endif
   1736   1.1  mycroft 
   1737   1.1  mycroft     switch (debug) {
   1738   1.5  mycroft 	case 0: if (severity > LOG_WARNING) return;
   1739   1.5  mycroft 	case 1: if (severity > LOG_NOTICE) return;
   1740   1.5  mycroft 	case 2: if (severity > LOG_INFO  ) return;
   1741   1.1  mycroft 	default:
   1742   1.1  mycroft 	    fmt[0] = '\0';
   1743   1.1  mycroft 	    if (severity == LOG_WARNING) strcat(fmt, "warning - ");
   1744   1.1  mycroft 	    strncat(fmt, format, 80);
   1745  1.15       is 	    format = fmt;
   1746  1.15       is 	    vfprintf(stderr, format, ap);
   1747   1.1  mycroft 	    if (syserr == 0)
   1748   1.1  mycroft 		fprintf(stderr, "\n");
   1749   1.1  mycroft 	    else
   1750   1.9   kleink 		fprintf(stderr, ": %s\n", strerror(syserr));
   1751   1.1  mycroft     }
   1752   1.5  mycroft     if (severity <= LOG_ERR) exit(-1);
   1753   1.1  mycroft }
   1754   1.1  mycroft 
   1755   1.1  mycroft /* dummies */
   1756   1.5  mycroft void accept_probe(src, dst, p, datalen, level)
   1757   1.5  mycroft 	u_int32_t src, dst, level;
   1758   1.5  mycroft 	char *p;
   1759   1.5  mycroft 	int datalen;
   1760   1.5  mycroft {
   1761   1.5  mycroft }
   1762   1.5  mycroft void accept_group_report(src, dst, group, r_type)
   1763   1.5  mycroft 	u_int32_t src, dst, group;
   1764   1.5  mycroft 	int r_type;
   1765   1.5  mycroft {
   1766   1.5  mycroft }
   1767   1.5  mycroft void accept_neighbor_request2(src, dst)
   1768   1.5  mycroft 	u_int32_t src, dst;
   1769   1.5  mycroft {
   1770   1.5  mycroft }
   1771   1.5  mycroft void accept_report(src, dst, p, datalen, level)
   1772   1.5  mycroft 	u_int32_t src, dst, level;
   1773   1.5  mycroft 	char *p;
   1774   1.5  mycroft 	int datalen;
   1775   1.5  mycroft {
   1776   1.5  mycroft }
   1777   1.5  mycroft void accept_neighbor_request(src, dst)
   1778   1.5  mycroft 	u_int32_t src, dst;
   1779   1.5  mycroft {
   1780   1.5  mycroft }
   1781   1.5  mycroft void accept_prune(src, dst, p, datalen)
   1782   1.5  mycroft 	u_int32_t src, dst;
   1783   1.5  mycroft 	char *p;
   1784   1.5  mycroft 	int datalen;
   1785   1.5  mycroft {
   1786   1.5  mycroft }
   1787   1.5  mycroft void accept_graft(src, dst, p, datalen)
   1788   1.5  mycroft 	u_int32_t src, dst;
   1789   1.5  mycroft 	char *p;
   1790   1.5  mycroft 	int datalen;
   1791   1.5  mycroft {
   1792   1.5  mycroft }
   1793   1.5  mycroft void accept_g_ack(src, dst, p, datalen)
   1794   1.5  mycroft 	u_int32_t src, dst;
   1795   1.5  mycroft 	char *p;
   1796   1.5  mycroft 	int datalen;
   1797   1.5  mycroft {
   1798   1.5  mycroft }
   1799   1.5  mycroft void add_table_entry(origin, mcastgrp)
   1800   1.5  mycroft 	u_int32_t origin, mcastgrp;
   1801   1.5  mycroft {
   1802   1.5  mycroft }
   1803   1.5  mycroft void accept_leave_message(src, dst, group)
   1804   1.5  mycroft 	u_int32_t src, dst, group;
   1805   1.5  mycroft {
   1806   1.5  mycroft }
   1807   1.5  mycroft void accept_mtrace(src, dst, group, data, no, datalen)
   1808   1.5  mycroft 	u_int32_t src, dst, group;
   1809   1.5  mycroft 	char *data;
   1810   1.5  mycroft 	u_int no;
   1811   1.5  mycroft 	int datalen;
   1812   1.5  mycroft {
   1813   1.5  mycroft }
   1814   1.5  mycroft void accept_membership_query(src, dst, group, tmo)
   1815   1.5  mycroft 	u_int32_t src, dst, group;
   1816   1.5  mycroft 	int tmo;
   1817   1.5  mycroft {
   1818   1.5  mycroft }
   1819   1.5  mycroft void accept_neighbors(src, dst, p, datalen, level)
   1820   1.5  mycroft 	u_int32_t src, dst, level;
   1821   1.5  mycroft 	u_char *p;
   1822   1.5  mycroft 	int datalen;
   1823   1.5  mycroft {
   1824   1.5  mycroft }
   1825   1.5  mycroft void accept_neighbors2(src, dst, p, datalen, level)
   1826   1.5  mycroft 	u_int32_t src, dst, level;
   1827   1.5  mycroft 	u_char *p;
   1828   1.5  mycroft 	int datalen;
   1829   1.5  mycroft {
   1830   1.5  mycroft }
   1831   1.5  mycroft void accept_info_request(src, dst, p, datalen)
   1832   1.5  mycroft 	u_int32_t src, dst;
   1833   1.5  mycroft 	u_char *p;
   1834   1.5  mycroft 	int datalen;
   1835   1.5  mycroft {
   1836   1.5  mycroft }
   1837   1.5  mycroft void accept_info_reply(src, dst, p, datalen)
   1838   1.5  mycroft 	u_int32_t src, dst;
   1839   1.5  mycroft 	u_char *p;
   1840   1.5  mycroft 	int datalen;
   1841   1.5  mycroft {
   1842   1.5  mycroft }
   1843