Luke Gorrie (lukego) wrote,
Luke Gorrie

slitch & netkit

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:

  • 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.
Here's a "screenshot" of Slitch:
  * (netstat)
  lisp0  UP    Type:TAP  HWaddr 00:FF:C5:50:B6:E4
         Inet addr:  Bcast:
         RX bytes:196325 (196.3KiB)  TX bytes:190508 (190.5KiB)>
  lisp1  DOWN  Type:TAP  HWaddr 00:FF:6B:5D:36:60
         Inet addr:  Bcast:
         RX bytes:0 (0 b)  TX bytes:0 (0 b)>
  * (show-tcp-connections)
  * (routes)
  Destination      Gateway          Netmask          Interface  lisp0    lisp0    lisp0
And here's some stuff in netkit:
  • 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.
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.
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.
Tags: hacking, lisp
  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.


April 13 2007, 20:52:39 UTC 12 years ago

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


April 16 2007, 07:26:22 UTC 12 years ago

Lisp was my first love (what a revelation compared to PLEX).
But nowadays, I never seem to get out of my Erlang slippers... :-)

Cheers, Tobbe
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. :-)


April 18 2007, 06:24:14 UTC 12 years ago

He he, you've got a good memory.
Recently I've spent a lot of time with Javascript
( but if I'm not
mistaken, you wrote a Lisp interpreter in Javascript
years ago... :-)

And it still works! I was showing it off to Patrik Winroth last week :-)

and in the Luke/Patrik/Tony "that's nothing, on the Atari I used to.." discussions I also found that the first commercial program I ever wrote is still in production use: Araknoid! :-)