Lisp Packet Generator を作る・その1
try tcpreplay - Shammerismでtcpreplayというのを使った。このときは取得済みパケットを再送というように行ったが、これで送信できるパケットを自分で作りたい。それに挑戦してみる。バイナリデータの書き出しはwriteでできるはず。パケットのバイナリをそのままwriteすればいいと予想。ということでとりあえずの叩き台を作成。
(with-open-file (packet "new packet.pcap" :direction :output :if-exists :supersede :if-does-not-exist :create) (write 10 packet))
しかし、以下のようなエラー。
;Compiler warnings : ; In an anonymous lambda form at position 113: In the call to WRITE with arguments (10 PACKET), ; the variable portion of the argument list (PACKET) contains an odd number ; of arguments and so can't be used to initialize keyword parameters ; for the current global definition of WRITE > Error: Permission denied : "new packet.pcap" > While executing: CCL::%CREATE-FILE, in process Listener(4). > Type cmd-. to abort, cmd-\ for a list of available restarts. > Type :? for other options. 1 >
writeの使い方が違うようだ。なんか1バイトずつ書き出すような関数があるはず、ということで、Lisp Hyper Spec を見てみるとwrite-byteというのがある。write-byteの説明によれば、with-open-fileは:element-type 'unsigned-byteというオプションも必要なようだ。
? (with-open-file (packet "newpacket.pcap" :direction :output :if-exists :supersede :if-does-not-exist :create :element-type 'unsigned-byte) (write-byte 10 packet)) 10 ?
内容を確認すると、バイナリで10というファイルになっているのがわかる。
$ od -h newpacket.pcap 0000000 000a 0000001 $
これを少しずつ応用して完成させていこう。