print-token.c revision 50514
144165Sjulian/* 244165Sjulian * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996 344165Sjulian * The Regents of the University of California. All rights reserved. 444165Sjulian * 544165Sjulian * Redistribution and use in source and binary forms, with or without 644165Sjulian * modification, are permitted provided that: (1) source code distributions 744165Sjulian * retain the above copyright notice and this paragraph in its entirety, (2) 844165Sjulian * distributions including binary code include the above copyright notice and 944165Sjulian * this paragraph in its entirety in the documentation or other materials 1044165Sjulian * provided with the distribution, and (3) all advertising materials mentioning 1144165Sjulian * features or use of this software display the following acknowledgement: 1244165Sjulian * ``This product includes software developed by the University of California, 1344165Sjulian * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 1444165Sjulian * the University nor the names of its contributors may be used to endorse 1544165Sjulian * or promote products derived from this software without specific prior 1644165Sjulian * written permission. 1744165Sjulian * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 1844165Sjulian * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 1944165Sjulian * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 2044165Sjulian * 2144165Sjulian * Hacked version of print-ether.c Larry Lile <lile@stdio.com> 2244165Sjulian */ 2344165Sjulian#ifndef lint 2444165Sjulianstatic const char rcsid[] = 2550514Slile "@(#) $Header: /home/ncvs/src/contrib/tcpdump/print-token.c,v 1.1 1999/02/20 11:17:55 julian Exp $"; 2644165Sjulian#endif 2744165Sjulian 2844165Sjulian#include <sys/param.h> 2944165Sjulian#include <sys/time.h> 3044165Sjulian#include <sys/socket.h> 3144165Sjulian 3244165Sjulian#if __STDC__ 3344165Sjulianstruct mbuf; 3444165Sjulianstruct rtentry; 3544165Sjulian#endif 3644165Sjulian#include <net/if.h> 3744165Sjulian 3844165Sjulian#include "token.h" 3944165Sjulian 4044165Sjulian#include <netinet/in.h> 4144165Sjulian#include <net/ethernet.h> 4244165Sjulian#include <netinet/in_systm.h> 4344165Sjulian#include <netinet/ip.h> 4444165Sjulian#include <netinet/ip_var.h> 4544165Sjulian#include <netinet/udp.h> 4644165Sjulian#include <netinet/udp_var.h> 4744165Sjulian#include <netinet/tcp.h> 4844165Sjulian#include <netinet/tcpip.h> 4944165Sjulian 5044165Sjulian#include <stdio.h> 5144165Sjulian#include <pcap.h> 5244165Sjulian 5344165Sjulian#include "interface.h" 5444165Sjulian#include "addrtoname.h" 5544165Sjulian#include "ethertype.h" 5644165Sjulian#include "llc.h" 5744165Sjulian 5844165Sjulianconst u_char *packetp; 5944165Sjulianconst u_char *snapend; 6044165Sjulian 6144165Sjulianstatic inline void 6244165Sjuliantoken_print(register const u_char *bp, u_int length) 6344165Sjulian{ 6444165Sjulian register const struct token_header *tp; 6544165Sjulian register const struct llc *lp; 6644165Sjulian u_short ether_type; 6744165Sjulian 6844165Sjulian tp = (const struct token_header *)bp; 6944165Sjulian lp = (struct llc *)(bp + TOKEN_HDR_LEN); 7050514Slile 7150514Slile if (IS_SOURCE_ROUTED) { 7250514Slile tp->ether_shost[0] = tp->ether_shost[0] & 0x7f; 7344165Sjulian lp = (struct llc *)(bp + TOKEN_HDR_LEN + RIF_LENGTH); 7450514Slile } 7544165Sjulian 7644165Sjulian /* 7744165Sjulian * Ethertype on ethernet is a short, but ethertype in an llc-snap has 7844165Sjulian * been defined as 2 u_chars. This is a stupid little hack to fix 7944165Sjulian * this for now but something better should be done using ntohs() 8044165Sjulian * XXX 8144165Sjulian */ 8244165Sjulian ether_type = ((u_short)lp->ethertype[1] << 16) | lp->ethertype[0]; 8344165Sjulian 8444165Sjulian if (qflag) 8544165Sjulian (void)printf("%s %s %d: ", 8644165Sjulian etheraddr_string(ESRC(tp)), 8744165Sjulian etheraddr_string(EDST(tp)), 8844165Sjulian length); 8944165Sjulian else 9044165Sjulian (void)printf("%s %s %s %d: ", 9144165Sjulian etheraddr_string(ESRC(tp)), 9244165Sjulian etheraddr_string(EDST(tp)), 9344165Sjulian etherproto_string(ether_type), 9444165Sjulian length); 9544165Sjulian} 9644165Sjulian 9744165Sjulian/* 9844165Sjulian * This is the top level routine of the printer. 'p' is the points 9944165Sjulian * to the ether header of the packet, 'tvp' is the timestamp, 10044165Sjulian * 'length' is the length of the packet off the wire, and 'caplen' 10144165Sjulian * is the number of bytes actually captured. 10244165Sjulian */ 10344165Sjulianvoid 10444165Sjuliantoken_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 10544165Sjulian{ 10644165Sjulian u_int caplen = h->caplen; 10744165Sjulian u_int length = h->len; 10844165Sjulian struct token_header *tp; 10944165Sjulian u_short ether_type; 11044165Sjulian extern u_short extracted_ethertype; 11150514Slile u_int route_len = 0, seg; 11244165Sjulian struct llc *lp; 11344165Sjulian 11444165Sjulian tp = (struct token_header *)p; 11544165Sjulian 11644165Sjulian ts_print(&h->ts); 11744165Sjulian 11844165Sjulian if (caplen < TOKEN_HDR_LEN) { 11944165Sjulian printf("[|token-ring]"); 12044165Sjulian goto out; 12144165Sjulian } 12244165Sjulian 12344165Sjulian /* 12444165Sjulian * Some printers want to get back at the ethernet addresses, 12544165Sjulian * and/or check that they're not walking off the end of the packet. 12644165Sjulian * Rather than pass them all the way down, we set these globals. 12744165Sjulian */ 12844165Sjulian tp = (struct token_header *)p; 12944165Sjulian 13044165Sjulian /* Adjust for source routing information in the MAC header */ 13144165Sjulian if (IS_SOURCE_ROUTED) { 13250514Slile 13350514Slile if (eflag) 13450514Slile token_print(p, length); 13550514Slile 13644165Sjulian route_len = RIF_LENGTH; 13750514Slile if (vflag) { 13850514Slile if (vflag > 1) 13950514Slile printf("ac %x fc %x ", tp->ac, tp->fc); 14050514Slile ether_type = ntohs((int)lp->ethertype); 14150514Slile 14250514Slile printf("%s ", broadcast_indicator[BROADCAST]); 14350514Slile printf("%s", direction[DIRECTION]); 14450514Slile 14550514Slile for (seg = 0; seg < SEGMENT_COUNT; seg++) 14650514Slile printf(" [%d:%d]", RING_NUMBER(seg), BRIDGE_NUMBER(seg)); 14750514Slile } else { 14850514Slile printf("rt = %x", ntohs(tp->rcf)); 14950514Slile 15050514Slile for (seg = 0; seg < SEGMENT_COUNT; seg++) 15150514Slile printf(":%x", ntohs(tp->rseg[seg])); 15250514Slile } 15350514Slile printf(" (%s) ", largest_frame[LARGEST_FRAME]); 15450514Slile } else { 15550514Slile if (eflag) 15650514Slile token_print(p, length); 15744165Sjulian } 15844165Sjulian 15944165Sjulian /* Set pointer to llc header, adjusted for routing information */ 16044165Sjulian lp = (struct llc *)(p + TOKEN_HDR_LEN + route_len); 16144165Sjulian 16244165Sjulian packetp = p; 16344165Sjulian snapend = p + caplen; 16444165Sjulian 16544165Sjulian /* Skip over token ring MAC header */ 16644165Sjulian length -= TOKEN_HDR_LEN + route_len; 16744165Sjulian caplen -= TOKEN_HDR_LEN + route_len; 16844165Sjulian p += TOKEN_HDR_LEN + route_len; 16944165Sjulian 17044165Sjulian extracted_ethertype = 0; 17144165Sjulian /* Try to print the LLC-layer header & higher layers */ 17244165Sjulian if (llc_print(p, length, caplen, ESRC(tp), EDST(tp)) == 0) { 17344165Sjulian /* ether_type not known, print raw packet */ 17444165Sjulian if (!eflag) 17544165Sjulian token_print((u_char *)tp, length); 17644165Sjulian if (extracted_ethertype) { 17744165Sjulian printf("(LLC %s) ", 17844165Sjulian etherproto_string(htons(extracted_ethertype))); 17944165Sjulian } 18044165Sjulian if (!xflag && !qflag) 18144165Sjulian default_print(p, caplen); 18244165Sjulian } 18344165Sjulian if (xflag) 18444165Sjulian default_print(p, caplen); 18544165Sjulian out: 18644165Sjulian putchar('\n'); 18744165Sjulian} 188