Home | History | Annotate | Line # | Download | only in mopprobe
mopprobe.c revision 1.4
      1  1.4  christos /*	$NetBSD: mopprobe.c,v 1.4 1997/04/17 21:09:27 christos Exp $	*/
      2  1.2   thorpej 
      3  1.1       cjs /*
      4  1.1       cjs  * Copyright (c) 1993-96 Mats O Jansson.  All rights reserved.
      5  1.1       cjs  *
      6  1.1       cjs  * Redistribution and use in source and binary forms, with or without
      7  1.1       cjs  * modification, are permitted provided that the following conditions
      8  1.1       cjs  * are met:
      9  1.1       cjs  * 1. Redistributions of source code must retain the above copyright
     10  1.1       cjs  *    notice, this list of conditions and the following disclaimer.
     11  1.1       cjs  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1       cjs  *    notice, this list of conditions and the following disclaimer in the
     13  1.1       cjs  *    documentation and/or other materials provided with the distribution.
     14  1.1       cjs  * 3. All advertising materials mentioning features or use of this software
     15  1.1       cjs  *    must display the following acknowledgement:
     16  1.1       cjs  *	This product includes software developed by Mats O Jansson.
     17  1.1       cjs  * 4. The name of the author may not be used to endorse or promote products
     18  1.1       cjs  *    derived from this software without specific prior written permission.
     19  1.1       cjs  *
     20  1.1       cjs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1       cjs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1       cjs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1       cjs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1       cjs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1       cjs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1       cjs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1       cjs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1       cjs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1       cjs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1       cjs  */
     31  1.1       cjs 
     32  1.1       cjs #ifndef LINT
     33  1.4  christos static char rcsid[] = "$NetBSD: mopprobe.c,v 1.4 1997/04/17 21:09:27 christos Exp $";
     34  1.1       cjs #endif
     35  1.1       cjs 
     36  1.1       cjs /*
     37  1.1       cjs  * mopprobe - MOP Probe Utility
     38  1.1       cjs  *
     39  1.1       cjs  * Usage:	mopprobe -a [ -3 | -4 ]
     40  1.1       cjs  *		mopprobe [ -3 | -4 ] interface
     41  1.1       cjs  */
     42  1.1       cjs 
     43  1.4  christos #include "os.h"
     44  1.1       cjs #include "common/common.h"
     45  1.1       cjs #include "common/mopdef.h"
     46  1.1       cjs #include "common/device.h"
     47  1.1       cjs #include "common/print.h"
     48  1.1       cjs #include "common/get.h"
     49  1.1       cjs #include "common/cmp.h"
     50  1.1       cjs #include "common/pf.h"
     51  1.1       cjs #include "common/nmadef.h"
     52  1.1       cjs 
     53  1.1       cjs /*
     54  1.1       cjs  * The list of all interfaces that are being listened to.  rarp_loop()
     55  1.1       cjs  * "selects" on the descriptors in this list.
     56  1.1       cjs  */
     57  1.1       cjs struct if_info *iflist;
     58  1.1       cjs 
     59  1.1       cjs #ifdef NO__P
     60  1.1       cjs void   Loop	     (/* void */);
     61  1.1       cjs void   Usage         (/* void */);
     62  1.1       cjs void   mopProcess    (/* struct if_info *, u_char * */);
     63  1.1       cjs #else
     64  1.1       cjs void   Loop	     __P((void));
     65  1.1       cjs void   Usage         __P((void));
     66  1.1       cjs void   mopProcess    __P((struct if_info *, u_char *));
     67  1.1       cjs #endif
     68  1.1       cjs 
     69  1.1       cjs int     AllFlag = 0;		/* listen on "all" interfaces  */
     70  1.1       cjs int     DebugFlag = 0;		/* print debugging messages    */
     71  1.1       cjs int	Not3Flag = 0;		/* Not MOP V3 messages         */
     72  1.1       cjs int	Not4Flag = 0;		/* Not MOP V4 messages         */
     73  1.1       cjs int     oflag = 0;		/* print only once             */
     74  1.1       cjs int	promisc = 1;		/* Need promisc mode           */
     75  1.1       cjs char	*Program;
     76  1.1       cjs 
     77  1.1       cjs void
     78  1.1       cjs main(argc, argv)
     79  1.1       cjs 	int     argc;
     80  1.1       cjs 	char  **argv;
     81  1.1       cjs {
     82  1.1       cjs 	int     op;
     83  1.1       cjs 	char   *interface;
     84  1.1       cjs 
     85  1.1       cjs 	extern int optind, opterr;
     86  1.1       cjs 
     87  1.1       cjs 	if ((Program = strrchr(argv[0], '/')))
     88  1.1       cjs 		Program++;
     89  1.1       cjs 	else
     90  1.1       cjs 		Program = argv[0];
     91  1.1       cjs 	if (*Program == '-')
     92  1.1       cjs 		Program++;
     93  1.1       cjs 
     94  1.1       cjs 	/* All error reporting is done through syslogs. */
     95  1.1       cjs 	openlog(Program, LOG_PID | LOG_CONS, LOG_DAEMON);
     96  1.1       cjs 
     97  1.1       cjs 	opterr = 0;
     98  1.1       cjs 	while ((op = getopt(argc, argv, "ado")) != EOF) {
     99  1.1       cjs 		switch (op) {
    100  1.1       cjs 		case '3':
    101  1.1       cjs 			Not3Flag++;
    102  1.1       cjs 			break;
    103  1.1       cjs 		case '4':
    104  1.1       cjs 			Not4Flag++;
    105  1.1       cjs 			break;
    106  1.1       cjs 		case 'a':
    107  1.1       cjs 			AllFlag++;
    108  1.1       cjs 			break;
    109  1.1       cjs 		case 'd':
    110  1.1       cjs 			DebugFlag++;
    111  1.1       cjs 			break;
    112  1.1       cjs 		case 'o':
    113  1.1       cjs 			oflag++;
    114  1.1       cjs 			break;
    115  1.1       cjs 
    116  1.1       cjs 		default:
    117  1.1       cjs 			Usage();
    118  1.1       cjs 			/* NOTREACHED */
    119  1.1       cjs 		}
    120  1.1       cjs 	}
    121  1.1       cjs 	interface = argv[optind++];
    122  1.1       cjs 
    123  1.1       cjs 	if ((AllFlag && interface) ||
    124  1.1       cjs 	    (!AllFlag && interface == 0) ||
    125  1.1       cjs 	    (Not3Flag && Not4Flag))
    126  1.1       cjs 		Usage();
    127  1.1       cjs 
    128  1.1       cjs 	if (AllFlag)
    129  1.1       cjs  		deviceInitAll();
    130  1.1       cjs 	else
    131  1.1       cjs 		deviceInitOne(interface);
    132  1.1       cjs 
    133  1.1       cjs 	Loop();
    134  1.1       cjs }
    135  1.1       cjs 
    136  1.1       cjs void
    137  1.1       cjs Usage()
    138  1.1       cjs {
    139  1.1       cjs 	(void) fprintf(stderr, "usage: %s -a [ -3 | -4 ]\n",Program);
    140  1.1       cjs 	(void) fprintf(stderr, "       %s [ -3 | -4 ] interface\n",Program);
    141  1.1       cjs 	exit(1);
    142  1.1       cjs }
    143  1.1       cjs 
    144  1.1       cjs /*
    145  1.1       cjs  * Process incomming packages.
    146  1.1       cjs  */
    147  1.1       cjs void
    148  1.1       cjs mopProcess(ii, pkt)
    149  1.1       cjs 	struct if_info *ii;
    150  1.1       cjs 	u_char *pkt;
    151  1.1       cjs {
    152  1.1       cjs 	u_char  *dst, *src, *p, mopcode, tmpc, ilen;
    153  1.1       cjs 	u_short *ptype, moplen, tmps, itype, len;
    154  1.1       cjs 	int	index, i, device, trans;
    155  1.1       cjs 
    156  1.1       cjs 	dst	= pkt;
    157  1.1       cjs 	src	= pkt+6;
    158  1.1       cjs 	ptype   = (u_short *)(pkt+12);
    159  1.1       cjs 	index   = 0;
    160  1.1       cjs 
    161  1.1       cjs 	if (*ptype < 1600) {
    162  1.1       cjs 		len = *ptype;
    163  1.1       cjs 		trans = TRANS_8023;
    164  1.1       cjs 		ptype = (u_short *)(pkt+20);
    165  1.1       cjs 		p = pkt+22;
    166  1.1       cjs 		if (Not4Flag) return;
    167  1.1       cjs 	} else {
    168  1.1       cjs 		len = 0;
    169  1.1       cjs 		trans = TRANS_ETHER;
    170  1.1       cjs 		p = pkt+14;
    171  1.1       cjs 		if (Not3Flag) return;
    172  1.1       cjs 	}
    173  1.1       cjs 
    174  1.1       cjs 	/* Ignore our own messages */
    175  1.1       cjs 
    176  1.1       cjs 	if (mopCmpEAddr(ii->eaddr,src) == 0) {
    177  1.1       cjs 		return;
    178  1.1       cjs 	}
    179  1.1       cjs 
    180  1.1       cjs 	/* Just check multicast */
    181  1.1       cjs 
    182  1.1       cjs 	if (mopCmpEAddr(rc_mcst,dst) != 0) {
    183  1.1       cjs 		return;
    184  1.1       cjs 	}
    185  1.1       cjs 
    186  1.1       cjs 	switch (trans) {
    187  1.1       cjs 	case TRANS_8023:
    188  1.1       cjs 		moplen = len;
    189  1.1       cjs 		break;
    190  1.1       cjs 	default:
    191  1.1       cjs 		moplen = mopGetShort(pkt,&index);
    192  1.1       cjs 	}
    193  1.1       cjs 	mopcode	= mopGetChar(p,&index);
    194  1.1       cjs 
    195  1.1       cjs 	/* Just process System Information */
    196  1.1       cjs 
    197  1.1       cjs 	if (mopcode != MOP_K_CODE_SID) {
    198  1.1       cjs 		return;
    199  1.1       cjs 	}
    200  1.1       cjs 
    201  1.1       cjs 	tmpc	= mopGetChar(pkt,&index);		/* Reserved  */
    202  1.1       cjs 	tmps	= mopGetShort(pkt,&index);		/* Receipt # */
    203  1.1       cjs 
    204  1.1       cjs 	device	= 0;					/* Unknown Device */
    205  1.1       cjs 
    206  1.1       cjs 	itype	= mopGetShort(pkt,&index);
    207  1.1       cjs 
    208  1.1       cjs 	while (index < (int)(moplen + 2)) {
    209  1.1       cjs 		ilen	= mopGetChar(pkt,&index);
    210  1.1       cjs 		switch (itype) {
    211  1.1       cjs 		case 0:
    212  1.1       cjs 			tmpc  = mopGetChar(pkt,&index);
    213  1.1       cjs 			index = index + tmpc;
    214  1.1       cjs 			break;
    215  1.1       cjs 	        case MOP_K_INFO_VER:
    216  1.1       cjs 			index = index + 3;
    217  1.1       cjs 			break;
    218  1.1       cjs 		case MOP_K_INFO_MFCT:
    219  1.1       cjs 			index = index + 2;
    220  1.1       cjs 			break;
    221  1.1       cjs 		case MOP_K_INFO_CNU:
    222  1.1       cjs 			index = index + 6;
    223  1.1       cjs 			break;
    224  1.1       cjs 		case MOP_K_INFO_RTM:
    225  1.1       cjs 			index = index + 2;
    226  1.1       cjs 			break;
    227  1.1       cjs 		case MOP_K_INFO_CSZ:
    228  1.1       cjs 			index = index + 2;
    229  1.1       cjs 			break;
    230  1.1       cjs 		case MOP_K_INFO_RSZ:
    231  1.1       cjs 			index = index + 2;
    232  1.1       cjs 			break;
    233  1.1       cjs 		case MOP_K_INFO_HWA:
    234  1.1       cjs 			index = index + 6;
    235  1.1       cjs 			break;
    236  1.1       cjs 		case MOP_K_INFO_TIME:
    237  1.1       cjs 			index = index + 10;
    238  1.1       cjs 			break;
    239  1.1       cjs 	        case MOP_K_INFO_SOFD:
    240  1.1       cjs 			device = mopGetChar(pkt,&index);
    241  1.1       cjs 			break;
    242  1.1       cjs 		case MOP_K_INFO_SFID:
    243  1.1       cjs 			tmpc = mopGetChar(pkt,&index);
    244  1.1       cjs 			if ((index > 0) && (index < 17))
    245  1.1       cjs 			  index = index + tmpc;
    246  1.1       cjs 			break;
    247  1.1       cjs 		case MOP_K_INFO_PRTY:
    248  1.1       cjs 			index = index + 1;
    249  1.1       cjs 			break;
    250  1.1       cjs 		case MOP_K_INFO_DLTY:
    251  1.1       cjs 			index = index + 1;
    252  1.1       cjs 			break;
    253  1.1       cjs 	        case MOP_K_INFO_DLBSZ:
    254  1.1       cjs 			index = index + 2;
    255  1.1       cjs 			break;
    256  1.1       cjs 		default:
    257  1.1       cjs 			if (((device = NMA_C_SOFD_LCS) ||   /* DECserver 100 */
    258  1.1       cjs 			     (device = NMA_C_SOFD_DS2) ||   /* DECserver 200 */
    259  1.1       cjs 			     (device = NMA_C_SOFD_DP2) ||   /* DECserver 250 */
    260  1.1       cjs 			     (device = NMA_C_SOFD_DS3)) &&  /* DECserver 300 */
    261  1.1       cjs 			    ((itype > 101) && (itype < 107)))
    262  1.1       cjs 			{
    263  1.1       cjs 				switch (itype) {
    264  1.1       cjs 				case 102:
    265  1.1       cjs 					index = index + ilen;
    266  1.1       cjs 					break;
    267  1.1       cjs 				case 103:
    268  1.1       cjs 					index = index + ilen;
    269  1.1       cjs 					break;
    270  1.1       cjs 				case 104:
    271  1.1       cjs 					index = index + 2;
    272  1.1       cjs 					break;
    273  1.1       cjs 				case 105:
    274  1.1       cjs 					(void)fprintf(stdout,"%x:%x:%x:%x:%x:%x\t",
    275  1.1       cjs 						      src[0],src[1],src[2],src[3],src[4],src[5]);
    276  1.1       cjs 					for (i = 0; i < ilen; i++) {
    277  1.1       cjs 					  (void)fprintf(stdout, "%c",pkt[index+i]);
    278  1.1       cjs 					}
    279  1.1       cjs 					index = index + ilen;
    280  1.1       cjs 					(void)fprintf(stdout, "\n");
    281  1.1       cjs 					break;
    282  1.1       cjs 				case 106:
    283  1.1       cjs 					index = index + ilen;
    284  1.1       cjs 					break;
    285  1.1       cjs 				};
    286  1.1       cjs 			} else {
    287  1.1       cjs 				index = index + ilen;
    288  1.1       cjs 			};
    289  1.1       cjs 		}
    290  1.1       cjs 		itype = mopGetShort(pkt,&index);
    291  1.1       cjs 	}
    292  1.1       cjs 
    293  1.1       cjs }
    294  1.1       cjs 
    295