1 /*
2  * Copyright (c) 1988-1990 The Regents of the University of California.
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 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
23 static char rcsid[] =
24     "@(#) $Header: print-udp.c,v 1.24 91/05/06 00:53:46 mccanne Exp $ (LBL)";
25 #endif
26 
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/udp.h>
35 #include <netinet/udp_var.h>
36 
37 #include <arpa/nameser.h>
38 #include <arpa/tftp.h>
39 #include <errno.h>
40 #include <sys/time.h>
41 #include <rpc/types.h>
42 #include <rpc/auth.h>
43 #include <rpc/auth_unix.h>
44 #include <rpc/svc.h>
45 #include <rpc/xdr.h>
46 #include <rpc/rpc_msg.h>
47 
48 #include "interface.h"
49 /* These must come after interface.h for BSD. */
50 /*#if BSD >= 199006*/
51 #include <nfs/nfsv2.h>
52 /*#endif*/
53 #include <nfs/nfs.h>
54 
55 #include "addrtoname.h"
56 #include "appletalk.h"
57 
58 #include "bootp.h"
59 
60 /* XXX probably should use getservbyname() and cache answers */
61 #define TFTP_PORT 69		/*XXX*/
62 #define SNMP_PORT 161		/*XXX*/
63 #define NTP_PORT 123		/*XXX*/
64 #define SNMPTRAP_PORT 162	/*XXX*/
65 #define RIP_PORT 520		/*XXX*/
66 
67 void
68 udp_print(up, length, ip)
69 	register struct udphdr *up;
70 	int length;
71 	register struct ip *ip;
72 {
73 	register u_char  *cp = (u_char *)(up + 1);
74 
75 	if (cp > snapend) {
76 		printf("[|udp]");
77 		return;
78 	}
79 	if (length < sizeof(struct udphdr)) {
80 		(void)printf(" truncated-udp %d", length);
81 		return;
82 	}
83 	length -= sizeof(struct udphdr);
84 
85 	NTOHS(up->uh_sport);
86 	NTOHS(up->uh_dport);
87 	NTOHS(up->uh_ulen);
88 
89 	if (! qflag) {
90 		register struct rpc_msg *rp;
91 		enum msg_type direction;
92 
93 		rp = (struct rpc_msg *)(up + 1);
94 		direction = (enum msg_type)ntohl(rp->rm_direction);
95 		if (up->uh_dport == NFS_PORT && direction == CALL) {
96 			nfsreq_print(rp, length, ip);
97 			return;
98 		}
99 		else if (up->uh_sport == NFS_PORT && direction==REPLY) {
100 			nfsreply_print(rp, length, ip);
101 			return;
102 		}
103 		else if (cp[2] == 2 && (atalk_port(up->uh_sport) ||
104 			 atalk_port(up->uh_dport))) {
105 			ddp_print((struct atDDP *)(&cp[3]), length - 3);
106 			return;
107 		}
108 	}
109 	(void)printf("%s.%s > %s.%s:",
110 		ipaddr_string(&ip->ip_src), udpport_string(up->uh_sport),
111 		ipaddr_string(&ip->ip_dst), udpport_string(up->uh_dport));
112 
113 	if (!qflag) {
114 #define ISPORT(p) (up->uh_dport == (p) || up->uh_sport == (p))
115 		if (ISPORT(NAMESERVER_PORT))
116 			ns_print((HEADER *)(up + 1), length);
117 		else if (ISPORT(TFTP_PORT))
118 			tftp_print((struct tftphdr *)(up + 1), length);
119 		else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
120 			bootp_print((struct bootp *)(up + 1), length,
121 			    up->uh_sport, up->uh_dport);
122 		else if (up->uh_dport == RIP_PORT)
123 			rip_print((u_char *)(up + 1), length);
124 		else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
125 			snmp_print((u_char *)(up + 1), length);
126 		else if (ISPORT(NTP_PORT))
127 			ntp_print((struct ntpdata *)(up + 1), length);
128 		else
129 			(void)printf(" udp %d", up->uh_ulen - sizeof(*up));
130 #undef ISPORT
131 	} else
132 		(void)printf(" udp %d", up->uh_ulen - sizeof(*up));
133 }
134