|slitch & netkit
||[Apr. 13th, 2007|06:40 pm]
Years ago I wrote bits of low-level IP networking code in Common Lisp that never took the shape of a finished program. I don't expect to finish this off so now I'm pushing the unpolished code out in the hope that someone will have fun with it. (An older version of some of this exists on Sourceforge.)
slitch and netkit are the two darcs repos. Slitch started out as an ethernet switch in Common Lisp (my therapy after hacking Linux) and later grew some other random bits from my experiments. netkit started as an attempt to rewrite Slitch without the cruft but ended up having different features.
The code is written for CMUCL on Linux. I hope it isn't too hard to port.
Here are some highlights from Slitch:
Here's a "screenshot" of Slitch:
- tuntap.lisp is an FFI interface to Linux tun/tap virtual network interfaces. This is good for communicating with the TCP/IP stack on the host machine.
- packet-socket.lisp is an FFI interface to Linux PF_PACKET sockets. This is good to communicate with the ethernet port directly (bypassing the host).
- netlib-structures.lisp contains protocol header definitions based on Frode Vatvedt Fjeld's binary-types package.
- binary-rw-gen.lisp is a macro-based compiler for binary-types encoders and decoders.
- switch.lisp is a working ethernet switch in a page and a half of code.
- root.lisp is a library for conveniently switching between normal-user/super-user mode on Linux.
- tcpip.lisp is a partial TCP/IP stack that can answer pings, send ICMP, talk to telnet, etc. Lacks important features like TCP retransmission.
lisp0 UP Type:TAP HWaddr 00:FF:C5:50:B6:E4
Inet addr:192.168.43.10/255.255.255.0 Bcast:192.168.43.255
RX bytes:196325 (196.3KiB) TX bytes:190508 (190.5KiB)>
lisp1 DOWN Type:TAP HWaddr 00:FF:6B:5D:36:60
Inet addr:0.0.0.0/0.0.0.0 Bcast:0.0.0.0
RX bytes:0 (0 b) TX bytes:0 (0 b)>
TCP Stream 192.168.43.10:80 / 192.168.43.1:33398: ESTABLISHED
TCP Stream 192.168.43.10:80 / 192.168.43.1:33400: ESTABLISHED
Destination Gateway Netmask Interface
192.168.128.43 0.0.0.0 255.255.255.255 lisp0
192.168.43.0 0.0.0.0 255.255.255.0 lisp0
192.168.128.0 192.168.43.1 255.255.255.0 lisp0
And here's some stuff in netkit:
This was all following on from related work in Erlang with Martin Björklund and Tony Rogvall, some of which exists in the Jungerl. These days I've switched to hacking this stuff in Squeak.
- packet.lisp is a stand-alone encoder and decoder for TCP/IP protocol headers. This time I wrote it in straight Lisp without binary-types. It's a semi-literate pbook program that you can print from packet.pdf to read on the bus.
- pcap.lisp is a decoder for the capture files produced by tcpdump.
- clim.lisp is a CLIM interface for manipulating pcap files. (This file seems to have syntax errors, I hope they're simple.) The image above is a screenshot.
I was reminded of this code by ILC conversations: some with the Dylan Hackers who've written something more polished in Dylan and some others about memory management. My own interest in memory management comes from writing Slitch with complete disregard for efficiency and then out of curiosity checking out its throughput and latency characteristics. There I discovered the large constant factors of CMUCL's garbage collector (scavenging static space and zeroing oldspace) and began to ponder what it all means.
2007-04-13 08:52 pm (UTC)
Very very cool.
I have a question though. Why do you switch to smalltalk?
Is it the GUI toolkit? If I'm not mistaken, the CL macros and REPL are very handy for hacking this stuff.
Thanks for sharing.
Squeak is a thing of beauty. Imagine if Dave Moon, Richard Stallman, Guy Steele, etc reunited and wrote a completely new and free successor to the Lisp Machine. Then we would have Squeak for Lisp.
.. and personally I'm usually working in some exciting new One-True-Language or another. Before it was Lisp and now it's Smalltalk. They seem to take about five years each for me. I have to keep going or I'll never get to APL, FORTH, Haskell, etc.
Mmmmm. APL! I used to poke APL in the late 80s. Reducing the core computation of a Game of Life implementation down to +/+/MATRIX[ x+'-1 0 1' ; y+'-1 0 1] is, to an extent, a work of beauty (then, of course, you need to set up looping through x and y around that and do some branching, but...).
2007-04-16 07:26 am (UTC)
Lisp was my first love (what a revelation compared to PLEX).
But nowadays, I never seem to get out of my Erlang slippers... :-)
The real reason I used Lisp for Slitch is that you'd already done TCP/IP in Erlang 10 years earlier so there were no bragging rights left for that. :-)
2007-04-18 06:24 am (UTC)
He he, you've got a good memory.
(http://tpl.tornkvist.org:8383/) but if I'm not
years ago... :-)