9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Sergey Reva <rs_rlab@mail.ru>
To: Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu>,
	Russ Cox <russcox@gmail.com>
Subject: Re: [9fans] srv to stdin, stdout
Date: Sat, 28 May 2005 20:55:58 +0300	[thread overview]
Message-ID: <3028305906.20050528205558@mail.ru> (raw)
In-Reply-To: <ee9e417a05052809346af76863@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 778 bytes --]

Hello Russ,

Saturday, May 28, 2005, 7:34:42 PM, you wrote:
RC> I don't understand how
RC> using standard input and output could break 
RC> Twrite transactions.
That's only begining...
Get out initialization of infd and outfd, so both should be 0... result the same!

in one window:
cpu% aux/listen1 tcp!*!33 /usr/rlab/encsrv/xorfs -s

in another:
cpu% 9fs tcp!myfs!33
post...
cpu% cd /n/myfs!33
cpu% lc
ctl             data    enc             key
cpu% echo test >data
echo: write error: mount rpc error
cpu%

and this on 'black screen'
mnt: proc rc 193: mistmatch from /net/tcp/8/data /n/myfs!33/data rep 0x80bfd6f0 tag 8 fid 453 T120 R119

source is attached...

Sergey
-- 
http://rs-rlab.narod.ru                            mailto:rs_rlab@mail.ru

[-- Attachment #2: xorfs.c --]
[-- Type: APPLICATION/OCTET-STREAM, Size: 3867 bytes --]

#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

char* srvname={"xorfs"};
char* mntpt=nil;

void myopen(Req *r);
void mywrite(Req *r);
void myread(Req *r);
void mycreate(Req *r);

enum {
	DataFile,
	CtlFile,
	KeyFile,
	EncFile,

	MAX_WORD=24,
};

char *Data=nil, *EncData=nil, *EncKey=nil;
vlong dlen, elen, klen;
int isdecoded=0, remote=0;

#define DBG print

Srv mysrv={
	.open=myopen,
	.read=myread,
	.write=mywrite,
	.create=mycreate,
};

void Crypto(char* dst, char* src, ulong dlen, char* key, ulong keylen)
{
	ulong i,n;

	for (i=0, n=0;i<dlen;i++,n++)
	{
		if (n>=keylen)
			n=0;

		dst[i]=src[i]^key[n];
	}
}

int isvalid(char* src, ulong srclen)
{
  	ulong i,j;

	for(i=0, j=0; i < srclen; i++, j++)
	{ 
		if(j>MAX_WORD)
			return 0;
		if( src[i]>' ' && src[i]<='~')
			continue;
		if( src[i] == ' ' || src[i] == '\n' || src[i] == '\t' || src[i] == '\r' )
		{
			j=0;
			continue;
		}
		return 0;
	}

	return 1;
}

void mywrite(Req *r)
{
	// r->ifcall.offset
	// r->ifcall.count
	// r->ifcall.data

	switch((ulong)r->fid->file->aux)
	{
	case CtlFile:
		break;
	case DataFile:
		isdecoded=0;
		if (Data!=nil)
			free(Data);
		Data=emalloc9p(r->ifcall.count);

		if (EncData!=nil)
			free(EncData);
		EncData=emalloc9p(r->ifcall.count);

		dlen=r->ifcall.count;
		memcpy(Data, r->ifcall.data, r->ifcall.count);

		DBG("Encsrv: write %d bytes to Data\n",r->ifcall.count);
		break;
	case EncFile:
		respond(r,"permission denied");
		return;
	case KeyFile:
		isdecoded=0;
		if (EncKey!=nil)
			free(EncKey);
		EncKey=emalloc9p(r->ifcall.count);
		klen=r->ifcall.count;
		memcpy(EncKey, r->ifcall.data, r->ifcall.count);
		DBG("Encsrv: write %d bytes to KeyFile\n",r->ifcall.count);
		break;
	default:
		respond(r,"unbelievable write");
		return;
	}

	respond(r,nil);
}

void myread(Req *r)
{
	ulong n;

	switch((ulong)r->fid->file->aux)
	{
	case CtlFile:
		if (!isdecoded)
		{
			DBG("Encsrv: decoding...");			
			Crypto(EncData, Data, dlen, EncKey, klen);
			isdecoded=1;
			DBG("done\n");
		}

		if (isvalid(EncData, dlen))
			readbuf(r,"valid",5);
		else
			readbuf(r,nil,0);
		break;
	case DataFile:
		n=dlen-r->ifcall.offset;
		if (n>0)
		{
			if (n>r->ifcall.count)
				n=r->ifcall.count;
			readbuf(r,Data,n);
		}
		else
			readbuf(r,Data,0);
		break;
	case EncFile:
		if (!isdecoded)
		{
			DBG("Encsrv: decoding...");			
			Crypto(EncData, Data, dlen, EncKey, klen);
			isdecoded=1;
			DBG("done\n");
		}

		n=dlen-r->ifcall.offset;
		if (n>0)
		{
			if (n>r->ifcall.count)
				n=r->ifcall.count;
			readbuf(r,EncData,n);
		}
		else
			readbuf(r,EncData,0);
		break;
	case KeyFile:
		n=klen-r->ifcall.offset;
		if (n>0)
		{
			if (n>r->ifcall.count)
				n=r->ifcall.count;
			readbuf(r,EncKey,n);
		}
		else
			readbuf(r,EncKey,0);
		break;
	default:
		respond(r,"unbelievable read");
		return;
	}

	respond(r,nil);
}

void myopen(Req *r)
{
	respond(r,nil);
}

void mycreate(Req *r)
{
	respond(r,"permission denied");
}

void main(int argc, char *argv[])
{

	ARGBEGIN {
	case 'm':
		mntpt=ARGF();
		break;
	case 'D':
		chatty9p++;
		break;
	case 's':
		remote++;
		break;
	default:
		print("usage %s -m mntpnt -s\n",argv[0]);
		exits(0);
		break;
	} ARGEND

	mysrv.tree=alloctree(nil,nil,DMDIR|0777,nil);
	createfile(mysrv.tree->root,"data",nil,0666,(void*)DataFile);
	createfile(mysrv.tree->root,"ctl",nil,0666,(void*)CtlFile);
	createfile(mysrv.tree->root,"key",nil,0666,(void*)KeyFile);
	createfile(mysrv.tree->root,"enc",nil,0666,(void*)EncFile);

	if (remote)
	{
		srv(&mysrv);
	}
	else
	{
		postmountsrv(&mysrv,srvname,mntpt,MREPL|MCREATE);
	}

	exits(0);
}

  reply	other threads:[~2005-05-28 17:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-28 14:12 Sergey Reva
2005-05-28 16:34 ` Russ Cox
2005-05-28 17:55   ` Sergey Reva [this message]
2005-05-28 18:24     ` Russ Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3028305906.20050528205558@mail.ru \
    --to=rs_rlab@mail.ru \
    --cc=9fans@cse.psu.edu \
    --cc=russcox@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).