Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/ndb/glean/misc.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
#include <ip.h>
#include <ctype.h>
#include "nbcache.h"

char *Netdir;

void
strlwr(char *s)
{
	for(; *s; s++)
		if(isupper(*s))
			*s = tolower(*s);
}

void
trim(char *name, char pad)
{
	char *p;

	p = strchr(name, 0) -1;
	while(p >= name && *p == pad)
		*p-- = 0;
}


uchar *
lookarp(uchar ip[IPv4addrlen])
{
	Biobuf *bp;
	uchar *mac;
	char path[128], ipstr[32], *line, *a[8];

	snprint(ipstr, sizeof(ipstr), "%V", ip);

	snprint(path, sizeof(path), "%s/arp", Netdir);
	if((bp = Bopen(path, OREAD)) == nil)
		return nil;

	while((line = Brdline(bp, '\n')) != nil){
		line[Blinelen(bp)-1] = 0;
		if(tokenize(line, a, nelem(a)) != 4)
			continue;

		if(strcmp(a[0], "ether") != 0)		/* garbled line */
			continue;
		if(strcmp(a[1], "OK") != 0){		/* host down */
			line = nil;
			break;
		}
		if(strcmp(a[2], ipstr) == 0)		/* match */
			break;
	}

	Bterm(bp);
	if(line == nil)
		return nil;
	if((mac = malloc(Etheraddrlen)) == nil)
		sysfatal("No memory %r\n");
	parseether(mac, a[3]);
	return mac;
}

char *
nicvendor(uchar *mac)
{
	Biobuf *bp;
	int len, l, best;
	char *line, *match, macstr[32];

	if((bp = Bopen("/lib/oui", OREAD)) == nil)
		return nil;

	best = -1;
	match = nil;
	len = snprint(macstr, sizeof(macstr), "%E", mac);
	while((line = Brdline(bp, '\n')) != nil){
		line[Blinelen(bp)-1] = 0;
		for(l = 4; l < len; l++){
			if(strncmp(macstr, line, l) == 0){
				if(l > best){
					best = l;
					free(match);
					match = smprint("%s", line+7);
				}
			}
		}
	}
	Bterm(bp);
	return match;
}


char *
servname(int flags, int svs)
{
	static char *unique[] = {
		[0x00] 	"workstation",
		[0x01]	"messenger service",
		[0x03]	"WinPopup",
		[0x06]	"RAS server",
		[0x1b]	"domain master browser",
		[0x1d]	"local master browser",
		[0x1F]	"NetDDE",
		[0x20]	"file sharing",
		[0x21]	"RAS client",
		[0x22]	"Exchange Interchange",
	 	[0x23]	"Exchange Store",
		[0x24]	"Exchange Directory",
		[0x2B]	"Lotus Notes",
		[0x30]	"modem driver",
		[0x31]	"modem client",
		[0x42]	"McAfee anti-virus",
		[0x43]	"SMS control client",
		[0x44]	"SMS control admin",
		[0x45]	"SMS chat client",
		[0x46]	"SMS file client",
		[0x4C]	"DEC PathWorks",
		[0x52]	"DEC PathWorks",
		[0x6A]	"Exchange IMC",
		[0x87]	"Exchange MTA",
		[0xBE]	"NetMon agent",
		[0xBf]	"NetMon application",
	};
	static char *group[0xff] = {
		[0x00]	"domain name",
		[0x01]	"local master browser",
		[0x03]	"messenger service",
		[0x1B]	"domain master browser",
		[0x1C]	"domain controller",
		[0x1D]	"local master browser",
		[0x1E]	"Browser Election service",
		[0x20]	"DCA IrmaLan gateway server",
		[0x2F]	"Lotus Notes",
		[0x33]	"Lotus Notes",
	};

	if(svs < 0)
		return nil;

	if(flags & 0x8000){
		if(svs >= nelem(group))
			return nil;
		return group[svs];
	}

	if(svs >= nelem(unique))
		return nil;
	return unique[svs];
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].