Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/aux/ncpfs/ncpfmt.c

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


#include <u.h>
#include <libc.h>
#include <ctype.h>
#include "ncp.h"

static char *
nwflags(int n)
{
	char buf[1024];

	snprint(buf, sizeof buf, "%s%s%s%s",
		(n & (1 << 0))? "bad service connection,": "",
		(n & (1 << 2))? "no connections avaliable,": "",
		(n & (1 << 4))? "server is down,": "",
		(n & (1 << 6))? "broadcast message waiting": "");
	return buf;
}

static int 
xd(Fmt *f, uchar *buf, int len)
{
	int n, i, j, addr;

	n = 0;
	for (addr = 0; addr < len; addr += 16, buf += 16) {
		n += fmtprint(f, "\t%04x  ", addr);
		for (i = 0; i < 16 && i < (len - addr); i++)
			n += fmtprint(f, "%02x ", buf[i]);
		for (j = i; j < 16; j++)
			n += fmtprint(f, "   ");
		n += fmtprint(f, "  ");
		for (i = 0; i < 16 && i < (len - addr); i++)
			if (isprint(buf[i]))
				n += fmtprint(f, "%c", buf[i]);
			else
				n += fmtprint(f, ".");
		n += fmtprint(f, "\n");
	}
	return n;
}

int
ncpfmt(Fmt *f)
{
	ulong len, sig, mtu, vers;
	uchar *b = va_arg(f->args, void *);
	uint n, seq, off, conn, task, func, flags, err;

	sig = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
	len = (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | b[7];

	SET(vers);
	SET(task);

	if (((b[16] << 8) | b[17]) == 0x2222){
		vers = (b[8] << 24) | (b[9] << 16) | (b[10] << 8) | b[11];
		mtu = (b[12] << 24) | (b[13] << 16) | (b[14] << 8) | b[15];
		seq = b[18];
		conn = (b[21] << 8) | b[19];
		task = b[20];
		func = b[22];
		n = fmtprint(f, "\n=> request sig=0x%08lux len=%lud mtu=%lud ver=%ld seq=%d conn=%ud task=%ud func=0x%02ux\n", 
			sig, len, mtu, vers, seq, conn, task, func);
		off = 23;
	}
	else
	if (((b[8] << 8) | b[9]) == 0x3333){
		seq = b[10];
		conn = (b[13] << 8) | b[11];
		task = b[12];
		err = b[14];
		flags = b[15];
		n = fmtprint(f, "<= reply sig=0x%08lux len=%lud conn=%ud task=%d stat=%q flags=%q err=%r\n", 
			sig, len, conn, task, nwerrstr(err), nwflags(flags));
		off = 16;
	}
	else
	if (((b[16] << 8) | b[17]) == 0x1111){
		vers = (b[8] << 24) | (b[9] << 16) | (b[10] << 8) | b[11];
		mtu = (b[12] << 24) | (b[13] << 16) | (b[14] << 8) | b[15];
		seq = b[18];
		conn = (b[21] << 8) | b[19];
		task = b[20];
		func = b[22];
		n = fmtprint(f, "=> attach sig=0x%08lux vers=%ld len=%lud mtu=%lud conn=%ud task=%d func=0x%02ux\n",
			sig, vers, len, mtu, conn, task, func);
		off = 21;
	}
	else
	if (((b[16] << 8) | b[17]) == 0x1111){
		vers = (b[8] << 24) | (b[9] << 16) | (b[10] << 8) | b[11];
		mtu = (b[12] << 24) | (b[13] << 16) | (b[14] << 8) | b[15];
		seq = b[18];
		conn = (b[21] << 8) | b[19];
		n = fmtprint(f, "\n=> detach sig=0x%08lux vers=%ld len=%lud mtu=%lud conn=%ud task=%d\n",
			sig, vers, len, mtu, conn, task);
		off = 23;
	}
	else
		return fmtprint(f, "unknown packet type 0x%02ux\n", ((b[8] << 8) | b[9]));
	
	USED(sig, vers, seq, task);

	if (off >= len)
		return n;

	n += xd(f, b + off, len - off);

	return n;
}


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