xref: /freebsd/contrib/tcpdump/print-ripng.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
1 /*
2  * Copyright (c) 1989, 1990, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 #ifndef lint
23 static const char rcsid[] =
24     "@(#) $Header: /tcpdump/master/tcpdump/print-ripng.c,v 1.10 2001/11/16 08:59:22 itojun Exp $";
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #ifdef INET6
32 
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 
38 #include <netinet/in.h>
39 
40 #include <errno.h>
41 #include <stdio.h>
42 
43 #include "route6d.h"
44 #include "interface.h"
45 #include "addrtoname.h"
46 
47 static int
48 rip6_entry_print(register const struct netinfo6 *ni, int metric)
49 {
50 	int l;
51 	l = printf("%s/%d", ip6addr_string(&ni->rip6_dest), ni->rip6_plen);
52 	if (ni->rip6_tag)
53 		l += printf(" [%d]", ntohs(ni->rip6_tag));
54 	if (metric)
55 		l += printf(" (%d)", ni->rip6_metric);
56 	return l;
57 }
58 
59 void
60 ripng_print(const u_char *dat, unsigned int length)
61 {
62 	register const struct rip6 *rp = (struct rip6 *)dat;
63 	register const struct netinfo6 *ni;
64 	register int amt = snapend - dat;
65 	register int i = min(length, amt) -
66 			 (sizeof(struct rip6) - sizeof(struct netinfo6));
67 	int j;
68 	int trunc;
69 
70 	if (i < 0)
71 		return;
72 
73 	switch (rp->rip6_cmd) {
74 
75 	case RIP6_REQUEST:
76 		j = length / sizeof(*ni);
77 		if (j == 1
78 		    &&  rp->rip6_nets->rip6_metric == HOPCNT_INFINITY6
79 		    &&  IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
80 			printf(" ripng-req dump");
81 			break;
82 		}
83 		if (j * sizeof(*ni) != length - 4)
84 			printf(" ripng-req %d[%u]:", j, length);
85 		else
86 			printf(" ripng-req %d:", j);
87 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
88 		for (ni = rp->rip6_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
89 			if (vflag > 1)
90 				printf("\n\t");
91 			else
92 				printf(" ");
93 			rip6_entry_print(ni, 0);
94 		}
95 		break;
96 	case RIP6_RESPONSE:
97 		j = length / sizeof(*ni);
98 		if (j * sizeof(*ni) != length - 4)
99 			printf(" ripng-resp %d[%u]:", j, length);
100 		else
101 			printf(" ripng-resp %d:", j);
102 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
103 		for (ni = rp->rip6_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
104 			if (vflag > 1)
105 				printf("\n\t");
106 			else
107 				printf(" ");
108 			rip6_entry_print(ni, ni->rip6_metric);
109 		}
110 		if (trunc)
111 			printf("[|ripng]");
112 		break;
113 	default:
114 		printf(" ripng-%d ?? %u", rp->rip6_cmd, length);
115 		break;
116 	}
117 	if (rp->rip6_vers != RIP6_VERSION)
118 		printf(" [vers %d]", rp->rip6_vers);
119 }
120 #endif /* INET6 */
121