10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6631Sss150715 * Common Development and Distribution License (the "License").
6*6631Sss150715 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*6631Sss150715 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <time.h>
360Sstevel@tonic-gate #include <sys/time.h>
370Sstevel@tonic-gate #include <sys/bufmod.h>
380Sstevel@tonic-gate #include <setjmp.h>
390Sstevel@tonic-gate #include <varargs.h>
400Sstevel@tonic-gate #include <sys/socket.h>
410Sstevel@tonic-gate #include <net/if.h>
420Sstevel@tonic-gate #include <netinet/in_systm.h>
430Sstevel@tonic-gate #include <netinet/in.h>
440Sstevel@tonic-gate #include <netinet/ip.h>
450Sstevel@tonic-gate #include <netinet/if_ether.h>
460Sstevel@tonic-gate #include <rpc/types.h>
470Sstevel@tonic-gate #include <rpc/xdr.h>
480Sstevel@tonic-gate #include <inttypes.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include "snoop.h"
510Sstevel@tonic-gate
520Sstevel@tonic-gate char *dlc_header;
530Sstevel@tonic-gate char *src_name, *dst_name;
540Sstevel@tonic-gate int pi_frame;
550Sstevel@tonic-gate int pi_time_hour;
560Sstevel@tonic-gate int pi_time_min;
570Sstevel@tonic-gate int pi_time_sec;
580Sstevel@tonic-gate int pi_time_usec;
590Sstevel@tonic-gate
600Sstevel@tonic-gate #ifndef MIN
610Sstevel@tonic-gate #define MIN(a, b) ((a) < (b) ? (a) : (b))
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate
640Sstevel@tonic-gate static void hexdump(char *, int);
650Sstevel@tonic-gate
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate * This routine invokes the packet interpreters
680Sstevel@tonic-gate * on a packet. There's some messing around
690Sstevel@tonic-gate * setting up a few packet-externals before
700Sstevel@tonic-gate * starting with the ethernet interpreter.
710Sstevel@tonic-gate * Yes, we assume here that all packets will
720Sstevel@tonic-gate * be ethernet packets.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate void
process_pkt(struct sb_hdr * hdrp,char * pktp,int num,int flags)750Sstevel@tonic-gate process_pkt(struct sb_hdr *hdrp, char *pktp, int num, int flags)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate int drops, pktlen;
780Sstevel@tonic-gate struct timeval *tvp;
790Sstevel@tonic-gate struct tm *tm;
800Sstevel@tonic-gate extern int x_offset;
810Sstevel@tonic-gate extern int x_length;
820Sstevel@tonic-gate int offset, length;
830Sstevel@tonic-gate static struct timeval ptv;
840Sstevel@tonic-gate
850Sstevel@tonic-gate if (hdrp == NULL)
860Sstevel@tonic-gate return;
870Sstevel@tonic-gate
880Sstevel@tonic-gate tvp = &hdrp->sbh_timestamp;
890Sstevel@tonic-gate if (ptv.tv_sec == 0)
900Sstevel@tonic-gate ptv = *tvp;
910Sstevel@tonic-gate drops = hdrp->sbh_drops;
920Sstevel@tonic-gate pktlen = hdrp->sbh_msglen;
930Sstevel@tonic-gate if (pktlen <= 0)
940Sstevel@tonic-gate return;
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* set up externals */
970Sstevel@tonic-gate dlc_header = pktp;
980Sstevel@tonic-gate pi_frame = num;
990Sstevel@tonic-gate tm = localtime(&tvp->tv_sec);
1000Sstevel@tonic-gate pi_time_hour = tm->tm_hour;
1010Sstevel@tonic-gate pi_time_min = tm->tm_min;
1020Sstevel@tonic-gate pi_time_sec = tm->tm_sec;
1030Sstevel@tonic-gate pi_time_usec = tvp->tv_usec;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate src_name = "?";
1060Sstevel@tonic-gate dst_name = "*";
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate click(hdrp->sbh_origlen);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate (*interface->interpreter)(flags, dlc_header, hdrp->sbh_msglen,
1110Sstevel@tonic-gate hdrp->sbh_origlen);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate show_pktinfo(flags, num, src_name, dst_name, &ptv, tvp, drops,
1140Sstevel@tonic-gate hdrp->sbh_origlen);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate if (x_offset >= 0) {
1170Sstevel@tonic-gate offset = MIN(x_offset, hdrp->sbh_msglen);
1180Sstevel@tonic-gate offset -= (offset % 2); /* round down */
1190Sstevel@tonic-gate length = MIN(hdrp->sbh_msglen - offset, x_length);
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate hexdump(dlc_header + offset, length);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate ptv = *tvp;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * *************************************************************
1300Sstevel@tonic-gate * The following routines constitute a library
1310Sstevel@tonic-gate * used by the packet interpreters to facilitate
1320Sstevel@tonic-gate * the display of packet data. This library
1330Sstevel@tonic-gate * of routines helps provide a consistent
1340Sstevel@tonic-gate * "look and feel".
1350Sstevel@tonic-gate */
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * Display the value of a flag bit in
1400Sstevel@tonic-gate * a byte together with some text that
1410Sstevel@tonic-gate * corresponds to its value - whether
1420Sstevel@tonic-gate * true or false.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate char *
getflag(int val,int mask,char * s_true,char * s_false)1450Sstevel@tonic-gate getflag(int val, int mask, char *s_true, char *s_false)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate static char buff[80];
1480Sstevel@tonic-gate char *p;
1490Sstevel@tonic-gate int set;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate (void) strcpy(buff, ".... .... = ");
1520Sstevel@tonic-gate if (s_false == NULL)
1530Sstevel@tonic-gate s_false = s_true;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate for (p = &buff[8]; p >= buff; p--) {
1560Sstevel@tonic-gate if (*p == ' ')
1570Sstevel@tonic-gate p--;
1580Sstevel@tonic-gate if (mask & 0x1) {
1590Sstevel@tonic-gate set = val & mask & 0x1;
1600Sstevel@tonic-gate *p = set ? '1':'0';
1610Sstevel@tonic-gate (void) strcat(buff, set ? s_true: s_false);
1620Sstevel@tonic-gate break;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate mask >>= 1;
1650Sstevel@tonic-gate val >>= 1;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate return (buff);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate XDR xdrm;
1710Sstevel@tonic-gate jmp_buf xdr_err;
1720Sstevel@tonic-gate int xdr_totlen;
1730Sstevel@tonic-gate char *prot_prefix;
1740Sstevel@tonic-gate char *prot_nest_prefix = "";
1750Sstevel@tonic-gate char *prot_title;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate void
show_header(char * pref,char * str,int len)1780Sstevel@tonic-gate show_header(char *pref, char *str, int len)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate prot_prefix = pref;
1810Sstevel@tonic-gate prot_title = str;
1820Sstevel@tonic-gate (void) sprintf(get_detail_line(0, len), "%s%s----- %s -----",
1830Sstevel@tonic-gate prot_nest_prefix, pref, str);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate void
xdr_init(char * addr,int len)1870Sstevel@tonic-gate xdr_init(char *addr, int len)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate xdr_totlen = len;
1900Sstevel@tonic-gate xdrmem_create(&xdrm, addr, len, XDR_DECODE);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate char *
get_line(int begin,int end)1940Sstevel@tonic-gate get_line(int begin, int end)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate char *line;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate line = get_detail_line(begin, end);
1990Sstevel@tonic-gate (void) strcpy(line, prot_nest_prefix);
2000Sstevel@tonic-gate (void) strcat(line, prot_prefix);
2010Sstevel@tonic-gate return (line + strlen(line));
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate int
get_line_remain(void)2050Sstevel@tonic-gate get_line_remain(void)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate return (MAXLINE - strlen(prot_nest_prefix) - strlen(prot_prefix));
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate void
show_line(char * str)2110Sstevel@tonic-gate show_line(char *str)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate (void) strcpy(get_line(0, 0), str);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate char
getxdr_char()2170Sstevel@tonic-gate getxdr_char()
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate char s;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if (xdr_char(&xdrm, &s))
2220Sstevel@tonic-gate return (s);
2230Sstevel@tonic-gate longjmp(xdr_err, 1);
2240Sstevel@tonic-gate /* NOTREACHED */
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate char
showxdr_char(char * fmt)2280Sstevel@tonic-gate showxdr_char(char *fmt)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate int pos; char val;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate pos = getxdr_pos();
2330Sstevel@tonic-gate val = getxdr_char();
2340Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
2350Sstevel@tonic-gate return (val);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate uchar_t
getxdr_u_char()2390Sstevel@tonic-gate getxdr_u_char()
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate uchar_t s;
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate if (xdr_u_char(&xdrm, &s))
2440Sstevel@tonic-gate return (s);
2450Sstevel@tonic-gate longjmp(xdr_err, 1);
2460Sstevel@tonic-gate /* NOTREACHED */
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate uchar_t
showxdr_u_char(char * fmt)2500Sstevel@tonic-gate showxdr_u_char(char *fmt)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate int pos;
2530Sstevel@tonic-gate uchar_t val;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate pos = getxdr_pos();
2560Sstevel@tonic-gate val = getxdr_u_char();
2570Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
2580Sstevel@tonic-gate return (val);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate short
getxdr_short()2620Sstevel@tonic-gate getxdr_short()
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate short s;
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate if (xdr_short(&xdrm, &s))
2670Sstevel@tonic-gate return (s);
2680Sstevel@tonic-gate longjmp(xdr_err, 1);
2690Sstevel@tonic-gate /* NOTREACHED */
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate short
showxdr_short(char * fmt)2730Sstevel@tonic-gate showxdr_short(char *fmt)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate int pos; short val;
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate pos = getxdr_pos();
2780Sstevel@tonic-gate val = getxdr_short();
2790Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
2800Sstevel@tonic-gate return (val);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate ushort_t
getxdr_u_short()2840Sstevel@tonic-gate getxdr_u_short()
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate ushort_t s;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate if (xdr_u_short(&xdrm, &s))
2890Sstevel@tonic-gate return (s);
2900Sstevel@tonic-gate longjmp(xdr_err, 1);
2910Sstevel@tonic-gate /* NOTREACHED */
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate ushort_t
showxdr_u_short(char * fmt)2950Sstevel@tonic-gate showxdr_u_short(char *fmt)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate int pos;
2980Sstevel@tonic-gate ushort_t val;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate pos = getxdr_pos();
3010Sstevel@tonic-gate val = getxdr_u_short();
3020Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
3030Sstevel@tonic-gate return (val);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate long
getxdr_long()3070Sstevel@tonic-gate getxdr_long()
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate long l;
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (xdr_long(&xdrm, &l))
3120Sstevel@tonic-gate return (l);
3130Sstevel@tonic-gate longjmp(xdr_err, 1);
3140Sstevel@tonic-gate /* NOTREACHED */
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate long
showxdr_long(char * fmt)3180Sstevel@tonic-gate showxdr_long(char *fmt)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate int pos; long val;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate pos = getxdr_pos();
3230Sstevel@tonic-gate val = getxdr_long();
3240Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
3250Sstevel@tonic-gate return (val);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate ulong_t
getxdr_u_long()3290Sstevel@tonic-gate getxdr_u_long()
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate ulong_t l;
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate if (xdr_u_long(&xdrm, &l))
3340Sstevel@tonic-gate return (l);
3350Sstevel@tonic-gate longjmp(xdr_err, 1);
3360Sstevel@tonic-gate /* NOTREACHED */
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate ulong_t
showxdr_u_long(char * fmt)3400Sstevel@tonic-gate showxdr_u_long(char *fmt)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate int pos;
3430Sstevel@tonic-gate ulong_t val;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate pos = getxdr_pos();
3460Sstevel@tonic-gate val = getxdr_u_long();
3470Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
3480Sstevel@tonic-gate return (val);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate longlong_t
getxdr_longlong()3520Sstevel@tonic-gate getxdr_longlong()
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate longlong_t l;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if (xdr_longlong_t(&xdrm, &l))
3570Sstevel@tonic-gate return (l);
3580Sstevel@tonic-gate longjmp(xdr_err, 1);
3590Sstevel@tonic-gate /* NOTREACHED */
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate longlong_t
showxdr_longlong(char * fmt)3630Sstevel@tonic-gate showxdr_longlong(char *fmt)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate int pos; longlong_t val;
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate pos = getxdr_pos();
3680Sstevel@tonic-gate val = getxdr_longlong();
3690Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
3700Sstevel@tonic-gate return (val);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate u_longlong_t
getxdr_u_longlong()3740Sstevel@tonic-gate getxdr_u_longlong()
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate u_longlong_t l;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (xdr_u_longlong_t(&xdrm, &l))
3790Sstevel@tonic-gate return (l);
3800Sstevel@tonic-gate longjmp(xdr_err, 1);
3810Sstevel@tonic-gate /* NOTREACHED */
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate u_longlong_t
showxdr_u_longlong(char * fmt)3850Sstevel@tonic-gate showxdr_u_longlong(char *fmt)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate int pos; u_longlong_t val;
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate pos = getxdr_pos();
3900Sstevel@tonic-gate val = getxdr_u_longlong();
3910Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, val);
3920Sstevel@tonic-gate return (val);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate bool_t
getxdr_bool()3960Sstevel@tonic-gate getxdr_bool()
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate bool_t b;
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate if (xdr_bool(&xdrm, &b))
4010Sstevel@tonic-gate return (b);
4020Sstevel@tonic-gate longjmp(xdr_err, 1);
4030Sstevel@tonic-gate /* NOTREACHED */
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate bool_t
showxdr_bool(char * fmt)4070Sstevel@tonic-gate showxdr_bool(char *fmt)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate int pos; bool_t val;
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate pos = getxdr_pos();
4120Sstevel@tonic-gate val = getxdr_bool();
4130Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt,
4140Sstevel@tonic-gate val ? "True" : "False");
4150Sstevel@tonic-gate return (val);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate char *
getxdr_opaque(char * p,int len)4190Sstevel@tonic-gate getxdr_opaque(char *p, int len)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate if (xdr_opaque(&xdrm, p, len))
4220Sstevel@tonic-gate return (p);
4230Sstevel@tonic-gate longjmp(xdr_err, 1);
4240Sstevel@tonic-gate /* NOTREACHED */
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate char *
getxdr_string(char * p,int len)4280Sstevel@tonic-gate getxdr_string(char *p, /* len+1 bytes or longer */
4290Sstevel@tonic-gate int len)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate if (xdr_string(&xdrm, &p, len))
4320Sstevel@tonic-gate return (p);
4330Sstevel@tonic-gate longjmp(xdr_err, 1);
4340Sstevel@tonic-gate /* NOTREACHED */
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate char *
showxdr_string(int len,char * fmt)4380Sstevel@tonic-gate showxdr_string(int len, /* XDR length */
4390Sstevel@tonic-gate char *fmt)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate static int buff_len = 0;
4420Sstevel@tonic-gate static char *buff = NULL;
4430Sstevel@tonic-gate int pos;
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate /*
4460Sstevel@tonic-gate * XDR strings don't necessarily have a trailing null over the
4470Sstevel@tonic-gate * wire. However, the XDR code will put one in for us. Make sure
4480Sstevel@tonic-gate * we have allocated room for it.
4490Sstevel@tonic-gate */
4500Sstevel@tonic-gate len++;
4510Sstevel@tonic-gate
4520Sstevel@tonic-gate if ((len > buff_len) || (buff_len == 0)) {
4530Sstevel@tonic-gate if (buff)
4540Sstevel@tonic-gate free(buff);
4550Sstevel@tonic-gate if ((buff = (char *)malloc(len)) == NULL)
4560Sstevel@tonic-gate pr_err("showxdr_string: no mem");
4570Sstevel@tonic-gate buff_len = len;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate pos = getxdr_pos();
4600Sstevel@tonic-gate getxdr_string(buff, len);
4610Sstevel@tonic-gate (void) strcpy(buff+60, "...");
4620Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, buff);
4630Sstevel@tonic-gate return (buff);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate char *
getxdr_bytes(uint_t * lenp)4670Sstevel@tonic-gate getxdr_bytes(uint_t *lenp)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate static char buff[1024];
4700Sstevel@tonic-gate char *p = buff;
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate if (xdr_bytes(&xdrm, &p, lenp, 1024))
4730Sstevel@tonic-gate return (buff);
4740Sstevel@tonic-gate longjmp(xdr_err, 1);
4750Sstevel@tonic-gate /* NOTREACHED */
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate char *
getxdr_context(char * p,int len)4790Sstevel@tonic-gate getxdr_context(char *p, int len)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate ushort_t size;
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate size = getxdr_u_short();
4840Sstevel@tonic-gate if (((int)size > 0) && ((int)size < len) && getxdr_opaque(p, size))
4850Sstevel@tonic-gate return (p);
4860Sstevel@tonic-gate longjmp(xdr_err, 1);
4870Sstevel@tonic-gate /* NOTREACHED */
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate char *
showxdr_context(char * fmt)4910Sstevel@tonic-gate showxdr_context(char *fmt)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate ushort_t size;
4940Sstevel@tonic-gate static char buff[1024];
4950Sstevel@tonic-gate int pos;
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate pos = getxdr_pos();
4980Sstevel@tonic-gate size = getxdr_u_short();
4990Sstevel@tonic-gate if (((int)size > 0) && ((int)size < 1024) &&
5000Sstevel@tonic-gate getxdr_opaque(buff, size)) {
5010Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, buff);
5020Sstevel@tonic-gate return (buff);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate longjmp(xdr_err, 1);
5050Sstevel@tonic-gate /* NOTREACHED */
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate enum_t
getxdr_enum()5090Sstevel@tonic-gate getxdr_enum()
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate enum_t e;
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate if (xdr_enum(&xdrm, &e))
5140Sstevel@tonic-gate return (e);
5150Sstevel@tonic-gate longjmp(xdr_err, 1);
5160Sstevel@tonic-gate /* NOTREACHED */
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate void
xdr_skip(int delta)5200Sstevel@tonic-gate xdr_skip(int delta)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate uint_t pos;
5230Sstevel@tonic-gate if (delta % 4 != 0 || delta < 0)
5240Sstevel@tonic-gate longjmp(xdr_err, 1);
5250Sstevel@tonic-gate /* Check for overflow */
5260Sstevel@tonic-gate pos = xdr_getpos(&xdrm);
5270Sstevel@tonic-gate if ((pos + delta) < pos)
5280Sstevel@tonic-gate longjmp(xdr_err, 1);
5290Sstevel@tonic-gate /* xdr_setpos() checks for buffer overrun */
5300Sstevel@tonic-gate if (xdr_setpos(&xdrm, pos + delta) == FALSE)
5310Sstevel@tonic-gate longjmp(xdr_err, 1);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate int
getxdr_pos()5350Sstevel@tonic-gate getxdr_pos()
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate return (xdr_getpos(&xdrm));
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate void
setxdr_pos(int pos)5410Sstevel@tonic-gate setxdr_pos(int pos)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate xdr_setpos(&xdrm, pos);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate void
show_space()5470Sstevel@tonic-gate show_space()
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate (void) get_line(0, 0);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate void
show_trailer()5530Sstevel@tonic-gate show_trailer()
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate show_space();
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate char *
getxdr_date()5590Sstevel@tonic-gate getxdr_date()
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate time_t sec;
5620Sstevel@tonic-gate int usec;
5630Sstevel@tonic-gate static char buff[64];
5640Sstevel@tonic-gate char *p;
5650Sstevel@tonic-gate struct tm my_time; /* private buffer to avoid collision */
5660Sstevel@tonic-gate /* between gmtime and strftime */
5670Sstevel@tonic-gate struct tm *tmp;
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate sec = getxdr_long();
5700Sstevel@tonic-gate usec = getxdr_long();
5710Sstevel@tonic-gate if (sec == -1)
5720Sstevel@tonic-gate return ("-1 ");
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate if (sec < 3600 * 24 * 365) { /* assume not a date */
5750Sstevel@tonic-gate (void) sprintf(buff, "%d.%06d", sec, usec);
5760Sstevel@tonic-gate } else {
5770Sstevel@tonic-gate tmp = gmtime(&sec);
5780Sstevel@tonic-gate (void) memcpy(&my_time, tmp, sizeof (struct tm));
5790Sstevel@tonic-gate strftime(buff, sizeof (buff), "%d-%h-%y %T.", &my_time);
5800Sstevel@tonic-gate p = buff + strlen(buff);
5810Sstevel@tonic-gate (void) sprintf(p, "%06d GMT", usec);
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate return (buff);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate char *
showxdr_date(char * fmt)5870Sstevel@tonic-gate showxdr_date(char *fmt)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate int pos;
5900Sstevel@tonic-gate char *p;
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate pos = getxdr_pos();
5930Sstevel@tonic-gate p = getxdr_date();
5940Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, p);
5950Sstevel@tonic-gate return (p);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate char *
getxdr_date_ns(void)5990Sstevel@tonic-gate getxdr_date_ns(void)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate time_t sec, nsec;
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate sec = getxdr_long();
6040Sstevel@tonic-gate nsec = getxdr_long();
6050Sstevel@tonic-gate if (sec == -1)
6060Sstevel@tonic-gate return ("-1 ");
6070Sstevel@tonic-gate else
6080Sstevel@tonic-gate return (format_time(sec, nsec));
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate /*
6120Sstevel@tonic-gate * Format the given time.
6130Sstevel@tonic-gate */
6140Sstevel@tonic-gate char *
format_time(int64_t sec,uint32_t nsec)6150Sstevel@tonic-gate format_time(int64_t sec, uint32_t nsec)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate static char buff[64];
6180Sstevel@tonic-gate char *p;
6190Sstevel@tonic-gate struct tm my_time; /* private buffer to avoid collision */
6200Sstevel@tonic-gate /* between gmtime and strftime */
6210Sstevel@tonic-gate struct tm *tmp;
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate if (sec < 3600 * 24 * 365) {
6240Sstevel@tonic-gate /* assume not a date; includes negative times */
6250Sstevel@tonic-gate (void) sprintf(buff, "%lld.%06d", sec, nsec);
6260Sstevel@tonic-gate } else if (sec > INT32_MAX) {
6270Sstevel@tonic-gate /*
6280Sstevel@tonic-gate * XXX No routines are available yet for formatting 64-bit
6290Sstevel@tonic-gate * times.
6300Sstevel@tonic-gate */
6310Sstevel@tonic-gate (void) sprintf(buff, "%lld.%06d", sec, nsec);
6320Sstevel@tonic-gate } else {
6330Sstevel@tonic-gate time_t sec32 = (time_t)sec;
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate tmp = gmtime(&sec32);
6360Sstevel@tonic-gate memcpy(&my_time, tmp, sizeof (struct tm));
6370Sstevel@tonic-gate strftime(buff, sizeof (buff), "%d-%h-%y %T.", &my_time);
6380Sstevel@tonic-gate p = buff + strlen(buff);
6390Sstevel@tonic-gate (void) sprintf(p, "%09d GMT", nsec);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate return (buff);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate char *
showxdr_date_ns(char * fmt)6450Sstevel@tonic-gate showxdr_date_ns(char *fmt)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate int pos;
6480Sstevel@tonic-gate char *p;
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate pos = getxdr_pos();
6510Sstevel@tonic-gate p = getxdr_date_ns();
6520Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, p);
6530Sstevel@tonic-gate return (p);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate char *
getxdr_time()6570Sstevel@tonic-gate getxdr_time()
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate time_t sec;
6600Sstevel@tonic-gate static char buff[64];
6610Sstevel@tonic-gate struct tm my_time; /* private buffer to avoid collision */
6620Sstevel@tonic-gate /* between gmtime and strftime */
6630Sstevel@tonic-gate struct tm *tmp;
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate sec = getxdr_long();
6660Sstevel@tonic-gate if (sec == -1)
6670Sstevel@tonic-gate return ("-1 ");
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate if (sec < 3600 * 24 * 365) { /* assume not a date */
6700Sstevel@tonic-gate (void) sprintf(buff, "%d", sec);
6710Sstevel@tonic-gate } else {
6720Sstevel@tonic-gate tmp = gmtime(&sec);
6730Sstevel@tonic-gate memcpy(&my_time, tmp, sizeof (struct tm));
6740Sstevel@tonic-gate strftime(buff, sizeof (buff), "%d-%h-%y %T", &my_time);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate return (buff);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate char *
showxdr_time(char * fmt)6800Sstevel@tonic-gate showxdr_time(char *fmt)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate int pos;
6830Sstevel@tonic-gate char *p;
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate pos = getxdr_pos();
6860Sstevel@tonic-gate p = getxdr_time();
6870Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, p);
6880Sstevel@tonic-gate return (p);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate char *
getxdr_hex(int len)6920Sstevel@tonic-gate getxdr_hex(int len)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate int i, j;
6950Sstevel@tonic-gate static char hbuff[1024];
6960Sstevel@tonic-gate char rbuff[1024];
6970Sstevel@tonic-gate static char *hexstr = "0123456789ABCDEF";
6980Sstevel@tonic-gate char toobig = 0;
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate if (len == 0) {
7010Sstevel@tonic-gate hbuff[0] = '\0';
7020Sstevel@tonic-gate return (hbuff);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate if (len > 1024)
7050Sstevel@tonic-gate len = 1024;
7060Sstevel@tonic-gate if (len < 0 || xdr_opaque(&xdrm, rbuff, len) == FALSE) {
7070Sstevel@tonic-gate longjmp(xdr_err, 1);
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate if (len * 2 > sizeof (hbuff)) {
7110Sstevel@tonic-gate toobig++;
7120Sstevel@tonic-gate len = sizeof (hbuff) / 2;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate j = 0;
7160Sstevel@tonic-gate for (i = 0; i < len; i++) {
7170Sstevel@tonic-gate hbuff[j++] = hexstr[rbuff[i] >> 4 & 0x0f];
7180Sstevel@tonic-gate hbuff[j++] = hexstr[rbuff[i] & 0x0f];
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate if (toobig) {
7220Sstevel@tonic-gate hbuff[len * 2 - strlen("<Too Long>")] = '\0';
7230Sstevel@tonic-gate strcat(hbuff, "<Too Long>");
7240Sstevel@tonic-gate } else
7250Sstevel@tonic-gate hbuff[j] = '\0';
7260Sstevel@tonic-gate
7270Sstevel@tonic-gate return (hbuff);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate char *
showxdr_hex(int len,char * fmt)7310Sstevel@tonic-gate showxdr_hex(int len, char *fmt)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate int pos;
7340Sstevel@tonic-gate char *p;
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate pos = getxdr_pos();
7370Sstevel@tonic-gate p = getxdr_hex(len);
7380Sstevel@tonic-gate (void) sprintf(get_line(pos, getxdr_pos()), fmt, p);
7390Sstevel@tonic-gate return (p);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate
7420Sstevel@tonic-gate static void
hexdump(char * data,int datalen)7430Sstevel@tonic-gate hexdump(char *data, int datalen)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate char *p;
7460Sstevel@tonic-gate ushort_t *p16 = (ushort_t *)data;
7470Sstevel@tonic-gate char *p8 = data;
7480Sstevel@tonic-gate int i, left, len;
7490Sstevel@tonic-gate int chunk = 16; /* 16 bytes per line */
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate printf("\n");
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate for (p = data; p < data + datalen; p += chunk) {
7540Sstevel@tonic-gate printf("\t%4d: ", p - data);
7550Sstevel@tonic-gate left = (data + datalen) - p;
7560Sstevel@tonic-gate len = MIN(chunk, left);
7570Sstevel@tonic-gate for (i = 0; i < (len / 2); i++)
7580Sstevel@tonic-gate printf("%04x ", ntohs(*p16++) & 0xffff);
7590Sstevel@tonic-gate if (len % 2) {
7600Sstevel@tonic-gate printf("%02x ", *((unsigned char *)p16));
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate for (i = 0; i < (chunk - left) / 2; i++)
7630Sstevel@tonic-gate printf(" ");
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate printf(" ");
7660Sstevel@tonic-gate for (i = 0; i < len; i++, p8++)
7670Sstevel@tonic-gate printf("%c", isprint(*p8) ? *p8 : '.');
7680Sstevel@tonic-gate printf("\n");
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate
7710Sstevel@tonic-gate printf("\n");
7720Sstevel@tonic-gate }
773