9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Sam <sah@softcardsystems.com>
To: <9fans@cse.psu.edu>
Subject: Re: [9fans] how to avoid a memset() optimization
Date: Mon,  6 Jan 2003 11:02:16 -0500	[thread overview]
Message-ID: <Pine.LNX.4.30.0301061054340.19189-100000@athena> (raw)
In-Reply-To: <53a650f30ccb9218b76af22fb32a1bc1@plan9.bell-labs.com>

ooh, a challenge.  Nothing pleases me more than proving myself
wrong.  be back in a few. ;)

btw - i don't fear the goto.  I use it a lot in small loop
situations to avoid empty for loops and such.  I'll play
with this function and see what I find. ;)

Cheers,

Sam

On Mon, 6
Jan 2003, Russ Cox wrote:

> > you just need a marker to see how far you got. (I'm not even sure
> > what the context of this message is, but every time i see the goto
> > solution to this I have to say something). ;)
>
> the goto seems much clearer to me.
> also easier to edit.  what happens when
> you have to lock A½?  you have to rewrite
> all your case statements!
>
> g% grep goto /sys/games/lib/fortunes |sed 1q
> If you want to go somewhere, goto is the best way to get there. K Thompson
> g%
>
> also you can't always write the code in your if-style,
> but you can always use a goto.  for example, you can't
> rewrite this without goto (or at least, if you do, it ends
> up a lot less clear or a lot more duplicated):
>
> static int
> p9skclient(Conv *c)
> {
> 	char *user;
> 	char cchal[CHALLEN];
> 	uchar secret[8];
> 	char buf[MAXAUTH];
> 	int speakfor, ret;
> 	Attr *a;
> 	Authenticator au;
> 	Key *k;
> 	Ticket t;
> 	Ticketreq tr;
>
> 	ret = -1;
> 	a = nil;
>  	k = nil;
>
> 	/* p9sk1: send client challenge */
> 	if(c->proto == &p9sk1){
> 		c->state = "write challenge";
> 		memrandom(cchal, CHALLEN);
> 		if(convwrite(c, cchal, CHALLEN) < 0)
> 			goto out;
> 	}
>
> 	/* read ticket request */
> 	c->state = "read tickreq";
> 	if(convread(c, buf, TICKREQLEN) < 0)
> 		goto out;
> 	convM2TR(buf, &tr);
>
> 	/* p9sk2: use server challenge as client challenge */
> 	if(c->proto == &p9sk2)
> 		memmove(cchal, tr.chal, CHALLEN);
>
> 	/*
> 	 * find a key.
> 	 *
> 	 * if the user is the factotum owner, any key will do.
> 	 * if not, then if we have a speakfor key,
> 	 * we will only vouch for the user's local identity.
> 	 *
> 	 * this logic is duplicated in p9any.c
> 	 */
> 	user = strfindattr(c->attr, "user");
> 	a = delattr(copyattr(c->attr), "role");
> 	a = addattr(a, "proto=p9sk1");
>
> 	if(strcmp(c->sysuser, owner) == 0){
> 		speakfor = 0;
> 		a = addattr(a, "proto=p9sk1 user? dom=%q", tr.authdom);
> 	}else if(user==nil || strcmp(c->sysuser, user)==0){
> 		speakfor = 1;
> 		a = delattr(a, "user");
> 		a = addattr(a, "proto=p9sk1 user? dom=%q role=speakfor", tr.authdom);
> 	}else{
> 		werrstr("will not authenticate for %q as %q", c->sysuser, user);
> 		goto out;
> 	}
>
> 	for(;;){
> 		c->state = "find key";
> 		k = keyfetch(c, "%A", a);
> 		if(k == nil)
> 			goto out;
>
> 		/* relay ticket request to auth server, get tickets */
> 		strcpy(tr.hostid, strfindattr(k->attr, "user"));
> 		if(speakfor)
> 			strcpy(tr.uid, c->sysuser);
> 		else
> 			strcpy(tr.uid, tr.hostid);
>
> 		c->state = "get tickets";
> 		if(gettickets(&tr, buf, k) < 0)
> 			goto out;
>
> 		convM2T(buf, &t, k->priv);
> 		if(t.num == AuthTc)
> 			break;
>
> 		/* we don't agree with the auth server about the key; try again */
> 		c->state = "replace key";
> 		if((k = keyreplace(c, k, "key mismatch with auth server")) == nil){
> 			werrstr("key mismatch with auth server");
> 			goto out;
> 		}
> 	}
>
> 	/* send second ticket and authenticator to server */
> 	c->state = "write ticket+auth";
> 	memmove(buf, buf+TICKETLEN, TICKETLEN);
> 	au.num = AuthAc;
> 	memmove(au.chal, tr.chal, CHALLEN);
> 	au.id = 0;
> 	convA2M(&au, buf+TICKETLEN, t.key);
> 	if(convwrite(c, buf, TICKETLEN+AUTHENTLEN) < 0)
> 		goto out;
>
> 	/* read authenticator from server */
> 	c->state = "read auth";
> 	if(convread(c, buf, AUTHENTLEN) < 0)
> 		goto out;
> 	convM2A(buf, &au, t.key);
> 	if(au.num != AuthAs || memcmp(au.chal, cchal, CHALLEN) != 0 || au.id != 0){
> 		werrstr("server lies through his teeth");
> 		goto out;
> 	}
>
> 	/* success */
> 	c->attr = addcap(c->attr, c->sysuser, &t);
> 	des56to64((uchar*)t.key, secret);
> 	c->attr = addattr(c->attr, "secret=%.8H", secret);
> 	ret = 0;
>
> out:
> 	freeattr(a);
> 	keyclose(k);
> 	return ret;
> }
>



  parent reply	other threads:[~2003-01-06 16:02 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-14 16:47 presotto
2002-11-15 10:50 ` Douglas A. Gwyn
2002-11-15 16:51   ` William Josephson
2002-11-18 10:38     ` Douglas A. Gwyn
2002-11-18 12:34       ` Ronald G. Minnich
2002-11-19  7:38 ` Roman V. Shaposhnick
2002-11-20  9:47   ` Douglas A. Gwyn
2002-11-21 20:55     ` Roman V. Shaposhnick
2002-11-22  9:59       ` Douglas A. Gwyn
2003-01-06 10:18   ` Ralph Corderoy
2003-01-06 15:42     ` Sam
2003-01-06 15:49       ` Russ Cox
2003-01-06 15:58         ` David Presotto
2003-01-06 16:02         ` Sam [this message]
2003-01-06 15:58     ` [9fans] Fs Kernel Conor Williams
  -- strict thread matches above, loose matches on Subject: below --
2003-01-06 10:47 [9fans] how to avoid a memset() optimization nigel
2003-01-06 11:15 ` Geoff Collyer
2002-11-19 14:32 presotto
2002-11-20  7:24 ` Tomas
2002-11-20 16:38   ` Douglas A. Gwyn
2002-11-19  8:21 Fco.J.Ballesteros
2002-11-18 21:36 Joel Salomon
2002-11-18 20:42 Andrew Simmons
2002-11-18 14:19 C H Forsyth
2002-11-15  1:56 Dennis Ritchie
2002-11-15 10:51 ` Douglas A. Gwyn
2002-11-15 12:03 ` Boyd Roberts
2002-11-14 18:55 jmk
2002-11-14 22:23 ` Steve Kilbane
2002-11-14 18:17 presotto
2002-11-14 18:11 Joel Salomon
2002-11-14 18:26 ` William Josephson
2002-11-14 17:44 rog
2002-11-15 10:50 ` Douglas A. Gwyn
2002-11-14 17:28 Russ Cox
     [not found] <nemo@plan9.escet.urjc.es>
2002-11-14 15:38 ` Fco.J.Ballesteros
2002-11-14 16:24   ` Scott Schwartz
2002-11-14  6:53 Russ Cox
2002-11-14 10:22 ` Douglas A. Gwyn
2002-11-14 13:20   ` Sam
2002-11-14 15:20     ` Scott Schwartz
2002-11-14 15:26       ` Boyd Roberts
2002-11-14 15:34         ` plan9
2002-11-14 15:59           ` Sam
2002-11-14 18:57         ` Steve Kilbane
2002-11-15 10:51           ` Douglas A. Gwyn
2002-11-14 15:50       ` Dan Cross
2002-11-14 17:21         ` Douglas A. Gwyn
2002-11-14 18:51           ` Dan Cross
2002-11-14 15:50     ` Douglas A. Gwyn
2002-11-19  7:20 ` Roman V. Shaposhnick
2002-11-14  2:48 Dennis Ritchie
2002-11-14  4:23 ` Ronald G. Minnich
2002-11-13 18:58 Rob `Commander' Pike
2002-11-13 14:40 C H Forsyth
2002-11-13 15:54 ` rob pike
2002-11-13 16:05   ` andrey mirtchovski
2002-11-13 16:32   ` Ronald G. Minnich
2002-11-14 10:21     ` Douglas A. Gwyn
2002-11-14 17:07       ` Ronald G. Minnich
2002-11-22  9:59     ` Clint Olsen
2002-11-13 16:56   ` William K. Josephson
2002-11-14 10:21     ` Douglas A. Gwyn
2002-11-14 16:48       ` William Josephson
2002-11-14 10:21   ` Douglas A. Gwyn
2002-11-14 14:46     ` Dan Cross
2002-11-14 16:59       ` Douglas A. Gwyn
2002-11-14 18:31         ` Tad Hunt
2002-11-15 10:50           ` Douglas A. Gwyn
2002-11-18 14:27         ` Aharon Robbins
2002-11-13 14:14 Skip Tavakkolian
2002-11-13 13:55 rog
2002-11-13 13:38 Skip Tavakkolian
2002-11-13 16:25 ` Boyd Roberts
2002-11-13 10:43 C H Forsyth
2002-11-14 10:21 ` Douglas A. Gwyn
2002-11-13  6:52 Geoff Collyer
2002-11-13 10:13 ` Boyd Roberts
2002-11-13  6:34 Andrew Simmons
2002-11-13  6:43 ` Doc Shipley
2002-11-13  1:47 Russ Cox
2002-11-13 10:16 ` Douglas A. Gwyn
2002-11-14  1:46 ` Roman V. Shaposhnick
2002-11-14  1:52   ` William Josephson
2002-11-14  6:42     ` Roman V. Shaposhnick
2002-11-13  0:31 Russ Cox
2002-11-13  1:26 ` Roman V. Shaposhnick
2002-11-13 10:15   ` Douglas A. Gwyn
2002-11-14  1:42     ` Roman V. Shaposhnick
2002-11-13 10:15 ` Douglas A. Gwyn
2002-11-13  0:20 presotto
2002-11-12 22:42 Roman V. Shaposhnick

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=Pine.LNX.4.30.0301061054340.19189-100000@athena \
    --to=sah@softcardsystems.com \
    --cc=9fans@cse.psu.edu \
    /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).