diff --git a/README.md b/README.md index e7ae736..33bbe6a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ playground ========== -A place for my head \ No newline at end of file +A place for my head + +Just random hacking/learning. diff --git a/golang/rndc/gen.pl b/golang/rndc/gen.pl new file mode 100755 index 0000000..48b30cc --- /dev/null +++ b/golang/rndc/gen.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Net::RNDC::Packet; + +my $pkt = Net::RNDC::Packet->new(key => 'abcd'); + +$pkt->{data}{somelist} = [ {cat => 'dog', small => ['thing'], }, 1, "hi", ['bird', 'mouse'] ]; + +open(my $o, '>', 'pkt.pkt') or die "Failed to write pkt.pkt: $!\n"; + +print $o $pkt->data; + +close($o); + +print < 0; { + data = append(data, value_fromwire(buf)) + } + var ret value + ret.xtype = ISCCC_CCMSGTYPE_LIST + ret.suba = &data + return ret +} + +func list_towire (val *value) *bytes.Buffer { + var header = new(bytes.Buffer) + binary.Write(header, binary.BigEndian, int8(ISCCC_CCMSGTYPE_LIST)) + var buf = new(bytes.Buffer) + + for _, d := range *val.suba { + buf.Write(value_towire(&d).Bytes()) + } + + binary.Write(header, binary.BigEndian, int32(buf.Len())) + var newbuf = new(bytes.Buffer) + newbuf.Write(header.Bytes()) + newbuf.Write(buf.Bytes()) + + return newbuf +} + +func table_fromwire (buf *bytes.Buffer) value { + data := make(table_head) + + for ; buf.Len() > 0; { + var length int8 + var key string + + binary.Read(buf, binary.BigEndian, &length) + d := make([]byte, length) + + buf.Read(d) + + key = string(d) + + data[key] = value_fromwire(buf) + } + + + var ret value + ret.xtype = ISCCC_CCMSGTYPE_TABLE + ret.subt = &data + return ret +} + +func table_towire (val *value, no_header int) *bytes.Buffer { + var header = new(bytes.Buffer) + binary.Write(header, binary.BigEndian, int8(ISCCC_CCMSGTYPE_TABLE)) + var buf = new(bytes.Buffer) + + for k, d := range *val.subt { + binary.Write(buf, binary.BigEndian, int8(len(k))) + buf.WriteString(k) + buf.Write(value_towire(&d).Bytes()) + } + + if (no_header > 0) { + return buf + } else { + var newbuf = new(bytes.Buffer) + binary.Write(header, binary.BigEndian, int32(buf.Len())) + newbuf.Write(header.Bytes()) + newbuf.Write(buf.Bytes()) + return newbuf + } + + return buf +} + +func display(v *value, indent string) { + switch v.xtype { + case ISCCC_CCMSGTYPE_BINARYDATA: + fmt.Print("\"", v.subv, "\"\n") + case ISCCC_CCMSGTYPE_LIST: + fmt.Print(indent, "(\n"); + cindent := indent + indent = indent + " " + for _, val := range *v.suba { + display(&val, indent) + } + fmt.Print(cindent, ")\n"); + + case ISCCC_CCMSGTYPE_TABLE: + fmt.Print(indent, "{\n"); + cindent := indent + indent := indent + " " + for key, val := range *v.subt { + fmt.Print(indent, "\"", key, "\":"); + display(&val, indent) + } + fmt.Print(cindent, "}\n"); + } +}