Plan 9 from Bell Labs’s /usr/web/sources/contrib/axel/8021x/v102/util.c

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


#include <u.h>
#include <libc.h>
#include <thread.h>
#include <ip.h>
#include "dat.h"
#include "fns.h"

static char tohex[16] = {
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	'a', 'b', 'c', 'd', 'e', 'f'
};

static char hexbuf[256];

char*
hexprefix(uchar *s, int n, int l)
{
	char *p;
	int c, i;

	memset(hexbuf, 0 , sizeof(hexbuf));
	p=hexbuf;
	for(i = 0; i < n && i < l && i < (sizeof(hexbuf)/2 -1); i++){
		c = s[i];
		*p++ = tohex[c>>4];
		*p++ = tohex[c&0xf]; 
	}
	return hexbuf;
}

void
put24(uchar *p, int x)
{
	p[0] = x>>16;
	p[1] = x>>8;
	p[2] = x;
}

int
apetheraddr(uchar *eaddr, char *dir)
{
	char fname[256];
	char buf[2048];
	int fd;
	int n;
	char *p, *e;
	char *s;
	int l;

	s = "Base station: ";
	l = strlen(s);
	snprint(fname, sizeof(fname), "%s/ifstats", dir);
	fd = open(fname, OREAD);
	if (fd < 0) {
		print("cannot open %s: %r", fname);
		return -1;
	}

	n = read(fd, buf, sizeof(buf));
	if (n < 0) {
		print("cannot read ifstats %s: %r", fname);
		close(fd);
		return -1;
	}
	close(fd);

	p = buf;
	e = p + n;
	while ((e - p > l) && strncmp(p, s, l) != 0) {
		p = strchr(p, '\n');
		p++;
	}
	if ((e - p > l) && strncmp(p, s, l) == 0) {
		p += l;
		if (parseether(eaddr, p) < 0) {
			print("cannot parse ether from ifstats");
			return -1;
		}
	}
	return 0;
}

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].