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
23static const char rcsid[] _U_ =
24    "@(#) $Header: /tcpdump/master/tcpdump/print-ripng.c,v 1.18 2005-01-04 00:15:54 guy Exp $";
25#endif
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#ifdef INET6
32
33#include <tcpdump-stdinc.h>
34#include <stdio.h>
35
36#include "route6d.h"
37#include "interface.h"
38#include "addrtoname.h"
39#include "extract.h"
40
41#if !defined(IN6_IS_ADDR_UNSPECIFIED) && !defined(_MSC_VER) /* MSVC inline */
42static int IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *addr)
43{
44    static const struct in6_addr in6addr_any;        /* :: */
45    return (memcmp(addr, &in6addr_any, sizeof(*addr)) == 0);
46}
47#endif
48
49static int
50rip6_entry_print(register const struct netinfo6 *ni, int metric)
51{
52	int l;
53	l = printf("%s/%d", ip6addr_string(&ni->rip6_dest), ni->rip6_plen);
54	if (ni->rip6_tag)
55		l += printf(" [%d]", EXTRACT_16BITS(&ni->rip6_tag));
56	if (metric)
57		l += printf(" (%d)", ni->rip6_metric);
58	return l;
59}
60
61void
62ripng_print(const u_char *dat, unsigned int length)
63{
64	register const struct rip6 *rp = (struct rip6 *)dat;
65	register const struct netinfo6 *ni;
66	register u_int amt;
67	register u_int i;
68	int j;
69	int trunc;
70
71	if (snapend < dat)
72		return;
73	amt = snapend - dat;
74	i = min(length, amt);
75	if (i < (sizeof(struct rip6) - sizeof(struct netinfo6)))
76		return;
77	i -= (sizeof(struct rip6) - sizeof(struct netinfo6));
78
79	switch (rp->rip6_cmd) {
80
81	case RIP6_REQUEST:
82		j = length / sizeof(*ni);
83		if (j == 1
84		    &&  rp->rip6_nets->rip6_metric == HOPCNT_INFINITY6
85		    &&  IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
86			printf(" ripng-req dump");
87			break;
88		}
89		if (j * sizeof(*ni) != length - 4)
90			printf(" ripng-req %d[%u]:", j, length);
91		else
92			printf(" ripng-req %d:", j);
93		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
94		for (ni = rp->rip6_nets; i >= sizeof(*ni);
95		    i -= sizeof(*ni), ++ni) {
96			if (vflag > 1)
97				printf("\n\t");
98			else
99				printf(" ");
100			rip6_entry_print(ni, 0);
101		}
102		break;
103	case RIP6_RESPONSE:
104		j = length / sizeof(*ni);
105		if (j * sizeof(*ni) != length - 4)
106			printf(" ripng-resp %d[%u]:", j, length);
107		else
108			printf(" ripng-resp %d:", j);
109		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
110		for (ni = rp->rip6_nets; i >= sizeof(*ni);
111		    i -= sizeof(*ni), ++ni) {
112			if (vflag > 1)
113				printf("\n\t");
114			else
115				printf(" ");
116			rip6_entry_print(ni, ni->rip6_metric);
117		}
118		if (trunc)
119			printf("[|ripng]");
120		break;
121	default:
122		printf(" ripng-%d ?? %u", rp->rip6_cmd, length);
123		break;
124	}
125	if (rp->rip6_vers != RIP6_VERSION)
126		printf(" [vers %d]", rp->rip6_vers);
127}
128#endif /* INET6 */
129