/*
 * Copyright (c) 1988-1990 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
static char rcsid[] =
    "@(#) $Header: /usr/staff/martinh/tcpview/RCS/print-udp.c,v 1.3 1993/04/22 20:33:45 martinh Exp $ (LBL)";
#endif

#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>

#include <arpa/nameser.h>
#include <arpa/tftp.h>
#include <errno.h>
#include <sys/time.h>
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <rpc/auth.h>
#include <rpc/auth_unix.h>
#include <rpc/svc.h>
#include <rpc/rpc_msg.h>

#ifdef TCPVIEW
#include "tcpview.h"
#endif

#include "interface.h"
/* These must come after interface.h for BSD. */
#if BSD >= 199006
#include <sys/ucred.h>
#include <sys/mount.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#endif

#include "addrtoname.h"
#include "appletalk.h"

#include "bootp.h"

#ifdef TCPVIEW
#include <netdb.h>

u_short TFTP_PORT=0;
u_short SUNRPC_PORT=0;
u_short SNMP_PORT=0;
u_short NTP_PORT=0;
u_short SNMPTRAP_PORT=0;
u_short RIP_PORT=0;

#ifdef __STDC__
struct servent *GetServByName(char *, char *);
#else
struct servent *GetServByName();
#endif

void get_ports()
{
  struct servent *sp;

  sp = GetServByName( "tftp", (char *)0);
  if( sp ) TFTP_PORT = ntohs(sp->s_port);
  sp = GetServByName( "sunrpc", (char *)0);
  if( sp ) SUNRPC_PORT = ntohs(sp->s_port);
  sp = GetServByName( "ntp", (char *)0);
  if( sp ) NTP_PORT = ntohs(sp->s_port);
  sp = GetServByName( "snmp", (char *)0);
  if( sp ) SNMP_PORT = ntohs(sp->s_port);
  sp = GetServByName( "snmp-trap", (char *)0);
  if( sp ) SNMPTRAP_PORT = ntohs(sp->s_port);
  sp = GetServByName( "route", (char *)0);
  if( sp ) RIP_PORT = ntohs(sp->s_port);
}

#else
/* XXX probably should use getservbyname() and cache answers */
#define TFTP_PORT 69		/*XXX*/
#define SUNRPC_PORT 111		/*XXX*/
#define SNMP_PORT 161		/*XXX*/
#define NTP_PORT 123		/*XXX*/
#define SNMPTRAP_PORT 162	/*XXX*/
#define RIP_PORT 520		/*XXX*/
#endif  /* TCPVIEW */


void
udp_print(up, length, ip)
	register struct udphdr *up;
	int length;
	register struct ip *ip;
{
	register u_char  *cp = (u_char *)(up + 1);
	u_short sport, dport, ulen;

#ifdef TCPVIEW
	if( TFTP_PORT==0 )
	  get_ports();
#endif

	if (cp > snapend) {
		printf("[|udp]");
		return;
	}
	if (length < sizeof(struct udphdr)) {
		(void)printf(" truncated-udp %d", length);
		return;
	}
	length -= sizeof(struct udphdr);

	sport = ntohs(up->uh_sport);
	dport = ntohs(up->uh_dport);
	ulen = ntohs(up->uh_ulen);

	if (! qflag) {
		register struct rpc_msg *rp;
		enum msg_type direction;

		rp = (struct rpc_msg *)(up + 1);
		direction = (enum msg_type)ntohl(rp->rm_direction);
		if (dport == NFS_PORT && direction == CALL) {
			nfsreq_print(rp, length, ip);
			return;
		}
		else if (sport == NFS_PORT && direction == REPLY) {
			nfsreply_print(rp, length, ip);
			return;
		}

		else if (dport == SUNRPC_PORT && direction == CALL) {
			sunrpcrequest_print(rp, length, ip);
			return;
		}

		else if (cp[2] == 2 && (atalk_port(sport) ||
			 atalk_port(dport))) {
			ddp_print((struct atDDP *)(&cp[3]), length - 3);
			return;
		}
	}
	(void)printf("%s.%s > %s.%s:",
		ipaddr_string(&ip->ip_src), udpport_string(sport),
		ipaddr_string(&ip->ip_dst), udpport_string(dport));

	if (!qflag) {
#define ISPORT(p) (dport == (p) || sport == (p))
		if (ISPORT(NAMESERVER_PORT))
			ns_print((HEADER *)(up + 1), length);
		else if (ISPORT(TFTP_PORT))
			tftp_print((struct tftphdr *)(up + 1), length);
		else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
			bootp_print((struct bootp *)(up + 1), length,
			    sport, dport);
		else if (dport == RIP_PORT)
			rip_print((u_char *)(up + 1), length);
		else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
			snmp_print((u_char *)(up + 1), length);
		else if (ISPORT(NTP_PORT))
			ntp_print((struct ntpdata *)(up + 1), length);
		else
			(void)printf(" udp %d", ulen - sizeof(*up));
#undef ISPORT
	} else
		(void)printf(" udp %d", ulen - sizeof(*up));
}






syntax highlighted by Code2HTML, v. 0.9.1