-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Sep 30, 2005 at 10:10:25AM +1000, Robert Collins wrote:
> On Fri, 2005-09-30 at 07:58 +1000, [EMAIL PROTECTED] wrote:

> > How about writing a network protocol stack. You get a packet and all
> > you know about it is that here is a block of memory. You then have
> > to figure out what sort of packet it is, how long it is and what
> > structure to give it. C handles this very nicely with pointers to
> > structures that can be cast into whatever you need.
> 
> Its straight forward in python and smalltalk. you have an array of
> octets, do what you need. What you don't have is something that can be
> pointed at a random memory region and used to bypass the type system and
> vm ;0

So with an array of octets you need an equation to reconstruct a 32 bit
number from the array and another equation to reconstruct the octets from
a number. Then you write functions something like:

read_u32_from_octet_array( array, index )         --> returns u32
write_u32_into_octet_array( array, index, data )  --> updates the array

...and so on for all the different things that are going to be in the array.

Excellent, you can now work on your network packets... but you have
now reconstructed the entire concept of a "struct" and done it in explicit
functions with explicit equations. Not only do you have the slowest network
stack in the whole world but worse than that, you still have no type
safety because if you write data from an index which is off by one and
then read it back you get completely bogus data. Some of those bogus values
could be packet length so when you get them wrong you read off the end of
the array... Oh, goody, python and smalltalk are protected from reading off
the end of the array but how is that going to help? You won't crash but
will instead throw a runtime exception which is only mildly more useful
than a crash.

Sure, you can't have a bad pointer, and your program won't crash.
On the other hand, you have to carefully juggle the array index values all
the time and one mistake will still leave an unrecoverable situation.
All your high level language with type protection hasn't done a thing
towards improving the correctness of your final program nor has it made
it any more difficult to make an error.

A much better method is to do what perl does which is to treat the network
packet as a binary "blob" and then provide special "pack" and "unpack" commands
that turn a binary blob into local language constructs and then turn local
language constructs back into a blob again. This is still slow, but each packet
only needs to be packed and unpacked a few times. Then all the difficult magic
is tied up in getting the right codes for the pack and unpack, once you get
that right, the rest is reasonably stable. Mind you, if you want to handle
a range of network protocols, you need a whole bunch of different pack/unpack
combos that pop into action.

At the end of the day though, C code to do the same thing using pointers
will be smaller, faster and probably easier to read as well.


        - Tel
 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQIVAwUBQz9gAsfOVl0KFTApAQKvsRAAouogH1xkowFocR6MtUppt7DIn5aY2N2F
8pGUqMEnEVc61jDTzTfomU2OpGS948o97QtdSfg2owonOl5A4XjkPYESlmhmoren
M9MKcHBEVz3Uo334SKjaih5rp17bpF0xFnGV7nWk5TAi1qznMlU57NE2wE34LzE5
FlbBXfceL8tctXewiRwQwFOsyb7MJp9NiFMM/l6qwE+amx+as23bUPyWYrmbjbvr
fml5IQNlQbhMR2JxINHDtEkxGYUivkQQyNuBwGYfE+v8pezSKXEpWQtEP2f03D79
Wvn3aVR+JOFXwTF/XaoB7Rae/sTmY0eyBiVmlw8k8pH/jeg6Lcxl7M+atOD/jykj
HRiXrIWyQ+Nj8mod6OvHvSMFNO4l93IYOXDOCNnshG3PaP9jeN/TGS8v8y2UtWzv
Dm+A2iWEagwu+GAvOwMH2BzDzJKXVEdd9MtcMLXhdM9MQpGiiw2AUSqmubH32EuL
KFDL9Kyzk0UYab92YgGSHiFiJe34i6Koa9uCvGhY1odPVo4X+3W+uINPUegB2Q/h
viU9hLt849RMSjQjSXnAdxoCrmvcehE2oMMxGlI+d7f+KXnSkKqs1cEPr1zdeaWD
/lVTaNMXukuIqkqBgxuYTU7pK/xyEXq4HUOH6TRCckvnL0oQkyAGN83ujVsKzyAh
4NHG1JJdTI4=
=UQi3
-----END PGP SIGNATURE-----
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to