xref: /titanic_41/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_udp.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS	*/
28 
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <net/if.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/in.h>
42 #include <netinet/ip.h>
43 #include <netinet/if_ether.h>
44 #include <netinet/udp.h>
45 #include "snoop.h"
46 
47 extern char *dlc_header;
48 
49 interpret_udp(flags, udp, iplen, fraglen)
50 	int flags;
51 	struct udphdr *udp;
52 	int iplen, fraglen;
53 {
54 	char *data;
55 	int udplen;
56 	int sunrpc;
57 	char *pname;
58 	char buff [32];
59 
60 	if (fraglen < sizeof (struct udphdr))
61 		return (fraglen);	/* incomplete header */
62 
63 	data = (char *)udp + sizeof (struct udphdr);
64 	udplen = ntohs((u_short)udp->uh_ulen) - sizeof (struct udphdr);
65 	fraglen -= sizeof (struct udphdr);
66 	if (fraglen > udplen)
67 		fraglen = udplen;
68 
69 	if (flags & F_SUM) {
70 		(void) sprintf(get_sum_line(),
71 			"UDP D=%d S=%d LEN=%d",
72 			ntohs(udp->uh_dport),
73 			ntohs(udp->uh_sport),
74 			ntohs((u_short)udp->uh_ulen));
75 	}
76 
77 	sunrpc = !reservedport(IPPROTO_UDP, ntohs(udp->uh_dport)) &&
78 		!reservedport(IPPROTO_UDP, ntohs(udp->uh_sport)) &&
79 		valid_rpc(data, udplen);
80 
81 	if (flags & F_DTAIL) {
82 		show_header("UDP:  ", "UDP Header", udplen);
83 		show_space();
84 		(void) sprintf(get_line((char *)udp->uh_sport - dlc_header, 1),
85 			"Source port = %d",
86 			ntohs(udp->uh_sport));
87 
88 		if (sunrpc) {
89 			pname = "(Sun RPC)";
90 		} else {
91 			pname = getportname(IPPROTO_UDP, ntohs(udp->uh_dport));
92 			if (pname == NULL) {
93 				pname = "";
94 			} else {
95 				(void) sprintf(buff, "(%s)", pname);
96 				pname = buff;
97 			}
98 		}
99 		(void) sprintf(get_line((char *)udp->uh_dport - dlc_header, 1),
100 			"Destination port = %d %s",
101 			ntohs(udp->uh_dport), pname);
102 		(void) sprintf(get_line((char *)udp->uh_ulen - dlc_header, 1),
103 			"Length = %d %s",
104 			ntohs((u_short)udp->uh_ulen),
105 			udplen > fraglen ?
106 				"(Not all data contained in this fragment)"
107 				: "");
108 			(void) sprintf(
109 				get_line((char *)udp->uh_sum - dlc_header, 1),
110 				"Checksum = %04X %s",
111 				ntohs(udp->uh_sum),
112 				udp->uh_sum == 0 ? "(no checksum)" : "");
113 		show_space();
114 	}
115 
116 
117 	/* go to the next protocol layer */
118 
119 	if (!interpret_reserved(flags, IPPROTO_UDP,
120 		ntohs(udp->uh_sport),
121 		ntohs(udp->uh_dport),
122 		data, fraglen)) {
123 		if (fraglen > 0 && sunrpc)
124 			interpret_rpc(flags, data, fraglen, IPPROTO_UDP);
125 	}
126 
127 	return (fraglen);
128 }
129