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