xref: /freebsd/contrib/tcpdump/print-msdp.c (revision 3047fefe49f57a673de8df152c199de12ec2c6d3)
1 /*
2  * Copyright (c) 2001 William C. Fenner.
3  *                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
7  * distributions retain the above copyright notice and this paragraph
8  * in its entirety, and (2) distributions including binary code include
9  * the above copyright notice and this paragraph in its entirety in
10  * the documentation or other materials provided with the distribution.
11  * The name of William C. Fenner may not be used to endorse or
12  * promote products derived from this software without specific prior
13  * written permission.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND
14  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
15  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
16  * FOR A PARTICULAR PURPOSE.
17  */
18 #ifndef lint
19 static const char rcsid[] =
20     "@(#) $Header: /tcpdump/master/tcpdump/print-msdp.c,v 1.2 2001/12/10 08:06:40 guy Exp $";
21 #endif
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 
31 #include <netinet/in.h>
32 
33 #include "interface.h"
34 #include "addrtoname.h"
35 #include "extract.h"
36 
37 #define MSDP_TYPE_MAX	7
38 
39 void
40 msdp_print(const unsigned char *sp, u_int length)
41 {
42 	unsigned int type, len;
43 
44 	TCHECK2(*sp, 3);
45 	/* See if we think we're at the beginning of a compound packet */
46 	type = *sp;
47 	len = EXTRACT_16BITS(sp + 1);
48 	if (len > 1500 || len < 3 || type == 0 || type > MSDP_TYPE_MAX)
49 		goto trunc;	/* not really truncated, but still not decodable */
50 	(void)printf(" msdp:");
51 	while (length > 0) {
52 		TCHECK2(*sp, 3);
53 		type = *sp;
54 		len = EXTRACT_16BITS(sp + 1);
55 		if (len > 1400 || vflag)
56 			printf(" [len %d]", len);
57 		if (len < 3)
58 			goto trunc;
59 		sp += 3;
60 		length -= 3;
61 		switch (type) {
62 		case 1:	/* IPv4 Source-Active */
63 		case 3: /* IPv4 Source-Active Response */
64 			if (type == 1)
65 				(void)printf(" SA");
66 			else
67 				(void)printf(" SA-Response");
68 			TCHECK(*sp);
69 			(void)printf(" %d entries", *sp);
70 			if (*sp * 12 + 8 < len) {
71 				(void)printf(" [w/data]");
72 				if (vflag > 1) {
73 					(void)printf(" ");
74 					ip_print(sp + *sp * 12 + 8 - 3,
75 					         len - (*sp * 12 + 8));
76 				}
77 			}
78 			break;
79 		case 2:
80 			(void)printf(" SA-Request");
81 			TCHECK2(*sp, 5);
82 			(void)printf(" for %s", ipaddr_string(sp + 1));
83 			break;
84 		case 4:
85 			(void)printf(" Keepalive");
86 			if (len != 3)
87 				(void)printf("[len=%d] ", len);
88 			break;
89 		case 5:
90 			(void)printf(" Notification");
91 			break;
92 		default:
93 			(void)printf(" [type=%d len=%d]", type, len);
94 			break;
95 		}
96 		sp += (len - 3);
97 		length -= (len - 3);
98 	}
99 	return;
100 trunc:
101 	(void)printf(" [|msdp]");
102 }
103