xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_udp.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <net/if.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/in.h>
40 #include <netinet/ip.h>
41 #include <netinet/if_ether.h>
42 #include <netinet/udp.h>
43 #include "snoop.h"
44 
45 extern char *dlc_header;
46 
47 int
48 interpret_udp(int flags, struct udphdr *udp, int iplen, int fraglen)
49 {
50 	char *data;
51 	int udplen;
52 	int sunrpc;
53 	char *pname;
54 	char buff [32];
55 
56 	if (fraglen < sizeof (struct udphdr))
57 		return (fraglen);	/* incomplete header */
58 
59 	data = (char *)udp + sizeof (struct udphdr);
60 	udplen = ntohs((ushort_t)udp->uh_ulen) - sizeof (struct udphdr);
61 	fraglen -= sizeof (struct udphdr);
62 	if (fraglen > udplen)
63 		fraglen = udplen;
64 
65 	if (flags & F_SUM) {
66 		(void) sprintf(get_sum_line(),
67 			"UDP D=%d S=%d LEN=%d",
68 			ntohs(udp->uh_dport),
69 			ntohs(udp->uh_sport),
70 			ntohs((ushort_t)udp->uh_ulen));
71 	}
72 
73 	sunrpc = !reservedport(IPPROTO_UDP, ntohs(udp->uh_dport)) &&
74 		!reservedport(IPPROTO_UDP, ntohs(udp->uh_sport)) &&
75 		valid_rpc(data, udplen);
76 
77 	if (flags & F_DTAIL) {
78 		show_header("UDP:  ", "UDP Header", udplen);
79 		show_space();
80 		(void) sprintf(get_line((char *)(uintptr_t)udp->uh_sport -
81 		    dlc_header, 1), "Source port = %d", ntohs(udp->uh_sport));
82 
83 		if (sunrpc) {
84 			pname = "(Sun RPC)";
85 		} else {
86 			pname = getportname(IPPROTO_UDP, ntohs(udp->uh_dport));
87 			if (pname == NULL) {
88 				pname = "";
89 			} else {
90 				(void) sprintf(buff, "(%s)", pname);
91 				pname = buff;
92 			}
93 		}
94 		(void) sprintf(get_line((char *)(uintptr_t)udp->uh_dport -
95 		    dlc_header, 1), "Destination port = %d %s",
96 		    ntohs(udp->uh_dport), pname);
97 		(void) sprintf(get_line((char *)(uintptr_t)udp->uh_ulen -
98 		    dlc_header, 1), "Length = %d %s",
99 		    ntohs((ushort_t)udp->uh_ulen),
100 		    udplen > fraglen ?
101 			"(Not all data contained in this fragment)"
102 			: "");
103 		    (void) sprintf(get_line((char *)(uintptr_t)udp->uh_sum -
104 			dlc_header, 1),	"Checksum = %04X %s",
105 			ntohs(udp->uh_sum),
106 			udp->uh_sum == 0 ? "(no checksum)" : "");
107 		show_space();
108 	}
109 
110 
111 	/* go to the next protocol layer */
112 
113 	if (!interpret_reserved(flags, IPPROTO_UDP,
114 		ntohs(udp->uh_sport),
115 		ntohs(udp->uh_dport),
116 		data, fraglen)) {
117 		if (fraglen > 0 && sunrpc)
118 			interpret_rpc(flags, data, fraglen, IPPROTO_UDP);
119 	}
120 
121 	return (fraglen);
122 }
123