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: /usr/staff/martinh/tcpview/RCS/print-udp.c,v 1.3 1993/04/22 20:33:45 martinh 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/xdr.h>
43 #include <rpc/auth.h>
44 #include <rpc/auth_unix.h>
45 #include <rpc/svc.h>
46 #include <rpc/rpc_msg.h>
47 
48 #ifdef TCPVIEW
49 #include "tcpview.h"
50 #endif
51 
52 #include "interface.h"
53 /* These must come after interface.h for BSD. */
54 #if BSD >= 199006
55 #include <sys/ucred.h>
56 #include <sys/mount.h>
57 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800100
58 #include <vfs/nfs/nfsport.h>
59 #include <vfs/nfs/rpcv2.h>
60 #include <vfs/nfs/nfsproto.h>
61 #else
62 #include <vfs/nfs/rpcv2.h>
63 #include <vfs/nfs/nfsproto.h>
64 #endif
65 #endif
66 
67 #include "addrtoname.h"
68 #include "appletalk.h"
69 
70 #include "bootp.h"
71 
72 #ifdef TCPVIEW
73 #include <netdb.h>
74 
75 u_short TFTP_PORT=0;
76 u_short SUNRPC_PORT=0;
77 u_short SNMP_PORT=0;
78 u_short NTP_PORT=0;
79 u_short SNMPTRAP_PORT=0;
80 u_short RIP_PORT=0;
81 
82 #ifdef __STDC__
83 struct servent *GetServByName(char *, char *);
84 #else
85 struct servent *GetServByName();
86 #endif
87 
get_ports()88 void get_ports()
89 {
90   struct servent *sp;
91 
92   sp = GetServByName( "tftp", (char *)0);
93   if( sp ) TFTP_PORT = ntohs(sp->s_port);
94   sp = GetServByName( "sunrpc", (char *)0);
95   if( sp ) SUNRPC_PORT = ntohs(sp->s_port);
96   sp = GetServByName( "ntp", (char *)0);
97   if( sp ) NTP_PORT = ntohs(sp->s_port);
98   sp = GetServByName( "snmp", (char *)0);
99   if( sp ) SNMP_PORT = ntohs(sp->s_port);
100   sp = GetServByName( "snmp-trap", (char *)0);
101   if( sp ) SNMPTRAP_PORT = ntohs(sp->s_port);
102   sp = GetServByName( "route", (char *)0);
103   if( sp ) RIP_PORT = ntohs(sp->s_port);
104 }
105 
106 #else
107 /* XXX probably should use getservbyname() and cache answers */
108 #define TFTP_PORT 69		/*XXX*/
109 #define SUNRPC_PORT 111		/*XXX*/
110 #define SNMP_PORT 161		/*XXX*/
111 #define NTP_PORT 123		/*XXX*/
112 #define SNMPTRAP_PORT 162	/*XXX*/
113 #define RIP_PORT 520		/*XXX*/
114 #endif  /* TCPVIEW */
115 
116 
117 void
udp_print(up,length,ip)118 udp_print(up, length, ip)
119 	register struct udphdr *up;
120 	int length;
121 	register struct ip *ip;
122 {
123 	register u_char  *cp = (u_char *)(up + 1);
124 	u_short sport, dport, ulen;
125 
126 #ifdef TCPVIEW
127 	if( TFTP_PORT==0 )
128 	  get_ports();
129 #endif
130 
131 	if (cp > snapend) {
132 		printf("[|udp]");
133 		return;
134 	}
135 	if (length < sizeof(struct udphdr)) {
136 		(void)printf(" truncated-udp %d", length);
137 		return;
138 	}
139 	length -= sizeof(struct udphdr);
140 
141 	sport = ntohs(up->uh_sport);
142 	dport = ntohs(up->uh_dport);
143 	ulen = ntohs(up->uh_ulen);
144 
145 	if (! qflag) {
146 		register struct rpc_msg *rp;
147 		enum msg_type direction;
148 
149 		rp = (struct rpc_msg *)(up + 1);
150 		direction = (enum msg_type)ntohl(rp->rm_direction);
151 		if (dport == NFS_PORT && direction == CALL) {
152 			nfsreq_print(rp, length, ip);
153 			return;
154 		}
155 		else if (sport == NFS_PORT && direction == REPLY) {
156 			nfsreply_print(rp, length, ip);
157 			return;
158 		}
159 
160 		else if (dport == SUNRPC_PORT && direction == CALL) {
161 			sunrpcrequest_print(rp, length, ip);
162 			return;
163 		}
164 
165 		else if (cp[2] == 2 && (atalk_port(sport) ||
166 			 atalk_port(dport))) {
167 			ddp_print((struct atDDP *)(&cp[3]), length - 3);
168 			return;
169 		}
170 	}
171 	(void)printf("%s.%s > %s.%s:",
172 		ipaddr_string(&ip->ip_src), udpport_string(sport),
173 		ipaddr_string(&ip->ip_dst), udpport_string(dport));
174 
175 	if (!qflag) {
176 #define ISPORT(p) (dport == (p) || sport == (p))
177 		if (ISPORT(NAMESERVER_PORT))
178 			ns_print((HEADER *)(up + 1), length);
179 		else if (ISPORT(TFTP_PORT))
180 			tftp_print((struct tftphdr *)(up + 1), length);
181 		else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
182 			bootp_print((struct bootp *)(up + 1), length,
183 			    sport, dport);
184 		else if (dport == RIP_PORT)
185 			rip_print((u_char *)(up + 1), length);
186 		else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
187 			snmp_print((u_char *)(up + 1), length);
188 		else if (ISPORT(NTP_PORT))
189 			ntp_print((struct ntpdata *)(up + 1), length);
190 		else
191 			(void)printf(" udp %d", ulen - sizeof(*up));
192 #undef ISPORT
193 	} else
194 		(void)printf(" udp %d", ulen - sizeof(*up));
195 }
196 
197 
198 
199 
200