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