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